KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > party > party > PartyWorker


1 /*
2  * $Id: PartyWorker.java 5905 2005-10-04 02:48:02Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.party.party;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Locale JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import javax.servlet.ServletRequest JavaDoc;
34 import javax.servlet.jsp.PageContext JavaDoc;
35
36 import org.ofbiz.base.util.Debug;
37 import org.ofbiz.base.util.UtilMisc;
38 import org.ofbiz.base.util.UtilFormatOut;
39 import org.ofbiz.base.util.UtilValidate;
40 import org.ofbiz.entity.GenericDelegator;
41 import org.ofbiz.entity.GenericEntityException;
42 import org.ofbiz.entity.GenericValue;
43 import org.ofbiz.entity.model.ModelEntity;
44 import org.ofbiz.entity.util.EntityUtil;
45
46 /**
47  * Worker methods for Party Information
48  *
49  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
50  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
51  * @version $Rev: 5905 $
52  * @since 2.0
53  */

54 public class PartyWorker {
55     
56     public static String JavaDoc module = PartyWorker.class.getName();
57     
58     public static Map JavaDoc getPartyOtherValues(ServletRequest JavaDoc request, String JavaDoc partyId, String JavaDoc partyAttr, String JavaDoc personAttr, String JavaDoc partyGroupAttr) {
59         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
60         Map JavaDoc result = new HashMap JavaDoc();
61         try {
62             GenericValue party = delegator.findByPrimaryKey("Party", UtilMisc.toMap("partyId", partyId));
63
64             if (party != null)
65                 result.put(partyAttr, party);
66         } catch (GenericEntityException e) {
67             Debug.logWarning(e, "Problems getting Party entity", module);
68         }
69
70         try {
71             GenericValue person = delegator.findByPrimaryKey("Person", UtilMisc.toMap("partyId", partyId));
72
73             if (person != null)
74                 result.put(personAttr, person);
75         } catch (GenericEntityException e) {
76             Debug.logWarning(e, "Problems getting Person entity", module);
77         }
78
79         try {
80             GenericValue partyGroup = delegator.findByPrimaryKey("PartyGroup", UtilMisc.toMap("partyId", partyId));
81
82             if (partyGroup != null)
83                 result.put(partyGroupAttr, partyGroup);
84         } catch (GenericEntityException e) {
85             Debug.logWarning(e, "Problems getting PartyGroup entity", module);
86         }
87         return result;
88     }
89     
90     public static void getPartyOtherValues(PageContext JavaDoc pageContext, String JavaDoc partyId, String JavaDoc partyAttr, String JavaDoc personAttr, String JavaDoc partyGroupAttr) {
91         Map JavaDoc partyMap = getPartyOtherValues(pageContext.getRequest(), partyId, partyAttr, personAttr, partyGroupAttr);
92         Iterator JavaDoc i = partyMap.entrySet().iterator();
93         while (i.hasNext()) {
94             Map.Entry JavaDoc e = (Map.Entry JavaDoc) i.next();
95             pageContext.setAttribute((String JavaDoc) e.getKey(), e.getValue());
96             
97         }
98     }
99
100     /**
101      * Generate a sequenced club id using the prefix passed and a sequence value + check digit
102      * @param delegator used to obtain a sequenced value
103      * @param prefix prefix inserted at the beginning of the ID
104      * @param length total length of the ID including prefix and check digit
105      * @return Sequenced Club ID string with a length as defined starting with the prefix defined
106      */

107     public static String JavaDoc createClubId(GenericDelegator delegator, String JavaDoc prefix, int length) {
108         final String JavaDoc clubSeqName = "PartyClubSeq";
109         String JavaDoc clubId = prefix != null ? prefix : "";
110
111         // generate the sequenced number and pad
112
Long JavaDoc seq = delegator.getNextSeqIdLong(clubSeqName);
113         clubId = clubId + UtilFormatOut.formatPaddedNumber(seq.longValue(), (length - prefix.length() - 1));
114
115         // get the check digit
116
int check = UtilValidate.getLuhnCheckDigit(clubId);
117         clubId = clubId + new Integer JavaDoc(check).toString();
118
119         return clubId;
120     }
121
122     public static GenericValue findPartyLatestUserLogin(String JavaDoc partyId, GenericDelegator delegator) {
123         try {
124             List JavaDoc userLoginList = delegator.findByAnd("UserLogin", UtilMisc.toMap("partyId", partyId), UtilMisc.toList("-" + ModelEntity.STAMP_FIELD));
125             return EntityUtil.getFirst(userLoginList);
126         } catch (GenericEntityException e) {
127             Debug.logError(e, "Error while finding latest UserLogin for party with ID [" + partyId + "]: " + e.toString(), module);
128             return null;
129         }
130     }
131     
132     public static Locale JavaDoc findPartyLastLocale(String JavaDoc partyId, GenericDelegator delegator) {
133         // just get the most recent UserLogin for this party, if there is one...
134
GenericValue userLogin = findPartyLatestUserLogin(partyId, delegator);
135         if (userLogin == null) {
136             return null;
137         }
138         String JavaDoc localeString = userLogin.getString("lastLocale");
139         if (UtilValidate.isNotEmpty(localeString)) {
140             return UtilMisc.parseLocale(localeString);
141         } else {
142             return null;
143         }
144     }
145 }
146
Popular Tags