/*
*
* Copyright (c) 2004 SourceTap - www.sourcetap.com
*
* The contents of this file are subject to the SourceTap Public License
* ("License"); You may not use this file except in compliance with the
* License. You may obtain a copy of the License at http://www.sourcetap.com/license.htm
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
*/
package com.sourcetap.sfa.util;
import java.util.List;
import org.ofbiz.base.util.Debug;
import org.ofbiz.base.util.StringUtil;
import org.ofbiz.base.util.UtilCache;
import org.ofbiz.base.util.UtilMisc;
import org.ofbiz.entity.GenericDelegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericPK;
import org.ofbiz.entity.GenericValue;
/**
* <p><b>Title:</b> Preference handler
* <p><b>Description:</b> Preference class - contains methods to check party preferences.
* <p>Copyright (c) 2001 The Open For Business Project - www.ofbiz.org
*
* <p>Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* <p>The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*@author David E. Jones
*@created May 21, 2001
*@version 1.0
*/
public class Preference {
private static Preference globalPreference = null;
public static final String module = Preference.class.getName();
GenericDelegator delegator = null;
/** Hashtable to cache user preferences by PartyId.
* For each PartyAttributePK there is a String in the cache specifying the preference value.
* In this way the cache speeds things up whether or not the user has a particular preference.
*/
public UtilCache partyAttributeCache = new UtilCache("PartyAttributeCache");
public Preference(GenericDelegator delegator) {
this.delegator = delegator;
}
/**
* DOCUMENT ME!
*
* @param delegator
*
* @return
*/
public static Preference getInstance(GenericDelegator delegator) {
if (globalPreference == null) //don't want to block here
{
synchronized (Preference.class) {
//must check if null again as one of the blocked threads can still enter
if (globalPreference == null) {
globalPreference = new Preference(delegator);
}
}
}
return globalPreference;
}
/**
* DOCUMENT ME!
*
* @return
*/
public GenericDelegator getDelegator() {
return delegator;
}
/**
* DOCUMENT ME!
*
* @param delegator
*/
public void setDelegator(GenericDelegator delegator) {
this.delegator = delegator;
}
/**
* DOCUMENT ME!
*
* @param partyId
* @param preferenceName
*
* @return
*/
public String getPreference(String partyId, String preferenceName) {
return getPreference(partyId, preferenceName, "");
}
/**
* DOCUMENT ME!
*
* @param partyId
* @param preferenceName
* @param defaultValue
*
* @return
*/
public String getPreference(String partyId, String preferenceName,
String defaultValue) {
Debug.logVerbose("[getPreference] partyId: " + partyId, module);
Debug.logVerbose("[getPreference] preferenceName: " + preferenceName, module);
Debug.logVerbose("[getPreference] defaultValue: " + defaultValue, module);
Debug.logVerbose("[getPreference] delegator.getDelegatorName(): " +
delegator.getDelegatorName(), module);
GenericPK partyAttributePK = delegator.makePK("PartyAttribute",
UtilMisc.toMap("partyId", partyId, "attrName", preferenceName));
String attrValue = (String) partyAttributeCache.get(partyAttributePK);
if (attrValue == null) {
try {
GenericValue gv = delegator.findByPrimaryKey(partyAttributePK);
if (gv != null) {
Debug.logVerbose(
"[getPreference] Found preference record.", module);
attrValue = gv.getString("attrValue");
partyAttributeCache.put(partyAttributePK, attrValue);
} else {
Debug.logVerbose(
"[getPreference] Did not find preference record.", module);
}
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
}
if (attrValue != null) {
Debug.logVerbose(
"[getPreference] Returning preference value from data base.", module);
return attrValue;
} else {
Debug.logVerbose("[getPreference] Returning default value.", module);
return defaultValue;
}
}
/** Finds whether or not a SecurityGroupPermission row exists given a groupId and permission.
* Uses the securityGroupPermissionCache to speed this up.
* The groupId,permission pair is cached instead of the userLoginId,permission pair to keep the cache small and to make it more changeable.
* @param userPartyId The ID of the user Party
* @param companyPartyId The ID of the company Party for the specified user
* @param preferenceName The name of the preference to find
* @param defaultValue the value to be returned if the preference is not found
* @return String specifying the attributeValue associated with the 1) party, 2) company, or 3) default in that order
*/
public String getPreference(String userPartyId, String companyPartyId,
String preferenceName, String defaultValue) {
String prefValue = getPreference(userPartyId, preferenceName, null);
if (prefValue == null) {
prefValue = getPreference(companyPartyId, preferenceName, null);
}
if (prefValue == null) {
return defaultValue;
}
return prefValue;
}
/**
* DOCUMENT ME!
*
* @param userPartyId
* @param companyPartyId
* @param preferenceName
* @param defaultValue
*
* @return
*/
public int getPreference(String userPartyId, String companyPartyId,
String preferenceName, int defaultValue) {
String prefStr = getPreference(userPartyId, companyPartyId,
preferenceName, String.valueOf(defaultValue));
try {
int retVal = Integer.parseInt(prefStr);
return retVal;
} catch (Exception e) {
Debug.logError(e, module);
return defaultValue;
}
}
/**
* DOCUMENT ME!
*/
public void clearCache() {
partyAttributeCache.clear();
}
/**
* DOCUMENT ME!
*
* @return
*/
public List getPreferenceDefinitions() {
try {
return delegator.findAll("UserPreferenceDefinitions");
} catch (GenericEntityException e) {
Debug.logError(e, module);
}
return null;
}
/**
* DOCUMENT ME!
*
* @param prefValues
* @param currentValue
*
* @return
*/
public String getPreferenceValueHtml(String prefValues, String currentValue) {
boolean foundCurrent = false;
List valueList = StringUtil.split(prefValues, ";");
if (valueList == null) {
return "";
}
String selectHTML = "";
for (int i = 0; i < valueList.size(); i++) {
String valuePair = (String) valueList.get(i);
List values = StringUtil.split(valuePair, ":");
if (values.size() != 2) {
return "Error in default value definition";
}
String displayValue = (String) values.get(0);
String codeValue = (String) values.get(1);
String selected = "";
if (codeValue.equals(currentValue)) {
foundCurrent = true;
selected = "SELECTED";
}
selectHTML = selectHTML + "<OPTION value='" + codeValue + "' " +
selected + ">" + displayValue + "</OPTION>";
}
String selected = "";
if (!foundCurrent) {
selected = "SELECTED";
}
selectHTML = "<SELECT name=prefValue><OPTION value=''" + selected +
"></OPTION>" + selectHTML + "</SELECT>";
return selectHTML;
}
}