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.user;
018:
019: import java.sql.Timestamp;
020: import java.util.ArrayList;
021: import java.util.Calendar;
022: import java.util.Iterator;
023: import java.util.List;
024:
025: import org.ofbiz.base.util.Debug;
026: import org.ofbiz.base.util.UtilMisc;
027: import org.ofbiz.entity.GenericDelegator;
028: import org.ofbiz.entity.GenericPK;
029: import org.ofbiz.entity.GenericValue;
030:
031: import com.sourcetap.sfa.event.DataMatrix;
032: import com.sourcetap.sfa.event.GenericEventProcessor;
033: import com.sourcetap.sfa.replication.GenericReplicator;
034: import com.sourcetap.sfa.security.SecurityLinkInfo;
035: import com.sourcetap.sfa.security.SecurityWrapper;
036: import com.sourcetap.sfa.util.UserInfo;
037:
038: /**
039: * DOCUMENT ME!
040: *
041: */
042: public class UserEventProcessor extends GenericEventProcessor {
043: public static final String module = UserEventProcessor.class
044: .getName();
045:
046: /**
047: * DOCUMENT ME!
048: *
049: * @param userInfo
050: * @param delegator
051: * @param dataMatrix
052: *
053: * @return
054: */
055: protected int preUpdate(UserInfo userInfo,
056: GenericDelegator delegator, DataMatrix dataMatrix) {
057: Debug.logVerbose("-->[UserEventProcessor.preUpdate()]", module);
058:
059: // Process just the first row for now.
060: GenericValue contactGV = dataMatrix.getCurrentBuffer()
061: .getGenericValue(0, 0);
062: GenericValue partyGV = dataMatrix.getCurrentBuffer()
063: .getGenericValue(0, 1);
064: GenericValue userLoginGV = dataMatrix.getCurrentBuffer()
065: .getGenericValue(0, 2);
066: GenericValue accountGV = dataMatrix.getCurrentBuffer()
067: .getGenericValue(0, 3);
068: GenericValue addressGV = dataMatrix.getCurrentBuffer()
069: .getGenericValue(0, 4);
070:
071: // Copy the contact ID to the address owner ID in case it is not already filled in.
072: String contactId = contactGV.getString("contactId");
073: addressGV.set("addressOwnerId", contactId);
074:
075: // Copy the contact ID to the user Login Party ID in case it is not already filled in.
076: userLoginGV.set("partyId", contactId);
077:
078: Debug.logVerbose("userLogin=" + userLoginGV.toString(), module);
079:
080: // Set the time stamps.
081: contactGV.set("modifiedBy", userInfo.getPartyId());
082: addressGV.set("modifiedBy", userInfo.getPartyId());
083: contactGV.set("modifiedDate", new Timestamp(Calendar
084: .getInstance().getTime().getTime()));
085: addressGV.set("modifiedDate", new Timestamp(Calendar
086: .getInstance().getTime().getTime()));
087:
088: return STATUS_CONTINUE;
089: }
090:
091: /**
092: * DOCUMENT ME!
093: *
094: * @param userInfo
095: * @param delegator
096: * @param dataMatrix
097: *
098: * @return
099: */
100: protected int preInsert(UserInfo userInfo,
101: GenericDelegator delegator, DataMatrix dataMatrix) {
102: Debug.logVerbose("in preInsert", module);
103:
104: // Process just the first row for now.
105: GenericValue contactGV = dataMatrix.getCurrentBuffer()
106: .getGenericValue(0, 0);
107: GenericValue partyGV = dataMatrix.getCurrentBuffer()
108: .getGenericValue(0, 1);
109: GenericValue userLoginGV = dataMatrix.getCurrentBuffer()
110: .getGenericValue(0, 2);
111: GenericValue accountGV = dataMatrix.getCurrentBuffer()
112: .getGenericValue(0, 3);
113: GenericValue addressGV = dataMatrix.getCurrentBuffer()
114: .getGenericValue(0, 4);
115:
116: GenericValue roleGV = new GenericValue(delegator
117: .getModelEntity("Role"));
118: roleGV.setDelegator(delegator);
119: GenericValue partyRoleGV = new GenericValue(delegator
120: .getModelEntity("Party"));
121: partyRoleGV.setDelegator(delegator);
122: GenericValue contactAddressGV = new GenericValue(delegator
123: .getModelEntity("Address"));
124: contactAddressGV.setDelegator(delegator);
125: GenericValue contactPartyRoleGV = new GenericValue(delegator
126: .getModelEntity("PartyRole"));
127: contactPartyRoleGV.setDelegator(delegator);
128:
129: String roleId = GenericReplicator.getNextSeqId("Party",
130: delegator);
131: String contactId = GenericReplicator.getNextSeqId("Party",
132: delegator);
133: String accountId = GenericReplicator.getNextSeqId("Account",
134: delegator);
135: String addressId = GenericReplicator.getNextSeqId("Address",
136: delegator);
137: String contactAddressId = GenericReplicator.getNextSeqId(
138: "Address", delegator);
139:
140: partyRoleGV.set("partyId", roleId);
141:
142: roleGV.set("roleId", roleId);
143: roleGV.set("roleName", accountGV.getString("accountName")
144: + " Administrator");
145: roleGV.set("rolePath", roleId);
146: roleGV.set("accountId", accountId);
147:
148: contactAddressGV.set("addressId", contactAddressId);
149: contactAddressGV.set("addressOwnerId", contactId);
150: contactAddressGV.set("addressOwnerType", "Contact");
151: contactAddressGV.set("isPrimary", "Y");
152:
153: contactPartyRoleGV.set("partyId", contactId);
154: contactPartyRoleGV.set("roleTypeId", "SFA_ADMIN");
155:
156: contactGV.set("roleId", roleId);
157:
158: userInfo.setRoleId(roleId);
159: userInfo.setPartyId(contactId);
160: userInfo.setUserName(userLoginGV.getString("userLoginId"));
161: userInfo.setAccountId(accountId);
162:
163: contactGV.set("contactId", contactId);
164: contactGV.set("contactOwnerId", contactId);
165: contactGV.set("accountId", accountId);
166: partyGV.set("partyId", contactId);
167: userLoginGV.set("partyId", contactId);
168: addressGV.set("addressId", addressId);
169: addressGV.set("addressOwnerId", accountId);
170: addressGV.set("addressOwnerType", "Account");
171: accountGV.set("accountOwnerId", contactId);
172:
173: SecurityWrapper.addRoleInformation(dataMatrix, 0, userInfo, "",
174: "Account", accountId, delegator);
175:
176: //set the account as base
177: accountGV.set("accountId", accountId);
178: accountGV.set("accountTypeId", "base");
179:
180: // Make this address the primary one for the contact.
181: addressGV.set("isPrimary", "Y");
182:
183: Debug.logVerbose("userLogin=" + userLoginGV.toString(), module);
184:
185: Timestamp now = new Timestamp(Calendar.getInstance().getTime()
186: .getTime());
187: String userLoginId = userLoginGV.getString("userLoginId");
188:
189: // Set the time stamps.
190: contactGV.set("createdDate", now);
191: contactGV.set("createdBy", contactId);
192: contactGV.set("modifiedBy", contactId);
193: contactGV.set("modifiedDate", now);
194:
195: addressGV.set("createdDate", now);
196: addressGV.set("modifiedDate", now);
197: addressGV.set("createdBy", contactId);
198: addressGV.set("modifiedBy", contactId);
199:
200: contactAddressGV.set("createdDate", now);
201: contactAddressGV.set("modifiedDate", now);
202: contactAddressGV.set("createdBy", contactId);
203: contactAddressGV.set("modifiedBy", contactId);
204:
205: accountGV.set("createdDate", now);
206: accountGV.set("modifiedDate", now);
207: accountGV.set("createdBy", contactId);
208: accountGV.set("modifiedBy", contactId);
209:
210: // default hiredate on contact.
211: contactGV.set("hireDate", now);
212:
213: // Set the contact type to 'user'
214: contactGV.set("contactTypeId", "user");
215:
216: dataMatrix.addEntity("Party", false, true);
217: dataMatrix.getCurrentBuffer().getContentsRow(0)
218: .add(partyRoleGV);
219:
220: dataMatrix.addEntity("Role", false, true);
221: dataMatrix.getCurrentBuffer().getContentsRow(0).add(roleGV);
222:
223: dataMatrix.addEntity("Address", false, true);
224: dataMatrix.getCurrentBuffer().getContentsRow(0).add(
225: contactAddressGV);
226:
227: dataMatrix.addEntity("PartyRole", false, true);
228: dataMatrix.getCurrentBuffer().getContentsRow(0).add(
229: contactPartyRoleGV);
230:
231: return STATUS_CONTINUE;
232: }
233:
234: /**
235: * copy standard template data to this account.
236: * Create a customizable sales process by copying the default sales process
237: *
238: * @param userInfo
239: * @param delegator
240: * @param dataMatrix
241: *
242: * @return
243: */
244: protected int postInsert(UserInfo userInfo,
245: GenericDelegator delegator, DataMatrix dataMatrix) {
246: try {
247:
248: GenericValue contactGV = dataMatrix.getCurrentBuffer()
249: .getGenericValue(0, 0);
250: String contactId = contactGV.getString("contactId");
251: Timestamp now = new Timestamp(Calendar.getInstance()
252: .getTime().getTime());
253:
254: // copy the standard process to this company, so that it can be modified
255: GenericValue dealStageType = delegator
256: .findByPrimaryKey(new GenericPK(delegator
257: .getModelEntity("DealStageType"), UtilMisc
258: .toMap("dealStageTypeId", "1")));
259:
260: String origStageTypeId = dealStageType
261: .getString("dealStageTypeId");
262: String newStageTypeId = GenericReplicator.getNextSeqId(
263: "DealStageType", delegator);
264: dealStageType.set("dealStageTypeId", newStageTypeId);
265: dealStageType.set("accountId", userInfo.getAccountId());
266: // Set the time stamps.
267: dealStageType.set("createdDate", now);
268: dealStageType.set("createdBy", contactId);
269: dealStageType.set("modifiedBy", contactId);
270: dealStageType.set("modifiedDate", now);
271:
272: ArrayList data = new ArrayList();
273: data.add(dealStageType);
274:
275: List stages = delegator.findByAnd(delegator
276: .getModelEntity("DealStage"), UtilMisc.toMap(
277: "stageTypeId", origStageTypeId), null);
278: Iterator stageIter = stages.iterator();
279: while (stageIter.hasNext()) {
280: GenericValue stageGV = (GenericValue) stageIter.next();
281: String origStageId = stageGV.getString("stageId");
282: String newStageId = GenericReplicator.getNextSeqId(
283: "DealStage", delegator);
284: stageGV.set("stageId", newStageId);
285: stageGV.set("stageTypeId", newStageTypeId);
286:
287: // Set the time stamps.
288: stageGV.set("createdDate", now);
289: stageGV.set("createdBy", contactId);
290: stageGV.set("modifiedBy", contactId);
291: stageGV.set("modifiedDate", now);
292:
293: data.add(stageGV);
294:
295: List steps = delegator.findByAnd(delegator
296: .getModelEntity("DealStageSteps"), UtilMisc
297: .toMap("dealStageId", origStageId), null);
298: Iterator stepIter = steps.iterator();
299: while (stepIter.hasNext()) {
300: GenericValue stepGV = (GenericValue) stepIter
301: .next();
302:
303: String newStepId = GenericReplicator.getNextSeqId(
304: "DealStageSteps", delegator);
305: stepGV.set("dealStageStepId", newStepId);
306: stepGV.set("dealStageId", newStageId);
307: stepGV.set("dealStageTypeId", newStageTypeId);
308: // Set the time stamps.
309: stepGV.set("createdDate", now);
310: stepGV.set("createdBy", contactId);
311: stepGV.set("modifiedBy", contactId);
312: stepGV.set("modifiedDate", now);
313:
314: data.add(stepGV);
315: }
316:
317: }
318:
319: delegator.storeAll(data);
320:
321: } catch (Exception e) {
322: Debug.logError(
323: "Unable to copy Sales Process for new company",
324: module);
325: }
326:
327: return STATUS_CONTINUE;
328: }
329:
330: /**
331: * DOCUMENT ME!
332: *
333: * @param userInfo
334: * @param entityNameList
335: * @param delegator
336: * @param dataMatrix
337: *
338: * @return
339: */
340: protected int postCreate(UserInfo userInfo, List entityNameList,
341: GenericDelegator delegator, DataMatrix dataMatrix) {
342: Debug.logVerbose("[postCreate]", module);
343:
344: // Get the empty generic values.
345: // Process just the first row for now.
346: GenericValue addressGV = dataMatrix.getCurrentBuffer()
347: .getGenericValue(0, 4);
348:
349: // Country
350: Debug.logVerbose("Setting country to \"USA\"", module);
351:
352: addressGV.set("country", "USA");
353:
354: return STATUS_CONTINUE;
355: }
356:
357: /**
358: * DOCUMENT ME!
359: *
360: * @param userInfo
361: * @param delegator
362: *
363: * @return
364: */
365: public SecurityLinkInfo getSecurityLinkInfo(UserInfo userInfo,
366: GenericDelegator delegator) {
367: return new SecurityLinkInfo("Account", "accountId", true);
368: }
369: }
|