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.opportunity;
018:
019: import java.sql.Timestamp;
020: import java.util.Calendar;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.LinkedList;
024: import java.util.List;
025: import java.util.Vector;
026:
027: import org.ofbiz.base.util.Debug;
028: import org.ofbiz.entity.GenericDelegator;
029: import org.ofbiz.entity.GenericEntityException;
030: import org.ofbiz.entity.GenericValue;
031:
032: import com.sourcetap.sfa.activity.ActivityEventProcessor;
033: import com.sourcetap.sfa.attachment.AbstractAttachmentEP;
034: import com.sourcetap.sfa.event.DataMatrix;
035: import com.sourcetap.sfa.event.GenericEventProcessor;
036: import com.sourcetap.sfa.replication.GenericReplicator;
037: import com.sourcetap.sfa.security.EntityAccessEventProcessor;
038: import com.sourcetap.sfa.security.EntityAccessHelper;
039: import com.sourcetap.sfa.security.SecurityLinkInfo;
040: import com.sourcetap.sfa.security.SecurityWrapper;
041: import com.sourcetap.sfa.ui.UIScreenSectionEntity;
042: import com.sourcetap.sfa.util.UserInfo;
043:
044: /**
045: * DOCUMENT ME!
046: *
047: */
048: public class OpportunityEventProcessor extends GenericEventProcessor {
049: public static final String module = OpportunityEventProcessor.class
050: .getName();
051:
052: /**
053: * DOCUMENT ME!
054: *
055: * @param userInfo
056: * @param delegator
057: * @param dataMatrix
058: *
059: * @return
060: */
061: protected int preUpdate(UserInfo userInfo,
062: GenericDelegator delegator, DataMatrix dataMatrix) {
063:
064: GenericValue dealGV = dataMatrix.getCurrentBuffer()
065: .getGenericValue(0, 0);
066:
067: String dealId = dealGV.getString("dealId");
068:
069: Timestamp now = new Timestamp(Calendar.getInstance().getTime()
070: .getTime());
071: String userPartyId = userInfo.getPartyId();
072:
073: // set probability
074: try {
075: HashMap dealStageMap = new HashMap();
076: dealStageMap.put("stageId", dealGV.get("stageId"));
077:
078: GenericValue probGV = delegator.findByPrimaryKey(
079: "DealStage", dealStageMap);
080:
081: dealGV.set("probability", probGV.get("probability"));
082: } catch (GenericEntityException e) {
083: Debug
084: .logError(
085: "-->[OpportunityEventProcessor.preUpdate()] Error finding deal stage.",
086: module);
087:
088: Debug.logError(e, module);
089:
090: return STATUS_ERROR;
091: }
092:
093: // Set the time stamps.
094: dealGV.set("modifiedDate", now);
095:
096: // Store the current user ID in the "modified by" field.
097: dealGV.set("modifiedBy", userPartyId);
098:
099: // Set the deal owner ID if it is empty.
100: String ownerId = dealGV.getString("ownerId");
101:
102: if ((ownerId == null) || ownerId.equals("")) {
103: ownerId = userPartyId;
104: dealGV.set("ownerId", ownerId);
105: } else {
106: }
107:
108: // Update team, role, and access information.
109: SecurityWrapper.updateRoleInformation(dataMatrix, 0, userInfo,
110: ownerId, "Deal", dealId, delegator);
111:
112: return STATUS_CONTINUE;
113: }
114:
115: /**
116: * DOCUMENT ME!
117: *
118: * @param userInfo
119: * @param delegator
120: * @param dataMatrix
121: *
122: * @return
123: */
124: protected int preInsert(UserInfo userInfo,
125: GenericDelegator delegator, DataMatrix dataMatrix) {
126:
127: GenericValue dealGV = dataMatrix.getCurrentBuffer()
128: .getGenericValue(0, 0);
129:
130: Timestamp now = new Timestamp(Calendar.getInstance().getTime()
131: .getTime());
132: String userPartyId = userInfo.getPartyId();
133:
134: // Generate new keys for the opportunity.
135: String dealId = dealGV.getString("dealId");
136:
137: if ((dealId == null) || (dealId.equals(""))) {
138: dealId = GenericReplicator.getNextSeqId("Deal", delegator);
139: dealGV.set("dealId", dealId);
140: }
141:
142: // Set the deal owner ID if it is empty.
143: String ownerId = dealGV.getString("ownerId");
144:
145: if ((ownerId == null) || ownerId.equals("")) {
146: ownerId = userPartyId;
147:
148: dealGV.set("ownerId", ownerId);
149: } else {
150: }
151:
152: //add the team access info
153: SecurityWrapper.addRoleInformation(dataMatrix, 0, userInfo,
154: ownerId, "Deal", dealId, delegator);
155:
156: //set probability
157: try {
158: HashMap dealStageMap = new HashMap();
159: dealStageMap.put("stageId", dealGV.get("stageId"));
160:
161: GenericValue probGV = delegator.findByPrimaryKey(
162: "DealStage", dealStageMap);
163: dealGV.set("probability", probGV.get("probability"));
164: } catch (Exception e) {
165: Debug.logWarning(
166: "unable to set probability for opportunity stage: "
167: + dealGV.get("stageId"), module);
168: }
169:
170: dealGV.set("createdBy", userPartyId);
171: dealGV.set("createdDate", now);
172: dealGV.set("modifiedBy", userPartyId);
173: dealGV.set("modifiedDate", now);
174:
175: return STATUS_CONTINUE;
176: }
177:
178: /**
179: * DOCUMENT ME!
180: *
181: * @param userInfo
182: * @param entityNameList
183: * @param delegator
184: * @param dataMatrix
185: *
186: * @return
187: */
188: protected int postCreate(UserInfo userInfo, List entityNameList,
189: GenericDelegator delegator, DataMatrix dataMatrix) {
190:
191: // Get the empty generic values.
192: GenericValue dealGV = dataMatrix.getCurrentBuffer()
193: .getGenericValue(0, 0);
194:
195: // Set default values on the new record so they will be displayed for the user to see before saving.
196: // Owner ID
197: dealGV.set("ownerId", userInfo.getPartyId());
198:
199: // Amount
200: dealGV.set("amount", new Float(0));
201:
202: // Is In Forcast
203: dealGV.set("isInForecast", "N");
204:
205: return STATUS_CONTINUE;
206: }
207:
208: /**
209: * DOCUMENT ME!
210: *
211: * @param userInfo
212: * @param delegator
213: * @param originatingEntityName
214: * @param entityGV
215: *
216: * @return
217: */
218: public int deleteAllRelated(UserInfo userInfo,
219: GenericDelegator delegator, String originatingEntityName,
220: GenericValue entityGV) {
221:
222: int status = STATUS_CONTINUE;
223:
224: // Delete related OpportunityProducts.
225: status = deleteOneRelated(userInfo, delegator, entityGV, "",
226: "OpportunityProduct", originatingEntityName,
227: new GenericEventProcessor());
228:
229: if (status != STATUS_CONTINUE) {
230: return status;
231: }
232:
233: // Delete related OpportunityContacts.
234: status = deleteOneRelated(userInfo, delegator, entityGV, "",
235: "OpportunityContact", originatingEntityName,
236: new GenericEventProcessor());
237:
238: if (status != STATUS_CONTINUE) {
239: return status;
240: }
241:
242: // Delete related DealStageStepsTrackings.
243: status = deleteOneRelated(userInfo, delegator, entityGV, "",
244: "DealStageStepsTracking", originatingEntityName,
245: new GenericEventProcessor());
246:
247: if (status != STATUS_CONTINUE) {
248: return status;
249: }
250:
251: // Delete related OpportunityStatusTrackings.
252: status = deleteOneRelated(userInfo, delegator, entityGV, "",
253: "OpportunityStatusTracking", originatingEntityName,
254: new GenericEventProcessor());
255:
256: if (status != STATUS_CONTINUE) {
257: return status;
258: }
259:
260: // Delete related Expenses.
261: status = deleteOneRelated(userInfo, delegator, entityGV, "",
262: "Expense", originatingEntityName,
263: new GenericEventProcessor());
264:
265: if (status != STATUS_CONTINUE) {
266: return status;
267: }
268:
269: // Delete related FileAttachments. (Note: This must happen before the DealFiles are deleted.)
270: status = deleteOneRelated(userInfo, delegator, entityGV, "",
271: "FileAttachment", originatingEntityName,
272: new AbstractAttachmentEP());
273:
274: if (status != STATUS_CONTINUE) {
275: return status;
276: }
277:
278: // Delete related DealFiles. (Note: This must happen after the FileAttachments are deleted.)
279: status = deleteOneRelated(userInfo, delegator, entityGV, "",
280: "DealFile", originatingEntityName,
281: new GenericEventProcessor());
282:
283: if (status != STATUS_CONTINUE) {
284: return status;
285: }
286:
287: // Delete related parties.
288: status = deleteOneRelated(userInfo, delegator, entityGV, "",
289: "Party", originatingEntityName,
290: new GenericEventProcessor());
291:
292: if (status != STATUS_CONTINUE) {
293: return status;
294: }
295:
296: // Delete related entity accesses.
297: status = deleteOneRelated(userInfo, delegator, entityGV, "",
298: "EntityAccess", originatingEntityName,
299: new EntityAccessEventProcessor());
300:
301: if (status != STATUS_CONTINUE) {
302: return status;
303: }
304:
305: // Delete related activities.
306: status = deleteOneRelated(userInfo, delegator, entityGV, "",
307: "Activity", originatingEntityName,
308: new ActivityEventProcessor());
309:
310: if (status != STATUS_CONTINUE) {
311: return status;
312: }
313:
314: return STATUS_CONTINUE;
315: }
316:
317: /**
318: * DOCUMENT ME!
319: *
320: * @param userInfo
321: * @param delegator
322: * @param entityGV
323: * @param relationTitle
324: * @param relatedEntityName
325: *
326: * @return
327: */
328: public List findOneRelated(UserInfo userInfo,
329: GenericDelegator delegator, GenericValue entityGV,
330: String relationTitle, String relatedEntityName) {
331: // Find instances of an entity related to the current entity so the instances can be deleted.
332:
333: if (relatedEntityName.equals("EntityAccess")) {
334: // Finding related EntityAccess records. Need special processing because the relationship cannot be
335: // defined in the sfa-config.xml file.
336: return EntityAccessHelper.findRelated(userInfo, delegator,
337: entityGV, relationTitle);
338: } else if (relatedEntityName.equals("FileAttachment")) {
339: // Finding related FileAttachment records. Need special processing because the relationship cannot be
340: // defined in the sfa-config.xml file.
341: // Get all the related DealFile records using the generic version of findOneRelated.
342: List dealFileGVL = super .findOneRelated(userInfo,
343: delegator, entityGV, "", "DealFile");
344:
345: // Make a List of FileAttachments tied to the identified DealFiles.
346: List fileAttachmenGVL = new LinkedList();
347: Iterator dealFileGVI = dealFileGVL.iterator();
348:
349: while (dealFileGVI.hasNext()) {
350: GenericValue dealFileGV = (GenericValue) dealFileGVI
351: .next();
352:
353: try {
354: GenericValue fileAttachmentGV = delegator
355: .getRelatedOne("FileAttachment", dealFileGV);
356:
357: if (fileAttachmentGV != null) {
358: fileAttachmenGVL.add(fileAttachmentGV);
359: }
360: } catch (GenericEntityException e) {
361: Debug
362: .logError(
363: "[OpportunityEventProcessor.findOneRelated] Error retrieving FileAttachment record: "
364: + e.getLocalizedMessage(),
365: module);
366: }
367: }
368:
369: return fileAttachmenGVL;
370: } else {
371: // Not finding EntityAccess or FileAttachment records. Just use the standard processing using relations.
372: return super .findOneRelated(userInfo, delegator, entityGV,
373: relationTitle, relatedEntityName);
374: }
375: }
376:
377: /**
378: * DOCUMENT ME!
379: *
380: * @param mainGV
381: * @param relatedSearchClause
382: * @param outGVV
383: * @param userInfo
384: * @param delegator
385: *
386: * @return
387: */
388: protected GenericValue retrieveOneRelatedGV(GenericValue mainGV,
389: UIScreenSectionEntity relatedSearchClause, Vector outGVV,
390: UserInfo userInfo, GenericDelegator delegator) {
391:
392: String relationRelEntityName = relatedSearchClause
393: .getEntityName();
394:
395: if (relationRelEntityName.equals("EntityAccess")) {
396: // Special processing for the Entity Access record since it can't be retrieved with a relationship because
397: // two of the fields are string constants, which can't be entered into the sfa-config.xml file.
398:
399: // Get deal ID from the deal record.
400: String dealId = mainGV.getString("dealId");
401:
402: HashMap entityAccessFindMap = new HashMap();
403: entityAccessFindMap.put("entityId", dealId);
404: entityAccessFindMap.put("entity", "Deal");
405: entityAccessFindMap.put("partyEntityType", "Team");
406:
407: GenericValue entityAccessGV = null;
408:
409: try {
410: List entityAccessGVL = delegator.findByAnd(
411: "EntityAccess", entityAccessFindMap);
412: Iterator entityAccessGVI = entityAccessGVL.iterator();
413:
414: if (entityAccessGVI.hasNext()) {
415: // Entity access record was found.
416: entityAccessGV = (GenericValue) entityAccessGVI
417: .next();
418: } else {
419: // Entity access record was not found.
420: }
421:
422: return entityAccessGV;
423: } catch (GenericEntityException e) {
424: Debug
425: .logError(
426: "[OpportunityEventProcessor.retrieveOneRelatedGV] An error occurred while searching for "
427: + "the entity access record for the deal: "
428: + e.getLocalizedMessage(),
429: module);
430:
431: return entityAccessGV;
432: }
433: } else {
434: // Retrieve all other related entities the regular way.
435: return super .retrieveOneRelatedGV(mainGV,
436: relatedSearchClause, outGVV, userInfo, delegator);
437: }
438: }
439:
440: /**
441: * DOCUMENT ME!
442: *
443: * @param userInfo
444: * @param delegator
445: *
446: * @return
447: */
448: public SecurityLinkInfo getSecurityLinkInfo(UserInfo userInfo,
449: GenericDelegator delegator) {
450: return new SecurityLinkInfo("Deal", "dealId", true);
451: }
452: }
|