001: /*
002: * $Id: PartyWorker.java,v 1.2 2003/12/17 21:29:10 ajzeneski Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.party.party;
026:
027: import java.util.HashMap;
028: import java.util.Iterator;
029: import java.util.Map;
030:
031: import javax.servlet.ServletRequest;
032: import javax.servlet.jsp.PageContext;
033:
034: import org.ofbiz.base.util.Debug;
035: import org.ofbiz.base.util.UtilMisc;
036: import org.ofbiz.base.util.UtilFormatOut;
037: import org.ofbiz.base.util.UtilValidate;
038: import org.ofbiz.entity.GenericDelegator;
039: import org.ofbiz.entity.GenericEntityException;
040: import org.ofbiz.entity.GenericValue;
041:
042: /**
043: * Worker methods for Party Information
044: *
045: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
046: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
047: * @version $Revision: 1.2 $
048: * @since 2.0
049: */
050: public class PartyWorker {
051:
052: public static String module = PartyWorker.class.getName();
053:
054: public static Map getPartyOtherValues(ServletRequest request,
055: String partyId, String partyAttr, String personAttr,
056: String partyGroupAttr) {
057: GenericDelegator delegator = (GenericDelegator) request
058: .getAttribute("delegator");
059: Map result = new HashMap();
060: try {
061: GenericValue party = delegator.findByPrimaryKey("Party",
062: UtilMisc.toMap("partyId", partyId));
063:
064: if (party != null)
065: result.put(partyAttr, party);
066: } catch (GenericEntityException e) {
067: Debug
068: .logWarning(e, "Problems getting Party entity",
069: module);
070: }
071:
072: try {
073: GenericValue person = delegator.findByPrimaryKey("Person",
074: UtilMisc.toMap("partyId", partyId));
075:
076: if (person != null)
077: result.put(personAttr, person);
078: } catch (GenericEntityException e) {
079: Debug.logWarning(e, "Problems getting Person entity",
080: module);
081: }
082:
083: try {
084: GenericValue partyGroup = delegator.findByPrimaryKey(
085: "PartyGroup", UtilMisc.toMap("partyId", partyId));
086:
087: if (partyGroup != null)
088: result.put(partyGroupAttr, partyGroup);
089: } catch (GenericEntityException e) {
090: Debug.logWarning(e, "Problems getting PartyGroup entity",
091: module);
092: }
093: return result;
094: }
095:
096: public static void getPartyOtherValues(PageContext pageContext,
097: String partyId, String partyAttr, String personAttr,
098: String partyGroupAttr) {
099: Map partyMap = getPartyOtherValues(pageContext.getRequest(),
100: partyId, partyAttr, personAttr, partyGroupAttr);
101: Iterator i = partyMap.entrySet().iterator();
102: while (i.hasNext()) {
103: Map.Entry e = (Map.Entry) i.next();
104: pageContext.setAttribute((String) e.getKey(), e.getValue());
105:
106: }
107: }
108:
109: /**
110: * Generate a sequenced club id using the prefix passed and a sequence value + check digit
111: * @param delegator used to obtain a sequenced value
112: * @param prefix prefix inserted at the beginning of the ID
113: * @param length total length of the ID including prefix and check digit
114: * @return Sequenced Club ID string with a length as defined starting with the prefix defined
115: */
116: public static String createClubId(GenericDelegator delegator,
117: String prefix, int length) {
118: final String clubSeqName = "PartyClubSeq";
119: String clubId = prefix != null ? prefix : "";
120:
121: // generate the sequenced number and pad
122: Long seq = delegator.getNextSeqId(clubSeqName);
123: clubId = clubId
124: + UtilFormatOut.formatPaddedNumber(seq.longValue(),
125: (length - prefix.length() - 1));
126:
127: // get the check digit
128: int check = UtilValidate.getLuhnCheckDigit(clubId);
129: clubId = clubId + new Integer(check).toString();
130:
131: return clubId;
132: }
133: }
|