001: /*
002: * $Id: FindByPrimaryKey.java,v 1.1 2003/08/17 06:06:11 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: package org.ofbiz.minilang.method.entityops;
025:
026: import java.util.Map;
027:
028: import org.ofbiz.base.util.Debug;
029: import org.ofbiz.base.util.UtilValidate;
030: import org.ofbiz.entity.GenericDelegator;
031: import org.ofbiz.entity.GenericEntity;
032: import org.ofbiz.entity.GenericEntityException;
033: import org.ofbiz.minilang.SimpleMethod;
034: import org.ofbiz.minilang.method.ContextAccessor;
035: import org.ofbiz.minilang.method.MethodContext;
036: import org.ofbiz.minilang.method.MethodOperation;
037: import org.w3c.dom.Element;
038:
039: /**
040: * Uses the delegator to find an entity value by its primary key
041: *
042: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
043: * @version $Revision: 1.1 $
044: * @since 2.0
045: */
046: public class FindByPrimaryKey extends MethodOperation {
047:
048: public static final String module = FindByPrimaryKey.class
049: .getName();
050:
051: ContextAccessor valueAcsr;
052: String entityName;
053: ContextAccessor mapAcsr;
054: String delegatorName;
055: String useCacheStr;
056:
057: public FindByPrimaryKey(Element element, SimpleMethod simpleMethod) {
058: super (element, simpleMethod);
059: valueAcsr = new ContextAccessor(element
060: .getAttribute("value-name"));
061: entityName = element.getAttribute("entity-name");
062: mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
063: delegatorName = element.getAttribute("delegator-name");
064: useCacheStr = element.getAttribute("use-cache");
065: }
066:
067: public boolean exec(MethodContext methodContext) {
068: String entityName = methodContext.expandString(this .entityName);
069: String delegatorName = methodContext
070: .expandString(this .delegatorName);
071: String useCacheStr = methodContext
072: .expandString(this .useCacheStr);
073:
074: boolean useCache = "true".equals(useCacheStr);
075:
076: GenericDelegator delegator = methodContext.getDelegator();
077: if (delegatorName != null && delegatorName.length() > 0) {
078: delegator = GenericDelegator
079: .getGenericDelegator(delegatorName);
080: }
081:
082: Map inMap = (Map) mapAcsr.get(methodContext);
083: if (UtilValidate.isEmpty(entityName)
084: && inMap instanceof GenericEntity) {
085: GenericEntity inEntity = (GenericEntity) inMap;
086: entityName = inEntity.getEntityName();
087: }
088: try {
089: if (useCache) {
090: valueAcsr.put(methodContext, delegator
091: .findByPrimaryKeyCache(entityName, inMap));
092: } else {
093: valueAcsr.put(methodContext, delegator
094: .findByPrimaryKey(entityName, inMap));
095: }
096: } catch (GenericEntityException e) {
097: Debug.logError(e, module);
098: String errMsg = "ERROR: Could not complete the "
099: + simpleMethod.getShortDescription()
100: + " process [problem finding the " + entityName
101: + " entity: " + e.getMessage() + "]";
102: methodContext.setErrorReturn(errMsg, simpleMethod);
103: return false;
104: }
105: return true;
106: }
107: }
|