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.List;
021: import java.util.Map;
022:
023: import org.ofbiz.base.util.Debug;
024: import org.ofbiz.entity.GenericEntityException;
025: import org.ofbiz.entity.GenericValue;
026: import org.ofbiz.minilang.SimpleMethod;
027: import org.ofbiz.minilang.method.ContextAccessor;
028: import org.ofbiz.minilang.method.MethodContext;
029: import org.ofbiz.minilang.method.MethodOperation;
030: import org.w3c.dom.Element;
031:
032: /**
033: * Gets a list of related entity instance according to the specified relation-name
034: */
035: public class GetRelated extends MethodOperation {
036:
037: public static final String module = GetRelated.class.getName();
038:
039: ContextAccessor valueAcsr;
040: ContextAccessor mapAcsr;
041: ContextAccessor orderByListAcsr;
042: String relationName;
043: String useCacheStr;
044: ContextAccessor listAcsr;
045:
046: public GetRelated(Element element, SimpleMethod simpleMethod) {
047: super (element, simpleMethod);
048: valueAcsr = new ContextAccessor(element
049: .getAttribute("value-name"));
050: relationName = element.getAttribute("relation-name");
051: listAcsr = new ContextAccessor(element
052: .getAttribute("list-name"));
053: mapAcsr = new ContextAccessor(element.getAttribute("map-name"));
054: orderByListAcsr = new ContextAccessor(element
055: .getAttribute("order-by-list-name"));
056:
057: useCacheStr = element.getAttribute("use-cache");
058: }
059:
060: public boolean exec(MethodContext methodContext) {
061: String relationName = methodContext
062: .expandString(this .relationName);
063: String useCacheStr = methodContext
064: .expandString(this .useCacheStr);
065: boolean useCache = "true".equals(useCacheStr);
066:
067: List orderByNames = null;
068: if (!orderByListAcsr.isEmpty()) {
069: orderByNames = (List) orderByListAcsr.get(methodContext);
070: }
071: Map constraintMap = null;
072: if (!mapAcsr.isEmpty()) {
073: constraintMap = (Map) mapAcsr.get(methodContext);
074: }
075:
076: GenericValue value = (GenericValue) valueAcsr
077: .get(methodContext);
078: if (value == null) {
079: Debug.logWarning("Value not found with name: " + valueAcsr
080: + ", not getting related...", module);
081: return true;
082: }
083: try {
084: if (useCache) {
085: listAcsr.put(methodContext, value.getRelatedCache(
086: relationName, constraintMap, orderByNames));
087: } else {
088: listAcsr.put(methodContext, value.getRelated(
089: relationName, constraintMap, orderByNames));
090: }
091: } catch (GenericEntityException e) {
092: String errMsg = "ERROR: Could not complete the "
093: + simpleMethod.getShortDescription()
094: + " process [problem getting related from entity with name "
095: + value.getEntityName()
096: + " for the relation-name: " + relationName + ": "
097: + e.getMessage() + "]";
098: Debug.logError(e, errMsg, module);
099: methodContext.setErrorReturn(errMsg, simpleMethod);
100: return false;
101: }
102: return true;
103: }
104:
105: public String rawString() {
106: // TODO: something more than the empty tag
107: return "<get-related/>";
108: }
109:
110: public String expandedString(MethodContext methodContext) {
111: // TODO: something more than a stub/dummy
112: return this.rawString();
113: }
114: }
|