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