/*
*
* Copyright (c) 2004 SourceTap - www.sourcetap.com
*
* The contents of this file are subject to the SourceTap Public License
* ("License"); You may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
*/
package com.sourcetap.sfa.opportunity;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
import org.ofbiz.base.util.Debug;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import com.sourcetap.sfa.activity.ActivityEventProcessor;
import com.sourcetap.sfa.attachment.AbstractAttachmentEP;
import com.sourcetap.sfa.event.DataMatrix;
import com.sourcetap.sfa.event.GenericEventProcessor;
import com.sourcetap.sfa.replication.GenericReplicator;
import com.sourcetap.sfa.security.EntityAccessEventProcessor;
import com.sourcetap.sfa.security.EntityAccessHelper;
import com.sourcetap.sfa.security.SecurityLinkInfo;
import com.sourcetap.sfa.security.SecurityWrapper;
import com.sourcetap.sfa.ui.UIScreenSectionEntity;
import com.sourcetap.sfa.util.UserInfo;
/**
* DOCUMENT ME!
*
*/
public class OpportunityEventProcessor extends GenericEventProcessor {
public static final String module = OpportunityEventProcessor.class.getName();
/**
* DOCUMENT ME!
*
* @param userInfo
* @param delegator
* @param dataMatrix
*
* @return
*/
protected int preUpdate(UserInfo userInfo, GenericDelegator delegator,
DataMatrix dataMatrix) {
GenericValue dealGV = dataMatrix.getCurrentBuffer().getGenericValue(0, 0);
String dealId = dealGV.getString("dealId");
Timestamp now = new Timestamp(Calendar.getInstance().getTime().getTime());
String userPartyId = userInfo.getPartyId();
// set probability
try {
HashMap dealStageMap = new HashMap();
dealStageMap.put("stageId", dealGV.get("stageId"));
GenericValue probGV = delegator.findByPrimaryKey("DealStage",
dealStageMap);
dealGV.set("probability", probGV.get("probability"));
} catch (GenericEntityException e) {
Debug.logError(
"-->[OpportunityEventProcessor.preUpdate()] Error finding deal stage.", module);
Debug.logError(e, module);
return STATUS_ERROR;
}
// Set the time stamps.
dealGV.set("modifiedDate", now);
// Store the current user ID in the "modified by" field.
dealGV.set("modifiedBy", userPartyId);
// Set the deal owner ID if it is empty.
String ownerId = dealGV.getString("ownerId");
if ((ownerId == null) || ownerId.equals("")) {
ownerId = userPartyId;
dealGV.set("ownerId", ownerId);
} else {
}
// Update team, role, and access information.
SecurityWrapper.updateRoleInformation(dataMatrix, 0, userInfo, ownerId,
"Deal", dealId, delegator);
return STATUS_CONTINUE;
}
/**
* DOCUMENT ME!
*
* @param userInfo
* @param delegator
* @param dataMatrix
*
* @return
*/
protected int preInsert(UserInfo userInfo, GenericDelegator delegator,
DataMatrix dataMatrix) {
GenericValue dealGV = dataMatrix.getCurrentBuffer().getGenericValue(0, 0);
Timestamp now = new Timestamp(Calendar.getInstance().getTime().getTime());
String userPartyId = userInfo.getPartyId();
// Generate new keys for the opportunity.
String dealId = dealGV.getString("dealId");
if ((dealId == null) || (dealId.equals(""))) {
dealId = GenericReplicator.getNextSeqId("Deal", delegator);
dealGV.set("dealId", dealId);
}
// Set the deal owner ID if it is empty.
String ownerId = dealGV.getString("ownerId");
if ((ownerId == null) || ownerId.equals("")) {
ownerId = userPartyId;
dealGV.set("ownerId", ownerId);
} else {
}
//add the team access info
SecurityWrapper.addRoleInformation(dataMatrix, 0, userInfo, ownerId,
"Deal", dealId, delegator);
//set probability
try {
HashMap dealStageMap = new HashMap();
dealStageMap.put("stageId", dealGV.get("stageId"));
GenericValue probGV = delegator.findByPrimaryKey("DealStage",
dealStageMap);
dealGV.set("probability", probGV.get("probability"));
} catch (Exception e) {
Debug.logWarning("unable to set probability for opportunity stage: " + dealGV.get("stageId"), module);
}
dealGV.set("createdBy", userPartyId);
dealGV.set("createdDate", now);
dealGV.set("modifiedBy", userPartyId);
dealGV.set("modifiedDate", now);
return STATUS_CONTINUE;
}
/**
* DOCUMENT ME!
*
* @param userInfo
* @param entityNameList
* @param delegator
* @param dataMatrix
*
* @return
*/
protected int postCreate(UserInfo userInfo, List entityNameList,
GenericDelegator delegator, DataMatrix dataMatrix) {
// Get the empty generic values.
GenericValue dealGV = dataMatrix.getCurrentBuffer().getGenericValue(0, 0);
// Set default values on the new record so they will be displayed for the user to see before saving.
// Owner ID
dealGV.set("ownerId", userInfo.getPartyId());
// Amount
dealGV.set("amount", new Float(0));
// Is In Forcast
dealGV.set("isInForecast", "N");
return STATUS_CONTINUE;
}
/**
* DOCUMENT ME!
*
* @param userInfo
* @param delegator
* @param originatingEntityName
* @param entityGV
*
* @return
*/
public int deleteAllRelated(UserInfo userInfo, GenericDelegator delegator,
String originatingEntityName, GenericValue entityGV) {
int status = STATUS_CONTINUE;
// Delete related OpportunityProducts.
status = deleteOneRelated(userInfo, delegator, entityGV, "",
"OpportunityProduct", originatingEntityName,
new GenericEventProcessor());
if (status != STATUS_CONTINUE) {
return status;
}
// Delete related OpportunityContacts.
status = deleteOneRelated(userInfo, delegator, entityGV, "",
"OpportunityContact", originatingEntityName,
new GenericEventProcessor());
if (status != STATUS_CONTINUE) {
return status;
}
// Delete related DealStageStepsTrackings.
status = deleteOneRelated(userInfo, delegator, entityGV, "",
"DealStageStepsTracking", originatingEntityName,
new GenericEventProcessor());
if (status != STATUS_CONTINUE) {
return status;
}
// Delete related OpportunityStatusTrackings.
status = deleteOneRelated(userInfo, delegator, entityGV, "",
"OpportunityStatusTracking", originatingEntityName,
new GenericEventProcessor());
if (status != STATUS_CONTINUE) {
return status;
}
// Delete related Expenses.
status = deleteOneRelated(userInfo, delegator, entityGV, "", "Expense",
originatingEntityName, new GenericEventProcessor());
if (status != STATUS_CONTINUE) {
return status;
}
// Delete related FileAttachments. (Note: This must happen before the DealFiles are deleted.)
status = deleteOneRelated(userInfo, delegator, entityGV, "",
"FileAttachment", originatingEntityName,
new AbstractAttachmentEP());
if (status != STATUS_CONTINUE) {
return status;
}
// Delete related DealFiles. (Note: This must happen after the FileAttachments are deleted.)
status = deleteOneRelated(userInfo, delegator, entityGV, "",
"DealFile", originatingEntityName, new GenericEventProcessor());
if (status != STATUS_CONTINUE) {
return status;
}
// Delete related parties.
status = deleteOneRelated(userInfo, delegator, entityGV, "", "Party",
originatingEntityName, new GenericEventProcessor());
if (status != STATUS_CONTINUE) {
return status;
}
// Delete related entity accesses.
status = deleteOneRelated(userInfo, delegator, entityGV, "",
"EntityAccess", originatingEntityName,
new EntityAccessEventProcessor());
if (status != STATUS_CONTINUE) {
return status;
}
// Delete related activities.
status = deleteOneRelated(userInfo, delegator, entityGV, "",
"Activity", originatingEntityName, new ActivityEventProcessor());
if (status != STATUS_CONTINUE) {
return status;
}
return STATUS_CONTINUE;
}
/**
* DOCUMENT ME!
*
* @param userInfo
* @param delegator
* @param entityGV
* @param relationTitle
* @param relatedEntityName
*
* @return
*/
public List findOneRelated(UserInfo userInfo, GenericDelegator delegator,
GenericValue entityGV, String relationTitle, String relatedEntityName) {
// Find instances of an entity related to the current entity so the instances can be deleted.
if (relatedEntityName.equals("EntityAccess")) {
// Finding related EntityAccess records. Need special processing because the relationship cannot be
// defined in the sfa-config.xml file.
return EntityAccessHelper.findRelated(userInfo, delegator,
entityGV, relationTitle);
} else if (relatedEntityName.equals("FileAttachment")) {
// Finding related FileAttachment records. Need special processing because the relationship cannot be
// defined in the sfa-config.xml file.
// Get all the related DealFile records using the generic version of findOneRelated.
List dealFileGVL = super.findOneRelated(userInfo, delegator,
entityGV, "", "DealFile");
// Make a List of FileAttachments tied to the identified DealFiles.
List fileAttachmenGVL = new LinkedList();
Iterator dealFileGVI = dealFileGVL.iterator();
while (dealFileGVI.hasNext()) {
GenericValue dealFileGV = (GenericValue) dealFileGVI.next();
try {
GenericValue fileAttachmentGV = delegator.getRelatedOne("FileAttachment",
dealFileGV);
if (fileAttachmentGV != null) {
fileAttachmenGVL.add(fileAttachmentGV);
}
} catch (GenericEntityException e) {
Debug.logError(
"[OpportunityEventProcessor.findOneRelated] Error retrieving FileAttachment record: " +
e.getLocalizedMessage(), module);
}
}
return fileAttachmenGVL;
} else {
// Not finding EntityAccess or FileAttachment records. Just use the standard processing using relations.
return super.findOneRelated(userInfo, delegator, entityGV,
relationTitle, relatedEntityName);
}
}
/**
* DOCUMENT ME!
*
* @param mainGV
* @param relatedSearchClause
* @param outGVV
* @param userInfo
* @param delegator
*
* @return
*/
protected GenericValue retrieveOneRelatedGV(GenericValue mainGV,
UIScreenSectionEntity relatedSearchClause, Vector outGVV, UserInfo userInfo,
GenericDelegator delegator) {
String relationRelEntityName = relatedSearchClause.getEntityName();
if (relationRelEntityName.equals("EntityAccess")) {
// Special processing for the Entity Access record since it can't be retrieved with a relationship because
// two of the fields are string constants, which can't be entered into the sfa-config.xml file.
// Get deal ID from the deal record.
String dealId = mainGV.getString("dealId");
HashMap entityAccessFindMap = new HashMap();
entityAccessFindMap.put("entityId", dealId);
entityAccessFindMap.put("entity", "Deal");
entityAccessFindMap.put("partyEntityType", "Team");
GenericValue entityAccessGV = null;
try {
List entityAccessGVL = delegator.findByAnd("EntityAccess",
entityAccessFindMap);
Iterator entityAccessGVI = entityAccessGVL.iterator();
if (entityAccessGVI.hasNext()) {
// Entity access record was found.
entityAccessGV = (GenericValue) entityAccessGVI.next();
} else {
// Entity access record was not found.
}
return entityAccessGV;
} catch (GenericEntityException e) {
Debug.logError(
"[OpportunityEventProcessor.retrieveOneRelatedGV] An error occurred while searching for " +
"the entity access record for the deal: " +
e.getLocalizedMessage(), module);
return entityAccessGV;
}
} else {
// Retrieve all other related entities the regular way.
return super.retrieveOneRelatedGV(mainGV, relatedSearchClause,
outGVV, userInfo, delegator);
}
}
/**
* DOCUMENT ME!
*
* @param userInfo
* @param delegator
*
* @return
*/
public SecurityLinkInfo getSecurityLinkInfo(UserInfo userInfo,
GenericDelegator delegator) {
return new SecurityLinkInfo("Deal", "dealId", true);
}
}