001: /**
002: * $Id: PSFTEPIUtils.java,v 1.8 2006/07/28 18:49:18 portalbld Exp $
003: * Copyright 2004 Sun Microsystems, Inc. All
004: * rights reserved. Use of this product is subject
005: * to license terms. Federal Acquisitions:
006: * Commercial Software -- Government Users
007: * Subject to Standard License Terms and
008: * Conditions.
009: *
010: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
011: * are trademarks or registered trademarks of Sun Microsystems,
012: * Inc. in the United States and other countries.
013: */package com.sun.portal.psftportlet.epiportlet;
014:
015: import com.sun.portal.iwayutil.connection.IWAYRequest;
016: import com.sun.portal.psftportlet.common.IWAYResponseParser;
017: import com.sun.portal.psftportlet.common.StreetAddress;
018: import com.sun.portal.psftportlet.common.PSFTPortletConstants;
019:
020: import com.sun.portal.log.common.PortalLogger;
021:
022: import java.util.Collections;
023: import java.util.Enumeration;
024: import java.util.Map;
025: import java.util.HashMap;
026: import java.util.ResourceBundle;
027: import java.util.Vector;
028: import java.util.Arrays;
029: import java.util.logging.Logger;
030:
031: /**
032: * This class has the utility methods used by PeopleSoft EPI portlet.
033: *
034: * @author Pradeep Gond
035: */
036:
037: public class PSFTEPIUtils {
038: private static Logger logger = PortalLogger
039: .getLogger(PSFTEPIUtils.class);
040: private Map maritalStatus;
041: private Map relationship;
042: private ResourceBundle rb;
043: private Vector maritalStatusValues;
044: private Vector relationshipValues;
045:
046: public PSFTEPIUtils(ResourceBundle rb) {
047: this .rb = rb;
048: init();
049: }
050:
051: private void init() {
052: initializeMaritalStatusMap();
053: initializeRelationshipMap();
054: Object[] obj = ((Map) maritalStatus
055: .get(PSFTPortletConstants.FORWARD)).values().toArray();
056: Arrays.sort(obj);
057: maritalStatusValues = new Vector(Arrays.asList(obj));
058: obj = ((Map) relationship.get(PSFTPortletConstants.FORWARD))
059: .values().toArray();
060: Arrays.sort(obj);
061: relationshipValues = new Vector(Arrays.asList(obj));
062: logger.fine("initialized.");
063: }
064:
065: private void initializeMaritalStatusMap() {
066: maritalStatus = getPropertiesMap(PSFTEPIConstants.MARITAL_STATUS_MAP);
067: }
068:
069: private void initializeRelationshipMap() {
070: relationship = getPropertiesMap(PSFTEPIConstants.RELATIONSHIP_MAP);
071: }
072:
073: private Map getPropertiesMap(String id) {
074: String mid = id + "[";
075: HashMap map = new HashMap(2);
076: Map forwardMap = new HashMap();
077: Map reverseMap = new HashMap();
078: Enumeration keys = rb.getKeys();
079:
080: while (keys.hasMoreElements()) {
081: String nextKey = keys.nextElement().toString();
082: if (nextKey.startsWith(mid)) {
083: String innerkey = nextKey.substring(mid.length(),
084: nextKey.indexOf(']'));
085: String value = rb.getString(nextKey);
086: forwardMap.put(innerkey, value);
087: reverseMap.put(value, innerkey);
088: }
089: }
090:
091: map.put(PSFTPortletConstants.FORWARD, Collections
092: .unmodifiableMap(forwardMap));
093: map.put(PSFTPortletConstants.REVERSE, Collections
094: .unmodifiableMap(reverseMap));
095:
096: return Collections.unmodifiableMap(map);
097: }
098:
099: public IWAYRequest getPersonalDataBrowseRequest(String userID) {
100: String iwayRequest = rb
101: .getString(PSFTEPIConstants.PERSONAL_DATA_BROWSE);
102: iwayRequest = iwayRequest.replaceAll(
103: PSFTEPIConstants.USER_ID_TAG, userID);
104: logger.info("iWayRequest => " + iwayRequest);
105:
106: return new IWAYRequest(iwayRequest);
107: }
108:
109: public IWAYRequest getEmergencyContactBrowseRequest(String userID) {
110: String iwayRequest = rb
111: .getString(PSFTEPIConstants.EMERGENCY_CONTACT_BROWSE);
112: iwayRequest = iwayRequest.replaceAll(
113: PSFTEPIConstants.USER_ID_TAG, userID);
114: logger.info("iWayRequest => " + iwayRequest);
115:
116: return new IWAYRequest(iwayRequest);
117: }
118:
119: public void parsePersonalData(IWAYResponseParser parser,
120: PSFTEPIObject obj) {
121: // employee Name
122: String[] resultArray = parser
123: .getComplexValues(PSFTEPIConstants.EMPLOYEE_NAME);
124: if (resultArray.length >= 3) {
125: obj.setEmployeeLastName(resultArray[0]);
126: obj.setEmployeeFirstName(resultArray[1]);
127: obj.setEmployeeNameRow(resultArray[2]);
128: }
129:
130: // home address
131: resultArray = parser
132: .getComplexValues(PSFTEPIConstants.HOME_ADDRESS);
133: if (resultArray.length >= 6) {
134: StreetAddress homeAddress = new StreetAddress(resultArray);
135: obj.setHomeAddress(homeAddress);
136: }
137:
138: // mail address
139: resultArray = parser
140: .getComplexValues(PSFTEPIConstants.MAIL_ADDRESS);
141: if (resultArray.length >= 6) {
142: StreetAddress mailAddress = new StreetAddress(resultArray);
143: obj.setMailAddress(mailAddress);
144: }
145:
146: // home phone number
147: resultArray = parser
148: .getNestedValues(PSFTEPIConstants.HOME_PHONE);
149: if (resultArray.length >= 2) {
150: obj.setPhoneNumber(resultArray[0]);
151: obj.setPhoneNumberRow(resultArray[1]);
152: }
153:
154: // home email address
155: resultArray = parser
156: .getNestedValues(PSFTEPIConstants.HOME_EMAIL);
157: if (resultArray.length >= 2) {
158: obj.setEmailID(resultArray[0]);
159: obj.setEmailIDRow(resultArray[1]);
160: }
161:
162: // marital status
163: resultArray = parser
164: .getNestedValues(PSFTEPIConstants.MARITAL_STATUS);
165: if (resultArray.length >= 2) {
166: obj.setMaritalStatus(getMaritalStatusValue(resultArray[0]));
167: obj.setMaritalStatusRow(resultArray[1]);
168: }
169: }
170:
171: private String getMaritalStatusValue(String key) {
172: String result = "";
173: Map map = (Map) maritalStatus.get(PSFTPortletConstants.FORWARD);
174:
175: if (map.containsKey(key)) {
176: result = (String) map.get(key);
177: }
178: logger.info(key + " => " + result);
179:
180: return result;
181: }
182:
183: public Vector getMaritalStatusValues() {
184: return maritalStatusValues;
185: }
186:
187: private String getMaritalStatusKey(String value) {
188: String result = "";
189: Map map = (Map) maritalStatus.get(PSFTPortletConstants.REVERSE);
190:
191: if (map.containsKey(value)) {
192: result = (String) map.get(value);
193: }
194: logger.info(value + " => " + result);
195:
196: return result;
197: }
198:
199: public void parseEmergencyContact(IWAYResponseParser parser,
200: PSFTEPIObject obj) {
201: String[] resultArray = parser
202: .getNestedValues(PSFTEPIConstants.EMERGENCY_CONTACT);
203: if (resultArray.length >= 2) {
204: obj.setEmergencyContact(resultArray[0]);
205: obj.setEmergencyContactRow(resultArray[1]);
206: }
207:
208: obj.setContactRelation(getRelationshipValue(parser
209: .getValue(PSFTEPIConstants.CONTACT_RELATION)));
210:
211: // contact address
212: resultArray = parser
213: .getComplexValues(PSFTEPIConstants.CONTACT_ADDRESS);
214: if (resultArray.length >= 6) {
215: StreetAddress contactAddress = new StreetAddress(
216: resultArray);
217: obj.setContactAddress(contactAddress);
218: }
219:
220: obj.setContactPhone(parser
221: .getValue(PSFTEPIConstants.CONTACT_PHONE));
222: }
223:
224: private String getRelationshipValue(String key) {
225: String result = "";
226: Map map = (Map) relationship.get(PSFTPortletConstants.FORWARD);
227:
228: if (map.containsKey(key)) {
229: result = (String) map.get(key);
230: }
231: logger.info(key + " => " + result);
232:
233: return result;
234: }
235:
236: public Vector getRelationshipValues() {
237: return relationshipValues;
238: }
239:
240: private String getRelationshipKey(String value) {
241: String result = "";
242: Map map = (Map) relationship.get(PSFTPortletConstants.REVERSE);
243:
244: if (map.containsKey(value)) {
245: result = (String) map.get(value);
246: }
247: logger.info(value + " => " + result);
248:
249: return result;
250: }
251:
252: public IWAYRequest getPersonalDataUpdateRequest(String userID,
253: PSFTEPIObject origObj, PSFTEPIObject newObj) {
254: String iwayRequest = rb
255: .getString(PSFTEPIConstants.PERSONAL_DATA_UPDATE);
256: iwayRequest = iwayRequest.replaceAll(
257: PSFTEPIConstants.USER_ID_TAG, userID);
258:
259: // create fields form the PSFTEPIObject
260: StringBuffer fields = new StringBuffer();
261:
262: // employee name
263: String value = rb.getString(PSFTEPIConstants.EMPLOYEE_NAME);
264: String[] tokens = value.split(PSFTPortletConstants.DELIM);
265: if (tokens.length >= 7) {
266: fields.append(
267: createUpdateNode(tokens[1], origObj
268: .getEmployeeNameRow())).append(
269: createUpdateNode(tokens[4], "1")).append(
270: createNodeWithValue(tokens[5], newObj
271: .getEmployeeLastName())).append(
272: createNodeWithValue(tokens[6], newObj
273: .getEmployeeFirstName())).append(
274: createEndElement(tokens[4])).append(
275: createEndElement(tokens[1]));
276: }
277:
278: // home Address
279: value = rb.getString(PSFTEPIConstants.HOME_ADDRESS);
280: fields.append(createStreeAddressNode(value, newObj
281: .getHomeAddress(), origObj.getHomeAddress().getRow()));
282:
283: // mail Address
284: value = rb.getString(PSFTEPIConstants.MAIL_ADDRESS);
285: fields.append(createStreeAddressNode(value, newObj
286: .getMailAddress(), origObj.getMailAddress().getRow()));
287:
288: // home phone
289: value = rb.getString(PSFTEPIConstants.HOME_PHONE);
290: fields.append(createNodeFromNestedType(value, newObj
291: .getPhoneNumber(), origObj.getPhoneNumberRow()));
292:
293: // home email
294: value = rb.getString(PSFTEPIConstants.HOME_EMAIL);
295: fields.append(createNodeFromNestedType(value, newObj
296: .getEmailID(), origObj.getEmailIDRow()));
297:
298: // marital status
299: value = rb.getString(PSFTEPIConstants.MARITAL_STATUS);
300: fields.append(createNodeFromNestedType(value,
301: getMaritalStatusKey(newObj.getMaritalStatus()), origObj
302: .getMaritalStatusRow()));
303:
304: iwayRequest = iwayRequest.replaceAll(
305: PSFTEPIConstants.FIELDS_TAG, fields.toString());
306: logger.fine("iWayrequest => " + iwayRequest);
307: return new IWAYRequest(iwayRequest);
308: }
309:
310: public IWAYRequest getEmergencyContactUpdateRequest(String userID,
311: PSFTEPIObject origObj, PSFTEPIObject newObj) {
312: String iwayRequest = rb
313: .getString(PSFTEPIConstants.EMERGENCY_CONTACT_UPDATE);
314: iwayRequest = iwayRequest.replaceAll(
315: PSFTEPIConstants.USER_ID_TAG, userID);
316:
317: // create fields form the PSFTEPIObject
318: StringBuffer fields = new StringBuffer();
319:
320: // emergency contact
321: String value = rb.getString(PSFTEPIConstants.EMERGENCY_CONTACT);
322: fields.append(createNodeFromNestedType(value, newObj
323: .getEmergencyContact(), origObj
324: .getEmergencyContactRow()));
325:
326: // emergency contact relationship
327: value = rb.getString(PSFTEPIConstants.CONTACT_RELATION);
328: fields.append(createNodeFromNestedType(value,
329: getRelationshipKey(newObj.getContactRelation()),
330: origObj.getEmergencyContactRow()));
331:
332: // emergency contact address
333: value = rb.getString(PSFTEPIConstants.CONTACT_ADDRESS);
334: String[] tokens = value.split(PSFTPortletConstants.DELIM);
335: if (tokens.length >= 10) {
336: StreetAddress addr = newObj.getContactAddress();
337: fields.append(
338: createUpdateNode(tokens[1], origObj
339: .getEmergencyContactRow()))
340: .append(
341: createNodeWithValue(tokens[5], addr
342: .getAddrLine1())).append(
343: createNodeWithValue(tokens[6], addr
344: .getAddrLine2())).append(
345: createNodeWithValue(tokens[7], addr
346: .getCity())).append(
347: createNodeWithValue(tokens[8], addr
348: .getState())).append(
349: createNodeWithValue(tokens[9], addr
350: .getPin())).append(
351: createEndElement(tokens[1]));
352: }
353:
354: // emergency contact phone
355: value = rb.getString(PSFTEPIConstants.CONTACT_PHONE);
356: fields.append(createNodeFromNestedType(value, newObj
357: .getContactPhone(), origObj.getEmergencyContactRow()));
358:
359: iwayRequest = iwayRequest.replaceAll(
360: PSFTEPIConstants.FIELDS_TAG, fields.toString());
361: logger.fine("iWayRequest => " + iwayRequest);
362:
363: return new IWAYRequest(iwayRequest);
364: }
365:
366: private String createStreeAddressNode(String value,
367: StreetAddress addr, String row) {
368: StringBuffer result = new StringBuffer();
369: String[] tokens = value.split(PSFTPortletConstants.DELIM);
370: if (tokens.length >= 10) {
371: result.append(createUpdateNode(tokens[1], row)).append(
372: createUpdateNode(tokens[4], "1"))
373: .append(
374: createNodeWithValue(tokens[5], addr
375: .getAddrLine1())).append(
376: createNodeWithValue(tokens[6], addr
377: .getAddrLine2())).append(
378: createNodeWithValue(tokens[7], addr
379: .getCity())).append(
380: createNodeWithValue(tokens[8], addr
381: .getState())).append(
382: createNodeWithValue(tokens[9], addr
383: .getPin())).append(
384: createEndElement(tokens[4])).append(
385: createEndElement(tokens[1]));
386: }
387:
388: return result.toString();
389: }
390:
391: private String createNodeFromNestedType(String value,
392: String nodeValue, String row) {
393: StringBuffer result = new StringBuffer();
394: String[] tokens = value.split(PSFTPortletConstants.DELIM);
395: if (tokens.length >= 5) {
396: result.append(createUpdateNode(tokens[1], row)).append(
397: createNodeWithValue(tokens[4], nodeValue)).append(
398: createEndElement(tokens[1]));
399: }
400:
401: return result.toString();
402: }
403:
404: private String createUpdateNode(String nodeName, String row) {
405: StringBuffer result = new StringBuffer();
406: result.append("<").append(nodeName).append(" row='")
407: .append(row).append("' action='update'>");
408:
409: return result.toString();
410: }
411:
412: private String createNodeWithValue(String nodeName, String value) {
413: StringBuffer result = new StringBuffer();
414: result.append(createStartElement(nodeName)).append(value)
415: .append(createEndElement(nodeName));
416:
417: return result.toString();
418: }
419:
420: private String createStartElement(String name) {
421: return "<" + name + ">";
422: }
423:
424: private String createEndElement(String name) {
425: return "</" + name + ">";
426: }
427: }
|