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.util;
018:
019: import java.util.List;
020:
021: import org.ofbiz.base.util.Debug;
022: import org.ofbiz.base.util.StringUtil;
023: import org.ofbiz.base.util.UtilCache;
024: import org.ofbiz.base.util.UtilMisc;
025: import org.ofbiz.entity.GenericDelegator;
026: import org.ofbiz.entity.GenericEntityException;
027: import org.ofbiz.entity.GenericPK;
028: import org.ofbiz.entity.GenericValue;
029:
030: /**
031: * <p><b>Title:</b> Preference handler
032: * <p><b>Description:</b> Preference class - contains methods to check party preferences.
033: * <p>Copyright (c) 2001 The Open For Business Project - www.ofbiz.org
034: *
035: * <p>Permission is hereby granted, free of charge, to any person obtaining a
036: * copy of this software and associated documentation files (the "Software"),
037: * to deal in the Software without restriction, including without limitation
038: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
039: * and/or sell copies of the Software, and to permit persons to whom the
040: * Software is furnished to do so, subject to the following conditions:
041: *
042: * <p>The above copyright notice and this permission notice shall be included
043: * in all copies or substantial portions of the Software.
044: *
045: * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
046: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
047: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
048: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
049: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
050: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
051: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
052: *
053: *@author David E. Jones
054: *@created May 21, 2001
055: *@version 1.0
056: */
057: public class Preference {
058: private static Preference globalPreference = null;
059: public static final String module = Preference.class.getName();
060:
061: GenericDelegator delegator = null;
062:
063: /** Hashtable to cache user preferences by PartyId.
064: * For each PartyAttributePK there is a String in the cache specifying the preference value.
065: * In this way the cache speeds things up whether or not the user has a particular preference.
066: */
067: public UtilCache partyAttributeCache = new UtilCache(
068: "PartyAttributeCache");
069:
070: public Preference(GenericDelegator delegator) {
071: this .delegator = delegator;
072: }
073:
074: /**
075: * DOCUMENT ME!
076: *
077: * @param delegator
078: *
079: * @return
080: */
081: public static Preference getInstance(GenericDelegator delegator) {
082: if (globalPreference == null) //don't want to block here
083: {
084: synchronized (Preference.class) {
085: //must check if null again as one of the blocked threads can still enter
086: if (globalPreference == null) {
087: globalPreference = new Preference(delegator);
088: }
089: }
090: }
091:
092: return globalPreference;
093: }
094:
095: /**
096: * DOCUMENT ME!
097: *
098: * @return
099: */
100: public GenericDelegator getDelegator() {
101: return delegator;
102: }
103:
104: /**
105: * DOCUMENT ME!
106: *
107: * @param delegator
108: */
109: public void setDelegator(GenericDelegator delegator) {
110: this .delegator = delegator;
111: }
112:
113: /**
114: * DOCUMENT ME!
115: *
116: * @param partyId
117: * @param preferenceName
118: *
119: * @return
120: */
121: public String getPreference(String partyId, String preferenceName) {
122: return getPreference(partyId, preferenceName, "");
123: }
124:
125: /**
126: * DOCUMENT ME!
127: *
128: * @param partyId
129: * @param preferenceName
130: * @param defaultValue
131: *
132: * @return
133: */
134: public String getPreference(String partyId, String preferenceName,
135: String defaultValue) {
136: Debug.logVerbose("[getPreference] partyId: " + partyId, module);
137: Debug.logVerbose("[getPreference] preferenceName: "
138: + preferenceName, module);
139: Debug.logVerbose("[getPreference] defaultValue: "
140: + defaultValue, module);
141: Debug.logVerbose(
142: "[getPreference] delegator.getDelegatorName(): "
143: + delegator.getDelegatorName(), module);
144:
145: GenericPK partyAttributePK = delegator.makePK("PartyAttribute",
146: UtilMisc.toMap("partyId", partyId, "attrName",
147: preferenceName));
148: String attrValue = (String) partyAttributeCache
149: .get(partyAttributePK);
150:
151: if (attrValue == null) {
152: try {
153: GenericValue gv = delegator
154: .findByPrimaryKey(partyAttributePK);
155:
156: if (gv != null) {
157: Debug.logVerbose(
158: "[getPreference] Found preference record.",
159: module);
160:
161: attrValue = gv.getString("attrValue");
162: partyAttributeCache
163: .put(partyAttributePK, attrValue);
164: } else {
165: Debug
166: .logVerbose(
167: "[getPreference] Did not find preference record.",
168: module);
169: }
170: } catch (GenericEntityException e) {
171: Debug.logError(e, module);
172: }
173: }
174:
175: if (attrValue != null) {
176: Debug
177: .logVerbose(
178: "[getPreference] Returning preference value from data base.",
179: module);
180:
181: return attrValue;
182: } else {
183: Debug.logVerbose(
184: "[getPreference] Returning default value.", module);
185:
186: return defaultValue;
187: }
188: }
189:
190: /** Finds whether or not a SecurityGroupPermission row exists given a groupId and permission.
191: * Uses the securityGroupPermissionCache to speed this up.
192: * The groupId,permission pair is cached instead of the userLoginId,permission pair to keep the cache small and to make it more changeable.
193: * @param userPartyId The ID of the user Party
194: * @param companyPartyId The ID of the company Party for the specified user
195: * @param preferenceName The name of the preference to find
196: * @param defaultValue the value to be returned if the preference is not found
197: * @return String specifying the attributeValue associated with the 1) party, 2) company, or 3) default in that order
198: */
199: public String getPreference(String userPartyId,
200: String companyPartyId, String preferenceName,
201: String defaultValue) {
202: String prefValue = getPreference(userPartyId, preferenceName,
203: null);
204:
205: if (prefValue == null) {
206: prefValue = getPreference(companyPartyId, preferenceName,
207: null);
208: }
209:
210: if (prefValue == null) {
211: return defaultValue;
212: }
213:
214: return prefValue;
215: }
216:
217: /**
218: * DOCUMENT ME!
219: *
220: * @param userPartyId
221: * @param companyPartyId
222: * @param preferenceName
223: * @param defaultValue
224: *
225: * @return
226: */
227: public int getPreference(String userPartyId, String companyPartyId,
228: String preferenceName, int defaultValue) {
229: String prefStr = getPreference(userPartyId, companyPartyId,
230: preferenceName, String.valueOf(defaultValue));
231:
232: try {
233: int retVal = Integer.parseInt(prefStr);
234:
235: return retVal;
236: } catch (Exception e) {
237: Debug.logError(e, module);
238:
239: return defaultValue;
240: }
241: }
242:
243: /**
244: * DOCUMENT ME!
245: */
246: public void clearCache() {
247: partyAttributeCache.clear();
248: }
249:
250: /**
251: * DOCUMENT ME!
252: *
253: * @return
254: */
255: public List getPreferenceDefinitions() {
256: try {
257: return delegator.findAll("UserPreferenceDefinitions");
258: } catch (GenericEntityException e) {
259: Debug.logError(e, module);
260: }
261:
262: return null;
263: }
264:
265: /**
266: * DOCUMENT ME!
267: *
268: * @param prefValues
269: * @param currentValue
270: *
271: * @return
272: */
273: public String getPreferenceValueHtml(String prefValues,
274: String currentValue) {
275: boolean foundCurrent = false;
276: List valueList = StringUtil.split(prefValues, ";");
277:
278: if (valueList == null) {
279: return "";
280: }
281:
282: String selectHTML = "";
283:
284: for (int i = 0; i < valueList.size(); i++) {
285: String valuePair = (String) valueList.get(i);
286: List values = StringUtil.split(valuePair, ":");
287:
288: if (values.size() != 2) {
289: return "Error in default value definition";
290: }
291:
292: String displayValue = (String) values.get(0);
293: String codeValue = (String) values.get(1);
294: String selected = "";
295:
296: if (codeValue.equals(currentValue)) {
297: foundCurrent = true;
298: selected = "SELECTED";
299: }
300:
301: selectHTML = selectHTML + "<OPTION value='" + codeValue
302: + "' " + selected + ">" + displayValue
303: + "</OPTION>";
304: }
305:
306: String selected = "";
307:
308: if (!foundCurrent) {
309: selected = "SELECTED";
310: }
311:
312: selectHTML = "<SELECT name=prefValue><OPTION value=''"
313: + selected + "></OPTION>" + selectHTML + "</SELECT>";
314:
315: return selectHTML;
316: }
317: }
|