📄 activityeventprocessor.java
字号:
/*
*
* 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.activity;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.ofbiz.base.util.Debug;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.condition.EntityCondition;
import org.ofbiz.entity.condition.EntityOperator;
import org.ofbiz.entity.condition.EntityWhereString;
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.util.QueryInfo;
import com.sourcetap.sfa.util.UserInfo;
/**
* DOCUMENT ME!
*
*/
public class ActivityEventProcessor extends GenericEventProcessor {
public static final String module = ActivityEventProcessor.class.getName();
/**
* DOCUMENT ME!
*
* @param userInfo
* @param delegator
* @param dataMatrix
*
* @return
*/
protected int preUpdate(UserInfo userInfo, GenericDelegator delegator,
DataMatrix dataMatrix) {
GenericValue activityGV = dataMatrix.getCurrentBuffer().getGenericValue(0,
0);
// Set the time stamps.
activityGV.set("modifiedDate",
new Timestamp(Calendar.getInstance().getTime().getTime()));
// Store the current user's party ID in the "modified by" field.
activityGV.set("modifiedBy", userInfo.getPartyId());
// Set the owner ID if it is not already set.
String ownerId = activityGV.getString("activityOwnerId");
if ((ownerId == null) || ownerId.equals("")) {
ownerId = userInfo.getPartyId();
activityGV.set("activityOwnerId", ownerId);
}
return STATUS_CONTINUE;
}
/**
* DOCUMENT ME!
*
* @param userInfo
* @param delegator
* @param dataMatrix
*
* @return
*/
protected int preInsert(UserInfo userInfo, GenericDelegator delegator,
DataMatrix dataMatrix) {
String userPartyId = userInfo.getPartyId();
GenericValue activityGV = dataMatrix.getCurrentBuffer().getGenericValue(0,
0);
String activityId = activityGV.getString("activityId");
if ((activityId == null) || (activityId.equals(""))) {
activityId = GenericReplicator.getNextSeqId("Activity", delegator);
}
activityGV.set("activityId", activityId);
// activityGV.set("activityOwnerId", userPartyId);
// Set the time stamps.
Timestamp currentTime = new Timestamp(Calendar.getInstance().getTime()
.getTime());
activityGV.set("modifiedDate", currentTime);
activityGV.set("createdDate", currentTime);
// Store the current user's Party ID in the "modified by" and "created by" fields.
activityGV.set("modifiedBy", userPartyId);
activityGV.set("createdBy", userPartyId);
// Set the owner ID if it is not already set.
String ownerId = activityGV.getString("activityOwnerId");
if ((ownerId == null) || ownerId.equals("")) {
ownerId = userPartyId;
activityGV.set("activityOwnerId", ownerId);
}
String contactId = activityGV.getString("contactId");
String participantId = ownerId;
if ((contactId != null) && (contactId.length() > 0)) {
participantId = contactId;
}
// Add the owner to the participants list.
GenericValue activityContactGV = new GenericValue(delegator.getModelEntity(
"ActivityContact"));
activityContactGV.setDelegator(delegator);
activityContactGV.set("activityId", activityId);
activityContactGV.set("contactId", participantId);
activityContactGV.set("createdBy", userPartyId);
activityContactGV.set("createdDate", currentTime);
activityContactGV.set("modifiedBy", userPartyId);
activityContactGV.set("modifiedDate", currentTime);
// Add the Activity Contact to the data matrix so it will be saved.
dataMatrix.addEntity("ActivityContact", false, true);
dataMatrix.getCurrentBuffer().getContentsRow(0).add(activityContactGV);
return STATUS_CONTINUE;
}
/**
* DOCUMENT ME!
*
* @param userInfo
* @param mainEntityName
* @param method
* @param fields
* @param orderBy
* @param queryInfo
* @param relatedSearchClauses
* @param delegator
* @param dataMatrix
*
* @return
*/
protected int preRetrieve(UserInfo userInfo, String mainEntityName,
int method, Map fields, List orderBy, QueryInfo queryInfo,
List relatedSearchClauses, GenericDelegator delegator,
DataMatrix dataMatrix) {
/*
select *
FROM ACTIVITY, ENTITY_ACCESS, ROLE
WHERE ENTITY_ACCESS.PARTY_ENTITY_TYPE = 'Role'
AND ( ENTITY_ACCESS.PARTY_ID = ROLE.ROLE_ID OR ENTITY_ACCESS.ENTITY_CREATED_BY = '10050.10051' )
AND ROLE.ROLE_PATH LIKE '%10000%'
AND ENTITY_ACCESS.ENTITY_ID in ( ACTIVITY.ACCOUNT_ID, ACTIVITY.LEAD_ID, ACTIVITY.OPPORTUNITY_ID, ACTIVITY.CONTACT_ID )
*/
queryInfo.addJoin("EntityAccess", "Role", Boolean.FALSE, "partyId", "roleId");
queryInfo.addCondition( "EntityAccess", "partyEntityType", EntityOperator.EQUALS, "Role");
queryInfo.addAlias("EntityAccess", "entityId", "entityId");
queryInfo.addAlias("EntityAccess", "entity", "entity");
// EntityCondition orCondition = new EntityConditionList(
// UtilMisc.toList(
// new EntityExpr("entityId", EntityOperator.EQUALS, "accountId"),
// new EntityExpr("entityId", EntityOperator.EQUALS, "leadId"),
// new EntityExpr("entityId", EntityOperator.EQUALS, "opportunityId") ),
// EntityOperator.OR);
// queryInfo.addCondition( "EntityAccess", "entityId", EntityOperator.IN, "Activity.ACCOUNT_ID, Activity.LEAD_ID, Activity.OPPORTUNITY_ID");
// TODO replace SQL with EntityCondition
String whereStr = " ( ( EntityAccess.ENTITY_ID = Activity.ACCOUNT_ID AND EntityAccess.ENTITY = 'Account' ) " +
" OR ( EntityAccess.ENTITY_ID = Activity.LEAD_ID AND EntityAccess.ENTITY = 'Lead' )" +
" OR ( EntityAccess.ENTITY_ID = Activity.OPPORTUNITY_ID AND EntityAccess.ENTITY = 'Deal' ) ) ";
EntityCondition whereCondition = new EntityWhereString(whereStr);
queryInfo.addCondition( whereCondition );
queryInfo.addCondition( "Role", "rolePath", EntityOperator.LIKE, "%" + userInfo.getRoleId() + "%");
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 activityGV = 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
String activityOwnerId = (activityGV.getString("activityOwnerId") == null)
? "" : activityGV.getString("activityOwnerId");
if (activityOwnerId.equals("")) {
activityGV.set("activityOwnerId", userInfo.getPartyId());
}
// Activity Type
activityGV.set("statusId", "10");
// Activity Date
String openDateString = (activityGV.getString("openDate") == null) ? ""
: activityGV.getString(
"openDate");
if (openDateString.equals("")) {
java.util.Date openDate = new java.util.Date();
activityGV.set("openDate", new java.sql.Date(openDate.getTime()));
}
/*
// Activity Start Time
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
String openStartTimeString = timeFormat.format(openDate);
try {
if (DEBUG) Debug.logVerbose("-->[ActivityEventProcessor.postCreate] Setting activity start time.",module);
// java.util.Date openStartTime = timeFormat.parse("08:00");
java.util.Date openStartTime = timeFormat.parse(openStartTimeString);
activityGV.set("openStartTime", new java.sql.Timestamp(openStartTime.getTime()));
} catch (ParseException e) {
}
// Activity End Time
java.util.Date delayedTime = new java.util.Date(openDate.getTime()+60*60*1000);
String openEndTimeString = timeFormat.format(delayedTime);
try {
if (DEBUG) Debug.logVerbose("-->[ActivityEventProcessor.postCreate] Setting activity start time.",module);
// java.util.Date openEndTime = timeFormat.parse("08:30");
java.util.Date openEndTime = timeFormat.parse(openEndTimeString);
activityGV.set("openEndTime", new java.sql.Timestamp(openEndTime.getTime()));
} catch (ParseException e) {
}
*/
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 contacts.
status = deleteOneRelated(userInfo, delegator, entityGV, "",
"ActivityContact", originatingEntityName,
new GenericEventProcessor());
if (status != STATUS_CONTINUE) {
return status;
}
// Delete related FileAttachments. (Note: This must happen before the ActivityFiles are deleted.)
status = deleteOneRelated(userInfo, delegator, entityGV, "",
"FileAttachment", originatingEntityName,
new AbstractAttachmentEP());
if (status != STATUS_CONTINUE) {
return status;
}
// Delete related ActivityFiles. (Note: This must happen after the FileAttachments are deleted.)
status = deleteOneRelated(userInfo, delegator, entityGV, "",
"ActivityFile", originatingEntityName,
new GenericEventProcessor());
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("FileAttachment")) {
// Finding related FileAttachment records. Need special processing because the relationship cannot be
// defined in the sfa-config.xml file.
// Get all the related ActivityFile records using the generic version of findOneRelated.
List activityFileGVL = super.findOneRelated(userInfo, delegator,
entityGV, "", "ActivityFile");
// Make a List of FileAttachments tied to the identified ActivityFiles.
List fileAttachmenGVL = new LinkedList();
Iterator activityFileGVI = activityFileGVL.iterator();
while (activityFileGVI.hasNext()) {
GenericValue activityFileGV = (GenericValue) activityFileGVI.next();
try {
GenericValue fileAttachmentGV = delegator.getRelatedOne("FileAttachment",
activityFileGV);
if (fileAttachmentGV != null) {
fileAttachmenGVL.add(fileAttachmentGV);
}
} catch (GenericEntityException e) {
Debug.logWarning(
"[ActivityEventProcessor.findOneRelated] Error retrieving FileAttachment record: " +
e.getLocalizedMessage(), module);
}
}
return fileAttachmenGVL;
} else {
// Not finding FileAttachment records. Just use the standard processing using relations.
return super.findOneRelated(userInfo, delegator, entityGV,
relationTitle, relatedEntityName);
}
}
/*
public SecurityLinkInfo getSecurityLinkInfo(UserInfo userInfo, GenericDelegator delegator)
{
return new SecurityLinkInfo( "Account", "accountId", false );
}
*/
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -