001: /*
002: *
003: * Copyright (c) 2004 SourceTap - www.sourcetap.com
004: *
005: * The contents of this file are subject to the SourceTap Public License
006: * ("License"); You may not use this file except in compliance with the
007: * License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
010: * the specific language governing rights and limitations under the License.
011: *
012: * The above copyright notice and this permission notice shall be included
013: * in all copies or substantial portions of the Software.
014: *
015: */
016:
017: package com.sourcetap.sfa.activity;
018:
019: import java.sql.Timestamp;
020: import java.util.Calendar;
021: import java.util.Iterator;
022: import java.util.LinkedList;
023: import java.util.List;
024: import java.util.Map;
025:
026: import org.ofbiz.base.util.Debug;
027: import org.ofbiz.entity.GenericDelegator;
028: import org.ofbiz.entity.GenericEntityException;
029: import org.ofbiz.entity.GenericValue;
030: import org.ofbiz.entity.condition.EntityCondition;
031: import org.ofbiz.entity.condition.EntityOperator;
032: import org.ofbiz.entity.condition.EntityWhereString;
033:
034: import com.sourcetap.sfa.attachment.AbstractAttachmentEP;
035: import com.sourcetap.sfa.event.DataMatrix;
036: import com.sourcetap.sfa.event.GenericEventProcessor;
037: import com.sourcetap.sfa.replication.GenericReplicator;
038: import com.sourcetap.sfa.util.QueryInfo;
039: import com.sourcetap.sfa.util.UserInfo;
040:
041: /**
042: * DOCUMENT ME!
043: *
044: */
045: public class ActivityEventProcessor extends GenericEventProcessor {
046: public static final String module = ActivityEventProcessor.class
047: .getName();
048:
049: /**
050: * DOCUMENT ME!
051: *
052: * @param userInfo
053: * @param delegator
054: * @param dataMatrix
055: *
056: * @return
057: */
058: protected int preUpdate(UserInfo userInfo,
059: GenericDelegator delegator, DataMatrix dataMatrix) {
060:
061: GenericValue activityGV = dataMatrix.getCurrentBuffer()
062: .getGenericValue(0, 0);
063:
064: // Set the time stamps.
065: activityGV.set("modifiedDate", new Timestamp(Calendar
066: .getInstance().getTime().getTime()));
067:
068: // Store the current user's party ID in the "modified by" field.
069: activityGV.set("modifiedBy", userInfo.getPartyId());
070:
071: // Set the owner ID if it is not already set.
072: String ownerId = activityGV.getString("activityOwnerId");
073:
074: if ((ownerId == null) || ownerId.equals("")) {
075: ownerId = userInfo.getPartyId();
076: activityGV.set("activityOwnerId", ownerId);
077: }
078:
079: return STATUS_CONTINUE;
080: }
081:
082: /**
083: * DOCUMENT ME!
084: *
085: * @param userInfo
086: * @param delegator
087: * @param dataMatrix
088: *
089: * @return
090: */
091: protected int preInsert(UserInfo userInfo,
092: GenericDelegator delegator, DataMatrix dataMatrix) {
093:
094: String userPartyId = userInfo.getPartyId();
095:
096: GenericValue activityGV = dataMatrix.getCurrentBuffer()
097: .getGenericValue(0, 0);
098: String activityId = activityGV.getString("activityId");
099:
100: if ((activityId == null) || (activityId.equals(""))) {
101: activityId = GenericReplicator.getNextSeqId("Activity",
102: delegator);
103: }
104:
105: activityGV.set("activityId", activityId);
106:
107: // activityGV.set("activityOwnerId", userPartyId);
108: // Set the time stamps.
109: Timestamp currentTime = new Timestamp(Calendar.getInstance()
110: .getTime().getTime());
111: activityGV.set("modifiedDate", currentTime);
112: activityGV.set("createdDate", currentTime);
113:
114: // Store the current user's Party ID in the "modified by" and "created by" fields.
115: activityGV.set("modifiedBy", userPartyId);
116: activityGV.set("createdBy", userPartyId);
117:
118: // Set the owner ID if it is not already set.
119: String ownerId = activityGV.getString("activityOwnerId");
120:
121: if ((ownerId == null) || ownerId.equals("")) {
122: ownerId = userPartyId;
123:
124: activityGV.set("activityOwnerId", ownerId);
125: }
126:
127: String contactId = activityGV.getString("contactId");
128: String participantId = ownerId;
129:
130: if ((contactId != null) && (contactId.length() > 0)) {
131: participantId = contactId;
132: }
133:
134: // Add the owner to the participants list.
135: GenericValue activityContactGV = new GenericValue(delegator
136: .getModelEntity("ActivityContact"));
137: activityContactGV.setDelegator(delegator);
138:
139: activityContactGV.set("activityId", activityId);
140: activityContactGV.set("contactId", participantId);
141: activityContactGV.set("createdBy", userPartyId);
142: activityContactGV.set("createdDate", currentTime);
143: activityContactGV.set("modifiedBy", userPartyId);
144: activityContactGV.set("modifiedDate", currentTime);
145:
146: // Add the Activity Contact to the data matrix so it will be saved.
147: dataMatrix.addEntity("ActivityContact", false, true);
148: dataMatrix.getCurrentBuffer().getContentsRow(0).add(
149: activityContactGV);
150:
151: return STATUS_CONTINUE;
152: }
153:
154: /**
155: * DOCUMENT ME!
156: *
157: * @param userInfo
158: * @param mainEntityName
159: * @param method
160: * @param fields
161: * @param orderBy
162: * @param queryInfo
163: * @param relatedSearchClauses
164: * @param delegator
165: * @param dataMatrix
166: *
167: * @return
168: */
169: protected int preRetrieve(UserInfo userInfo, String mainEntityName,
170: int method, Map fields, List orderBy, QueryInfo queryInfo,
171: List relatedSearchClauses, GenericDelegator delegator,
172: DataMatrix dataMatrix) {
173:
174: /*
175: select *
176: FROM ACTIVITY, ENTITY_ACCESS, ROLE
177: WHERE ENTITY_ACCESS.PARTY_ENTITY_TYPE = 'Role'
178: AND ( ENTITY_ACCESS.PARTY_ID = ROLE.ROLE_ID OR ENTITY_ACCESS.ENTITY_CREATED_BY = '10050.10051' )
179: AND ROLE.ROLE_PATH LIKE '%10000%'
180: AND ENTITY_ACCESS.ENTITY_ID in ( ACTIVITY.ACCOUNT_ID, ACTIVITY.LEAD_ID, ACTIVITY.OPPORTUNITY_ID, ACTIVITY.CONTACT_ID )
181: */
182: queryInfo.addJoin("EntityAccess", "Role", Boolean.FALSE,
183: "partyId", "roleId");
184: queryInfo.addCondition("EntityAccess", "partyEntityType",
185: EntityOperator.EQUALS, "Role");
186: queryInfo.addAlias("EntityAccess", "entityId", "entityId");
187: queryInfo.addAlias("EntityAccess", "entity", "entity");
188:
189: // EntityCondition orCondition = new EntityConditionList(
190: // UtilMisc.toList(
191: // new EntityExpr("entityId", EntityOperator.EQUALS, "accountId"),
192: // new EntityExpr("entityId", EntityOperator.EQUALS, "leadId"),
193: // new EntityExpr("entityId", EntityOperator.EQUALS, "opportunityId") ),
194: // EntityOperator.OR);
195: // queryInfo.addCondition( "EntityAccess", "entityId", EntityOperator.IN, "Activity.ACCOUNT_ID, Activity.LEAD_ID, Activity.OPPORTUNITY_ID");
196:
197: // TODO replace SQL with EntityCondition
198: String whereStr = " ( ( EntityAccess.ENTITY_ID = Activity.ACCOUNT_ID AND EntityAccess.ENTITY = 'Account' ) "
199: + " OR ( EntityAccess.ENTITY_ID = Activity.LEAD_ID AND EntityAccess.ENTITY = 'Lead' )"
200: + " OR ( EntityAccess.ENTITY_ID = Activity.OPPORTUNITY_ID AND EntityAccess.ENTITY = 'Deal' ) ) ";
201:
202: EntityCondition whereCondition = new EntityWhereString(whereStr);
203: queryInfo.addCondition(whereCondition);
204: queryInfo.addCondition("Role", "rolePath", EntityOperator.LIKE,
205: "%" + userInfo.getRoleId() + "%");
206:
207: return STATUS_CONTINUE;
208: }
209:
210: /**
211: * DOCUMENT ME!
212: *
213: * @param userInfo
214: * @param entityNameList
215: * @param delegator
216: * @param dataMatrix
217: *
218: * @return
219: */
220: protected int postCreate(UserInfo userInfo, List entityNameList,
221: GenericDelegator delegator, DataMatrix dataMatrix) {
222:
223: // Get the empty generic values.
224: GenericValue activityGV = dataMatrix.getCurrentBuffer()
225: .getGenericValue(0, 0);
226:
227: // Set default values on the new record so they will be displayed for the user to see before saving.
228: // Owner ID
229: String activityOwnerId = (activityGV
230: .getString("activityOwnerId") == null) ? ""
231: : activityGV.getString("activityOwnerId");
232:
233: if (activityOwnerId.equals("")) {
234: activityGV.set("activityOwnerId", userInfo.getPartyId());
235: }
236:
237: // Activity Type
238: activityGV.set("statusId", "10");
239:
240: // Activity Date
241: String openDateString = (activityGV.getString("openDate") == null) ? ""
242: : activityGV.getString("openDate");
243:
244: if (openDateString.equals("")) {
245: java.util.Date openDate = new java.util.Date();
246:
247: activityGV.set("openDate", new java.sql.Date(openDate
248: .getTime()));
249: }
250:
251: /*
252: // Activity Start Time
253: SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
254: String openStartTimeString = timeFormat.format(openDate);
255: try {
256: if (DEBUG) Debug.logVerbose("-->[ActivityEventProcessor.postCreate] Setting activity start time.",module);
257: // java.util.Date openStartTime = timeFormat.parse("08:00");
258: java.util.Date openStartTime = timeFormat.parse(openStartTimeString);
259: activityGV.set("openStartTime", new java.sql.Timestamp(openStartTime.getTime()));
260: } catch (ParseException e) {
261: }
262:
263: // Activity End Time
264: java.util.Date delayedTime = new java.util.Date(openDate.getTime()+60*60*1000);
265: String openEndTimeString = timeFormat.format(delayedTime);
266: try {
267: if (DEBUG) Debug.logVerbose("-->[ActivityEventProcessor.postCreate] Setting activity start time.",module);
268: // java.util.Date openEndTime = timeFormat.parse("08:30");
269: java.util.Date openEndTime = timeFormat.parse(openEndTimeString);
270: activityGV.set("openEndTime", new java.sql.Timestamp(openEndTime.getTime()));
271: } catch (ParseException e) {
272: }
273: */
274: return STATUS_CONTINUE;
275: }
276:
277: /**
278: * DOCUMENT ME!
279: *
280: * @param userInfo
281: * @param delegator
282: * @param originatingEntityName
283: * @param entityGV
284: *
285: * @return
286: */
287: public int deleteAllRelated(UserInfo userInfo,
288: GenericDelegator delegator, String originatingEntityName,
289: GenericValue entityGV) {
290:
291: int status = STATUS_CONTINUE;
292:
293: // Delete related contacts.
294: status = deleteOneRelated(userInfo, delegator, entityGV, "",
295: "ActivityContact", originatingEntityName,
296: new GenericEventProcessor());
297:
298: if (status != STATUS_CONTINUE) {
299: return status;
300: }
301:
302: // Delete related FileAttachments. (Note: This must happen before the ActivityFiles are deleted.)
303: status = deleteOneRelated(userInfo, delegator, entityGV, "",
304: "FileAttachment", originatingEntityName,
305: new AbstractAttachmentEP());
306:
307: if (status != STATUS_CONTINUE) {
308: return status;
309: }
310:
311: // Delete related ActivityFiles. (Note: This must happen after the FileAttachments are deleted.)
312: status = deleteOneRelated(userInfo, delegator, entityGV, "",
313: "ActivityFile", originatingEntityName,
314: new GenericEventProcessor());
315:
316: if (status != STATUS_CONTINUE) {
317: return status;
318: }
319:
320: return STATUS_CONTINUE;
321: }
322:
323: /**
324: * DOCUMENT ME!
325: *
326: * @param userInfo
327: * @param delegator
328: * @param entityGV
329: * @param relationTitle
330: * @param relatedEntityName
331: *
332: * @return
333: */
334: public List findOneRelated(UserInfo userInfo,
335: GenericDelegator delegator, GenericValue entityGV,
336: String relationTitle, String relatedEntityName) {
337: // Find instances of an entity related to the current entity so the instances can be deleted.
338:
339: if (relatedEntityName.equals("FileAttachment")) {
340: // Finding related FileAttachment records. Need special processing because the relationship cannot be
341: // defined in the sfa-config.xml file.
342: // Get all the related ActivityFile records using the generic version of findOneRelated.
343: List activityFileGVL = super .findOneRelated(userInfo,
344: delegator, entityGV, "", "ActivityFile");
345:
346: // Make a List of FileAttachments tied to the identified ActivityFiles.
347: List fileAttachmenGVL = new LinkedList();
348: Iterator activityFileGVI = activityFileGVL.iterator();
349:
350: while (activityFileGVI.hasNext()) {
351: GenericValue activityFileGV = (GenericValue) activityFileGVI
352: .next();
353:
354: try {
355: GenericValue fileAttachmentGV = delegator
356: .getRelatedOne("FileAttachment",
357: activityFileGV);
358:
359: if (fileAttachmentGV != null) {
360: fileAttachmenGVL.add(fileAttachmentGV);
361: }
362: } catch (GenericEntityException e) {
363: Debug
364: .logWarning(
365: "[ActivityEventProcessor.findOneRelated] Error retrieving FileAttachment record: "
366: + e.getLocalizedMessage(),
367: module);
368: }
369: }
370:
371: return fileAttachmenGVL;
372: } else {
373: // Not finding FileAttachment records. Just use the standard processing using relations.
374: return super .findOneRelated(userInfo, delegator, entityGV,
375: relationTitle, relatedEntityName);
376: }
377: }
378:
379: /*
380: public SecurityLinkInfo getSecurityLinkInfo(UserInfo userInfo, GenericDelegator delegator)
381: {
382: return new SecurityLinkInfo( "Account", "accountId", false );
383: }
384: */
385: }
|