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.account;
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.base.util.UtilMisc;
029: import org.ofbiz.entity.GenericDelegator;
030: import org.ofbiz.entity.GenericEntityException;
031: import org.ofbiz.entity.GenericValue;
032: import org.ofbiz.entity.condition.EntityConditionList;
033: import org.ofbiz.entity.condition.EntityExpr;
034: import org.ofbiz.entity.condition.EntityOperator;
035: import org.ofbiz.entity.model.ModelEntity;
036:
037: import com.sourcetap.sfa.activity.ActivityEventProcessor;
038: import com.sourcetap.sfa.address.AddressHelper;
039: import com.sourcetap.sfa.attachment.AbstractAttachmentEP;
040: import com.sourcetap.sfa.contact.ContactEventProcessor;
041: import com.sourcetap.sfa.event.DataMatrix;
042: import com.sourcetap.sfa.event.GenericEventProcessor;
043: import com.sourcetap.sfa.forecast.ForecastEventProcessor;
044: import com.sourcetap.sfa.opportunity.OpportunityEventProcessor;
045: import com.sourcetap.sfa.replication.GenericReplicator;
046: import com.sourcetap.sfa.security.EntityAccessEventProcessor;
047: import com.sourcetap.sfa.security.EntityAccessHelper;
048: import com.sourcetap.sfa.security.SecurityLinkInfo;
049: import com.sourcetap.sfa.security.SecurityWrapper;
050: import com.sourcetap.sfa.ui.UIScreenSectionEntity;
051: import com.sourcetap.sfa.util.QueryInfo;
052: import com.sourcetap.sfa.util.UserInfo;
053:
054: /**
055: * DOCUMENT ME!
056: *
057: */
058: public class AccountEventProcessor extends GenericEventProcessor {
059: public static final String module = AccountEventProcessor.class
060: .getName();
061:
062: /**
063: * DOCUMENT ME!
064: *
065: * @param userInfo
066: * @param delegator
067: * @param dataMatrix
068: *
069: * @return
070: */
071: protected int preUpdate(UserInfo userInfo,
072: GenericDelegator delegator, DataMatrix dataMatrix) {
073:
074: Timestamp now = new Timestamp(Calendar.getInstance().getTime()
075: .getTime());
076:
077: // Just process the first row for now.
078: GenericValue accountGV = dataMatrix.getCurrentBuffer()
079: .getGenericValue(0, 0);
080: GenericValue addressGV = dataMatrix.getCurrentBuffer()
081: .getGenericValue(0, 1);
082: GenericValue partyGV = dataMatrix.getCurrentBuffer()
083: .getGenericValue(0, 2);
084:
085: String accountId = accountGV.getString("accountId");
086:
087: // Update the address if it is new.
088: String addressId = addressGV.getString("addressId");
089: String mailingAddress = AddressHelper.getMailingAddress(
090: addressGV.getString("mailingAddress"), addressGV
091: .getString("address1"), addressGV
092: .getString("address2"), addressGV
093: .getString("address3"));
094: addressGV.set("mailingAddress", mailingAddress);
095:
096: if ((addressId == null) || addressId.equals("")) {
097: addressId = GenericReplicator.getNextSeqId("Address",
098: delegator);
099: addressGV.set("addressId", addressId);
100: addressGV.set("addressOwnerId", accountId);
101: addressGV.set("addressOwnerType", "Account");
102: addressGV.set("isPrimary", "Y");
103: addressGV.set("createdDate", now);
104: addressGV.set("createdBy", userInfo.getPartyId());
105: }
106:
107: // Set the time stamps.
108: accountGV.set("modifiedDate", now);
109: addressGV.set("modifiedDate", now);
110:
111: // Store the current user's party ID in the "modified by" field.
112: accountGV.set("modifiedBy", userInfo.getPartyId());
113: addressGV.set("modifiedBy", userInfo.getPartyId());
114:
115: // Set the account owner ID if it is empty.
116: String accountOwnerId = accountGV.getString("accountOwnerId");
117:
118: if ((accountOwnerId == null) || accountOwnerId.equals("")) {
119: accountGV.set("accountOwnerId", userInfo.getPartyId());
120: }
121:
122: // Update team, role, and access information.
123: SecurityWrapper.updateRoleInformation(dataMatrix, 0, userInfo,
124: accountOwnerId, "Account", accountId, delegator);
125:
126: Debug.logVerbose("[preUpdate] Account GV: "
127: + accountGV.toString(), module);
128: Debug.logVerbose("[preUpdate] Address GV: "
129: + addressGV.toString(), module);
130: Debug.logVerbose("[preUpdate] Party GV: " + partyGV.toString(),
131: module);
132:
133: return STATUS_CONTINUE;
134: }
135:
136: /**
137: * DOCUMENT ME!
138: *
139: * @param userInfo
140: * @param delegator
141: * @param dataMatrix
142: *
143: * @return
144: */
145: protected int preInsert(UserInfo userInfo,
146: GenericDelegator delegator, DataMatrix dataMatrix) {
147:
148: // Just process the first row for now.
149: GenericValue accountGV = dataMatrix.getCurrentBuffer()
150: .getGenericValue(0, 0);
151: GenericValue addressGV = dataMatrix.getCurrentBuffer()
152: .getGenericValue(0, 1);
153: GenericValue partyGV = dataMatrix.getCurrentBuffer()
154: .getGenericValue(0, 2);
155:
156: String accountId = GenericReplicator.getNextSeqId("Party",
157: delegator);
158:
159: // Account key has been auto-generated. Just read it.
160: accountGV.set("accountId", accountId);
161:
162: String addressId = GenericReplicator.getNextSeqId("Address",
163: delegator);
164: addressGV.set("addressId", addressId);
165: addressGV.set("addressOwnerId", accountId);
166: addressGV.set("addressOwnerType", "Account");
167: addressGV.set("isPrimary", "Y");
168:
169: String mailingAddress = AddressHelper.getMailingAddress(
170: addressGV.getString("mailingAddress"), addressGV
171: .getString("address1"), addressGV
172: .getString("address2"), addressGV
173: .getString("address3"));
174: addressGV.set("mailingAddress", mailingAddress);
175:
176: // Mark the party ID the same as the account ID.
177: partyGV.set("partyId", accountId);
178:
179: // Set the time stamps.
180: Timestamp now = new Timestamp(Calendar.getInstance().getTime()
181: .getTime());
182: accountGV.set("createdDate", now);
183: addressGV.set("createdDate", now);
184: accountGV.set("modifiedDate", now);
185: addressGV.set("modifiedDate", now);
186:
187: // Store the current user ID in the "modified by" field.
188: accountGV.set("createdBy", userInfo.getPartyId());
189: addressGV.set("createdBy", userInfo.getPartyId());
190: accountGV.set("modifiedBy", userInfo.getPartyId());
191: addressGV.set("modifiedBy", userInfo.getPartyId());
192:
193: // Set the account owner ID if it is empty.
194: String accountOwnerId = accountGV.getString("accountOwnerId");
195:
196: if ((accountOwnerId == null) || accountOwnerId.equals("")) {
197: accountOwnerId = userInfo.getPartyId();
198: accountGV.set("accountOwnerId", accountOwnerId);
199: }
200:
201: // Add team, role, and access information.
202: SecurityWrapper.addRoleInformation(dataMatrix, 0, userInfo,
203: accountOwnerId, "Account", accountId, delegator);
204:
205: Debug.logVerbose("[preInsert] Account GV: "
206: + accountGV.toString(), module);
207: Debug.logVerbose("[preInsert] Address GV: "
208: + addressGV.toString(), module);
209: Debug.logVerbose("[preInsert] Party GV: " + partyGV.toString(),
210: module);
211:
212: return STATUS_CONTINUE;
213: }
214:
215: /**
216: * DOCUMENT ME!
217: *
218: * @param userInfo
219: * @param entityNameList
220: * @param delegator
221: * @param dataMatrix
222: *
223: * @return
224: */
225: protected int postCreate(UserInfo userInfo, List entityNameList,
226: GenericDelegator delegator, DataMatrix dataMatrix) {
227:
228: // Get the empty generic values.
229: GenericValue accountGV = dataMatrix.getCurrentBuffer()
230: .getGenericValue(0, 0);
231: GenericValue addressGV = dataMatrix.getCurrentBuffer()
232: .getGenericValue(0, 1);
233: GenericValue partyGV = dataMatrix.getCurrentBuffer()
234: .getGenericValue(0, 2);
235:
236: // Set default values on the new record so they will be displayed for the user to see before saving.
237: // Account owner ID
238: accountGV.set("accountOwnerId", userInfo.getPartyId());
239: accountGV.set("accountTypeId", "cust");
240:
241: addressGV.set("country", "USA");
242:
243: // Status
244: accountGV.set("statusId", "A");
245:
246: return STATUS_CONTINUE;
247: }
248:
249: /**
250: * DOCUMENT ME!
251: *
252: * @param userInfo
253: * @param delegator
254: * @param originatingEntityName
255: * @param entityGV
256: *
257: * @return
258: */
259: public int deleteAllRelated(UserInfo userInfo,
260: GenericDelegator delegator, String originatingEntityName,
261: GenericValue entityGV) {
262:
263: int status = STATUS_CONTINUE;
264:
265: // Delete related addresses.
266: status = deleteOneRelated(userInfo, delegator, entityGV, "",
267: "Address", originatingEntityName,
268: new GenericEventProcessor());
269:
270: if (status != STATUS_CONTINUE) {
271: return status;
272: }
273:
274: // Delete related contacts.
275: status = deleteOneRelated(userInfo, delegator, entityGV, "",
276: "Contact", originatingEntityName,
277: new ContactEventProcessor());
278:
279: if (status != STATUS_CONTINUE) {
280: return status;
281: }
282:
283: // Delete related activities.
284: status = deleteOneRelated(userInfo, delegator, entityGV, "",
285: "Activity", originatingEntityName,
286: new ActivityEventProcessor());
287:
288: if (status != STATUS_CONTINUE) {
289: return status;
290: }
291:
292: // Delete related opportunities.
293: status = deleteOneRelated(userInfo, delegator, entityGV, "",
294: "Deal", originatingEntityName,
295: new OpportunityEventProcessor());
296:
297: if (status != STATUS_CONTINUE) {
298: return status;
299: }
300:
301: // Delete related forecasts.
302: status = deleteOneRelated(userInfo, delegator, entityGV, "",
303: "Forecast", originatingEntityName,
304: new ForecastEventProcessor());
305:
306: if (status != STATUS_CONTINUE) {
307: return status;
308: }
309:
310: // Delete related parties.
311: status = deleteOneRelated(userInfo, delegator, entityGV, "",
312: "Party", originatingEntityName,
313: new GenericEventProcessor());
314:
315: if (status != STATUS_CONTINUE) {
316: return status;
317: }
318:
319: // Delete related entity accesses.
320: status = deleteOneRelated(userInfo, delegator, entityGV, "",
321: "EntityAccess", originatingEntityName,
322: new EntityAccessEventProcessor());
323:
324: if (status != STATUS_CONTINUE) {
325: return status;
326: }
327:
328: // Delete related FileAttachments. (Note: This must happen before the AccountFiles are deleted.)
329: status = deleteOneRelated(userInfo, delegator, entityGV, "",
330: "FileAttachment", originatingEntityName,
331: new AbstractAttachmentEP());
332:
333: if (status != STATUS_CONTINUE) {
334: return status;
335: }
336:
337: // Delete related AccountFiles. (Note: This must happen after the FileAttachments are deleted.)
338: status = deleteOneRelated(userInfo, delegator, entityGV, "",
339: "AccountFile", originatingEntityName,
340: new GenericEventProcessor());
341:
342: if (status != STATUS_CONTINUE) {
343: return status;
344: }
345:
346: return STATUS_CONTINUE;
347: }
348:
349: /**
350: * DOCUMENT ME!
351: *
352: * @param userInfo
353: * @param delegator
354: * @param entityGV
355: * @param relationTitle
356: * @param relatedEntityName
357: *
358: * @return
359: */
360: public List findOneRelated(UserInfo userInfo,
361: GenericDelegator delegator, GenericValue entityGV,
362: String relationTitle, String relatedEntityName) {
363: // Find instances of an entity related to the current entity so the instances
364: // can be deleted.
365:
366: if (relatedEntityName.equals("EntityAccess")) {
367: // Finding related EntityAccess records. Need special processing because
368: // the relationship cannot be defined in the sfa-config.xml file.
369: return EntityAccessHelper.findRelated(userInfo, delegator,
370: entityGV, relationTitle);
371: } else if (relatedEntityName.equals("FileAttachment")) {
372: // Finding related FileAttachment records. Need special processing because
373: // the relationship cannot be defined in the sfa-config.xml file.
374: // Get all the related AccountFile records using the generic version of findOneRelated.
375: List accountFileGVL = super .findOneRelated(userInfo,
376: delegator, entityGV, "", "AccountFile");
377:
378: // Make a List of FileAttachments tied to the identified AccountFiles.
379: List fileAttachmenGVL = new LinkedList();
380: Iterator accountFileGVI = accountFileGVL.iterator();
381:
382: while (accountFileGVI.hasNext()) {
383: GenericValue accountFileGV = (GenericValue) accountFileGVI
384: .next();
385:
386: try {
387: GenericValue fileAttachmentGV = delegator
388: .getRelatedOne("FileAttachment",
389: accountFileGV);
390:
391: if (fileAttachmentGV != null) {
392: fileAttachmenGVL.add(fileAttachmentGV);
393: }
394: } catch (GenericEntityException e) {
395: Debug
396: .logWarning(
397: "[AccountEventProcessor.findOneRelated] Error retrieving FileAttachment record: "
398: + e.getLocalizedMessage(),
399: module);
400: }
401: }
402:
403: return fileAttachmenGVL;
404: } else {
405: // Not finding EntityAccess or FileAttachment records. Just use the standard processing using relations.
406: return super .findOneRelated(userInfo, delegator, entityGV,
407: relationTitle, relatedEntityName);
408: }
409: }
410:
411: /**
412: * DOCUMENT ME!
413: *
414: * @param mainGV
415: * @param relatedSearchClause
416: * @param outGVV
417: * @param userInfo
418: * @param delegator
419: *
420: * @return
421: */
422: protected GenericValue retrieveOneRelatedGV(GenericValue mainGV,
423: UIScreenSectionEntity relatedSearchClause, Vector outGVV,
424: UserInfo userInfo, GenericDelegator delegator) {
425:
426: String relationRelEntityName = (String) relatedSearchClause
427: .getEntityName();
428:
429: if (relationRelEntityName.equals("Address")) {
430: // Special processing for the Address record since it can't be retrieved with a relationship
431: // defined in the sfa-config.xml file.
432:
433: // Get account ID from the account record.
434: String accountId = mainGV.getString("accountId");
435:
436: HashMap addressFindMap = new HashMap();
437: addressFindMap.put("addressOwnerId", accountId);
438: addressFindMap.put("isPrimary", "Y");
439:
440: GenericValue addressGV = null;
441:
442: try {
443: List addressGVL = delegator.findByAnd("Address",
444: addressFindMap);
445: Iterator addressGVI = addressGVL.iterator();
446:
447: if (addressGVI.hasNext()) {
448: // Address record was found.
449: addressGV = (GenericValue) addressGVI.next();
450:
451: } else {
452: // Address record was not found. Allow generic event processor to create an empty one.
453: }
454:
455: return addressGV;
456: } catch (GenericEntityException e) {
457: Debug
458: .logError(
459: "[AccountEventProcessor.retrieveOneRelatedGV] An error occurred while searching for "
460: + "the address record for the account: "
461: + e.getLocalizedMessage(),
462: module);
463:
464: return addressGV;
465: }
466: } else {
467: // Retrieve all other related entities the regular way.
468: return super .retrieveOneRelatedGV(mainGV,
469: relatedSearchClause, outGVV, userInfo, delegator);
470: }
471: }
472:
473: /**
474: * Add entity clauses for one related entity. This will join related tables to the query
475: * during a retrieve so query values can be entered that are in related entities.
476: * <P>
477: * This version overrides the ancestor to handle the Account entity, which
478: * has no relation defined.
479: *
480: * @author <a href='mailto:jnutting@sourcetap.com'>John Nutting</a>
481: *
482: * @param delegator Reference to the OFBIZ delegator being used to connect to the data base
483: * @param relationTitle Relation title
484: * @param relatedEntityName Name of related entity
485: * @param primaryEntityName Name of the primary entity
486: * @param primaryME ModelEntity object for the primary entity
487: * @param queryInfo critera to be used in search.
488: */
489: public void addOneRelationClause(GenericDelegator delegator,
490: String relationTitle, String relatedAndFields,
491: String relatedEntityName, String primaryEntityName,
492: ModelEntity primaryME, boolean isOuterJoin,
493: QueryInfo queryInfo) throws GenericEntityException {
494: if (relatedEntityName.equals("Address")) {
495: // Adding entity clauses for the Address entity. Need to build it manually.
496: // Join the address owner ID field
497:
498: queryInfo.addJoin("Account", "Address", Boolean
499: .valueOf(isOuterJoin), "accountId",
500: "addressOwnerId");
501:
502: if (isOuterJoin) {
503: queryInfo.addAlias("Address", "isPrimary", "isPrimary");
504: queryInfo.addAlias("Address", "addressId", "addressId");
505: queryInfo.addCondition(new EntityConditionList(UtilMisc
506: .toList(new EntityExpr("isPrimary",
507: EntityOperator.EQUALS, "Y"),
508: new EntityExpr("addressId",
509: EntityOperator.EQUALS, null)),
510: EntityOperator.OR));
511: } else
512: queryInfo.addCondition("Address", "isPrimary",
513: EntityOperator.EQUALS, "Y");
514:
515: } else {
516: // Use the parent script for all other related entities.
517: super .addOneRelationClause(delegator, relationTitle,
518: relatedAndFields, relatedEntityName,
519: primaryEntityName, primaryME, isOuterJoin,
520: queryInfo);
521: }
522: }
523:
524: /**
525: * DOCUMENT ME!
526: *
527: * @param userInfo
528: * @param delegator
529: *
530: * @return
531: */
532: public SecurityLinkInfo getSecurityLinkInfo(UserInfo userInfo,
533: GenericDelegator delegator) {
534: return new SecurityLinkInfo("Account", "accountId", true);
535: }
536: }
|