* @return updateFlag if any thing goes wrong then it will return false else it will return true.
*/
public boolean performGlobalReplace(HashMap replaceInfo)
{
boolean updateFlag = true;
CVDal cvdal = new CVDal(this.dataSource);
Collection allTables = new ArrayList();
batchQuery = new ArrayList();
try {
int individualID = ((Integer) replaceInfo.get("individualID")).intValue();
Integer primaryTableID = (Integer) replaceInfo.get("tableID");
SearchVO searchVO = (SearchVO) replaceInfo.get("searchVO");
String fieldInfo = (String) replaceInfo.get("fieldInfo");
String fieldValue = (String) replaceInfo.get("fieldValue");
String fieldName = (String) replaceInfo.get("fieldName");
InitialContext ic = CVUtility.getInitialContext();
AdvancedSearchLocalHome home = (AdvancedSearchLocalHome) ic.lookup("local/AdvancedSearch");
AdvancedSearchLocal remote = (AdvancedSearchLocal) home.create();
remote.setDataSource(dataSource);
ArrayList resultsIDs = new ArrayList();
resultsIDs.addAll(remote.performSearch(individualID,searchVO));
//Parse out the field Information which are fieldtableid, fieldID and fieldType
// Field type as its own idenfication
// for example "6" is a phone type field. "8" is a multiple selection field type
int fieldTableID = 0;
int replaceFieldID = 0;
int replaceFieldType = 0;
if(fieldInfo != null){
StringTokenizer fieldToken = new StringTokenizer(fieldInfo, "*");
String fieldIDString = null;
String tableIDString = null;
String fieldTypeString = null;
if(fieldToken != null){
while (fieldToken.hasMoreTokens())
{
tableIDString = (String) fieldToken.nextToken();
fieldIDString = (String) fieldToken.nextToken();
fieldTypeString = (String) fieldToken.nextToken();
}//end of while (fieldInfo.hasMoreTokens())
if(fieldIDString != null && fieldTypeString != null && !fieldIDString.equals("") && !fieldTypeString.equals("")){
fieldTableID = Integer.parseInt(tableIDString);
replaceFieldID = Integer.parseInt(fieldIDString);
replaceFieldType = Integer.parseInt(fieldTypeString);
}//end of if(fieldIDString != null && fieldTypeString != null && !fieldIDString.equals("") && !fieldTypeString.equals(""))
}//end of if(fieldToken != null)
}//end of if(fieldInfo != null)
String primaryTable = null;
String primaryKey = null;
String relateTable = null;
int queryTableID = 0;
String displayName = null;
String relateKey = null;
String relationShipQuery = null;
String isOnGlobalReplaceTable = null;
String realTableName = null;
String updateField = null;
String subRelationShipQuery = null;
//Get the Primary Table for this module.
String mainTableQuery = "SELECT TablePrimaryKey, TableName FROM "+
"searchtable where SearchTableID = ?";
cvdal.setSqlQueryToNull();
cvdal.clearParameters();
cvdal.setSqlQuery(mainTableQuery);
cvdal.setInt(1, primaryTableID.intValue());
Collection mainTableResults = cvdal.executeQuery();
cvdal.setSqlQueryToNull();
if (mainTableResults != null)
{
Iterator mainTableIterator = mainTableResults.iterator();
//Only get the first one.
if (mainTableIterator.hasNext())
{
HashMap mainTableHashMap = (HashMap) mainTableIterator.next();
primaryTable = (String) mainTableHashMap.get("TableName");
primaryKey = (String) mainTableHashMap.get("TablePrimaryKey");
if (!allTables.contains(primaryTable))
{
allTables.add(primaryTable);
} //end of if statement (!tables.contains(tableName))
} //end of while loop (mainTableIterator.hasNext())
} //end of if statement (mainTableResults != null)
//end of Get the Primary Table for this module.
//Get the Primary Table for this module.
mainTableQuery = "SELECT sf.RealFieldName, sf.SearchTableID , sf.DisplayName , st.TableName, sf.RelationshipQuery, " +
" sf.IsOnGlobalReplaceTable, sf.RealTableName, sf.SubRelationshipQuery FROM searchtable st,searchfield sf where sf.SearchTableID "+
" = st.SearchTableID and sf.searchFieldID = ?";
cvdal.setSqlQueryToNull();
cvdal.clearParameters();
cvdal.setSqlQuery(mainTableQuery);
cvdal.setInt(1, replaceFieldID);
mainTableResults = cvdal.executeQuery();
cvdal.setSqlQueryToNull();
if (mainTableResults != null)
{
Iterator mainTableIterator = mainTableResults.iterator();
//Only get the first one.
if (mainTableIterator.hasNext())
{
HashMap mainTableHashMap = (HashMap) mainTableIterator.next();
relateTable = (String) mainTableHashMap.get("TableName");
relateKey = (String) mainTableHashMap.get("RealFieldName");
queryTableID = ((Number) mainTableHashMap.get("SearchTableID")).intValue();
displayName = (String) mainTableHashMap.get("DisplayName");
relationShipQuery = (String)mainTableHashMap.get("RelationshipQuery");
isOnGlobalReplaceTable = (String)mainTableHashMap.get("IsOnGlobalReplaceTable");
realTableName = (String)mainTableHashMap.get("RealTableName");
subRelationShipQuery = (String)mainTableHashMap.get("SubRelationshipQuery");
if (!allTables.contains(primaryTable))
{
allTables.add(primaryTable);
} //end of if statement (!tables.contains(tableName))
} //end of while loop (mainTableIterator.hasNext())
} //end of if statement (mainTableResults != null)
//end of Get the Primary Table for this module.
// Update the Custom Field Table with its values.
if(fieldTableID == GlobalReplaceConstantKeys.CUSTOM_FIELD_TABLEID ){
for (int i = 0; i < resultsIDs.size(); i++)
{
int recordID =((Number)resultsIDs.get(i)).intValue();
if(replaceFieldType == GlobalReplaceConstantKeys.FIELD_TYPE_MULTIPLE){
String finalTableQuery = "UPDATE customfieldmultiple SET ValueID = ? WHERE " +
" CustomFieldID = ? AND RecordID in (?) ";
cvdal.setSqlQueryToNull();
cvdal.clearParameters();
cvdal.setSqlQuery(finalTableQuery);
cvdal.setString(1, fieldValue);
cvdal.setInt(2, replaceFieldID);
cvdal.setInt(3, recordID);
int rowsAffected = cvdal.executeUpdate();
if(rowsAffected == 0){
batchQuery.add("insert into customfieldmultiple " +
"(customfieldid,recordid,valueid) values " +
"(" + replaceFieldID + "," + recordID + "," + fieldValue +
")");
}//end of if(rowsAffected == 0)
}//end of if(replaceFieldType == GlobalReplaceConstantKeys.FIELD_TYPE_MULTIPLE)
else{
String finalTableQuery = "UPDATE customfieldscalar SET Value = ? "
+" cf.CustomFieldID = ? AND RecordID in (?) ";
cvdal.setSqlQueryToNull();
cvdal.clearParameters();
cvdal.setSqlQuery(finalTableQuery);
cvdal.setString(1, fieldValue);
cvdal.setInt(2, replaceFieldID);
cvdal.setInt(3, recordID);
int rowsAffected = cvdal.executeUpdate();
if(rowsAffected == 0){
batchQuery.add("insert into customfieldscalar " +
"(customfieldid,recordid,value) values " + "(" +
replaceFieldID + "," + recordID + ",'" +
fieldValue + "')");
}//end of if(rowsAffected == 0)
}//end of else for if(replaceFieldType == GlobalReplaceConstantKeys.FIELD_TYPE_MULTIPLE)
}//end of for (int i = 0; i < resultsIDs.size(); i++)
}else {
updateField = relateTable + "." + relateKey;
String thisRelationship = "";
//We must check wheather the field is occuring in the table.
// If it not occuring in the table then we must have to get the alias table name &
// collect the Relationship to that table build a relationship information.
if (isOnGlobalReplaceTable != null && isOnGlobalReplaceTable.equals("N")){
//We must have to do this because the tablename is attached to the field name.
updateField = relateKey;
if (!allTables.contains(realTableName))
{
allTables.add(realTableName);
} //end of if (!allTables.contains(realTableName))
// alias determination
Collection alias = new ArrayList();
StringTokenizer aliasCommaTokens = new StringTokenizer(realTableName, ",");
while(aliasCommaTokens.hasMoreTokens()){
String aliasRealTable = aliasCommaTokens.nextToken();
StringTokenizer aliasTokens = new StringTokenizer(aliasRealTable, " ");
String tempTableName = aliasTokens.nextToken();
if(aliasTokens.hasMoreTokens())
{
alias.add(aliasTokens.nextToken());
}//end of if(aliasTokens.hasMoreTokens())
}//end of while(aliasCommaTokens.hasMoreTokens())
if (relationShipQuery != null)
{
thisRelationship = relationShipQuery;
StringTokenizer relationshipTokens = new StringTokenizer(relationShipQuery, " ");
while (relationshipTokens.hasMoreTokens())
{
String thisToken = relationshipTokens.nextToken();
int index = thisToken.indexOf(".");
if (index > -1)
{
String tableName = thisToken.substring(0, index);
//Incase if you added the new line to the Query then before
//check for the occurance. We will eliminate the new line return character.
tableName = tableName.replaceAll("\n","");
if ((!allTables.contains(tableName)) && (!alias.contains(tableName)))
{
allTables.add(tableName);
} //end of if statement (!tables.contains(tableName))
} //end of if statement (index > -1)
} //end of while loop (relationshipTokens.hasMoreTokens())
} //end of if statement (thisRelationship != null)
if(subRelationShipQuery != null && !subRelationShipQuery.equals("")){
thisRelationship += subRelationShipQuery;
}
}//end of if (isOnTable != null && isOnTable.equals("N"))
// We must have to identify the relation of the table
// incase for some reason if the record is not occuring for the table then
// we must have add new record for that related table.
int index = updateField.indexOf(".");
int insertType = 0;
if (index > -1)
{
int lenField = updateField.length();
String tableName = updateField.substring(0, index);
if (tableName != null && tableName.startsWith("moc")){
insertType = GlobalReplaceConstantKeys.METHOD_OF_CONTACT_TABLEID;
}
if (tableName != null && tableName.startsWith("address")){
insertType = GlobalReplaceConstantKeys.ADDRESS_TABLEID;
fieldName = updateField.substring(index+1,lenField);
}
} //end of if statement (index > -1)
//building the final query
StringBuffer selectQuery = new StringBuffer();
selectQuery.append("UPDATE ");
Iterator thisTableIterator = allTables.iterator();
while (thisTableIterator.hasNext())
{
String currentTable = (String) thisTableIterator.next();
selectQuery.append(currentTable);
if (thisTableIterator.hasNext())
{
selectQuery.append(", ");
} //end of if statement (thisTableIterator.hasNext())
} //end of while loop (thisTableIterator.hasNext())
selectQuery.append(" SET " + updateField + " = ? ");
selectQuery.append(" WHERE ");
if (thisRelationship != null && thisRelationship.length() > 0)
{
selectQuery.append(thisRelationship);
selectQuery.append(" AND ");
} //end of if statement (thisRelationship != null ...
selectQuery.append( primaryTable + "." + primaryKey + " in ( ? )");
for (int i = 0; i < resultsIDs.size(); i++)
{
int recordID =((Number)resultsIDs.get(i)).intValue();
cvdal.setSqlQueryToNull();
cvdal.clearParameters();
cvdal.setSqlQuery(selectQuery.toString());
cvdal.setString(1,fieldValue);
cvdal.setInt(2,recordID);
int rowsAffected = cvdal.executeUpdate();
if(rowsAffected == 0){
HashMap fieldDetails = new HashMap();
fieldDetails.put("contactID",new Integer(recordID));
fieldDetails.put("contactType",primaryTableID);
fieldDetails.put("insertType",new Integer(insertType));
fieldDetails.put("fieldName",fieldName);
fieldDetails.put("fieldValue",fieldValue);
this.insertGlobalReplaceRecord(fieldDetails,cvdal);
}
}//end of for (int i = 0; i < resultsIDs.size(); i++)
}//end of else block
// In-case if we are updating the individual's List then we must have to update the entity's List ID
// same thing for entity.
if(displayName != null && displayName.equals("Marketing List") && queryTableID != 0){
//intialize the ContactHelperLocal
ContactHelperLocalHome contactHelperLocalHome = (ContactHelperLocalHome)ic.lookup("local/ContactHelper");
ContactHelperLocal contactHelperLocal = contactHelperLocalHome.create();
contactHelperLocal.setDataSource(this.dataSource);
ArrayList recordIDs= new ArrayList();
for (int i = 0; i < resultsIDs.size(); i++)
{
int recordID =((Number)resultsIDs.get(i)).intValue();
// If we are processing the Individual then we must have to get
// Entity ID. So that we can move the entity as well as all associated individual
// to the new List selected by the user.
if(queryTableID == 2){
recordID = contactHelperLocal.getEntityIDForIndividual(recordID);
}//end of if(queryTableID == 2)
recordIDs.add(recordID+"");
}//end of for (int i = 0; i < resultsIDs.size(); i++)
String entityIDs[] = new String[recordIDs.size()];
for(int i = 0; i < recordIDs.size(); i++){
entityIDs[i]= (String) recordIDs.get(i);
}//end of for(int i = 0; i < recordIDs.size(); i++)
int marketingListID = Integer.parseInt(fieldValue);
if(marketingListID != 0 && entityIDs.length != 0 ){
// Reason for adminIndividualID to set as -1. We know that admin is a super user
// he can move any thing. thats why its hard coded to -1
int adminIndividualID = -1;
contactHelperLocal.entityMove(adminIndividualID, marketingListID, entityIDs );
}//end of if(marketingListID != 0 && entityIDs.length != 0 )
}//end of if(displayName != null && displayName.equals("Marketing List") && queryTableID != 0)
// inserting the batched query to the database.
int[] batchResult = cvdal.batchProcess(batchQuery);
} //end of try block
catch (Exception e)
{
logger.error("[Exception] GlobalReplaceEJB.performGlobalReplace:", e);
updateFlag = false;
} //end of catch block (Exception)
finally
{
cvdal.destroy();
cvdal = null;
} //end of finally block
return updateFlag;
} //end of performGlobalReplace method