001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.minilang.method.entityops;
019:
020: import java.util.HashSet;
021: import java.util.List;
022: import java.util.Map;
023:
024: import org.ofbiz.base.util.Debug;
025: import org.ofbiz.base.util.UtilValidate;
026: import org.ofbiz.entity.GenericDelegator;
027: import org.ofbiz.entity.GenericEntity;
028: import org.ofbiz.entity.GenericEntityException;
029: import org.ofbiz.minilang.SimpleMethod;
030: import org.ofbiz.minilang.method.ContextAccessor;
031: import org.ofbiz.minilang.method.MethodContext;
032: import org.ofbiz.minilang.method.MethodOperation;
033: import org.w3c.dom.Element;
034:
035: /**
036: * Uses the delegator to find an entity value by its primary key
037: */
038: public class FindByPrimaryKey extends MethodOperation {
039:
040: public static final String module = FindByPrimaryKey.class
041: .getName();
042:
043: ContextAccessor valueAcsr;
044: String entityName;
045: ContextAccessor mapAcsr;
046: String delegatorName;
047: String useCacheStr;
048: ContextAccessor fieldsToSelectListAcsr;
049:
050: public FindByPrimaryKey(Element element, SimpleMethod simpleMethod) {
051: super (element, simpleMethod);
052: valueAcsr = new ContextAccessor(element
053: .getAttribute("value-name"));
054: entityName = element.getAttribute("entity-name");
055: mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
056: fieldsToSelectListAcsr = new ContextAccessor(element
057: .getAttribute("fields-to-select-list"));
058: delegatorName = element.getAttribute("delegator-name");
059: useCacheStr = element.getAttribute("use-cache");
060: }
061:
062: public boolean exec(MethodContext methodContext) {
063: String entityName = methodContext.expandString(this .entityName);
064: String delegatorName = methodContext
065: .expandString(this .delegatorName);
066: String useCacheStr = methodContext
067: .expandString(this .useCacheStr);
068:
069: boolean useCache = "true".equals(useCacheStr);
070:
071: GenericDelegator delegator = methodContext.getDelegator();
072: if (delegatorName != null && delegatorName.length() > 0) {
073: delegator = GenericDelegator
074: .getGenericDelegator(delegatorName);
075: }
076:
077: Map inMap = (Map) mapAcsr.get(methodContext);
078: if (UtilValidate.isEmpty(entityName)
079: && inMap instanceof GenericEntity) {
080: GenericEntity inEntity = (GenericEntity) inMap;
081: entityName = inEntity.getEntityName();
082: }
083:
084: List fieldsToSelectList = null;
085: if (!fieldsToSelectListAcsr.isEmpty()) {
086: fieldsToSelectList = (List) fieldsToSelectListAcsr
087: .get(methodContext);
088: }
089:
090: try {
091: if (fieldsToSelectList != null) {
092: valueAcsr.put(methodContext, delegator
093: .findByPrimaryKeyPartial(delegator.makePK(
094: "Product", inMap), new HashSet(
095: fieldsToSelectList)));
096: } else {
097: if (useCache) {
098: valueAcsr.put(methodContext, delegator
099: .findByPrimaryKeyCache(entityName, inMap));
100: } else {
101: valueAcsr.put(methodContext, delegator
102: .findByPrimaryKey(entityName, inMap));
103: }
104: }
105: } catch (GenericEntityException e) {
106: Debug.logError(e, module);
107: String errMsg = "ERROR: Could not complete the "
108: + simpleMethod.getShortDescription()
109: + " process [problem finding the " + entityName
110: + " entity: " + e.getMessage() + "]";
111: methodContext.setErrorReturn(errMsg, simpleMethod);
112: return false;
113: }
114: return true;
115: }
116:
117: public String rawString() {
118: // TODO: something more than the empty tag
119: return "<find-by-primary-key/>";
120: }
121:
122: public String expandedString(MethodContext methodContext) {
123: // TODO: something more than a stub/dummy
124: return this.rawString();
125: }
126: }
|