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.contact;
018:
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.List;
022:
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpServletResponse;
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:
031: import com.sourcetap.sfa.event.DataMatrix;
032: import com.sourcetap.sfa.event.GenericEventProcessor;
033: import com.sourcetap.sfa.event.GenericWebEventProcessor;
034: import com.sourcetap.sfa.ui.UICache;
035: import com.sourcetap.sfa.ui.UIWebScreenSection;
036: import com.sourcetap.sfa.util.UserInfo;
037:
038: /**
039: * DOCUMENT ME!
040: *
041: */
042: public class ContactWebEventProcessor extends GenericWebEventProcessor {
043: public static final String module = ContactWebEventProcessor.class
044: .getName();
045:
046: /**
047: * DOCUMENT ME!
048: *
049: * @param userInfo
050: * @param uiWebScreenSection
051: * @param request
052: * @param response
053: * @param delegator
054: * @param eventProcessor
055: * @param dataMatrix
056: * @param uiCache
057: *
058: * @return
059: */
060: protected int preInsert(UserInfo userInfo,
061: UIWebScreenSection uiWebScreenSection,
062: HttpServletRequest request, HttpServletResponse response,
063: GenericDelegator delegator,
064: GenericEventProcessor eventProcessor,
065: DataMatrix dataMatrix, UICache uiCache) {
066:
067: // If the screen section is on the composite account create window, need to copy the account ID
068: // from the account to the contact.
069:
070: if (request.getAttribute("com.sourcetap.sfa.account.accountId") != null) {
071: String accountId = (String) request
072: .getAttribute("com.sourcetap.sfa.account.accountId");
073: GenericValue contactGV = dataMatrix.getCurrentBuffer()
074: .getGenericValue(0, 0);
075:
076: contactGV.set("accountId", accountId);
077: }
078:
079: return STATUS_CONTINUE;
080: }
081:
082: /**
083: * DOCUMENT ME!
084: *
085: * @param userInfo
086: * @param uiWebScreenSection
087: * @param request
088: * @param response
089: * @param delegator
090: * @param eventProcessor
091: * @param dataMatrix
092: * @param entityNameList
093: *
094: * @return
095: */
096: protected int postCreate(UserInfo userInfo,
097: UIWebScreenSection uiWebScreenSection,
098: HttpServletRequest request, HttpServletResponse response,
099: GenericDelegator delegator,
100: GenericEventProcessor eventProcessor,
101: DataMatrix dataMatrix, List entityNameList) {
102:
103: GenericValue contactGV = dataMatrix.getCurrentBuffer()
104: .getGenericValue(0, 0);
105: GenericValue addressGV = dataMatrix.getCurrentBuffer()
106: .getGenericValue(0, 1);
107:
108: // Copy the account address to the new contact. This is done here instead of the regular
109: // event processor because the web event processor has to set the account ID fist so we
110: // can use it to look up the address.
111: if (contactGV.getString("accountId") != null) {
112: // The account ID is filled in. Get it.
113: String accountId = contactGV.getString("accountId");
114:
115: HashMap accountFindMap = new HashMap();
116: accountFindMap.put("accountId", accountId);
117:
118: try {
119:
120: GenericValue accountGV = delegator.findByPrimaryKey(
121: "Account", accountFindMap);
122:
123: if (accountGV != null) {
124:
125: String phone = accountGV.getString("phone");
126: String fax = accountGV.getString("fax");
127:
128: contactGV.set("businessPhone", phone);
129: contactGV.set("faxPhone", fax);
130: }
131: } catch (GenericEntityException e) {
132: // An error occurred while looking for the account record.
133: Debug.logError(
134: "-->[ContactWebEventProcessor.postCreate] Error looking for account: "
135: + e.getLocalizedMessage(), module);
136: }
137:
138: HashMap addressFindMap = new HashMap();
139: addressFindMap.put("addressOwnerId", accountId);
140: addressFindMap.put("isPrimary", "Y");
141:
142: try {
143:
144: List accountAddressGVL = delegator.findByAnd("Address",
145: addressFindMap);
146:
147: if (accountAddressGVL.size() > 0) {
148: // The account has an address. Copy the address fields to the contact's address.
149:
150: Iterator accountAddressGVI = accountAddressGVL
151: .iterator();
152: GenericValue accountAddressGV = (GenericValue) accountAddressGVI
153: .next();
154: String mailingAddress = accountAddressGV
155: .getString("mailingAddress");
156: String city = accountAddressGV.getString("city");
157: String state = accountAddressGV.getString("state");
158: String zip = accountAddressGV.getString("zip");
159: String country = accountAddressGV
160: .getString("country");
161:
162: addressGV.set("mailingAddress", mailingAddress);
163: addressGV.set("city", city);
164: addressGV.set("state", state);
165: addressGV.set("zip", zip);
166: addressGV.set("country", country);
167: }
168: } catch (GenericEntityException e) {
169: // An error occurred while looking for the contact record. Don't set the account ID.
170: Debug.logError(
171: "-->[ContactWebEventProcessor.postCreate] Error looking for address: "
172: + e.getLocalizedMessage(), module);
173: }
174: }
175:
176: return STATUS_CONTINUE;
177: }
178:
179: /**
180: * DOCUMENT ME!
181: *
182: * @param userInfo
183: * @param uiWebScreenSection
184: * @param request
185: * @param response
186: * @param delegator
187: * @param eventProcessor
188: * @param dataMatrix
189: * @param uiCache
190: *
191: * @return
192: */
193: protected int postInsert(UserInfo userInfo,
194: UIWebScreenSection uiWebScreenSection,
195: HttpServletRequest request, HttpServletResponse response,
196: GenericDelegator delegator,
197: GenericEventProcessor eventProcessor,
198: DataMatrix dataMatrix, UICache uiCache) {
199:
200: // Need to get the contact ID and store it
201: // in the request object so the other event processors can read it and store
202: // it on the other entities being created.
203: GenericValue contactGV = dataMatrix.getCurrentBuffer()
204: .getGenericValue(0, 0);
205: String contactId = contactGV.getString("contactId");
206:
207: request.setAttribute("com.sourcetap.sfa.contact.contactId",
208: contactId);
209:
210: return STATUS_CONTINUE;
211: }
212:
213: /**
214: * This function gets the application path to be used to reconstruct the URI when a
215: * UI History record is logged. Example: "/accounts"
216: * @author John Nutting
217: * @param url The URL used to open the screen section
218: * @return String containing the application path
219: */
220: protected String getUiHistoryAppPath(String url) {
221: return "/contacts";
222: }
223:
224: /**
225: * This function gets the description to store in the UI history table. This description
226: * will show up in the UI History drop list.
227: * @author John Nutting
228: * @param dataMatrix DataMatrix object containing the data from the screen
229: * @param delegator Generic delegator through which the data base is accessed
230: * @param action The action being performed on the screen
231: * @param uiWebScreenSection The UIWebScreenSection being used to construct the screen section
232: * @return String containing the UI History description
233: */
234: protected String getUiHistoryDescription(DataMatrix dataMatrix,
235: GenericDelegator delegator, String action,
236: UIWebScreenSection uiWebScreenSection) {
237: GenericValue primaryGV = dataMatrix.getCurrentBuffer()
238: .getGenericValue(0, 0);
239:
240: return "Contact: " + primaryGV.getString("firstName") + " "
241: + primaryGV.getString("lastName");
242: }
243: }
|