groupRS.close(); System.out.println("delete user relations (ic_group_tree) whith group_id "+userinfo[4]);
	public void runFix(String userSelectSql,boolean requireGroups, boolean moveGroups){		// get users connected to given group id		// delete from ic_group_tree ids related to given group id		// remove relation in ic_user 		// delete from ic_group		// creeate new ic_groups for users		DatastoreInterface datastoreInterface = DatastoreInterface.getInstance();		boolean sap = (datastoreInterface instanceof SapDBDatastoreInterface);		String userSQL = userSelectSql!=null?userSelectSql:getDefaultUserSelectSQL();		String updateUserGroupSQL = "update ic_user set user_representative=null where ic_user_id = ?";		String selectRelatedGroupsSQL = "select ic_group_id from ic_group_tree where child_ic_group_id = ?";		String deleteUserGroupRelationSQL = " delete from ic_group_tree where child_ic_group_id = ?";		String deleteUserGroupRelationSQL2 = " delete from ic_group_relation where related_ic_group_id = ?";		String deleteUserGroupSQL = "delete from ic_group where ic_group_id = ?";		String insertGroupSQL = "insert into ic_group (ic_group_id,group_type,name,extra_info) values (?,'ic_user_representative',?,'Fixed "+df.format(new Date())+"')";		String insertGroupRelationSQL = "insert into ic_group_relation(IC_GROUP_ID,RELATIONSHIP_TYPE,INITIATION_DATE ,RELATED_IC_GROUP_ID ,GROUP_RELATION_STATUS ,INIT_MODIFICATION_DATE) values (?,'GROUP_PARENT', '"+com.idega.util.IWTimestamp.RightNow().toSQLString()+"',?,'ST_ACTIVE','"+com.idega.util.IWTimestamp.RightNow().toSQLString()+"')";		if (sap) {			insertGroupRelationSQL = "insert into ic_group_relation(IC_GROUP_ID,RELATIONSHIP_TYPE,INITIATION_DATE ,RELATED_IC_GROUP_ID ,GROUP_RELATION_STATUS ,INIT_MODIFICATION_DATE,IC_GROUP_RELATION_ID) values (?,'GROUP_PARENT', '"+com.idega.util.IWTimestamp.RightNow().toSQLString()+"',?,'ST_ACTIVE','"+com.idega.util.IWTimestamp.RightNow().toSQLString()+"', ?)";		}		String selectGroupTreeTopNodes = "select distinct ic_group_id from ic_group_tree where ic_group_id not in (select child_ic_group_id from ic_group_tree)";						String updateGroupMetadataSQL = "update ic_group_ic_metadata set ic_group_id = ? where ic_group_id = ?";		String updateGroupProtocolSQL = "update ic_group_protocol set ic_group_id = ? where ic_group_id = ?";		String updateGroupNewworkSQL = "update ic_group_network set ic_group_id = ? where ic_group_id = ?";		String updateGroupAddressSQL = "update ic_group_address set ic_group_id = ? where ic_group_id = ?";		String updateGroupEmailSQL = "update ic_group_email set ic_group_id = ? where ic_group_id = ?";		String updateGroupPhoneSQL = "update ic_group_phone set ic_group_id = ? where ic_group_id = ?";		String updateICUserPrimaryGroupByPrimaryGroupSQL = "update ic_user set primary_group = ? where primary_group = ?";		String updateICUserPrimaryGroupByUserID = "update ic_user set primary_group = ? where ic_user_id = ?";		String updateGroupPermissionSQL = "update ic_permission set group_id = ? where group_id = ?";		String updateGroupPermissionSQL2 = "update ic_permission set PERMISSION_CONTEXT_VALUE = ? where PERMISSION_CONTEXT_VALUE = ? and PERMISSION_CONTEXT_TYPE = '"+AccessController.CATEGORY_STRING_GROUP_ID+"'";		String groupRelationNextValSQL = "select ic_group_relation_seq.NEXTVAL AS NEXTID FROM DUAL";				java.sql.Connection conn = null;		java.sql.Statement stmt = null;		javax.transaction.TransactionManager t = com.idega.transaction.IdegaTransactionManager.getInstance();		try {			t.begin();			conn = com.idega.util.database.ConnectionBroker.getConnection();			stmt = conn.createStatement();			java.sql.PreparedStatement updateUserGroupStatement = conn.prepareStatement(updateUserGroupSQL);			java.sql.PreparedStatement selectRelatedGroupsStatement = conn.prepareStatement(selectRelatedGroupsSQL);			java.sql.PreparedStatement deleteUserGroupRelation = conn.prepareStatement(deleteUserGroupRelationSQL);			java.sql.PreparedStatement deleteUserGroupRelation2 = conn.prepareStatement(deleteUserGroupRelationSQL2);			java.sql.PreparedStatement deleteUserGroup = conn.prepareStatement(deleteUserGroupSQL);			java.sql.PreparedStatement insertGroupStatement = conn.prepareStatement(insertGroupSQL);			java.sql.PreparedStatement insertGroupRelationStatement = conn.prepareStatement(insertGroupRelationSQL);			java.sql.PreparedStatement checkGroupExistance = conn.prepareStatement("select ic_group_id from ic_group where ic_group_id = ? and group_type <> 'ic_user_representative'");			java.sql.PreparedStatement checkRepresentativeGroupExistance = conn.prepareStatement("select ic_group_id from ic_group where ic_group_id = ? and group_type = 'ic_user_representative'");			java.sql.PreparedStatement groupRelationNextVal = null;			if (sap) {				groupRelationNextVal = conn.prepareStatement(groupRelationNextValSQL);			}						java.sql.PreparedStatement updateGroupMetadata = conn.prepareStatement(updateGroupMetadataSQL);			java.sql.PreparedStatement updateGroupProtocol = conn.prepareStatement(updateGroupProtocolSQL);			java.sql.PreparedStatement updateGroupNetwork = conn.prepareStatement(updateGroupNewworkSQL);			java.sql.PreparedStatement updateGroupAddress = conn.prepareStatement(updateGroupAddressSQL);			java.sql.PreparedStatement updateGroupEmail = conn.prepareStatement(updateGroupEmailSQL);			java.sql.PreparedStatement updateGroupPhone = conn.prepareStatement(updateGroupPhoneSQL);			java.sql.PreparedStatement updateGroupPermission = conn.prepareStatement(updateGroupPermissionSQL);			java.sql.PreparedStatement updateGroupPermission2 = conn.prepareStatement(updateGroupPermissionSQL2);			java.sql.PreparedStatement updateICUserPrimaryGroupPG = conn.prepareStatement(updateICUserPrimaryGroupByPrimaryGroupSQL);			java.sql.PreparedStatement updateICUserPrimaryGroupUs = conn.prepareStatement(updateICUserPrimaryGroupByUserID);						// Group Tree Domain fixer			ResultSet rs2 = stmt.executeQuery(selectGroupTreeTopNodes);			GroupHome gHome = (GroupHome) IDOLookup.getHome(Group.class);			while (rs2.next()) {				Group g = gHome.findByPrimaryKey(new Integer(rs2.getString("ic_group_id")));				try {					gBus.addGroupUnderDomainRoot(this.getIWApplicationContext().getDomain(),g);					System.out.println("adding groupID = "+g.getPrimaryKey()+" to domain = "+this.getIWApplicationContext().getDomain().getDomainName());				} catch(Exception e) {					e.printStackTrace();				}				fixGroupRelatiosRecursive(g, insertGroupRelationStatement, groupRelationNextVal, sap);			}			rs2.close();			java.sql.ResultSet rs = stmt.executeQuery(userSQL);			java.sql.ResultSet groupRS;			java.util.ArrayList groupIds;			java.util.ArrayList users = new java.util.ArrayList();			GroupHome groupHome = (GroupHome) IDOLookup.getHome(Group.class);			Group group;			Group newGroup;						java.util.HashMap groupMap = new java.util.HashMap();			//int i = 0;			System.out.println("Starting olduser->newuser transformation");			while(rs.next()){				String[] userinfo = new String[5];				userinfo[0] = rs.getString(1);				userinfo[1] = rs.getString(2);				userinfo[2] = rs.getString(3);				userinfo[3] = rs.getString(4);				userinfo[4] = rs.getString(5);							System.out.println("handling user "+userinfo[0]);				System.out.print("getting related groups for group"+userinfo[4]);				selectRelatedGroupsStatement.setString(1,userinfo[4]);				groupRS = selectRelatedGroupsStatement.executeQuery();				groupIds = new java.util.ArrayList(2);				while(groupRS.next()){					String groupid = groupRS.getString(1);					if(groupid!=null)						groupIds.add(groupid);					System.out.print(" "+groupid);									}				System.out.println();				groupMap.put(userinfo[0],groupIds);				groupRS.close();				System.out.println("delete user relations (ic_group_tree) whith group_id "+userinfo[4]);				deleteUserGroupRelation.setString(1,userinfo[4]);				deleteUserGroupRelation.execute();												System.out.println("delete user relations (ic_group_relation) whith group_id "+userinfo[4]);				deleteUserGroupRelation2.setString(1,userinfo[4]);				deleteUserGroupRelation2.execute();								System.out.println("Update ic_user set user_representative as null where user id is "+userinfo[0]);				updateUserGroupStatement.setString(1,userinfo[0]);				updateUserGroupStatement.executeUpdate();				users.add(userinfo);								System.out.println("delete user group (ic_group) whith group_id "+userinfo[4]);				deleteUserGroup.setString(1,userinfo[4]);				deleteUserGroup.execute();											}			// Create user_representative groups and recreate relations			UserHome uHome = (UserHome) IDOLookup.getHome(User.class); 			for (java.util.Iterator iter = users.iterator(); iter.hasNext();) {				String[] userinfo = (String[]) iter.next();								String userID = userinfo[0];				String firstName = userinfo[1];				//String middleName = userinfo[2];				String lastName = userinfo[3];				//String userGroupID = userinfo[4];								System.out.println("Checking ic_group with id "+userID);								checkGroupExistance.setString(1,userID);				checkRepresentativeGroupExistance.setString(1, userID);								rs = checkGroupExistance.executeQuery();				groupRS = checkRepresentativeGroupExistance.executeQuery();								if (groupRS.next()) {					System.out.println("User "+userID+" has a ic_user_representative group");				}				// print user id to log if user groups not required				else if(!requireGroups && rs.next()){					// Group found that is not a ic_user_representative										if (moveGroups) {						group = groupHome.findByPrimaryKey(new Integer(userID));						System.out.println("User "+userID+" has a wrong kind of group : "+group.getGroupType()+", MOVING");						newGroup = groupHome.create();						newGroup.setAbbrevation(group.getAbbrevation());						newGroup.setAliasID(group.getAliasID());						newGroup.setCreated(IWTimestamp.RightNow().getTimestamp());						newGroup.setDescription(group.getDescription());						newGroup.setExtraInfo(group.getExtraInfo());						newGroup.setGroupType(group.getGroupType());						newGroup.setHomeFolderID(group.getHomeFolderID());						newGroup.setName(group.getName());						newGroup.setPermissionControllingGroupID(group.getPermissionControllingGroupID());						newGroup.setShortName(group.getShortName());						newGroup.setUniqueId(group.getUniqueId());						newGroup.store();												String newGroupPK = newGroup.getPrimaryKey().toString();						String oldGroupPK = group.getPrimaryKey().toString();												updateGroupMetadata.setString(1, newGroupPK);						updateGroupMetadata.setString(2, oldGroupPK);						updateGroupMetadata.execute();												updateGroupProtocol.setString(1, newGroupPK);						updateGroupProtocol.setString(2, oldGroupPK);						updateGroupProtocol.execute();												updateGroupNetwork.setString(1, newGroupPK);						updateGroupNetwork.setString(2, oldGroupPK);						updateGroupNetwork.execute();												updateGroupAddress.setString(1, newGroupPK);						updateGroupAddress.setString(2, oldGroupPK);						updateGroupAddress.execute();												updateGroupEmail.setString(1, newGroupPK);						updateGroupEmail.setString(2, oldGroupPK);						updateGroupEmail.execute();												updateGroupPhone.setString(1, newGroupPK);						updateGroupPhone.setString(2, oldGroupPK);						updateGroupPhone.execute();							updateGroupPermission.setString(1, newGroupPK);						updateGroupPermission.setString(2, oldGroupPK);						updateGroupPermission.execute();						updateGroupPermission2.setString(1, newGroupPK);						updateGroupPermission2.setString(2, oldGroupPK);						updateGroupPermission2.execute();												updateICUserPrimaryGroupPG.setString(1, newGroupPK);						updateICUserPrimaryGroupPG.setString(2, oldGroupPK);						updateICUserPrimaryGroupPG.execute();												group.setGroupType("ic_user_representative");						group.setName(firstName+" "+lastName);						group.setExtraInfo("UserTransformer, old group is now id = "+newGroupPK+", "+df.format(new Date()));						group.store();												ArrayList list = (ArrayList) groupMap.get(userID);						ArrayList newList = new ArrayList();						if (list != null) {							Iterator itera = list.iterator();							String gID;							while (itera.hasNext()) {								gID = (String) itera.next();								if (gID.equals(oldGroupPK)) {									newList.add(newGroupPK);								} else {									newList.add(gID);								}							}						}												groupMap.put(userID, newList);						User user = uHome.findByPrimaryKey(new Integer(userID));						List parents = user.getParentGroups();												userRelations(sap, insertGroupRelationStatement, groupRelationNextVal, groupMap, userID, ( parents == null || parents.isEmpty() ),updateICUserPrimaryGroupUs);																	} else {						System.out.println("User "+userID+" has a wrong kind of group");					}				}				else{					System.out.println("Insert into ic_group with id "+ userID );					insertGroupStatement.setString(1,userID);					insertGroupStatement.setString(2,"'"+firstName + " "+ lastName+"'");					insertGroupStatement.execute();									userRelations(sap, insertGroupRelationStatement, groupRelationNextVal, groupMap, userID, false, updateICUserPrimaryGroupUs);				}						}						rs.close();		t.commit();		}		catch (Exception e) {			e.printStackTrace();			try {				t.rollback();				Text rollBack = new Text("ROLLBACK: No data was changed (see output log)");				rollBack.setFontColor("#FF0000");				add(rollBack);			}			catch (javax.transaction.SystemException ex) {				ex.printStackTrace();			}		}		finally{			if (stmt != null) {                try {					stmt.close();				} catch (java.sql.SQLException e1) {					// TODO Auto-generated catch block					e1.printStackTrace();				}            }				if (conn != null) {				com.idega.util.database.ConnectionBroker.freeConnection(conn);            }				}			}