Migrate ALL java files from /src to /src/main/java - OFBIZ-7790
This is the first of two steps in the above mentioned JIRA. This first step changes the java source directory for every component from /src to /src/main/java. The next step will be adding unit tests to every component in /src/test/java Thank you to Deepak Dixit for tips on how to use the SVN commands to make the appropriate file moving. git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1752838 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
+104
@@ -0,0 +1,104 @@
|
||||
# Licensed to the Apache Software Foundation (ASF) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The ASF licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
org.ofbiz.minilang.method.callops.CallClassMethod$CallClassMethodFactory
|
||||
org.ofbiz.minilang.method.callops.CallObjectMethod$CallObjectMethodFactory
|
||||
org.ofbiz.minilang.method.callops.CallScript$CallScriptFactory
|
||||
org.ofbiz.minilang.method.callops.CallService$CallServiceFactory
|
||||
org.ofbiz.minilang.method.callops.CallServiceAsynch$CallServiceAsynchFactory
|
||||
org.ofbiz.minilang.method.callops.CallSimpleMapProcessor$CallSimpleMapProcessorFactory
|
||||
org.ofbiz.minilang.method.callops.CallSimpleMethod$CallSimpleMethodFactory
|
||||
org.ofbiz.minilang.method.callops.Return$ReturnFactory
|
||||
org.ofbiz.minilang.method.callops.SetServiceFields$SetServiceFieldsFactory
|
||||
org.ofbiz.minilang.method.conditional.CompareCondition$CompareConditionFactory
|
||||
org.ofbiz.minilang.method.conditional.CompareFieldCondition$CompareFieldConditionFactory
|
||||
org.ofbiz.minilang.method.conditional.EmptyCondition$EmptyConditionFactory
|
||||
org.ofbiz.minilang.method.conditional.HasPermissionCondition$HasPermissionConditionFactory
|
||||
org.ofbiz.minilang.method.conditional.MasterIf$MasterIfFactory
|
||||
org.ofbiz.minilang.method.conditional.RegexpCondition$RegexpConditionFactory
|
||||
org.ofbiz.minilang.method.conditional.ValidateMethodCondition$ValidateMethodConditionFactory
|
||||
org.ofbiz.minilang.method.entityops.ClearCacheLine$ClearCacheLineFactory
|
||||
org.ofbiz.minilang.method.entityops.ClearEntityCaches$ClearEntityCachesFactory
|
||||
org.ofbiz.minilang.method.entityops.CloneValue$CloneValueFactory
|
||||
org.ofbiz.minilang.method.entityops.CreateValue$CreateValueFactory
|
||||
org.ofbiz.minilang.method.entityops.EntityAnd$EntityAndFactory
|
||||
org.ofbiz.minilang.method.entityops.EntityCondition$EntityConditionFactory
|
||||
org.ofbiz.minilang.method.entityops.EntityCount$EntityCountFactory
|
||||
org.ofbiz.minilang.method.entityops.EntityData$EntityDataFactory
|
||||
org.ofbiz.minilang.method.entityops.EntityOne$EntityOneFactory
|
||||
org.ofbiz.minilang.method.entityops.FilterListByAnd$FilterListByAndFactory
|
||||
org.ofbiz.minilang.method.entityops.FilterListByDate$FilterListByDateFactory
|
||||
org.ofbiz.minilang.method.entityops.FindByAnd$FindByAndFactory
|
||||
org.ofbiz.minilang.method.entityops.FindByPrimaryKey$FindByPrimaryKeyFactory
|
||||
org.ofbiz.minilang.method.entityops.GetRelated$GetRelatedFactory
|
||||
org.ofbiz.minilang.method.entityops.GetRelatedOne$GetRelatedOneFactory
|
||||
org.ofbiz.minilang.method.entityops.MakeNextSeqId$MakeNextSeqIdFactory
|
||||
org.ofbiz.minilang.method.entityops.MakeValue$MakeValueFactory
|
||||
org.ofbiz.minilang.method.entityops.OrderValueList$OrderValueListFactory
|
||||
org.ofbiz.minilang.method.entityops.RefreshValue$RefreshValueFactory
|
||||
org.ofbiz.minilang.method.entityops.RemoveByAnd$RemoveByAndFactory
|
||||
org.ofbiz.minilang.method.entityops.RemoveList$RemoveListFactory
|
||||
org.ofbiz.minilang.method.entityops.RemoveRelated$RemoveRelatedFactory
|
||||
org.ofbiz.minilang.method.entityops.RemoveValue$RemoveValueFactory
|
||||
org.ofbiz.minilang.method.entityops.SequencedIdToEnv$SequencedIdFactory
|
||||
org.ofbiz.minilang.method.entityops.SetCurrentUserLogin$SetCurrentUserLoginFactory
|
||||
org.ofbiz.minilang.method.entityops.SetNonpkFields$SetNonpkFieldsFactory
|
||||
org.ofbiz.minilang.method.entityops.SetPkFields$SetPkFieldsFactory
|
||||
org.ofbiz.minilang.method.entityops.StoreList$StoreListFactory
|
||||
org.ofbiz.minilang.method.entityops.StoreValue$StoreValueFactory
|
||||
org.ofbiz.minilang.method.entityops.TransactionBegin$TransactionBeginFactory
|
||||
org.ofbiz.minilang.method.entityops.TransactionCommit$TransactionCommitFactory
|
||||
org.ofbiz.minilang.method.entityops.TransactionRollback$TransactionRollbackFactory
|
||||
org.ofbiz.minilang.method.envops.AddError$AddErrorFactory
|
||||
org.ofbiz.minilang.method.envops.Assert$AssertFactory
|
||||
org.ofbiz.minilang.method.envops.Break$BreakFactory
|
||||
org.ofbiz.minilang.method.envops.CheckErrors$CheckErrorsFactory
|
||||
org.ofbiz.minilang.method.envops.CheckId$CheckIdFactory
|
||||
org.ofbiz.minilang.method.envops.ClearField$ClearFieldFactory
|
||||
org.ofbiz.minilang.method.envops.Continue$ContinueFactory
|
||||
org.ofbiz.minilang.method.envops.CreateObject$CreateObjectFactory
|
||||
org.ofbiz.minilang.method.envops.FieldToList$FieldToListFactory
|
||||
org.ofbiz.minilang.method.envops.FirstFromList$FirstFromListFactory
|
||||
org.ofbiz.minilang.method.envops.Iterate$IterateFactory
|
||||
org.ofbiz.minilang.method.envops.IterateMap$IterateMapFactory
|
||||
org.ofbiz.minilang.method.envops.ListToList$ListToListFactory
|
||||
org.ofbiz.minilang.method.envops.Loop$LoopFactory
|
||||
org.ofbiz.minilang.method.envops.MapToMap$MapToMapFactory
|
||||
org.ofbiz.minilang.method.envops.Now$NowFactory
|
||||
org.ofbiz.minilang.method.envops.Now$NowDateToEnvFactory
|
||||
org.ofbiz.minilang.method.envops.Now$NowTimestampFactory
|
||||
org.ofbiz.minilang.method.envops.OrderMapList$OrderMapListFactory
|
||||
org.ofbiz.minilang.method.envops.SetCalendar$SetCalendarFactory
|
||||
org.ofbiz.minilang.method.envops.SetOperation$SetOperationFactory
|
||||
org.ofbiz.minilang.method.envops.StringAppend$StringAppendFactory
|
||||
org.ofbiz.minilang.method.envops.StringToList$StringToListFactory
|
||||
org.ofbiz.minilang.method.envops.ToString$ToStringFactory
|
||||
org.ofbiz.minilang.method.envops.While$WhileFactory
|
||||
org.ofbiz.minilang.method.eventops.FieldToRequest$FieldToRequestFactory
|
||||
org.ofbiz.minilang.method.eventops.FieldToSession$FieldToSessionFactory
|
||||
org.ofbiz.minilang.method.eventops.RequestParametersToList$RequestParametersToListFactory
|
||||
org.ofbiz.minilang.method.eventops.RequestToField$RequestToFieldFactory
|
||||
org.ofbiz.minilang.method.eventops.SessionToField$SessionToFieldFactory
|
||||
org.ofbiz.minilang.method.eventops.WebappPropertyToField$WebappPropertyToFieldFactory
|
||||
org.ofbiz.minilang.method.ifops.CheckPermission$CheckPermissionFactory
|
||||
org.ofbiz.minilang.method.ifops.IfInstanceOf$IfInstanceOfFactory
|
||||
org.ofbiz.minilang.method.ifops.IfNotEmpty$IfNotEmptyFactory
|
||||
org.ofbiz.minilang.method.otherops.Calculate$CalculateFactory
|
||||
org.ofbiz.minilang.method.otherops.Log$LogFactory
|
||||
org.ofbiz.minilang.method.otherops.PropertyToField$PropertyToFieldFactory
|
||||
org.ofbiz.minilang.method.otherops.Trace$TraceFactory
|
||||
org.ofbiz.minilang.method.serviceops.FieldToResult$FieldToResultFactory
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
# Licensed to the Apache Software Foundation (ASF) under one
|
||||
# or more contributor license agreements. See the NOTICE file
|
||||
# distributed with this work for additional information
|
||||
# regarding copyright ownership. The ASF licenses this file
|
||||
# to you under the Apache License, Version 2.0 (the
|
||||
# "License"); you may not use this file except in compliance
|
||||
# with the License. You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing,
|
||||
# software distributed under the License is distributed on an
|
||||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
# KIND, either express or implied. See the License for the
|
||||
# specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
org.ofbiz.minilang.method.conditional.CombinedCondition$OrConditionFactory
|
||||
org.ofbiz.minilang.method.conditional.CombinedCondition$XorConditionFactory
|
||||
org.ofbiz.minilang.method.conditional.CombinedCondition$AndConditionFactory
|
||||
org.ofbiz.minilang.method.conditional.CombinedCondition$NotConditionFactory
|
||||
org.ofbiz.minilang.method.conditional.CompareCondition$CompareConditionFactory
|
||||
org.ofbiz.minilang.method.conditional.CompareFieldCondition$CompareFieldConditionFactory
|
||||
org.ofbiz.minilang.method.conditional.EmptyCondition$EmptyConditionFactory
|
||||
org.ofbiz.minilang.method.conditional.HasPermissionCondition$HasPermissionConditionFactory
|
||||
org.ofbiz.minilang.method.conditional.RegexpCondition$RegexpConditionFactory
|
||||
org.ofbiz.minilang.method.conditional.ValidateMethodCondition$ValidateMethodConditionFactory
|
||||
@@ -0,0 +1,97 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang;
|
||||
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* A single Mini-language element. This class is the superclass for all XML element models.
|
||||
*/
|
||||
public class MiniLangElement {
|
||||
|
||||
// This must be private so subclasses cannot reference it.
|
||||
private static final String module = MiniLangElement.class.getName();
|
||||
|
||||
private final Object lineNumber;
|
||||
protected final SimpleMethod simpleMethod;
|
||||
private final String tagName;
|
||||
|
||||
public MiniLangElement(Element element, SimpleMethod simpleMethod) {
|
||||
this.lineNumber = element.getUserData("startLine");
|
||||
this.simpleMethod = simpleMethod;
|
||||
this.tagName = element.getTagName().intern();
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates <code>aic</code> with this element's artifact information.
|
||||
* @param aic The artifact information context
|
||||
*/
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the source code line number for this element.
|
||||
* @return The source code line number for this element
|
||||
*/
|
||||
public String getLineNumber() {
|
||||
return this.lineNumber == null ? "unknown" : this.lineNumber.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the containing {@link SimpleMethod} object.
|
||||
* @return The containing {@link SimpleMethod} object
|
||||
*/
|
||||
public SimpleMethod getSimpleMethod() {
|
||||
return this.simpleMethod;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns this element's tag name.
|
||||
* @return This element's tag name
|
||||
*/
|
||||
public String getTagName() {
|
||||
return this.tagName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs a trace message.
|
||||
* @param methodContext
|
||||
* @param messages
|
||||
*/
|
||||
public void outputTraceMessage(MethodContext methodContext, String... messages) {
|
||||
String lineSep = System.getProperty("line.separator");
|
||||
StringBuilder buf = new StringBuilder(getSimpleMethod().getFileName());
|
||||
buf.append(", Line ").append(getLineNumber()).append(" <").append(getTagName()).append("> element: ");
|
||||
for (int i = 0; i < messages.length; i++) {
|
||||
buf.append(messages[i]);
|
||||
if (i < messages.length - 1 && messages.length > 1) {
|
||||
buf.append(lineSep);
|
||||
}
|
||||
}
|
||||
Debug.log(methodContext.getTraceLogLevel(), null, buf.toString(), module);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "<".concat(this.tagName).concat(">");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang;
|
||||
|
||||
/**
|
||||
* Thrown to indicate a Mini-language error.
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class MiniLangException extends org.ofbiz.base.util.GeneralException {
|
||||
|
||||
public MiniLangException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public MiniLangException(String str) {
|
||||
super(str);
|
||||
}
|
||||
|
||||
public MiniLangException(String str, Throwable nested) {
|
||||
super(str, nested);
|
||||
}
|
||||
|
||||
public MiniLangException(Throwable nested) {
|
||||
super(nested);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang;
|
||||
|
||||
/**
|
||||
* Thrown to indicate a Mini-language run-time error.
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class MiniLangRuntimeException extends MiniLangException {
|
||||
|
||||
private final MiniLangElement element;
|
||||
|
||||
public MiniLangRuntimeException(String str, MiniLangElement element) {
|
||||
super(str);
|
||||
this.element = element;
|
||||
}
|
||||
|
||||
public MiniLangRuntimeException(Throwable nested, MiniLangElement element) {
|
||||
super(nested);
|
||||
this.element = element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
StringBuilder sb = new StringBuilder(super.getMessage());
|
||||
if (this.element != null) {
|
||||
SimpleMethod method = this.element.getSimpleMethod();
|
||||
sb.append(" Method = ").append(method.getMethodName()).append(", File = ").append(method.getFromLocation());
|
||||
sb.append(", Element = <").append(this.element.getTagName()).append(">");
|
||||
sb.append(", Line ").append(this.element.getLineNumber());
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,325 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang;
|
||||
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.lang.reflect.Method;
|
||||
import java.net.URL;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import org.ofbiz.base.conversion.Converter;
|
||||
import org.ofbiz.base.conversion.Converters;
|
||||
import org.ofbiz.base.conversion.LocalizedConverter;
|
||||
import org.ofbiz.base.location.FlexibleLocation;
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.ScriptUtil;
|
||||
import org.ofbiz.base.util.UtilProperties;
|
||||
import org.ofbiz.base.util.UtilXml;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.entity.GenericEntity;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodObject;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
/**
|
||||
* Mini-language utilities.
|
||||
*/
|
||||
public final class MiniLangUtil {
|
||||
|
||||
public static final String module = MiniLangUtil.class.getName();
|
||||
|
||||
private static final Set<String> SCRIPT_PREFIXES;
|
||||
|
||||
static {
|
||||
Set<String> scriptPrefixes = new HashSet<String>();
|
||||
for (String scriptName : ScriptUtil.SCRIPT_NAMES) {
|
||||
scriptPrefixes.add(scriptName.concat(":"));
|
||||
}
|
||||
SCRIPT_PREFIXES = Collections.unmodifiableSet(scriptPrefixes);
|
||||
}
|
||||
|
||||
private MiniLangUtil() {}
|
||||
|
||||
/**
|
||||
* Returns <code>true</code> if <code>str</code> contains a script.
|
||||
* @param str The string to test
|
||||
* @return <code>true</code> if <code>str</code> contains a script
|
||||
*/
|
||||
public static boolean containsScript(String str) {
|
||||
if (str.length() > 0) {
|
||||
for (String scriptPrefix : SCRIPT_PREFIXES) {
|
||||
if (str.contains(scriptPrefix)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <code>true</code> if the Mini-language script engine has been configured to
|
||||
* auto-correct version changes.
|
||||
* @return <code>true</code> if the Mini-language script engine has been configured to
|
||||
* auto-correct version changes
|
||||
*/
|
||||
public static boolean autoCorrectOn() {
|
||||
return "true".equals(UtilProperties.getPropertyValue("minilang", "autocorrect"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls an object method.
|
||||
* @param operation A reference to the <code>MethodOperation</code> calling this method
|
||||
* @param methodContext
|
||||
* @param parameters
|
||||
* @param methodClass
|
||||
* @param methodObject
|
||||
* @param methodName
|
||||
* @param retFieldFma
|
||||
* @throws MiniLangRuntimeException
|
||||
*/
|
||||
public static void callMethod(MethodOperation operation, MethodContext methodContext, List<MethodObject<?>> parameters, Class<?> methodClass, Object methodObject, String methodName, FlexibleMapAccessor<Object> retFieldFma) throws MiniLangRuntimeException {
|
||||
Object[] args = null;
|
||||
Class<?>[] parameterTypes = null;
|
||||
if (parameters != null) {
|
||||
args = new Object[parameters.size()];
|
||||
parameterTypes = new Class<?>[parameters.size()];
|
||||
int i = 0;
|
||||
for (MethodObject<?> methodObjectDef : parameters) {
|
||||
args[i] = methodObjectDef.getObject(methodContext);
|
||||
Class<?> typeClass = null;
|
||||
try {
|
||||
typeClass = methodObjectDef.getTypeClass(methodContext);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new MiniLangRuntimeException(e, operation);
|
||||
}
|
||||
parameterTypes[i] = typeClass;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
try {
|
||||
Method method = methodClass.getMethod(methodName, parameterTypes);
|
||||
Object retValue = method.invoke(methodObject, args);
|
||||
if (!retFieldFma.isEmpty()) {
|
||||
retFieldFma.put(methodContext.getEnvMap(), retValue);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new MiniLangRuntimeException(e, operation);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <code>obj</code> converted to the type specified in <code>targetClass</code>.
|
||||
* @param obj
|
||||
* @param targetClass
|
||||
* @param locale
|
||||
* @param timeZone
|
||||
* @param format
|
||||
* @return The converted object
|
||||
* @throws Exception
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Object convertType(Object obj, Class<?> targetClass, Locale locale, TimeZone timeZone, String format) throws Exception {
|
||||
if (obj == null || obj == GenericEntity.NULL_FIELD) {
|
||||
return null;
|
||||
}
|
||||
if (obj instanceof Node) {
|
||||
Node node = (Node) obj;
|
||||
String nodeValue = node.getTextContent();
|
||||
if (targetClass == String.class) {
|
||||
return nodeValue;
|
||||
} else {
|
||||
return convertType(nodeValue, targetClass, locale, timeZone, format);
|
||||
}
|
||||
}
|
||||
if (targetClass == PlainString.class) {
|
||||
return obj.toString();
|
||||
}
|
||||
Class<?> sourceClass = obj.getClass();
|
||||
if (sourceClass == targetClass) {
|
||||
return obj;
|
||||
}
|
||||
Converter<Object, Object> converter = (Converter<Object, Object>) Converters.getConverter(sourceClass, targetClass);
|
||||
LocalizedConverter<Object, Object> localizedConverter = null;
|
||||
if (converter instanceof LocalizedConverter) {
|
||||
localizedConverter = (LocalizedConverter) converter;
|
||||
if (locale == null) {
|
||||
locale = Locale.getDefault();
|
||||
}
|
||||
if (timeZone == null) {
|
||||
timeZone = TimeZone.getDefault();
|
||||
}
|
||||
if (format != null && format.isEmpty()) {
|
||||
format = null;
|
||||
}
|
||||
return localizedConverter.convert(obj, locale, timeZone, format);
|
||||
}
|
||||
return converter.convert(obj);
|
||||
}
|
||||
|
||||
/**
|
||||
* Flags a Mini-language XML document as corrected.
|
||||
* @param element
|
||||
*/
|
||||
public static void flagDocumentAsCorrected(Element element) {
|
||||
Document doc = element.getOwnerDocument();
|
||||
if (doc != null) {
|
||||
doc.setUserData("autoCorrected", "true", null);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a <code>object</code>-derived <code>Class</code> that is
|
||||
* suitable for use with the conversion framework. If the argument is
|
||||
* <code>null</code> or a <code>String</code>, the method returns the
|
||||
* {@link PlainString} class.
|
||||
*
|
||||
* @param object
|
||||
* @return A <code>Class</code> that is
|
||||
* suitable for use with the conversion framework
|
||||
*/
|
||||
public static Class<?> getObjectClassForConversion(Object object) {
|
||||
if (object == null || object instanceof String) {
|
||||
return PlainString.class;
|
||||
} else {
|
||||
if (object instanceof java.util.Map<?, ?>) {
|
||||
return java.util.Map.class;
|
||||
} else if (object instanceof java.util.List<?>) {
|
||||
return java.util.List.class;
|
||||
} else if (object instanceof java.util.Set<?>) {
|
||||
return java.util.Set.class;
|
||||
} else {
|
||||
return object.getClass();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <code>true</code> if <code>attributeValue</code> is a
|
||||
* constant value (it does not contain an expression).
|
||||
*
|
||||
* @param attributeValue The value to test
|
||||
* @return <code>true</code> if <code>attributeValue</code> is a
|
||||
* constant value
|
||||
*/
|
||||
public static boolean isConstantAttribute(String attributeValue) {
|
||||
if (attributeValue.length() > 0) {
|
||||
return !FlexibleStringExpander.containsExpression(FlexibleStringExpander.getInstance(attributeValue));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <code>true</code> if <code>attributeValue</code> is a
|
||||
* constant value (it does not contain an expression) or a constant
|
||||
* plus expression value.
|
||||
*
|
||||
* @param attributeValue The value to test
|
||||
* @return <code>true</code> if <code>attributeValue</code> is a
|
||||
* constant value or a constant plus expression value
|
||||
*/
|
||||
public static boolean isConstantPlusExpressionAttribute(String attributeValue) {
|
||||
if (attributeValue.length() > 0) {
|
||||
if (attributeValue.startsWith("${") && attributeValue.endsWith("}")) {
|
||||
// A lot of existing code uses concatenated expressions, and they can be difficult
|
||||
// to convert to a single expression, so we will allow them for now.
|
||||
String expression = attributeValue.substring(2, attributeValue.length() - 1);
|
||||
if (!expression.contains("${")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
FlexibleStringExpander fse = FlexibleStringExpander.getInstance(attributeValue);
|
||||
return FlexibleStringExpander.containsConstant(fse);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <code>true</code> if <code>document</code> contains corrections.
|
||||
*
|
||||
* @param document The document to test
|
||||
* @return <code>true</code> if <code>document</code> contains corrections
|
||||
*/
|
||||
public static boolean isDocumentAutoCorrected(Document document) {
|
||||
return "true".equals(document.getUserData("autoCorrected"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes a Mini-language <code>Document</code> to disk. The XML file is styled by the
|
||||
* config/MiniLang.xslt style sheet.
|
||||
*
|
||||
* @param xmlURL
|
||||
* @param document
|
||||
*/
|
||||
public static void writeMiniLangDocument(URL xmlURL, Document document) {
|
||||
URL styleSheetURL = null;
|
||||
InputStream styleSheetInStream = null;
|
||||
Transformer transformer = null;
|
||||
try {
|
||||
styleSheetURL = FlexibleLocation.resolveLocation("component://minilang/config/MiniLang.xslt");
|
||||
styleSheetInStream = styleSheetURL.openStream();
|
||||
TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
||||
transformer = transformerFactory.newTransformer(new StreamSource(styleSheetInStream));
|
||||
} catch (Exception e) {
|
||||
Debug.logWarning(e, "Error reading minilang/config/MiniLang.xslt: ", module);
|
||||
return;
|
||||
} finally {
|
||||
if (styleSheetInStream != null) {
|
||||
try {
|
||||
styleSheetInStream.close();
|
||||
} catch (IOException e) {
|
||||
Debug.logWarning(e, "Error closing minilang/config/MiniLang.xslt: ", module);
|
||||
}
|
||||
}
|
||||
}
|
||||
FileOutputStream fos = null;
|
||||
try {
|
||||
fos = new FileOutputStream(xmlURL.getFile());
|
||||
UtilXml.transformDomDocument(transformer, document, fos);
|
||||
Debug.logInfo("Saved Mini-language file " + xmlURL, module);
|
||||
} catch (Exception e) {
|
||||
Debug.logWarning(e, "Error writing mini-language file " + xmlURL + ": ", module);
|
||||
} finally {
|
||||
if (fos != null) {
|
||||
try {
|
||||
fos.close();
|
||||
} catch (IOException e) {
|
||||
Debug.logWarning(e, "Error closing " + xmlURL + ": ", module);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class PlainString {}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,362 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.ScriptUtil;
|
||||
import org.ofbiz.base.util.UtilProperties;
|
||||
import org.w3c.dom.Element;
|
||||
import org.w3c.dom.NamedNodeMap;
|
||||
import org.w3c.dom.Node;
|
||||
|
||||
/**
|
||||
* Mini-language validation.
|
||||
*/
|
||||
public final class MiniLangValidate {
|
||||
|
||||
public static final String module = MiniLangValidate.class.getName();
|
||||
|
||||
/**
|
||||
* Tests <code>element</code> for invalid attribute names.
|
||||
*
|
||||
* @param method The <code><simple-method></code> that contains <code>element</code>
|
||||
* @param element The <code>element</code> to test
|
||||
* @param validAttributeNames The valid attribute names
|
||||
* @throws ValidationException If an invalid attribute name is found and <code>validation.level=strict</code>
|
||||
*/
|
||||
public static void attributeNames(SimpleMethod method, Element element, String... validAttributeNames) throws ValidationException {
|
||||
Set<String> validNames = new HashSet<String>();
|
||||
for (String name : validAttributeNames) {
|
||||
validNames.add(name);
|
||||
}
|
||||
NamedNodeMap nnm = element.getAttributes();
|
||||
for (int i = 0; i < nnm.getLength(); i++) {
|
||||
String attributeName = nnm.item(i).getNodeName();
|
||||
if (!validNames.contains(attributeName)) {
|
||||
handleError("Attribute name \"" + attributeName + "\" is not valid.", method, element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <code>attributeValue</code> if it is not empty, else returns <code>defaultValue</code>.
|
||||
* No <code>null</code> checks are performed.
|
||||
*
|
||||
* @param attributeValue
|
||||
* @param defaultValue
|
||||
* @return <code>attributeValue</code> if it is not empty, else returns <code>defaultValue</code>
|
||||
*/
|
||||
public static String checkAttribute(String attributeValue, String defaultValue) {
|
||||
return attributeValue.isEmpty() ? defaultValue : attributeValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests <code>element</code> for invalid child elements.
|
||||
*
|
||||
* @param method The <code><simple-method></code> that contains <code>element</code>
|
||||
* @param element The <code>element</code> to test
|
||||
* @param validChildElementNames The valid child element tag names
|
||||
* @throws ValidationException If an invalid child element is found and <code>validation.level=strict</code>
|
||||
*/
|
||||
public static void childElements(SimpleMethod method, Element element, String... validChildElementNames) throws ValidationException {
|
||||
Set<String> validNames = new HashSet<String>();
|
||||
for (String name : validChildElementNames) {
|
||||
validNames.add(name);
|
||||
}
|
||||
Node node = element.getFirstChild();
|
||||
while (node != null) {
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element childElement = (Element) node;
|
||||
if (!validNames.contains(childElement.getTagName())) {
|
||||
handleError("Child element <" + childElement.getTagName() + "> is not valid.", method, element);
|
||||
}
|
||||
}
|
||||
node = node.getNextSibling();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if element attributes are constant type.
|
||||
*
|
||||
* @param method The <code><simple-method></code> that contains <code>element</code>
|
||||
* @param element The <code>element</code> to test
|
||||
* @param attributeNames The attributes to test
|
||||
* @throws ValidationException If an invalid attribute is found and <code>validation.level=strict</code>
|
||||
*/
|
||||
public static void constantAttributes(SimpleMethod method, Element element, String... attributeNames) throws ValidationException {
|
||||
for (String name : attributeNames) {
|
||||
String attributeValue = element.getAttribute(name);
|
||||
if (!MiniLangUtil.isConstantAttribute(attributeValue)) {
|
||||
handleError("Constant attribute \"" + name + "\" cannot contain an expression.", method, element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if element attributes are constant+expr type.
|
||||
*
|
||||
* @param method The <code><simple-method></code> that contains <code>element</code>
|
||||
* @param element The <code>element</code> to test
|
||||
* @param attributeNames The attributes to test
|
||||
* @throws ValidationException If an invalid attribute is found and <code>validation.level=strict</code>
|
||||
*/
|
||||
public static void constantPlusExpressionAttributes(SimpleMethod method, Element element, String... attributeNames) throws ValidationException {
|
||||
for (String name : attributeNames) {
|
||||
String attributeValue = element.getAttribute(name);
|
||||
if (!MiniLangUtil.isConstantPlusExpressionAttribute(attributeValue)) {
|
||||
handleError("Constant+expr attribute \"" + name + "\" is missing a constant value (expression-only constants are not allowed).", method, element);
|
||||
}
|
||||
if (MiniLangUtil.containsScript(attributeValue)) {
|
||||
handleError("Constant+expr attribute \"" + name + "\" cannot contain a script (remove script).", method, element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests <code>element</code> for a deprecated attribute.
|
||||
*
|
||||
* @param method The <code><simple-method></code> that contains <code>element</code>
|
||||
* @param element The <code>element</code> to test
|
||||
* @param attributeName The name of the deprecated attribute
|
||||
* @param fixInstruction Instructions to fix the deprecated attribute
|
||||
* @throws ValidationException If the deprecated attribute is found and <code>validation.level=strict</code>
|
||||
*/
|
||||
public static void deprecatedAttribute(SimpleMethod method, Element element, String attributeName, String fixInstruction) throws ValidationException {
|
||||
String attributeValue = element.getAttribute(attributeName);
|
||||
if (attributeValue.length() > 0) {
|
||||
handleError("Attribute \"" + attributeName + "\" is deprecated (" + fixInstruction + ")", method, element);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if element attributes are expression type.
|
||||
*
|
||||
* @param method The <code><simple-method></code> that contains <code>element</code>
|
||||
* @param element The <code>element</code> to test
|
||||
* @param attributeNames The attributes to test
|
||||
* @throws ValidationException If an invalid attribute is found and <code>validation.level=strict</code>
|
||||
*/
|
||||
public static void expressionAttributes(SimpleMethod method, Element element, String... attributeNames) throws ValidationException {
|
||||
for (String name : attributeNames) {
|
||||
String attributeValue = element.getAttribute(name);
|
||||
if (attributeValue.length() > 0) {
|
||||
if (attributeValue.startsWith("${") && attributeValue.endsWith("}")) {
|
||||
attributeValue = attributeValue.substring(2, attributeValue.length() - 1);
|
||||
}
|
||||
if (MiniLangUtil.containsScript(attributeValue)) {
|
||||
handleError("Expression attribute \"" + name + "\" cannot contain a script (remove script).", method, element);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles a Mini-language validation error.
|
||||
*
|
||||
* @param errorMessage The error message
|
||||
* @param method The <code><simple-method></code> that contains <code>element</code>
|
||||
* @param element The <code>element</code> that contains the error
|
||||
* @throws ValidationException If <code>validation.level=strict</code>, otherwise a warning is logged
|
||||
*/
|
||||
public static void handleError(String errorMessage, SimpleMethod method, Element element) throws ValidationException {
|
||||
ValidationException e = new ValidationException(errorMessage, method, element);
|
||||
if (strictOn()) {
|
||||
throw e;
|
||||
} else {
|
||||
Debug.logWarning(e.getMessage(), module);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <code>true</code> if <code>validation.level=lenient</code>.
|
||||
*
|
||||
* @return <code>true</code> if <code>validation.level=lenient</code>
|
||||
*/
|
||||
public static boolean lenientOn() {
|
||||
return "lenient".equals(UtilProperties.getPropertyValue("minilang", "validation.level"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests <code>element</code> for child elements.
|
||||
*
|
||||
* @param method The <code><simple-method></code> that contains <code>element</code>
|
||||
* @param element The <code>element</code> to test
|
||||
* @throws ValidationException If a child element is found and <code>validation.level=strict</code>
|
||||
*/
|
||||
public static void noChildElements(SimpleMethod method, Element element) throws ValidationException {
|
||||
Node node = element.getFirstChild();
|
||||
while (node != null) {
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element childElement = (Element) node;
|
||||
handleError("Child element <" + childElement.getTagName() + "> is not valid.", method, element);
|
||||
}
|
||||
node = node.getNextSibling();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests <code>element</code> for any one required attribute from a set of attribute names.
|
||||
*
|
||||
* @param method The <code><simple-method></code> that contains <code>element</code>
|
||||
* @param element The <code>element</code> to test
|
||||
* @param attributeNames The required attribute names
|
||||
* @throws ValidationException If none of the required attributes are found and <code>validation.level=strict</code>
|
||||
*/
|
||||
public static void requireAnyAttribute(SimpleMethod method, Element element, String... attributeNames) throws ValidationException {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String name : attributeNames) {
|
||||
String attributeValue = element.getAttribute(name);
|
||||
if (attributeValue.length() > 0) {
|
||||
return;
|
||||
}
|
||||
if (sb.length() > 0) {
|
||||
sb.append(" ");
|
||||
}
|
||||
sb.append("\"").append(name).append("\"");
|
||||
}
|
||||
handleError("Element must include one of " + sb + " attributes.", method, element);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests <code>element</code> for any one required child element from a set of tag names.
|
||||
*
|
||||
* @param method The <code><simple-method></code> that contains <code>element</code>
|
||||
* @param element The <code>element</code> to test
|
||||
* @param elementNames The required child element tag names
|
||||
* @throws ValidationException If none of the required child elements are found and <code>validation.level=strict</code>
|
||||
*/
|
||||
public static void requireAnyChildElement(SimpleMethod method, Element element, String... elementNames) throws ValidationException {
|
||||
Set<String> childElementNames = new HashSet<String>();
|
||||
Node node = element.getFirstChild();
|
||||
while (node != null) {
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element childElement = (Element) node;
|
||||
childElementNames.add(childElement.getTagName());
|
||||
}
|
||||
node = node.getNextSibling();
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (String name : elementNames) {
|
||||
if (childElementNames.contains(name)) {
|
||||
return;
|
||||
}
|
||||
if (sb.length() > 0) {
|
||||
sb.append(", ");
|
||||
}
|
||||
sb.append("<").append(name).append(">");
|
||||
}
|
||||
handleError("Element must include one of " + sb + " child elements.", method, element);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests <code>element</code> for required attributes.
|
||||
*
|
||||
* @param method The <code><simple-method></code> that contains <code>element</code>
|
||||
* @param element The <code>element</code> to test
|
||||
* @param attributeNames The required attribute names
|
||||
* @throws ValidationException If any of the required attributes are not found and <code>validation.level=strict</code>
|
||||
*/
|
||||
public static void requiredAttributes(SimpleMethod method, Element element, String... attributeNames) throws ValidationException {
|
||||
for (String name : attributeNames) {
|
||||
String attributeValue = element.getAttribute(name);
|
||||
if (attributeValue.length() == 0) {
|
||||
handleError("Required attribute \"" + name + "\" is missing.", method, element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests <code>element</code> for required child elements.
|
||||
*
|
||||
* @param method The <code><simple-method></code> that contains <code>element</code>
|
||||
* @param element The <code>element</code> to test
|
||||
* @param elementNames The required child element tag names
|
||||
* @throws ValidationException If any of the required child elements are not found and <code>validation.level=strict</code>
|
||||
*/
|
||||
public static void requiredChildElements(SimpleMethod method, Element element, String... elementNames) throws ValidationException {
|
||||
Set<String> childElementNames = new HashSet<String>();
|
||||
Node node = element.getFirstChild();
|
||||
while (node != null) {
|
||||
if (node.getNodeType() == Node.ELEMENT_NODE) {
|
||||
Element childElement = (Element) node;
|
||||
childElementNames.add(childElement.getTagName());
|
||||
}
|
||||
node = node.getNextSibling();
|
||||
}
|
||||
for (String name : elementNames) {
|
||||
if (!childElementNames.contains(name)) {
|
||||
handleError("Required child element <" + name + "> is missing.", method, element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if element attributes are script type.
|
||||
*
|
||||
* @param method The <code><simple-method></code> that contains <code>element</code>
|
||||
* @param element The <code>element</code> to test
|
||||
* @param attributeNames The attributes to test
|
||||
* @throws ValidationException If an invalid attribute is found and <code>validation.level=strict</code>
|
||||
*/
|
||||
public static void scriptAttributes(SimpleMethod method, Element element, String... attributeNames) throws ValidationException {
|
||||
for (String name : attributeNames) {
|
||||
String attributeValue = element.getAttribute(name).trim();
|
||||
if (attributeValue.length() > 0) {
|
||||
if (attributeValue.startsWith("${") && attributeValue.endsWith("}")) {
|
||||
handleError("Script attribute \"" + name + "\" enclosed in \"${}\" (remove enclosing ${}).", method, element);
|
||||
}
|
||||
boolean scriptFound = false;
|
||||
for (String scriptName : ScriptUtil.SCRIPT_NAMES) {
|
||||
String scriptPrefix = scriptName.concat(":");
|
||||
if (attributeValue.contains(scriptPrefix)) {
|
||||
scriptFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!scriptFound) {
|
||||
handleError("Script attribute \"" + name + "\" does not contain a script.", method, element);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <code>true</code> if <code>validation.level=strict</code>.
|
||||
*
|
||||
* @return <code>true</code> if <code>validation.level=strict</code>
|
||||
*/
|
||||
public static boolean strictOn() {
|
||||
return "strict".equals(UtilProperties.getPropertyValue("minilang", "validation.level"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <code>true</code> if <code>validation.level</code> is set to lenient or strict.
|
||||
*
|
||||
* @return <code>true</code> if <code>validation.level</code> is set to lenient or strict
|
||||
*/
|
||||
public static boolean validationOn() {
|
||||
return !"none".equals(UtilProperties.getPropertyValue("minilang", "validation.level"));
|
||||
}
|
||||
|
||||
private MiniLangValidate() {}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import org.ofbiz.base.location.FlexibleLocation;
|
||||
import org.ofbiz.base.util.UtilXml;
|
||||
import org.ofbiz.base.util.cache.UtilCache;
|
||||
import org.ofbiz.minilang.operation.MapProcessor;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* SimpleMapProcessor Mini Language
|
||||
*/
|
||||
public class SimpleMapProcessor {
|
||||
|
||||
private static final UtilCache<String, Map<String, MapProcessor>> simpleMapProcessorsResourceCache = UtilCache.createUtilCache("minilang.SimpleMapProcessorsResource", 0, 0);
|
||||
private static final UtilCache<URL, Map<String, MapProcessor>> simpleMapProcessorsURLCache = UtilCache.createUtilCache("minilang.SimpleMapProcessorsURL", 0, 0);
|
||||
|
||||
protected static Map<String, MapProcessor> getAllProcessors(URL xmlURL) throws MiniLangException {
|
||||
Map<String, MapProcessor> mapProcessors = new HashMap<String, MapProcessor>();
|
||||
// read in the file
|
||||
Document document = null;
|
||||
try {
|
||||
document = UtilXml.readXmlDocument(xmlURL, true);
|
||||
} catch (java.io.IOException e) {
|
||||
throw new MiniLangException("Could not read XML file", e);
|
||||
} catch (org.xml.sax.SAXException e) {
|
||||
throw new MiniLangException("Could not parse XML file", e);
|
||||
} catch (javax.xml.parsers.ParserConfigurationException e) {
|
||||
throw new MiniLangException("XML parser not setup correctly", e);
|
||||
}
|
||||
if (document == null) {
|
||||
throw new MiniLangException("Could not find SimpleMapProcessor XML document: " + xmlURL.toString());
|
||||
}
|
||||
Element rootElement = document.getDocumentElement();
|
||||
for (Element simpleMapProcessorElement : UtilXml.childElementList(rootElement, "simple-map-processor")) {
|
||||
MapProcessor processor = new MapProcessor(simpleMapProcessorElement);
|
||||
mapProcessors.put(simpleMapProcessorElement.getAttribute("name"), processor);
|
||||
}
|
||||
return mapProcessors;
|
||||
}
|
||||
|
||||
protected static Map<String, MapProcessor> getProcessors(String xmlResource, String name, ClassLoader loader) throws MiniLangException {
|
||||
Map<String, MapProcessor> simpleMapProcessors = simpleMapProcessorsResourceCache.get(xmlResource);
|
||||
if (simpleMapProcessors == null) {
|
||||
URL xmlURL = null;
|
||||
try {
|
||||
xmlURL = FlexibleLocation.resolveLocation(xmlResource, loader);
|
||||
} catch (MalformedURLException e) {
|
||||
throw new MiniLangException("Could not find SimpleMapProcessor XML document in resource: " + xmlResource + "; error was: " + e.toString(), e);
|
||||
}
|
||||
if (xmlURL == null) {
|
||||
throw new MiniLangException("Could not find SimpleMapProcessor XML document in resource: " + xmlResource);
|
||||
}
|
||||
simpleMapProcessors = simpleMapProcessorsResourceCache.putIfAbsentAndGet(xmlResource, getAllProcessors(xmlURL));
|
||||
}
|
||||
return simpleMapProcessors;
|
||||
}
|
||||
|
||||
protected static Map<String, MapProcessor> getProcessors(URL xmlURL, String name) throws MiniLangException {
|
||||
Map<String, MapProcessor> simpleMapProcessors = simpleMapProcessorsURLCache.get(xmlURL);
|
||||
if (simpleMapProcessors == null) {
|
||||
simpleMapProcessors = simpleMapProcessorsURLCache.putIfAbsentAndGet(xmlURL, getAllProcessors(xmlURL));
|
||||
}
|
||||
return simpleMapProcessors;
|
||||
}
|
||||
|
||||
public static void runSimpleMapProcessor(String xmlResource, String name, Map<String, Object> inMap, Map<String, Object> results, List<Object> messages, Locale locale) throws MiniLangException {
|
||||
runSimpleMapProcessor(xmlResource, name, inMap, results, messages, locale, null);
|
||||
}
|
||||
|
||||
public static void runSimpleMapProcessor(String xmlResource, String name, Map<String, Object> inMap, Map<String, Object> results, List<Object> messages, Locale locale, ClassLoader loader) throws MiniLangException {
|
||||
if (loader == null) {
|
||||
loader = Thread.currentThread().getContextClassLoader();
|
||||
}
|
||||
Map<String, MapProcessor> mapProcessors = getProcessors(xmlResource, name, loader);
|
||||
MapProcessor processor = mapProcessors.get(name);
|
||||
if (processor == null) {
|
||||
throw new MiniLangException("Could not find SimpleMapProcessor named " + name + " in XML document resource: " + xmlResource);
|
||||
}
|
||||
if (processor != null) {
|
||||
processor.exec(inMap, results, messages, locale, loader);
|
||||
}
|
||||
}
|
||||
|
||||
public static void runSimpleMapProcessor(URL xmlURL, String name, Map<String, Object> inMap, Map<String, Object> results, List<Object> messages, Locale locale, ClassLoader loader) throws MiniLangException {
|
||||
if (loader == null)
|
||||
loader = Thread.currentThread().getContextClassLoader();
|
||||
Map<String, MapProcessor> mapProcessors = getProcessors(xmlURL, name);
|
||||
MapProcessor processor = mapProcessors.get(name);
|
||||
if (processor == null) {
|
||||
throw new MiniLangException("Could not find SimpleMapProcessor named " + name + " in XML document: " + xmlURL.toString());
|
||||
}
|
||||
if (processor != null) {
|
||||
processor.exec(inMap, results, messages, locale, loader);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,744 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.ServiceLoader;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.ofbiz.base.location.FlexibleLocation;
|
||||
import org.ofbiz.base.util.Assert;
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.UtilGenerics;
|
||||
import org.ofbiz.base.util.UtilMisc;
|
||||
import org.ofbiz.base.util.UtilProperties;
|
||||
import org.ofbiz.base.util.UtilValidate;
|
||||
import org.ofbiz.base.util.UtilXml;
|
||||
import org.ofbiz.base.util.cache.UtilCache;
|
||||
import org.ofbiz.entity.GenericEntity;
|
||||
import org.ofbiz.entity.GenericValue;
|
||||
import org.ofbiz.entity.transaction.GenericTransactionException;
|
||||
import org.ofbiz.entity.transaction.TransactionUtil;
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.ofbiz.minilang.method.MethodOperation.DeprecatedOperation;
|
||||
import org.ofbiz.service.DispatchContext;
|
||||
import org.ofbiz.service.ModelService;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <simple-method> element.
|
||||
* <p>
|
||||
* The Mini-language script engine follows the
|
||||
* <a href="http://en.wikipedia.org/wiki/Flyweight_pattern">flyweight</a>
|
||||
* design pattern. Mini-language XML files are parsed twice - first into a W3C DOM
|
||||
* tree, then the DOM tree is parsed into element model objects. Each XML element
|
||||
* has a model class, and each model class has its own factory.
|
||||
* </p>
|
||||
* <p>
|
||||
* Mini-language can be extended by:<br />
|
||||
* <ul>
|
||||
* <li>Creating model classes that extend {@link org.ofbiz.minilang.method.MethodOperation}</li>
|
||||
* <li>Creating factories for the model classes that implement {@link org.ofbiz.minilang.method.MethodOperation.Factory}</li>
|
||||
* <li>Create a service provider information file for the factory classes
|
||||
* (see <a href="http://docs.oracle.com/javase/6/docs/api/java/util/ServiceLoader.html" target="_blank">ServiceLoader</a>)
|
||||
* </li>
|
||||
* </ul>
|
||||
* </p>
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-The{{%3Csimplemethod%3E}}element">Mini-language Reference</a>
|
||||
*/
|
||||
public final class SimpleMethod extends MiniLangElement {
|
||||
|
||||
public static final String module = SimpleMethod.class.getName();
|
||||
private static final String err_resource = "MiniLangErrorUiLabels";
|
||||
private static final String[] DEPRECATED_ATTRIBUTES = {"parameter-map-name", "locale-name", "delegator-name", "security-name", "dispatcher-name", "user-login-name"};
|
||||
private static final Map<String, MethodOperation.Factory<MethodOperation>> methodOperationFactories;
|
||||
private static final UtilCache<String, Map<String, SimpleMethod>> simpleMethodsDirectCache = UtilCache.createUtilCache("minilang.SimpleMethodsDirect", 0, 0);
|
||||
private static final UtilCache<String, SimpleMethod> simpleMethodsResourceCache = UtilCache.createUtilCache("minilang.SimpleMethodsResource", 0, 0);
|
||||
|
||||
static {
|
||||
Map<String, MethodOperation.Factory<MethodOperation>> mapFactories = new HashMap<String, MethodOperation.Factory<MethodOperation>>();
|
||||
Iterator<MethodOperation.Factory<MethodOperation>> it = UtilGenerics.cast(ServiceLoader.load(MethodOperation.Factory.class, SimpleMethod.class.getClassLoader()).iterator());
|
||||
while (it.hasNext()) {
|
||||
MethodOperation.Factory<MethodOperation> factory = it.next();
|
||||
mapFactories.put(factory.getName(), factory);
|
||||
}
|
||||
methodOperationFactories = Collections.unmodifiableMap(mapFactories);
|
||||
}
|
||||
|
||||
// This method is needed only during the v1 to v2 transition
|
||||
private static boolean autoCorrect(Element element) {
|
||||
boolean elementModified = false;
|
||||
for (int i = 0; i < DEPRECATED_ATTRIBUTES.length; i++) {
|
||||
if (!element.getAttribute(DEPRECATED_ATTRIBUTES[i]).isEmpty()) {
|
||||
element.removeAttribute(DEPRECATED_ATTRIBUTES[i]);
|
||||
elementModified = true;
|
||||
}
|
||||
}
|
||||
return elementModified;
|
||||
}
|
||||
|
||||
private static void compileAllSimpleMethods(Element rootElement, Map<String, SimpleMethod> simpleMethods, String location) throws MiniLangException {
|
||||
for (Element simpleMethodElement : UtilXml.childElementList(rootElement, "simple-method")) {
|
||||
SimpleMethod simpleMethod = new SimpleMethod(simpleMethodElement, location);
|
||||
if (simpleMethods.containsKey(simpleMethod.getMethodName())) {
|
||||
MiniLangValidate.handleError("Duplicate method name found", simpleMethod, simpleMethodElement);
|
||||
}
|
||||
simpleMethods.put(simpleMethod.getMethodName(), simpleMethod);
|
||||
}
|
||||
}
|
||||
|
||||
private static Map<String, SimpleMethod> getAllDirectSimpleMethods(String name, String content, String fromLocation) throws MiniLangException {
|
||||
if (UtilValidate.isEmpty(fromLocation)) {
|
||||
fromLocation = "<location not known>";
|
||||
}
|
||||
Map<String, SimpleMethod> simpleMethods = new HashMap<String, SimpleMethod>();
|
||||
Document document = null;
|
||||
try {
|
||||
document = UtilXml.readXmlDocument(content, true, true);
|
||||
} catch (Exception e) {
|
||||
throw new MiniLangException("Could not read SimpleMethod XML document [" + name + "]: ", e);
|
||||
}
|
||||
compileAllSimpleMethods(document.getDocumentElement(), simpleMethods, fromLocation);
|
||||
return simpleMethods;
|
||||
}
|
||||
|
||||
private static Map<String, SimpleMethod> getAllSimpleMethods(URL xmlURL) throws MiniLangException {
|
||||
Map<String, SimpleMethod> simpleMethods = new LinkedHashMap<String, SimpleMethod>();
|
||||
Document document = null;
|
||||
try {
|
||||
document = UtilXml.readXmlDocument(xmlURL, true, true);
|
||||
} catch (Exception e) {
|
||||
throw new MiniLangException("Could not read SimpleMethod XML document [" + xmlURL + "]: ", e);
|
||||
}
|
||||
Element rootElement = document.getDocumentElement();
|
||||
if (!"simple-methods".equalsIgnoreCase(rootElement.getTagName())) {
|
||||
rootElement = UtilXml.firstChildElement(rootElement, "simple-methods");
|
||||
}
|
||||
|
||||
compileAllSimpleMethods(rootElement, simpleMethods, xmlURL.toString());
|
||||
if (MiniLangUtil.isDocumentAutoCorrected(document)) {
|
||||
MiniLangUtil.writeMiniLangDocument(xmlURL, document);
|
||||
}
|
||||
return simpleMethods;
|
||||
}
|
||||
|
||||
public static Map<String, SimpleMethod> getDirectSimpleMethods(String name, String content, String fromLocation) throws MiniLangException {
|
||||
Assert.notNull("name", name, "content", content);
|
||||
Map<String, SimpleMethod> simpleMethods = simpleMethodsDirectCache.get(name);
|
||||
if (simpleMethods == null) {
|
||||
simpleMethods = getAllDirectSimpleMethods(name, content, fromLocation);
|
||||
simpleMethods = simpleMethodsDirectCache.putIfAbsentAndGet(name, simpleMethods);
|
||||
}
|
||||
return simpleMethods;
|
||||
}
|
||||
|
||||
public static SimpleMethod getSimpleMethod(String xmlResource, String methodName, ClassLoader loader) throws MiniLangException {
|
||||
Assert.notNull("methodName", methodName);
|
||||
String key = xmlResource.concat("#").concat(methodName);
|
||||
SimpleMethod method = simpleMethodsResourceCache.get(key);
|
||||
if (method == null) {
|
||||
Map<String, SimpleMethod> simpleMethods = getSimpleMethods(xmlResource, loader);
|
||||
for (Map.Entry<String, SimpleMethod> entry : simpleMethods.entrySet()) {
|
||||
String putKey = xmlResource.concat("#").concat(entry.getKey());
|
||||
simpleMethodsResourceCache.putIfAbsent(putKey, entry.getValue());
|
||||
}
|
||||
}
|
||||
return simpleMethodsResourceCache.get(key);
|
||||
}
|
||||
|
||||
public static SimpleMethod getSimpleMethod(URL xmlUrl, String methodName) throws MiniLangException {
|
||||
Assert.notNull("methodName", methodName);
|
||||
String xmlResource = xmlUrl.toString();
|
||||
String key = xmlResource.concat("#").concat(methodName);
|
||||
SimpleMethod method = simpleMethodsResourceCache.get(key);
|
||||
if (method == null) {
|
||||
Map<String, SimpleMethod> simpleMethods = getAllSimpleMethods(xmlUrl);
|
||||
for (Map.Entry<String, SimpleMethod> entry : simpleMethods.entrySet()) {
|
||||
String putKey = xmlResource.concat("#").concat(entry.getKey());
|
||||
simpleMethodsResourceCache.putIfAbsent(putKey, entry.getValue());
|
||||
}
|
||||
}
|
||||
return simpleMethodsResourceCache.get(key);
|
||||
}
|
||||
|
||||
private static Map<String, SimpleMethod> getSimpleMethods(String xmlResource, ClassLoader loader) throws MiniLangException {
|
||||
Assert.notNull("xmlResource", xmlResource);
|
||||
URL xmlURL = null;
|
||||
try {
|
||||
xmlURL = FlexibleLocation.resolveLocation(xmlResource, loader);
|
||||
} catch (MalformedURLException e) {
|
||||
throw new MiniLangException("Could not find SimpleMethod XML document in resource: " + xmlResource + "; error was: " + e.toString(), e);
|
||||
}
|
||||
if (xmlURL == null) {
|
||||
throw new MiniLangException("Could not find SimpleMethod XML document in resource: " + xmlResource);
|
||||
}
|
||||
return getAllSimpleMethods(xmlURL);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a List of <code>SimpleMethod</code> objects compiled from <code>xmlResource</code>.
|
||||
* The ordering in the List is the same as the XML file.
|
||||
* <p>This method is used by unit test framework to run tests in the order they appear in the XML file.
|
||||
* Method caching is bypassed since the methods are executed only once.</p>
|
||||
*
|
||||
* @param xmlResource
|
||||
* @param loader
|
||||
* @return
|
||||
* @throws MiniLangException
|
||||
*/
|
||||
public static List<SimpleMethod> getSimpleMethodsList(String xmlResource, ClassLoader loader) throws MiniLangException {
|
||||
Map<String, SimpleMethod> simpleMethodMap = getSimpleMethods(xmlResource, loader);
|
||||
return new ArrayList<SimpleMethod>(simpleMethodMap.values());
|
||||
}
|
||||
|
||||
public static List<MethodOperation> readOperations(Element simpleMethodElement, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
Assert.notNull("simpleMethodElement", simpleMethodElement, "simpleMethod", simpleMethod);
|
||||
List<? extends Element> operationElements = UtilXml.childElementList(simpleMethodElement);
|
||||
ArrayList<MethodOperation> methodOperations = new ArrayList<MethodOperation>(operationElements.size());
|
||||
if (UtilValidate.isNotEmpty(operationElements)) {
|
||||
for (Element curOperElem : operationElements) {
|
||||
String nodeName = UtilXml.getNodeNameIgnorePrefix(curOperElem);
|
||||
MethodOperation methodOp = null;
|
||||
MethodOperation.Factory<MethodOperation> factory = methodOperationFactories.get(nodeName);
|
||||
if (factory != null) {
|
||||
methodOp = factory.createMethodOperation(curOperElem, simpleMethod);
|
||||
} else if ("else".equals(nodeName)) {
|
||||
// don't add anything, but don't complain either, this one is handled in the individual operations
|
||||
} else {
|
||||
MiniLangValidate.handleError("Invalid element found", simpleMethod, curOperElem);
|
||||
}
|
||||
if (methodOp == null) {
|
||||
continue;
|
||||
}
|
||||
methodOperations.add(methodOp);
|
||||
DeprecatedOperation depOp = methodOp.getClass().getAnnotation(DeprecatedOperation.class);
|
||||
if (depOp != null) {
|
||||
MiniLangValidate.handleError("The " + nodeName + " operation has been deprecated in favor of the " + depOp.value() + " operation", simpleMethod, curOperElem);
|
||||
}
|
||||
}
|
||||
}
|
||||
methodOperations.trimToSize();
|
||||
return methodOperations;
|
||||
}
|
||||
|
||||
public static String runSimpleEvent(String xmlResource, String methodName, HttpServletRequest request, HttpServletResponse response) throws MiniLangException {
|
||||
return runSimpleMethod(xmlResource, methodName, new MethodContext(request, response, null));
|
||||
}
|
||||
|
||||
public static String runSimpleEvent(String xmlResource, String methodName, HttpServletRequest request, HttpServletResponse response, ClassLoader loader) throws MiniLangException {
|
||||
return runSimpleMethod(xmlResource, methodName, new MethodContext(request, response, loader));
|
||||
}
|
||||
|
||||
public static String runSimpleEvent(URL xmlURL, String methodName, HttpServletRequest request, HttpServletResponse response, ClassLoader loader) throws MiniLangException {
|
||||
return runSimpleMethod(xmlURL, methodName, new MethodContext(request, response, loader));
|
||||
}
|
||||
|
||||
public static String runSimpleMethod(String xmlResource, String methodName, MethodContext methodContext) throws MiniLangException {
|
||||
Assert.notNull("methodContext", methodContext);
|
||||
SimpleMethod simpleMethod = getSimpleMethod(xmlResource, methodName, methodContext.getLoader());
|
||||
if (simpleMethod == null) {
|
||||
throw new MiniLangException("Could not find SimpleMethod " + methodName + " in XML document in resource: " + xmlResource);
|
||||
}
|
||||
return simpleMethod.exec(methodContext);
|
||||
}
|
||||
|
||||
public static String runSimpleMethod(URL xmlURL, String methodName, MethodContext methodContext) throws MiniLangException {
|
||||
SimpleMethod simpleMethod = getSimpleMethod(xmlURL, methodName);
|
||||
if (simpleMethod == null) {
|
||||
throw new MiniLangException("Could not find SimpleMethod " + methodName + " in XML document from URL: " + xmlURL.toString());
|
||||
}
|
||||
return simpleMethod.exec(methodContext);
|
||||
}
|
||||
|
||||
public static Map<String, Object> runSimpleService(String xmlResource, String methodName, DispatchContext ctx, Map<String, ? extends Object> context) throws MiniLangException {
|
||||
MethodContext methodContext = new MethodContext(ctx, context, null);
|
||||
runSimpleMethod(xmlResource, methodName, methodContext);
|
||||
return methodContext.getResults();
|
||||
}
|
||||
|
||||
public static Map<String, Object> runSimpleService(String xmlResource, String methodName, DispatchContext ctx, Map<String, ? extends Object> context, ClassLoader loader) throws MiniLangException {
|
||||
MethodContext methodContext = new MethodContext(ctx, context, loader);
|
||||
runSimpleMethod(xmlResource, methodName, methodContext);
|
||||
return methodContext.getResults();
|
||||
}
|
||||
|
||||
public static Map<String, Object> runSimpleService(URL xmlURL, String methodName, DispatchContext ctx, Map<String, ? extends Object> context, ClassLoader loader) throws MiniLangException {
|
||||
MethodContext methodContext = new MethodContext(ctx, context, loader);
|
||||
runSimpleMethod(xmlURL, methodName, methodContext);
|
||||
return methodContext.getResults();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execs the given operations returning true if all return true, or returning false and stopping if any return false.
|
||||
* @throws MiniLangException
|
||||
*/
|
||||
public static boolean runSubOps(List<MethodOperation> methodOperations, MethodContext methodContext) throws MiniLangException {
|
||||
Assert.notNull("methodOperations", methodOperations, "methodContext", methodContext);
|
||||
for (MethodOperation methodOperation : methodOperations) {
|
||||
if (!methodOperation.exec(methodContext)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private final String defaultErrorCode;
|
||||
private final String defaultSuccessCode;
|
||||
private final String eventErrorMessageListName;
|
||||
private final String eventErrorMessageName;
|
||||
private final String eventEventMessageListName;
|
||||
private final String eventEventMessageName;
|
||||
private final String eventRequestName;
|
||||
private final String eventResponseCodeName;
|
||||
private final String eventResponseName;
|
||||
private final String eventSessionName;
|
||||
private final String fromLocation;
|
||||
private final boolean loginRequired;
|
||||
private final String methodName;
|
||||
private final List<MethodOperation> methodOperations;
|
||||
private final String serviceErrorMessageListName;
|
||||
private final String serviceErrorMessageMapName;
|
||||
private final String serviceErrorMessageName;
|
||||
private final String serviceResponseMessageName;
|
||||
private final String serviceSuccessMessageListName;
|
||||
private final String serviceSuccessMessageName;
|
||||
private final String shortDescription;
|
||||
private final boolean useTransaction;
|
||||
|
||||
public SimpleMethod(Element simpleMethodElement, String fromLocation) throws MiniLangException {
|
||||
super(simpleMethodElement, null);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
String locationMsg = " File = ".concat(fromLocation);
|
||||
if (simpleMethodElement.getAttribute("method-name").isEmpty()) {
|
||||
MiniLangValidate.handleError("Element must include the \"method-name\" attribute.".concat(locationMsg), null, simpleMethodElement);
|
||||
}
|
||||
for (int i = 0; i < DEPRECATED_ATTRIBUTES.length; i++) {
|
||||
if (!simpleMethodElement.getAttribute(DEPRECATED_ATTRIBUTES[i]).isEmpty()) {
|
||||
MiniLangValidate.handleError("Attribute \"" + DEPRECATED_ATTRIBUTES[i] + "\" is deprecated (no replacement)." + locationMsg, null, simpleMethodElement);
|
||||
}
|
||||
}
|
||||
}
|
||||
boolean elementModified = autoCorrect(simpleMethodElement);
|
||||
if (elementModified && MiniLangUtil.autoCorrectOn()) {
|
||||
MiniLangUtil.flagDocumentAsCorrected(simpleMethodElement);
|
||||
}
|
||||
this.fromLocation = fromLocation;
|
||||
methodName = simpleMethodElement.getAttribute("method-name");
|
||||
shortDescription = simpleMethodElement.getAttribute("short-description");
|
||||
defaultErrorCode = UtilXml.elementAttribute(simpleMethodElement, "default-error-code", "error");
|
||||
defaultSuccessCode = UtilXml.elementAttribute(simpleMethodElement, "default-success-code", "success");
|
||||
eventRequestName = UtilXml.elementAttribute(simpleMethodElement, "event-request-object-name", "request");
|
||||
eventSessionName = UtilXml.elementAttribute(simpleMethodElement, "event-session-object-name", "session");
|
||||
eventResponseName = UtilXml.elementAttribute(simpleMethodElement, "event-response-object-name", "response");
|
||||
eventResponseCodeName = UtilXml.elementAttribute(simpleMethodElement, "event-response-code-name", "_response_code_");
|
||||
eventErrorMessageName = UtilXml.elementAttribute(simpleMethodElement, "event-error-message-name", "_error_message_");
|
||||
eventErrorMessageListName = UtilXml.elementAttribute(simpleMethodElement, "event-error-message-list-name", "_error_message_list_");
|
||||
eventEventMessageName = UtilXml.elementAttribute(simpleMethodElement, "event-event-message-name", "_event_message_");
|
||||
eventEventMessageListName = UtilXml.elementAttribute(simpleMethodElement, "event-event-message-list-name", "_event_message_list_");
|
||||
serviceResponseMessageName = UtilXml.elementAttribute(simpleMethodElement, "service-response-message-name", "responseMessage");
|
||||
serviceErrorMessageName = UtilXml.elementAttribute(simpleMethodElement, "service-error-message-name", "errorMessage");
|
||||
serviceErrorMessageListName = UtilXml.elementAttribute(simpleMethodElement, "service-error-message-list-name", "errorMessageList");
|
||||
serviceErrorMessageMapName = UtilXml.elementAttribute(simpleMethodElement, "service-error-message-map-name", "errorMessageMap");
|
||||
serviceSuccessMessageName = UtilXml.elementAttribute(simpleMethodElement, "service-success-message-name", "successMessage");
|
||||
serviceSuccessMessageListName = UtilXml.elementAttribute(simpleMethodElement, "service-success-message-list-name", "successMessageList");
|
||||
loginRequired = !"false".equals(simpleMethodElement.getAttribute("login-required"));
|
||||
useTransaction = !"false".equals(simpleMethodElement.getAttribute("use-transaction"));
|
||||
methodOperations = Collections.unmodifiableList(readOperations(simpleMethodElement, this));
|
||||
}
|
||||
|
||||
public void addErrorMessage(MethodContext methodContext, String message) {
|
||||
String messageListName = methodContext.getMethodType() == MethodContext.EVENT ? getEventErrorMessageListName() : getServiceErrorMessageListName();
|
||||
addMessage(methodContext, messageListName, message);
|
||||
}
|
||||
|
||||
public void addMessage(MethodContext methodContext, String message) {
|
||||
String messageListName = methodContext.getMethodType() == MethodContext.EVENT ? getEventEventMessageListName() : getServiceSuccessMessageListName();
|
||||
addMessage(methodContext, messageListName, message);
|
||||
}
|
||||
|
||||
private void addMessage(MethodContext methodContext, String messageListName, String message) {
|
||||
List<String> messages = methodContext.getEnv(messageListName);
|
||||
if (messages == null) {
|
||||
messages = new LinkedList<String>();
|
||||
methodContext.putEnv(messageListName, messages);
|
||||
}
|
||||
messages.add(message);
|
||||
}
|
||||
|
||||
/** Execute the Simple Method operations */
|
||||
public String exec(MethodContext methodContext) throws MiniLangException {
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "Begin simple-method. Script is running as " + (methodContext.getMethodType() == MethodContext.EVENT ? "an event." : "a service."));
|
||||
}
|
||||
Locale locale = methodContext.getLocale();
|
||||
GenericValue userLogin = methodContext.getUserLogin();
|
||||
if (loginRequired) {
|
||||
if (userLogin == null) {
|
||||
Map<String, Object> messageMap = UtilMisc.<String, Object> toMap("shortDescription", shortDescription);
|
||||
String errMsg = UtilProperties.getMessage(SimpleMethod.err_resource, "simpleMethod.must_logged_process", messageMap, locale) + ".";
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "login-required attribute set to \"true\" but UserLogin GenericValue was not found, returning error message:", errMsg);
|
||||
}
|
||||
return returnError(methodContext, errMsg);
|
||||
}
|
||||
}
|
||||
if (userLogin != null) {
|
||||
methodContext.putEnv(getUserLoginEnvName(), userLogin);
|
||||
}
|
||||
methodContext.putEnv("nullField", GenericEntity.NULL_FIELD);
|
||||
methodContext.putEnv(getDelegatorEnvName(), methodContext.getDelegator());
|
||||
methodContext.putEnv(getSecurityEnvName(), methodContext.getSecurity());
|
||||
methodContext.putEnv(getDispatcherEnvName(), methodContext.getDispatcher());
|
||||
methodContext.putEnv("locale", locale);
|
||||
methodContext.putEnv(getParameterMapName(), methodContext.getParameters());
|
||||
if (methodContext.getMethodType() == MethodContext.EVENT) {
|
||||
methodContext.putEnv(eventRequestName, methodContext.getRequest());
|
||||
methodContext.putEnv(eventSessionName, methodContext.getRequest().getSession());
|
||||
methodContext.putEnv(eventResponseName, methodContext.getResponse());
|
||||
}
|
||||
methodContext.putEnv("simpleMethod", this);
|
||||
methodContext.putEnv("methodName", this.getMethodName());
|
||||
methodContext.putEnv("methodShortDescription", this.getShortDescription());
|
||||
// if using transaction, try to start here
|
||||
boolean beganTransaction = false;
|
||||
if (useTransaction) {
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "use-transaction attribute set to \"true\", beginning transaction.");
|
||||
}
|
||||
try {
|
||||
beganTransaction = TransactionUtil.begin();
|
||||
} catch (GenericTransactionException e) {
|
||||
String errMsg = UtilProperties.getMessage(SimpleMethod.err_resource, "simpleMethod.error_begin_transaction", locale) + ": " + e.getMessage();
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "An exception was thrown while beginning a transaction, returning error message:", errMsg);
|
||||
}
|
||||
return returnError(methodContext, errMsg);
|
||||
}
|
||||
}
|
||||
// declare errorMsg here just in case transaction ops fail
|
||||
String errorMsg = "";
|
||||
boolean finished = false;
|
||||
try {
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "Begin running sub-elements.");
|
||||
}
|
||||
finished = runSubOps(methodOperations, methodContext);
|
||||
} catch (Throwable t) {
|
||||
// make SURE nothing gets thrown through
|
||||
String errMsg = UtilProperties.getMessage(SimpleMethod.err_resource, "simpleMethod.error_running", locale) + ": " + t.getMessage();
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "An exception was thrown while running sub-elements, error message was:", errMsg);
|
||||
}
|
||||
finished = false;
|
||||
errorMsg += errMsg;
|
||||
}
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "End running sub-elements.");
|
||||
}
|
||||
String returnValue = null;
|
||||
String response = null;
|
||||
StringBuilder summaryErrorStringBuffer = new StringBuilder();
|
||||
if (methodContext.getMethodType() == MethodContext.EVENT) {
|
||||
boolean forceError = false;
|
||||
String tempErrorMsg = (String) methodContext.getEnv(eventErrorMessageName);
|
||||
if (errorMsg.length() > 0 || UtilValidate.isNotEmpty(tempErrorMsg)) {
|
||||
errorMsg += tempErrorMsg;
|
||||
methodContext.getRequest().setAttribute("_ERROR_MESSAGE_", errorMsg);
|
||||
forceError = true;
|
||||
summaryErrorStringBuffer.append(errorMsg);
|
||||
}
|
||||
List<Object> tempErrorMsgList = UtilGenerics.checkList(methodContext.getEnv(eventErrorMessageListName));
|
||||
if (UtilValidate.isNotEmpty(tempErrorMsgList)) {
|
||||
methodContext.getRequest().setAttribute("_ERROR_MESSAGE_LIST_", tempErrorMsgList);
|
||||
forceError = true;
|
||||
summaryErrorStringBuffer.append("; ");
|
||||
summaryErrorStringBuffer.append(tempErrorMsgList.toString());
|
||||
}
|
||||
String eventMsg = (String) methodContext.getEnv(eventEventMessageName);
|
||||
if (UtilValidate.isNotEmpty(eventMsg)) {
|
||||
methodContext.getRequest().setAttribute("_EVENT_MESSAGE_", eventMsg);
|
||||
}
|
||||
List<String> eventMsgList = UtilGenerics.checkList(methodContext.getEnv(eventEventMessageListName));
|
||||
if (UtilValidate.isNotEmpty(eventMsgList)) {
|
||||
methodContext.getRequest().setAttribute("_EVENT_MESSAGE_LIST_", eventMsgList);
|
||||
}
|
||||
response = (String) methodContext.getEnv(eventResponseCodeName);
|
||||
if (UtilValidate.isEmpty(response)) {
|
||||
if (forceError) {
|
||||
// override response code, always use error code
|
||||
Debug.logInfo("No response code string found, but error messages found so assuming error; returning code [" + defaultErrorCode + "]", module);
|
||||
response = defaultErrorCode;
|
||||
} else {
|
||||
Debug.logInfo("No response code string or errors found, assuming success; returning code [" + defaultSuccessCode + "]", module);
|
||||
response = defaultSuccessCode;
|
||||
}
|
||||
} else if ("null".equalsIgnoreCase(response)) {
|
||||
response = null;
|
||||
}
|
||||
returnValue = response;
|
||||
} else {
|
||||
boolean forceError = false;
|
||||
String tempErrorMsg = (String) methodContext.getEnv(serviceErrorMessageName);
|
||||
if (errorMsg.length() > 0 || UtilValidate.isNotEmpty(tempErrorMsg)) {
|
||||
errorMsg += tempErrorMsg;
|
||||
methodContext.putResult(ModelService.ERROR_MESSAGE, errorMsg);
|
||||
forceError = true;
|
||||
summaryErrorStringBuffer.append(errorMsg);
|
||||
}
|
||||
List<Object> errorMsgList = UtilGenerics.checkList(methodContext.getEnv(serviceErrorMessageListName));
|
||||
if (UtilValidate.isNotEmpty(errorMsgList)) {
|
||||
methodContext.putResult(ModelService.ERROR_MESSAGE_LIST, errorMsgList);
|
||||
forceError = true;
|
||||
summaryErrorStringBuffer.append("; ");
|
||||
summaryErrorStringBuffer.append(errorMsgList.toString());
|
||||
}
|
||||
Map<String, Object> errorMsgMap = UtilGenerics.checkMap(methodContext.getEnv(serviceErrorMessageMapName));
|
||||
if (UtilValidate.isNotEmpty(errorMsgMap)) {
|
||||
methodContext.putResult(ModelService.ERROR_MESSAGE_MAP, errorMsgMap);
|
||||
forceError = true;
|
||||
summaryErrorStringBuffer.append("; ");
|
||||
summaryErrorStringBuffer.append(errorMsgMap.toString());
|
||||
}
|
||||
String successMsg = (String) methodContext.getEnv(serviceSuccessMessageName);
|
||||
if (UtilValidate.isNotEmpty(successMsg)) {
|
||||
methodContext.putResult(ModelService.SUCCESS_MESSAGE, successMsg);
|
||||
}
|
||||
List<Object> successMsgList = UtilGenerics.checkList(methodContext.getEnv(serviceSuccessMessageListName));
|
||||
if (UtilValidate.isNotEmpty(successMsgList)) {
|
||||
methodContext.putResult(ModelService.SUCCESS_MESSAGE_LIST, successMsgList);
|
||||
}
|
||||
response = (String) methodContext.getEnv(serviceResponseMessageName);
|
||||
if (UtilValidate.isEmpty(response)) {
|
||||
if (forceError) {
|
||||
// override response code, always use error code
|
||||
Debug.logVerbose("No response code string found, but error messages found so assuming error; returning code [" + defaultErrorCode + "]", module);
|
||||
response = defaultErrorCode;
|
||||
} else {
|
||||
Debug.logVerbose("No response code string or errors found, assuming success; returning code [" + defaultSuccessCode + "]", module);
|
||||
response = defaultSuccessCode;
|
||||
}
|
||||
}
|
||||
methodContext.putResult(ModelService.RESPONSE_MESSAGE, response);
|
||||
returnValue = response;
|
||||
}
|
||||
// decide whether or not to commit based on the response message, ie only rollback if error is returned and not finished
|
||||
boolean doCommit = true;
|
||||
if (!finished && defaultErrorCode.equals(response)) {
|
||||
doCommit = false;
|
||||
}
|
||||
if (doCommit) {
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "Begin commit transaction.");
|
||||
}
|
||||
// commit here passing beganTransaction to perform it properly
|
||||
try {
|
||||
TransactionUtil.commit(beganTransaction);
|
||||
} catch (GenericTransactionException e) {
|
||||
String errMsg = "Error trying to commit transaction, could not process method: " + e.getMessage();
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "An exception was thrown while committing a transaction, returning error message:", errMsg);
|
||||
}
|
||||
errorMsg += errMsg;
|
||||
}
|
||||
} else {
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "Begin roll back transaction.");
|
||||
}
|
||||
// rollback here passing beganTransaction to either rollback, or set rollback only
|
||||
try {
|
||||
TransactionUtil.rollback(beganTransaction, "Error in simple-method [" + this.getShortDescription() + "]: " + summaryErrorStringBuffer, null);
|
||||
} catch (GenericTransactionException e) {
|
||||
String errMsg = "Error trying to rollback transaction, could not process method: " + e.getMessage();
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "An exception was thrown while rolling back a transaction, returning error message:", errMsg);
|
||||
}
|
||||
errorMsg += errMsg;
|
||||
}
|
||||
}
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "End simple-method.");
|
||||
}
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
for (MethodOperation methodOp : this.methodOperations) {
|
||||
methodOp.gatherArtifactInfo(aic);
|
||||
}
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public Set<String> getAllEntityNamesUsed() throws MiniLangException {
|
||||
ArtifactInfoContext aic = new ArtifactInfoContext();
|
||||
gatherArtifactInfo(aic);
|
||||
return aic.getEntityNames();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public Set<String> getAllServiceNamesCalled() throws MiniLangException {
|
||||
ArtifactInfoContext aic = new ArtifactInfoContext();
|
||||
gatherArtifactInfo(aic);
|
||||
return aic.getServiceNames();
|
||||
}
|
||||
|
||||
public String getDefaultErrorCode() {
|
||||
return this.defaultErrorCode;
|
||||
}
|
||||
|
||||
public String getDefaultSuccessCode() {
|
||||
return this.defaultSuccessCode;
|
||||
}
|
||||
|
||||
public String getDelegatorEnvName() {
|
||||
return "delegator";
|
||||
}
|
||||
|
||||
public String getDispatcherEnvName() {
|
||||
return "dispatcher";
|
||||
}
|
||||
|
||||
public String getEventErrorMessageListName() {
|
||||
return this.eventErrorMessageListName;
|
||||
}
|
||||
|
||||
public String getEventErrorMessageName() {
|
||||
return this.eventErrorMessageName;
|
||||
}
|
||||
|
||||
public String getEventEventMessageListName() {
|
||||
return this.eventEventMessageListName;
|
||||
}
|
||||
|
||||
public String getEventEventMessageName() {
|
||||
return this.eventEventMessageName;
|
||||
}
|
||||
|
||||
// event fields
|
||||
public String getEventRequestName() {
|
||||
return this.eventRequestName;
|
||||
}
|
||||
|
||||
public String getEventResponseCodeName() {
|
||||
return this.eventResponseCodeName;
|
||||
}
|
||||
|
||||
public String getEventSessionName() {
|
||||
return this.eventSessionName;
|
||||
}
|
||||
|
||||
public String getFileName() {
|
||||
return this.fromLocation.substring(this.fromLocation.lastIndexOf("/") + 1);
|
||||
}
|
||||
|
||||
public String getFromLocation() {
|
||||
return this.fromLocation;
|
||||
}
|
||||
|
||||
public String getLocationAndName() {
|
||||
return this.fromLocation + "#" + this.methodName;
|
||||
}
|
||||
|
||||
public boolean getLoginRequired() {
|
||||
return this.loginRequired;
|
||||
}
|
||||
|
||||
public String getMethodName() {
|
||||
return this.methodName;
|
||||
}
|
||||
|
||||
public List<MethodOperation> getMethodOperations() {
|
||||
return this.methodOperations;
|
||||
}
|
||||
|
||||
public String getParameterMapName() {
|
||||
return "parameters";
|
||||
}
|
||||
|
||||
public String getSecurityEnvName() {
|
||||
return "security";
|
||||
}
|
||||
|
||||
public String getServiceErrorMessageListName() {
|
||||
return this.serviceErrorMessageListName;
|
||||
}
|
||||
|
||||
public String getServiceErrorMessageMapName() {
|
||||
return this.serviceErrorMessageMapName;
|
||||
}
|
||||
|
||||
public String getServiceErrorMessageName() {
|
||||
return this.serviceErrorMessageName;
|
||||
}
|
||||
|
||||
public String getServiceResponseMessageName() {
|
||||
return this.serviceResponseMessageName;
|
||||
}
|
||||
|
||||
public String getServiceSuccessMessageListName() {
|
||||
return this.serviceSuccessMessageListName;
|
||||
}
|
||||
|
||||
public String getServiceSuccessMessageName() {
|
||||
return this.serviceSuccessMessageName;
|
||||
}
|
||||
|
||||
public String getShortDescription() {
|
||||
return this.shortDescription + " [" + this.fromLocation + "#" + this.methodName + "]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public SimpleMethod getSimpleMethod() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getUserLoginEnvName() {
|
||||
return "userLogin";
|
||||
}
|
||||
|
||||
public boolean getUseTransaction() {
|
||||
return this.useTransaction;
|
||||
}
|
||||
|
||||
private String returnError(MethodContext methodContext, String errorMsg) {
|
||||
if (methodContext.getMethodType() == MethodContext.EVENT) {
|
||||
methodContext.getRequest().setAttribute("_ERROR_MESSAGE_", errorMsg);
|
||||
} else {
|
||||
methodContext.putResult(ModelService.ERROR_MESSAGE, errorMsg);
|
||||
methodContext.putResult(ModelService.RESPONSE_MESSAGE, ModelService.RESPOND_ERROR);
|
||||
}
|
||||
return defaultErrorCode;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
import java.util.Set;
|
||||
import java.util.Vector;
|
||||
|
||||
import org.apache.bsf.BSFDeclaredBean;
|
||||
import org.apache.bsf.BSFException;
|
||||
import org.apache.bsf.BSFManager;
|
||||
import org.apache.bsf.util.BSFEngineImpl;
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
|
||||
/**
|
||||
* <P>
|
||||
* This is the OFBiz MiniLang SimpleMethod adapter for IBM's Bean Scripting Famework. It is an implementation of the BSFEngine class, allowing BSF aware applications to use SimpleMethod as a scripting
|
||||
* language.
|
||||
*
|
||||
* <P>
|
||||
* There should only be ONE simple-method in the XML file and it will be run as an event.
|
||||
*/
|
||||
public class SimpleMethodBsfEngine extends BSFEngineImpl {
|
||||
|
||||
public static final String module = SimpleMethodBsfEngine.class.getName();
|
||||
|
||||
protected Map<String, Object> context = new HashMap<String, Object>();
|
||||
|
||||
/**
|
||||
* This is an implementation of the apply() method. It executes the funcBody text in an "anonymous" method call with arguments.
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Override
|
||||
public Object apply(String source, int lineNo, int columnNo, Object funcBody, Vector namesVec, Vector argsVec) throws BSFException {
|
||||
// if (namesVec.size() != argsVec.size()) throw new BSFException("number of params/names mismatch");
|
||||
// if (!(funcBody instanceof String)) throw new BSFException("apply: function body must be a string");
|
||||
throw new BSFException("The apply method is not yet supported for simple-methods");
|
||||
}
|
||||
|
||||
/**
|
||||
* Invoke method name on the specified scripted object. The object may be null to indicate the global namespace of the interpreter.
|
||||
*
|
||||
* @param object
|
||||
* may be null for the global namespace.
|
||||
*/
|
||||
public Object call(Object object, String name, Object[] args) throws BSFException {
|
||||
throw new BSFException("The call method is not yet supported for SimpleMethods");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void declareBean(BSFDeclaredBean bean) throws BSFException {
|
||||
context.put(bean.name, bean.bean);
|
||||
}
|
||||
|
||||
public Object eval(String source, int lineNo, int columnNo, Object expr) throws BSFException {
|
||||
if (!(expr instanceof String))
|
||||
throw new BSFException("simple-method expression must be a string");
|
||||
// right now only supports one method per file, so get all methods and just run the first...
|
||||
Map<String, SimpleMethod> simpleMethods = null;
|
||||
try {
|
||||
simpleMethods = SimpleMethod.getDirectSimpleMethods(source, (String) expr, "<bsf source>");
|
||||
} catch (MiniLangException e) {
|
||||
throw new BSFException("Error loading/parsing simple-method XML source: " + e.getMessage());
|
||||
}
|
||||
Set<String> smNames = simpleMethods.keySet();
|
||||
if (smNames.size() == 0)
|
||||
throw new BSFException("Did not find any simple-methods in the file");
|
||||
String methodName = smNames.iterator().next();
|
||||
if (smNames.size() > 1)
|
||||
Debug.logWarning("Found more than one simple-method in the file, running the [" + methodName + "] method, you should remove all but one method from this file", module);
|
||||
SimpleMethod simpleMethod = simpleMethods.get(methodName);
|
||||
MethodContext methodContext = new MethodContext(context, null, MethodContext.EVENT);
|
||||
try {
|
||||
return simpleMethod.exec(methodContext);
|
||||
} catch (MiniLangException e) {
|
||||
throw new BSFException(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exec(String source, int lineNo, int columnNo, Object script) throws BSFException {
|
||||
eval(source, lineNo, columnNo, script);
|
||||
}
|
||||
|
||||
@SuppressWarnings("rawtypes")
|
||||
@Override
|
||||
public void initialize(BSFManager mgr, String lang, Vector declaredBeans) throws BSFException {
|
||||
super.initialize(mgr, lang, declaredBeans);
|
||||
// declare the bsf manager for callbacks, etc.
|
||||
context.put("bsf", mgr);
|
||||
for (int i = 0; i < declaredBeans.size(); i++) {
|
||||
BSFDeclaredBean bean = (BSFDeclaredBean) declaredBeans.get(i);
|
||||
declareBean(bean);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* public void compileApply (String source, int lineNo, int columnNo, Object funcBody, Vector paramNames, Vector arguments, CodeBuffer cb) throws BSFException;
|
||||
*
|
||||
* public void compileExpr (String source, int lineNo, int columnNo, Object expr, CodeBuffer cb) throws BSFException;
|
||||
*
|
||||
* public void compileScript (String source, int lineNo, int columnNo, Object script, CodeBuffer cb) throws BSFException;
|
||||
*/
|
||||
|
||||
public void setDebug(boolean debug) {
|
||||
// interpreter.DEBUG=debug;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void terminate() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void undeclareBean(BSFDeclaredBean bean) throws BSFException {
|
||||
context.remove(bean.name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.ofbiz.service.DispatchContext;
|
||||
import org.ofbiz.service.GenericServiceException;
|
||||
import org.ofbiz.service.ModelService;
|
||||
import org.ofbiz.service.ServiceDispatcher;
|
||||
import org.ofbiz.service.engine.GenericAsyncEngine;
|
||||
|
||||
/**
|
||||
* Mini-language Service Engine.
|
||||
*/
|
||||
public final class SimpleServiceEngine extends GenericAsyncEngine {
|
||||
|
||||
/** Creates new Engine */
|
||||
public SimpleServiceEngine(ServiceDispatcher dispatcher) {
|
||||
super(dispatcher);
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the service synchronously and return the result
|
||||
*
|
||||
* @param context
|
||||
* Map of name, value pairs composing the context
|
||||
* @return Map of name, value pairs composing the result
|
||||
*/
|
||||
@Override
|
||||
public Map<String, Object> runSync(String localName, ModelService modelService, Map<String, Object> context) throws GenericServiceException {
|
||||
Map<String, Object> result = serviceInvoker(localName, modelService, context);
|
||||
if (result == null)
|
||||
throw new GenericServiceException("Service did not return expected result");
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the service synchronously and IGNORE the result
|
||||
*
|
||||
* @param context
|
||||
* Map of name, value pairs composing the context
|
||||
*/
|
||||
@Override
|
||||
public void runSyncIgnore(String localName, ModelService modelService, Map<String, Object> context) throws GenericServiceException {
|
||||
runSync(localName, modelService, context);
|
||||
}
|
||||
|
||||
// Invoke the simple method from a service context
|
||||
private Map<String, Object> serviceInvoker(String localName, ModelService modelService, Map<String, ? extends Object> context) throws GenericServiceException {
|
||||
// static java service methods should be: public Map methodName(DispatchContext dctx, Map context)
|
||||
DispatchContext dctx = dispatcher.getLocalContext(localName);
|
||||
// check the package and method names
|
||||
if (modelService.location == null || modelService.invoke == null)
|
||||
throw new GenericServiceException("Cannot locate service to invoke (location or invoke name missing)");
|
||||
// get the classloader to use
|
||||
ClassLoader classLoader = null;
|
||||
if (dctx != null)
|
||||
classLoader = dctx.getClassLoader();
|
||||
// if the classLoader is null, no big deal, SimpleMethod will use the
|
||||
// current thread's ClassLoader by default if null passed in
|
||||
try {
|
||||
return SimpleMethod.runSimpleService(this.getLocation(modelService), modelService.invoke, dctx, context, classLoader);
|
||||
} catch (MiniLangException e) {
|
||||
throw new GenericServiceException("Error running simple method [" + modelService.invoke + "] in XML file [" + modelService.location + "]: ", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang;
|
||||
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Thrown to indicate that a Mini-language element is invalid.
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public class ValidationException extends MiniLangException {
|
||||
|
||||
private final SimpleMethod method;
|
||||
private final Element element;
|
||||
|
||||
public ValidationException(String str, SimpleMethod method, Element element) {
|
||||
super(str);
|
||||
this.method = method;
|
||||
this.element = element;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
StringBuilder sb = new StringBuilder(super.getMessage());
|
||||
if (method != null) {
|
||||
sb.append(" Method = ").append(method.getMethodName()).append(", File = ").append(method.getFromLocation());
|
||||
}
|
||||
if (element != null) {
|
||||
sb.append(", Element = <").append(element.getTagName()).append(">");
|
||||
Object lineNumber = element.getUserData("startLine");
|
||||
if (lineNumber != null) {
|
||||
sb.append(", Line ").append(lineNumber);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.artifact;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
|
||||
/**
|
||||
* An object used for gathering artifact information.
|
||||
*/
|
||||
public final class ArtifactInfoContext {
|
||||
|
||||
private final Set<String> entityNameSet = new HashSet<String>();
|
||||
private final Set<String> serviceNameSet = new HashSet<String>();
|
||||
private final Set<String> simpleMethodNameSet = new HashSet<String>();
|
||||
|
||||
/**
|
||||
* Adds an entity name to this context.
|
||||
* @param name The entity name to add to this context
|
||||
*/
|
||||
public void addEntityName(String name) {
|
||||
if (name != null) {
|
||||
this.entityNameSet.add(name);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a service name to this context.
|
||||
* @param name The service name to add to this context
|
||||
*/
|
||||
public void addServiceName(String name) {
|
||||
if (name != null) {
|
||||
this.serviceNameSet.add(name);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a visited <code>SimpleMethod</code> to this context.
|
||||
* @param method the <code>SimpleMethod</code> to add to this context
|
||||
*/
|
||||
public void addSimpleMethod(SimpleMethod method) {
|
||||
this.simpleMethodNameSet.add(method.getLocationAndName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the entity names in this context.
|
||||
* @return The entity names in this context
|
||||
*/
|
||||
public Set<String> getEntityNames() {
|
||||
return this.entityNameSet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the service names in this context.
|
||||
* @return The service names in this context
|
||||
*/
|
||||
public Set<String> getServiceNames() {
|
||||
return this.serviceNameSet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <code>true</code> if this context has visited <code>method</code>.
|
||||
* @param method The <code>SimpleMethod</code> to test
|
||||
* @return <code>true</code> if this context has visited <code>method</code>
|
||||
*/
|
||||
public boolean hasVisited(SimpleMethod method) {
|
||||
return simpleMethodNameSet.contains(method.getLocationAndName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method;
|
||||
|
||||
import org.ofbiz.base.util.ObjectType;
|
||||
import org.ofbiz.base.util.UtilGenerics;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <field> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cfield%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class FieldObject<T> extends MethodObject<T> {
|
||||
|
||||
private final FlexibleMapAccessor<Object> fieldFma;
|
||||
private final String type;
|
||||
|
||||
public FieldObject(Element element, SimpleMethod simpleMethod) {
|
||||
super(element, simpleMethod);
|
||||
this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field"));
|
||||
String typeAttribute = element.getAttribute("type");
|
||||
if (typeAttribute.isEmpty()) {
|
||||
this.type = "java.lang.String";
|
||||
} else {
|
||||
this.type = typeAttribute;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public T getObject(MethodContext methodContext) {
|
||||
return (T) this.fieldFma.get(methodContext.getEnvMap());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<T> getTypeClass(MethodContext methodContext) throws ClassNotFoundException {
|
||||
return UtilGenerics.cast(ObjectType.loadClass(this.type, methodContext.getLoader()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTypeName() {
|
||||
return this.type;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method;
|
||||
|
||||
import org.ofbiz.base.util.UtilProperties;
|
||||
import org.ofbiz.base.util.UtilXml;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.minilang.MiniLangElement;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <fail-message> and <fail-property> elements.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{<failmessage>}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class MessageElement extends MiniLangElement {
|
||||
|
||||
public static MessageElement fromParentElement(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
Element childElement = UtilXml.firstChildElement(element, "fail-message");
|
||||
if (childElement != null) {
|
||||
return new MessageElement(childElement, simpleMethod);
|
||||
} else {
|
||||
childElement = UtilXml.firstChildElement(element, "fail-property");
|
||||
if (childElement != null) {
|
||||
return new MessageElement(childElement, simpleMethod);
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private final FlexibleStringExpander messageFse;
|
||||
private final String propertykey;
|
||||
private final String propertyResource;
|
||||
|
||||
public MessageElement(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if ("fail-message".equals(element.getTagName())) {
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "message");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "message");
|
||||
MiniLangValidate.constantPlusExpressionAttributes(simpleMethod, element, "message");
|
||||
}
|
||||
this.messageFse = FlexibleStringExpander.getInstance(element.getAttribute("message"));
|
||||
this.propertykey = null;
|
||||
this.propertyResource = null;
|
||||
} else {
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "property", "resource");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "property", "resource");
|
||||
MiniLangValidate.constantAttributes(simpleMethod, element, "property", "resource");
|
||||
}
|
||||
this.messageFse = null;
|
||||
this.propertykey = element.getAttribute("property");
|
||||
this.propertyResource = element.getAttribute("resource");
|
||||
}
|
||||
}
|
||||
|
||||
public String getMessage(MethodContext methodContext) {
|
||||
if (messageFse != null) {
|
||||
return messageFse.expandString(methodContext.getEnvMap());
|
||||
} else {
|
||||
return UtilProperties.getMessage(propertyResource, propertykey, methodContext.getEnvMap(), methodContext.getLocale());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (this.messageFse != null) {
|
||||
sb.append("<fail-message message=\"").append(this.messageFse).append("\" />");
|
||||
}
|
||||
if (this.propertykey != null) {
|
||||
sb.append("<fail-property property=\"").append(this.propertykey).append(" resource=\"").append(this.propertyResource).append("\" />");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,306 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.UtilHttp;
|
||||
import org.ofbiz.base.util.UtilMisc;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.entity.Delegator;
|
||||
import org.ofbiz.entity.GenericValue;
|
||||
import org.ofbiz.security.Security;
|
||||
import org.ofbiz.service.DispatchContext;
|
||||
import org.ofbiz.service.LocalDispatcher;
|
||||
|
||||
/**
|
||||
* A container for the Mini-language script engine state.
|
||||
*/
|
||||
public final class MethodContext {
|
||||
|
||||
public static final int EVENT = 1;
|
||||
public static final int SERVICE = 2;
|
||||
|
||||
private Delegator delegator;
|
||||
private LocalDispatcher dispatcher;
|
||||
private Map<String, Object> env = new HashMap<String, Object>();
|
||||
private ClassLoader loader;
|
||||
private Locale locale;
|
||||
private int methodType;
|
||||
private Map<String, Object> parameters;
|
||||
private HttpServletRequest request = null;
|
||||
private HttpServletResponse response = null;
|
||||
private Map<String, Object> results = new HashMap<String, Object>();
|
||||
private Security security;
|
||||
private TimeZone timeZone;
|
||||
private int traceCount = 0;
|
||||
private int traceLogLevel = Debug.INFO;
|
||||
private GenericValue userLogin;
|
||||
|
||||
public MethodContext(DispatchContext ctx, Map<String, ? extends Object> context, ClassLoader loader) {
|
||||
this.methodType = MethodContext.SERVICE;
|
||||
this.parameters = UtilMisc.makeMapWritable(context);
|
||||
this.loader = loader;
|
||||
this.locale = (Locale) context.get("locale");
|
||||
this.timeZone = (TimeZone) context.get("timeZone");
|
||||
this.dispatcher = ctx.getDispatcher();
|
||||
this.delegator = ctx.getDelegator();
|
||||
this.security = ctx.getSecurity();
|
||||
this.userLogin = (GenericValue) context.get("userLogin");
|
||||
if (this.loader == null) {
|
||||
try {
|
||||
this.loader = Thread.currentThread().getContextClassLoader();
|
||||
} catch (SecurityException e) {
|
||||
this.loader = this.getClass().getClassLoader();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public MethodContext(HttpServletRequest request, HttpServletResponse response, ClassLoader loader) {
|
||||
this.methodType = MethodContext.EVENT;
|
||||
this.parameters = UtilHttp.getCombinedMap(request);
|
||||
this.loader = loader;
|
||||
this.request = request;
|
||||
this.response = response;
|
||||
this.locale = UtilHttp.getLocale(request);
|
||||
this.timeZone = UtilHttp.getTimeZone(request);
|
||||
this.dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");
|
||||
this.delegator = (Delegator) request.getAttribute("delegator");
|
||||
this.security = (Security) request.getAttribute("security");
|
||||
this.userLogin = (GenericValue) request.getSession().getAttribute("userLogin");
|
||||
if (this.loader == null) {
|
||||
try {
|
||||
this.loader = Thread.currentThread().getContextClassLoader();
|
||||
} catch (SecurityException e) {
|
||||
this.loader = this.getClass().getClassLoader();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This is a very simple constructor which assumes the needed objects (dispatcher, delegator, security, request, response, etc) are in the context. Will result in calling method as a
|
||||
* service or event, as specified.
|
||||
*/
|
||||
public MethodContext(Map<String, ? extends Object> context, ClassLoader loader, int methodType) {
|
||||
this.methodType = methodType;
|
||||
this.parameters = UtilMisc.makeMapWritable(context);
|
||||
this.loader = loader;
|
||||
this.locale = (Locale) context.get("locale");
|
||||
this.timeZone = (TimeZone) context.get("timeZone");
|
||||
this.dispatcher = (LocalDispatcher) context.get("dispatcher");
|
||||
this.delegator = (Delegator) context.get("delegator");
|
||||
this.security = (Security) context.get("security");
|
||||
this.userLogin = (GenericValue) context.get("userLogin");
|
||||
if (methodType == MethodContext.EVENT) {
|
||||
this.request = (HttpServletRequest) context.get("request");
|
||||
this.response = (HttpServletResponse) context.get("response");
|
||||
if (this.locale == null)
|
||||
this.locale = UtilHttp.getLocale(request);
|
||||
if (this.timeZone == null)
|
||||
this.timeZone = UtilHttp.getTimeZone(request);
|
||||
// make sure the delegator and other objects are in place, getting from
|
||||
// request if necessary; assumes this came through the ControlServlet
|
||||
// or something similar
|
||||
if (this.request != null) {
|
||||
if (this.dispatcher == null)
|
||||
this.dispatcher = (LocalDispatcher) this.request.getAttribute("dispatcher");
|
||||
if (this.delegator == null)
|
||||
this.delegator = (Delegator) this.request.getAttribute("delegator");
|
||||
if (this.security == null)
|
||||
this.security = (Security) this.request.getAttribute("security");
|
||||
if (this.userLogin == null)
|
||||
this.userLogin = (GenericValue) this.request.getSession().getAttribute("userLogin");
|
||||
}
|
||||
}
|
||||
if (this.loader == null) {
|
||||
try {
|
||||
this.loader = Thread.currentThread().getContextClassLoader();
|
||||
} catch (SecurityException e) {
|
||||
this.loader = this.getClass().getClassLoader();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Delegator getDelegator() {
|
||||
return this.delegator;
|
||||
}
|
||||
|
||||
public LocalDispatcher getDispatcher() {
|
||||
return this.dispatcher;
|
||||
}
|
||||
|
||||
public <T> T getEnv(FlexibleMapAccessor<T> fma) {
|
||||
return fma.get(this.env);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the named value from the environment. Supports the "." (dot) syntax to access Map members and the "[]" (bracket) syntax to access List entries. This value is expanded, supporting the
|
||||
* insertion of other environment values using the "${}" notation.
|
||||
*
|
||||
* @param key
|
||||
* The name of the environment value to get. Can contain "." and "[]" syntax elements as described above.
|
||||
* @return The environment value if found, otherwise null.
|
||||
*/
|
||||
public <T> T getEnv(String key) {
|
||||
String ekey = FlexibleStringExpander.expandString(key, this.env);
|
||||
FlexibleMapAccessor<T> fma = FlexibleMapAccessor.getInstance(ekey);
|
||||
return this.getEnv(fma);
|
||||
}
|
||||
|
||||
public Map<String, Object> getEnvMap() {
|
||||
return this.env;
|
||||
}
|
||||
|
||||
public ClassLoader getLoader() {
|
||||
return this.loader;
|
||||
}
|
||||
|
||||
public Locale getLocale() {
|
||||
return this.locale;
|
||||
}
|
||||
|
||||
public int getMethodType() {
|
||||
return this.methodType;
|
||||
}
|
||||
|
||||
public Object getParameter(String key) {
|
||||
return this.parameters.get(key);
|
||||
}
|
||||
|
||||
public Map<String, Object> getParameters() {
|
||||
return this.parameters;
|
||||
}
|
||||
|
||||
public HttpServletRequest getRequest() {
|
||||
return this.request;
|
||||
}
|
||||
|
||||
public HttpServletResponse getResponse() {
|
||||
return this.response;
|
||||
}
|
||||
|
||||
public Object getResult(String key) {
|
||||
return this.results.get(key);
|
||||
}
|
||||
|
||||
public Map<String, Object> getResults() {
|
||||
return this.results;
|
||||
}
|
||||
|
||||
public Security getSecurity() {
|
||||
return this.security;
|
||||
}
|
||||
|
||||
public TimeZone getTimeZone() {
|
||||
return this.timeZone;
|
||||
}
|
||||
|
||||
public int getTraceLogLevel() {
|
||||
return this.traceLogLevel;
|
||||
}
|
||||
|
||||
public GenericValue getUserLogin() {
|
||||
return this.userLogin;
|
||||
}
|
||||
|
||||
public boolean isTraceOn() {
|
||||
return this.traceCount > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls putEnv for each entry in the Map, thus allowing for the additional flexibility in naming supported in that method.
|
||||
*/
|
||||
public void putAllEnv(Map<String, ? extends Object> values) {
|
||||
for (Map.Entry<String, ? extends Object> entry : values.entrySet()) {
|
||||
this.putEnv(entry.getKey(), entry.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
public <T> void putEnv(FlexibleMapAccessor<T> fma, T value) {
|
||||
fma.put(this.env, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts the named value in the environment. Supports the "." (dot) syntax to access Map members and the "[]" (bracket) syntax to access List entries. If the brackets for a list are empty the value
|
||||
* will be appended to end of the list, otherwise the value will be set in the position of the number in the brackets. If a "+" (plus sign) is included inside the square brackets before the index
|
||||
* number the value will inserted/added at that index instead of set at that index. This value is expanded, supporting the insertion of other environment values using the "${}" notation.
|
||||
*
|
||||
* @param key
|
||||
* The name of the environment value to get. Can contain "." syntax elements as described above.
|
||||
* @param value
|
||||
* The value to set in the named environment location.
|
||||
*/
|
||||
public <T> void putEnv(String key, T value) {
|
||||
String ekey = FlexibleStringExpander.expandString(key, this.env);
|
||||
FlexibleMapAccessor<T> fma = FlexibleMapAccessor.getInstance(ekey);
|
||||
this.putEnv(fma, value);
|
||||
}
|
||||
|
||||
public void putParameter(String key, Object value) {
|
||||
this.parameters.put(key, value);
|
||||
}
|
||||
|
||||
public void putResult(String key, Object value) {
|
||||
this.results.put(key, value);
|
||||
}
|
||||
|
||||
public <T> T removeEnv(FlexibleMapAccessor<T> fma) {
|
||||
return fma.remove(this.env);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the named value from the environment. Supports the "." (dot) syntax to access Map members and the "[]" (bracket) syntax to access List entries. This value is expanded, supporting the
|
||||
* insertion of other environment values using the "${}" notation.
|
||||
*
|
||||
* @param key
|
||||
* The name of the environment value to get. Can contain "." syntax elements as described above.
|
||||
*/
|
||||
public <T> T removeEnv(String key) {
|
||||
String ekey = FlexibleStringExpander.expandString(key, this.env);
|
||||
FlexibleMapAccessor<T> fma = FlexibleMapAccessor.getInstance(ekey);
|
||||
return this.removeEnv(fma);
|
||||
}
|
||||
|
||||
public void setTraceOff() {
|
||||
if (this.traceCount > 0) {
|
||||
this.traceCount--;
|
||||
}
|
||||
}
|
||||
|
||||
public void setTraceOn(int logLevel) {
|
||||
if (this.traceCount == 0) {
|
||||
// Outermost trace element sets the logging level
|
||||
this.traceLogLevel = logLevel;
|
||||
}
|
||||
this.traceCount++;
|
||||
}
|
||||
|
||||
public void setUserLogin(GenericValue userLogin, String userLoginEnvName) {
|
||||
this.userLogin = userLogin;
|
||||
this.putEnv(userLoginEnvName, userLogin);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method;
|
||||
|
||||
import org.ofbiz.minilang.MiniLangElement;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* A single Object value to be used as a parameter or whatever
|
||||
*/
|
||||
public abstract class MethodObject<T> extends MiniLangElement {
|
||||
|
||||
public MethodObject(Element element, SimpleMethod simpleMethod) {
|
||||
super(element, simpleMethod);
|
||||
}
|
||||
|
||||
/** Get the Object value */
|
||||
public abstract T getObject(MethodContext methodContext);
|
||||
|
||||
/** Get the Class for the type of the object */
|
||||
public abstract Class<T> getTypeClass(MethodContext methodContext) throws ClassNotFoundException;
|
||||
|
||||
/** Get the name for the type of the object */
|
||||
public abstract String getTypeName();
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.minilang.MiniLangElement;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* An abstract class for Mini-language element models.
|
||||
*/
|
||||
public abstract class MethodOperation extends MiniLangElement {
|
||||
|
||||
|
||||
protected MethodOperation(Element element, SimpleMethod simpleMethod) {
|
||||
super(element, simpleMethod);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes the operation.
|
||||
* Returns <code>true</code> if script execution should continue, or
|
||||
* <code>false</code> if script execution should stop.
|
||||
*
|
||||
* @throws MiniLangException */
|
||||
public abstract boolean exec(MethodContext methodContext) throws MiniLangException;
|
||||
|
||||
/** Create a string representation of the operation, using the current context.
|
||||
* @deprecated No replacement.
|
||||
*/
|
||||
@Deprecated
|
||||
public String expandedString(MethodContext methodContext) {
|
||||
return FlexibleStringExpander.expandString(toString(), methodContext.getEnvMap());
|
||||
}
|
||||
|
||||
/** Create a string representation of the operation - similar to the original XML.
|
||||
* @deprecated Use {@link #toString()}.
|
||||
*/
|
||||
@Deprecated
|
||||
public String rawString() {
|
||||
return toString();
|
||||
}
|
||||
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Target(ElementType.TYPE)
|
||||
public @interface DeprecatedOperation {
|
||||
String value();
|
||||
}
|
||||
|
||||
public interface Factory<M extends MethodOperation> {
|
||||
|
||||
M createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException;
|
||||
|
||||
String getName();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method;
|
||||
|
||||
import org.ofbiz.base.util.UtilXml;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Specifies a <code>java.lang.String</code> to be passed as an argument to a method call.
|
||||
*/
|
||||
public final class StringObject extends MethodObject<String> {
|
||||
|
||||
private final FlexibleStringExpander cdataValueFse;
|
||||
private final FlexibleStringExpander valueFse;
|
||||
|
||||
public StringObject(Element element, SimpleMethod simpleMethod) {
|
||||
super(element, simpleMethod);
|
||||
this.cdataValueFse = FlexibleStringExpander.getInstance(UtilXml.elementValue(element));
|
||||
this.valueFse = FlexibleStringExpander.getInstance(element.getAttribute("value"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getObject(MethodContext methodContext) {
|
||||
String value = this.valueFse.expandString(methodContext.getEnvMap());
|
||||
String cdataValue = this.cdataValueFse.expandString(methodContext.getEnvMap());
|
||||
return value.concat(cdataValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<String> getTypeClass(MethodContext methodContext) throws ClassNotFoundException {
|
||||
return java.lang.String.class;
|
||||
}
|
||||
|
||||
/** Get the name for the type of the object */
|
||||
@Override
|
||||
public String getTypeName() {
|
||||
return "java.lang.String";
|
||||
}
|
||||
}
|
||||
+127
@@ -0,0 +1,127 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
|
||||
package org.ofbiz.minilang.method.callops;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.util.ObjectType;
|
||||
import org.ofbiz.base.util.UtilXml;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangUtil;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.FieldObject;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodObject;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.ofbiz.minilang.method.StringObject;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <call-class-method> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Ccallclassmethod%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class CallClassMethod extends MethodOperation {
|
||||
|
||||
public static final String module = CallClassMethod.class.getName();
|
||||
|
||||
private final String className;
|
||||
private final Class<?> methodClass;
|
||||
private final String methodName;
|
||||
private final List<MethodObject<?>> parameters;
|
||||
private final FlexibleMapAccessor<Object> retFieldFma;
|
||||
|
||||
public CallClassMethod(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.handleError("<call-class-method> element is deprecated (use <script>)", simpleMethod, element);
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "class-name", "method-name", "ret-field");
|
||||
MiniLangValidate.constantAttributes(simpleMethod, element, "class-name", "method-name");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "class-name", "method-name");
|
||||
MiniLangValidate.childElements(simpleMethod, element, "string", "field");
|
||||
}
|
||||
this.className = element.getAttribute("class-name");
|
||||
Class<?> methodClass = null;
|
||||
try {
|
||||
methodClass = ObjectType.loadClass(this.className);
|
||||
} catch (ClassNotFoundException e) {
|
||||
MiniLangValidate.handleError("Class not found with name " + this.className, simpleMethod, element);
|
||||
}
|
||||
this.methodClass = methodClass;
|
||||
this.methodName = element.getAttribute("method-name");
|
||||
this.retFieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("ret-field"));
|
||||
List<? extends Element> parameterElements = UtilXml.childElementList(element);
|
||||
if (parameterElements.size() > 0) {
|
||||
ArrayList<MethodObject<?>> parameterList = new ArrayList<MethodObject<?>>(parameterElements.size());
|
||||
for (Element parameterElement : parameterElements) {
|
||||
if ("string".equals(parameterElement.getNodeName())) {
|
||||
parameterList.add(new StringObject(parameterElement, simpleMethod));
|
||||
} else if ("field".equals(parameterElement.getNodeName())) {
|
||||
parameterList.add(new FieldObject<Object>(parameterElement, simpleMethod));
|
||||
}
|
||||
}
|
||||
parameterList.trimToSize();
|
||||
this.parameters = Collections.unmodifiableList(parameterList);
|
||||
} else {
|
||||
this.parameters = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
MiniLangUtil.callMethod(this, methodContext, parameters, methodClass, null, methodName, retFieldFma);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<call-class-method ");
|
||||
if (!this.className.isEmpty()) {
|
||||
sb.append("class-name=\"").append(this.className).append("\" ");
|
||||
}
|
||||
if (!this.methodName.isEmpty()) {
|
||||
sb.append("method-name=\"").append(this.methodName).append("\" ");
|
||||
}
|
||||
if (!this.retFieldFma.isEmpty()) {
|
||||
sb.append("ret-field=\"").append(this.retFieldFma).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <call-class-method> element.
|
||||
*/
|
||||
public static final class CallClassMethodFactory implements Factory<CallClassMethod> {
|
||||
@Override
|
||||
public CallClassMethod createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new CallClassMethod(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "call-class-method";
|
||||
}
|
||||
}
|
||||
}
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.callops;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.util.UtilXml;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangUtil;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.FieldObject;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodObject;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.ofbiz.minilang.method.StringObject;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <call-object-method> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Ccallobjectmethod%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class CallObjectMethod extends MethodOperation {
|
||||
|
||||
public static final String module = CallClassMethod.class.getName();
|
||||
|
||||
private final String methodName;
|
||||
private final FlexibleMapAccessor<Object> objFieldFma;
|
||||
private final List<MethodObject<?>> parameters;
|
||||
private final FlexibleMapAccessor<Object> retFieldFma;
|
||||
|
||||
public CallObjectMethod(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.handleError("<call-object-method> element is deprecated (use <script>)", simpleMethod, element);
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "obj-field", "method-name", "ret-field");
|
||||
MiniLangValidate.constantAttributes(simpleMethod, element, "method-name");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "obj-field", "method-name");
|
||||
MiniLangValidate.childElements(simpleMethod, element, "string", "field");
|
||||
}
|
||||
this.methodName = element.getAttribute("method-name");
|
||||
this.objFieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("obj-field"));
|
||||
this.retFieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("ret-field"));
|
||||
List<? extends Element> parameterElements = UtilXml.childElementList(element);
|
||||
if (parameterElements.size() > 0) {
|
||||
ArrayList<MethodObject<?>> parameterList = new ArrayList<MethodObject<?>>(parameterElements.size());
|
||||
for (Element parameterElement : parameterElements) {
|
||||
if ("string".equals(parameterElement.getNodeName())) {
|
||||
parameterList.add(new StringObject(parameterElement, simpleMethod));
|
||||
} else if ("field".equals(parameterElement.getNodeName())) {
|
||||
parameterList.add(new FieldObject<Object>(parameterElement, simpleMethod));
|
||||
}
|
||||
}
|
||||
parameterList.trimToSize();
|
||||
this.parameters = Collections.unmodifiableList(parameterList);
|
||||
} else {
|
||||
this.parameters = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
Object methodObject = this.objFieldFma.get(methodContext.getEnvMap());
|
||||
if (methodObject == null) {
|
||||
throw new MiniLangRuntimeException("Object not found: " + this.objFieldFma, this);
|
||||
}
|
||||
MiniLangUtil.callMethod(this, methodContext, parameters, methodObject.getClass(), methodObject, methodName, retFieldFma);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<call-class-method ");
|
||||
if (!this.objFieldFma.isEmpty()) {
|
||||
sb.append("obj-field=\"").append(this.objFieldFma).append("\" ");
|
||||
}
|
||||
if (!this.methodName.isEmpty()) {
|
||||
sb.append("method-name=\"").append(this.methodName).append("\" ");
|
||||
}
|
||||
if (!this.retFieldFma.isEmpty()) {
|
||||
sb.append("ret-field=\"").append(this.retFieldFma).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <call-object-method> element.
|
||||
*/
|
||||
public static final class CallObjectMethodFactory implements Factory<CallObjectMethod> {
|
||||
@Override
|
||||
public CallObjectMethod createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new CallObjectMethod(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "call-object-method";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.callops;
|
||||
|
||||
import org.ofbiz.base.util.ScriptUtil;
|
||||
import org.ofbiz.base.util.Scriptlet;
|
||||
import org.ofbiz.base.util.StringUtil;
|
||||
import org.ofbiz.base.util.UtilXml;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangUtil;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <script> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cscript%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class CallScript extends MethodOperation {
|
||||
|
||||
public static final String module = CallScript.class.getName();
|
||||
|
||||
// This method is needed only during the v1 to v2 transition
|
||||
private static boolean autoCorrect(Element element) {
|
||||
String errorListAttr = element.getAttribute("error-list-name");
|
||||
if (errorListAttr.length() > 0) {
|
||||
element.removeAttribute("error-list-name");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* Developers - the location attribute is a constant for security reasons.
|
||||
* Script invocations should always be hard-coded.
|
||||
*/
|
||||
private final String location;
|
||||
private final String method;
|
||||
private final Scriptlet scriptlet;
|
||||
|
||||
public CallScript(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "location", "script");
|
||||
MiniLangValidate.requireAnyAttribute(simpleMethod, element, "location", "script");
|
||||
MiniLangValidate.constantAttributes(simpleMethod, element, "location");
|
||||
MiniLangValidate.scriptAttributes(simpleMethod, element, "script");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
boolean elementModified = autoCorrect(element);
|
||||
if (elementModified && MiniLangUtil.autoCorrectOn()) {
|
||||
MiniLangUtil.flagDocumentAsCorrected(element);
|
||||
}
|
||||
String scriptLocation = element.getAttribute("location");
|
||||
if (scriptLocation.isEmpty()) {
|
||||
this.location = null;
|
||||
this.method = null;
|
||||
} else {
|
||||
int pos = scriptLocation.lastIndexOf("#");
|
||||
if (pos == -1) {
|
||||
this.location = scriptLocation;
|
||||
this.method = null;
|
||||
} else {
|
||||
this.location = scriptLocation.substring(0, pos);
|
||||
this.method = scriptLocation.substring(pos + 1);
|
||||
}
|
||||
}
|
||||
String inlineScript = element.getAttribute("script");
|
||||
if (inlineScript.isEmpty()) {
|
||||
inlineScript = UtilXml.elementValue(element);
|
||||
}
|
||||
if (inlineScript != null && MiniLangUtil.containsScript(inlineScript)) {
|
||||
this.scriptlet = new Scriptlet(StringUtil.convertOperatorSubstitutions(inlineScript));
|
||||
} else {
|
||||
this.scriptlet = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
if (this.location != null) {
|
||||
if (location.endsWith(".xml")) {
|
||||
SimpleMethod.runSimpleMethod(location, method, methodContext);
|
||||
} else {
|
||||
ScriptUtil.executeScript(this.location, this.method, methodContext.getEnvMap());
|
||||
}
|
||||
}
|
||||
if (this.scriptlet != null) {
|
||||
try {
|
||||
this.scriptlet.executeScript(methodContext.getEnvMap());
|
||||
} catch (Exception e) {
|
||||
throw new MiniLangRuntimeException(e.getMessage(), this);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<script ");
|
||||
if (this.location != null) {
|
||||
sb.append("location=\"").append(this.location);
|
||||
if (this.method != null) {
|
||||
sb.append("#").append(this.method);
|
||||
}
|
||||
sb.append("\" ");
|
||||
}
|
||||
if (this.scriptlet != null) {
|
||||
sb.append("script=\"").append(this.scriptlet).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <script> element.
|
||||
*/
|
||||
public static final class CallScriptFactory implements Factory<CallScript> {
|
||||
@Override
|
||||
public CallScript createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new CallScript(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "script";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,463 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.callops;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.UtilGenerics;
|
||||
import org.ofbiz.base.util.UtilMisc;
|
||||
import org.ofbiz.base.util.UtilProperties;
|
||||
import org.ofbiz.base.util.UtilValidate;
|
||||
import org.ofbiz.base.util.UtilXml;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.collections.FlexibleServletAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.entity.GenericValue;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.ofbiz.service.GenericServiceException;
|
||||
import org.ofbiz.service.ModelService;
|
||||
import org.ofbiz.service.ServiceUtil;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <call-service> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Ccallservice%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class CallService extends MethodOperation {
|
||||
|
||||
public static final String module = CallService.class.getName();
|
||||
public static final String resource = "MiniLangErrorUiLabels";
|
||||
|
||||
private final boolean breakOnError;
|
||||
private final FlexibleMessage defaultMessage;
|
||||
private final String errorCode;
|
||||
private final FlexibleMessage errorPrefix;
|
||||
private final FlexibleMessage errorSuffix;
|
||||
private final boolean includeUserLogin;
|
||||
private final FlexibleMapAccessor<Map<String, Object>> inMapFma;
|
||||
private final FlexibleMessage messagePrefix;
|
||||
private final FlexibleMessage messageSuffix;
|
||||
private final boolean requireNewTransaction;
|
||||
private final List<String> resultsToMapList;
|
||||
private final List<ResultToField> resultToFieldList;
|
||||
private final List<ResultToRequest> resultToRequestList;
|
||||
private final List<ResultToResult> resultToResultList;
|
||||
private final List<ResultToSession> resultToSessionList;
|
||||
private final FlexibleStringExpander serviceNameFse;
|
||||
private final String successCode;
|
||||
private final FlexibleMessage successPrefix;
|
||||
private final FlexibleMessage successSuffix;
|
||||
private final int transactionTimeout;
|
||||
|
||||
public CallService(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "service-name", "in-map-name", "include-user-login", "break-on-error", "error-code", "require-new-transaction", "transaction-timeout", "success-code");
|
||||
MiniLangValidate.constantAttributes(simpleMethod, element, "include-user-login", "break-on-error", "error-code", "require-new-transaction", "transaction-timeout", "success-code");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "service-name", "in-map-name");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "service-name");
|
||||
MiniLangValidate.childElements(simpleMethod, element, "error-prefix", "error-suffix", "success-prefix", "success-suffix", "message-prefix", "message-suffix", "default-message", "results-to-map", "result-to-field", "result-to-request", "result-to-session", "result-to-result");
|
||||
}
|
||||
serviceNameFse = FlexibleStringExpander.getInstance(element.getAttribute("service-name"));
|
||||
inMapFma = FlexibleMapAccessor.getInstance(element.getAttribute("in-map-name"));
|
||||
includeUserLogin = !"false".equals(element.getAttribute("include-user-login"));
|
||||
breakOnError = !"false".equals(element.getAttribute("break-on-error"));
|
||||
errorCode = element.getAttribute("error-code");
|
||||
requireNewTransaction = "true".equals(element.getAttribute("require-new-transaction"));
|
||||
String timeoutStr = UtilXml.checkEmpty(element.getAttribute("transaction-timeout"));
|
||||
int timeout = -1;
|
||||
if (!timeoutStr.isEmpty()) {
|
||||
try {
|
||||
timeout = Integer.parseInt(timeoutStr);
|
||||
} catch (NumberFormatException e) {
|
||||
MiniLangValidate.handleError("Exception thrown while parsing transaction-timeout attribute: " + e.getMessage(), simpleMethod, element);
|
||||
timeout = 0;
|
||||
}
|
||||
}
|
||||
transactionTimeout = timeout;
|
||||
successCode = element.getAttribute("success-code");
|
||||
errorPrefix = new FlexibleMessage(UtilXml.firstChildElement(element, "error-prefix"), "service.error.prefix");
|
||||
errorSuffix = new FlexibleMessage(UtilXml.firstChildElement(element, "error-suffix"), "service.error.suffix");
|
||||
successPrefix = new FlexibleMessage(UtilXml.firstChildElement(element, "success-prefix"), "service.success.prefix");
|
||||
successSuffix = new FlexibleMessage(UtilXml.firstChildElement(element, "success-suffix"), "service.success.suffix");
|
||||
messagePrefix = new FlexibleMessage(UtilXml.firstChildElement(element, "message-prefix"), "service.message.prefix");
|
||||
messageSuffix = new FlexibleMessage(UtilXml.firstChildElement(element, "message-suffix"), "service.message.suffix");
|
||||
defaultMessage = new FlexibleMessage(UtilXml.firstChildElement(element, "default-message"), null);// "service.default.message"
|
||||
List<? extends Element> resultsToMapElements = UtilXml.childElementList(element, "results-to-map");
|
||||
if (UtilValidate.isNotEmpty(resultsToMapElements)) {
|
||||
List<String> resultsToMapList = new ArrayList<String>(resultsToMapElements.size());
|
||||
for (Element resultsToMapElement : resultsToMapElements) {
|
||||
resultsToMapList.add(resultsToMapElement.getAttribute("map-name"));
|
||||
}
|
||||
this.resultsToMapList = Collections.unmodifiableList(resultsToMapList);
|
||||
} else {
|
||||
this.resultsToMapList = null;
|
||||
}
|
||||
List<? extends Element> resultToFieldElements = UtilXml.childElementList(element, "result-to-field");
|
||||
if (UtilValidate.isNotEmpty(resultToFieldElements)) {
|
||||
List<ResultToField> resultToFieldList = new ArrayList<ResultToField>(resultToFieldElements.size());
|
||||
for (Element resultToFieldElement : resultToFieldElements) {
|
||||
resultToFieldList.add(new ResultToField(resultToFieldElement));
|
||||
}
|
||||
this.resultToFieldList = Collections.unmodifiableList(resultToFieldList);
|
||||
} else {
|
||||
this.resultToFieldList = null;
|
||||
}
|
||||
List<? extends Element> resultToRequestElements = UtilXml.childElementList(element, "result-to-request");
|
||||
if (UtilValidate.isNotEmpty(resultToRequestElements)) {
|
||||
List<ResultToRequest> resultToRequestList = new ArrayList<ResultToRequest>(resultToRequestElements.size());
|
||||
for (Element resultToRequestElement : resultToRequestElements) {
|
||||
resultToRequestList.add(new ResultToRequest(resultToRequestElement));
|
||||
}
|
||||
this.resultToRequestList = Collections.unmodifiableList(resultToRequestList);
|
||||
} else {
|
||||
this.resultToRequestList = null;
|
||||
}
|
||||
List<? extends Element> resultToSessionElements = UtilXml.childElementList(element, "result-to-session");
|
||||
if (UtilValidate.isNotEmpty(resultToSessionElements)) {
|
||||
List<ResultToSession> resultToSessionList = new ArrayList<ResultToSession>(resultToSessionElements.size());
|
||||
for (Element resultToSessionElement : resultToSessionElements) {
|
||||
resultToSessionList.add(new ResultToSession(resultToSessionElement));
|
||||
}
|
||||
this.resultToSessionList = Collections.unmodifiableList(resultToSessionList);
|
||||
} else {
|
||||
this.resultToSessionList = null;
|
||||
}
|
||||
List<? extends Element> resultToResultElements = UtilXml.childElementList(element, "result-to-result");
|
||||
if (UtilValidate.isNotEmpty(resultToResultElements)) {
|
||||
List<ResultToResult> resultToResultList = new ArrayList<ResultToResult>(resultToResultElements.size());
|
||||
for (Element resultToResultElement : resultToResultElements) {
|
||||
resultToResultList.add(new ResultToResult(resultToResultElement));
|
||||
}
|
||||
this.resultToResultList = Collections.unmodifiableList(resultToResultList);
|
||||
} else {
|
||||
this.resultToResultList = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "Begin call-service.");
|
||||
}
|
||||
String serviceName = serviceNameFse.expandString(methodContext.getEnvMap());
|
||||
String errorCode = this.errorCode;
|
||||
if (errorCode.isEmpty()) {
|
||||
errorCode = simpleMethod.getDefaultErrorCode();
|
||||
}
|
||||
String successCode = this.successCode;
|
||||
if (successCode.isEmpty()) {
|
||||
successCode = simpleMethod.getDefaultSuccessCode();
|
||||
}
|
||||
Map<String, Object> inMap = inMapFma.get(methodContext.getEnvMap());
|
||||
if (inMap == null) {
|
||||
inMap = new HashMap<String, Object>();
|
||||
}
|
||||
// before invoking the service, clear messages
|
||||
if (methodContext.getMethodType() == MethodContext.EVENT) {
|
||||
methodContext.removeEnv(simpleMethod.getEventErrorMessageName());
|
||||
methodContext.removeEnv(simpleMethod.getEventEventMessageName());
|
||||
methodContext.removeEnv(simpleMethod.getEventResponseCodeName());
|
||||
} else {
|
||||
methodContext.removeEnv(simpleMethod.getServiceErrorMessageName());
|
||||
methodContext.removeEnv(simpleMethod.getServiceSuccessMessageName());
|
||||
methodContext.removeEnv(simpleMethod.getServiceResponseMessageName());
|
||||
}
|
||||
// add UserLogin to context if expected
|
||||
if (includeUserLogin) {
|
||||
GenericValue userLogin = methodContext.getUserLogin();
|
||||
if (userLogin != null && inMap.get("userLogin") == null) {
|
||||
inMap.put("userLogin", userLogin);
|
||||
}
|
||||
}
|
||||
// always add Locale to context unless null
|
||||
Locale locale = methodContext.getLocale();
|
||||
if (locale != null) {
|
||||
inMap.put("locale", locale);
|
||||
}
|
||||
// invoke the service
|
||||
Map<String, Object> result = null;
|
||||
try {
|
||||
ModelService modelService = methodContext.getDispatcher().getDispatchContext().getModelService(serviceName);
|
||||
int timeout = modelService.transactionTimeout;
|
||||
if (this.transactionTimeout >= 0) {
|
||||
timeout = this.transactionTimeout;
|
||||
}
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "Invoking service \"" + serviceName + "\", require-new-transaction = " + requireNewTransaction + ", transaction-timeout = " + timeout + ", IN attributes:", inMap.toString());
|
||||
}
|
||||
result = methodContext.getDispatcher().runSync(serviceName, inMap, timeout, requireNewTransaction);
|
||||
} catch (GenericServiceException e) {
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "Service engine threw an exception: " + e.getMessage());
|
||||
}
|
||||
String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [problem invoking the [" + serviceName + "] service with the map named [" + inMapFma + "] containing [" + inMap + "]: " + e.getMessage() + "]";
|
||||
Debug.logError(e, errMsg, module);
|
||||
if (breakOnError) {
|
||||
if (methodContext.getMethodType() == MethodContext.EVENT) {
|
||||
methodContext.putEnv(simpleMethod.getEventErrorMessageName(), errMsg);
|
||||
methodContext.putEnv(simpleMethod.getEventResponseCodeName(), errorCode);
|
||||
} else {
|
||||
methodContext.putEnv(simpleMethod.getServiceErrorMessageName(), errMsg);
|
||||
methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), errorCode);
|
||||
}
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "break-on-error set to \"true\", halting script execution. End call-service.");
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "End call-service.");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
if (resultsToMapList != null) {
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "Processing " + resultsToMapList.size() + " <results-to-map> elements.");
|
||||
}
|
||||
for (String mapName : resultsToMapList) {
|
||||
methodContext.putEnv(mapName, UtilMisc.makeMapWritable(result));
|
||||
}
|
||||
}
|
||||
if (resultToFieldList != null) {
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "Processing " + resultToFieldList.size() + " <result-to-field> elements.");
|
||||
}
|
||||
for (ResultToField rtfDef : resultToFieldList) {
|
||||
rtfDef.exec(methodContext, result);
|
||||
}
|
||||
}
|
||||
if (resultToResultList != null) {
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "Processing " + resultToResultList.size() + " <result-to-result> elements.");
|
||||
}
|
||||
for (ResultToResult rtrDef : resultToResultList) {
|
||||
rtrDef.exec(methodContext, result);
|
||||
}
|
||||
}
|
||||
if (methodContext.getMethodType() == MethodContext.EVENT) {
|
||||
if (resultToRequestList != null) {
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "Processing " + resultToRequestList.size() + " <result-to-request> elements.");
|
||||
}
|
||||
for (ResultToRequest rtrDef : resultToRequestList) {
|
||||
rtrDef.exec(methodContext, result);
|
||||
}
|
||||
}
|
||||
if (resultToSessionList != null) {
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "Processing " + resultToSessionList.size() + " <result-to-session> elements.");
|
||||
}
|
||||
for (ResultToSession rtsDef : resultToSessionList) {
|
||||
rtsDef.exec(methodContext, result);
|
||||
}
|
||||
}
|
||||
}
|
||||
String errorPrefixStr = errorPrefix.getMessage(methodContext.getLoader(), methodContext);
|
||||
String errorSuffixStr = errorSuffix.getMessage(methodContext.getLoader(), methodContext);
|
||||
String successPrefixStr = successPrefix.getMessage(methodContext.getLoader(), methodContext);
|
||||
String successSuffixStr = successSuffix.getMessage(methodContext.getLoader(), methodContext);
|
||||
String messagePrefixStr = messagePrefix.getMessage(methodContext.getLoader(), methodContext);
|
||||
String messageSuffixStr = messageSuffix.getMessage(methodContext.getLoader(), methodContext);
|
||||
String errorMessage = null;
|
||||
List<String> errorMessageList = null;
|
||||
// See if there is a single message
|
||||
if (result.containsKey(ModelService.ERROR_MESSAGE)) {
|
||||
errorMessage = ServiceUtil.makeErrorMessage(result, messagePrefixStr, messageSuffixStr, errorPrefixStr, errorSuffixStr);
|
||||
} else if (result.containsKey(ModelService.ERROR_MESSAGE_LIST)) {
|
||||
errorMessageList = UtilGenerics.checkList(result.get(ModelService.ERROR_MESSAGE_LIST));
|
||||
}
|
||||
if ((UtilValidate.isNotEmpty(errorMessage) || UtilValidate.isNotEmpty(errorMessageList)) && breakOnError) {
|
||||
if (methodContext.getMethodType() == MethodContext.EVENT) {
|
||||
if (UtilValidate.isNotEmpty(errorMessage)) {
|
||||
if (Debug.verboseOn()) {
|
||||
errorMessage += UtilProperties.getMessage(resource, "simpleMethod.error_show_service_name", UtilMisc.toMap("serviceName", serviceName, "methodName", simpleMethod.getMethodName()), locale);
|
||||
}
|
||||
methodContext.putEnv(simpleMethod.getEventErrorMessageName(), errorMessage);
|
||||
} else {
|
||||
if (Debug.verboseOn()) {
|
||||
errorMessageList.add(UtilProperties.getMessage(resource, "simpleMethod.error_show_service_name", UtilMisc.toMap("serviceName", serviceName, "methodName", simpleMethod.getMethodName()), locale));
|
||||
}
|
||||
methodContext.putEnv(simpleMethod.getEventErrorMessageListName(), errorMessageList);
|
||||
}
|
||||
} else {
|
||||
ServiceUtil.addErrors(UtilMisc.<String, String> getListFromMap(methodContext.getEnvMap(), this.simpleMethod.getServiceErrorMessageListName()), UtilMisc.<String, String, Object> getMapFromMap(methodContext.getEnvMap(), this.simpleMethod.getServiceErrorMessageMapName()), result);
|
||||
Debug.logError(new Exception(errorMessage), module);
|
||||
}
|
||||
}
|
||||
String successMessage = ServiceUtil.makeSuccessMessage(result, messagePrefixStr, messageSuffixStr, successPrefixStr, successSuffixStr);
|
||||
if (UtilValidate.isNotEmpty(successMessage)) {
|
||||
if (methodContext.getMethodType() == MethodContext.EVENT) {
|
||||
methodContext.putEnv(simpleMethod.getEventEventMessageName(), successMessage);
|
||||
} else {
|
||||
methodContext.putEnv(simpleMethod.getServiceSuccessMessageName(), successMessage);
|
||||
}
|
||||
}
|
||||
String defaultMessageStr = defaultMessage.getMessage(methodContext.getLoader(), methodContext);
|
||||
if (UtilValidate.isEmpty(errorMessage) && UtilValidate.isEmpty(errorMessageList) && UtilValidate.isEmpty(successMessage) && UtilValidate.isNotEmpty(defaultMessageStr)) {
|
||||
if (methodContext.getMethodType() == MethodContext.EVENT) {
|
||||
methodContext.putEnv(simpleMethod.getEventEventMessageName(), defaultMessageStr);
|
||||
} else {
|
||||
methodContext.putEnv(simpleMethod.getServiceSuccessMessageName(), defaultMessageStr);
|
||||
}
|
||||
}
|
||||
String responseCode = result.containsKey(ModelService.RESPONSE_MESSAGE) ? (String) result.get(ModelService.RESPONSE_MESSAGE) : successCode;
|
||||
if (errorCode.equals(responseCode)) {
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "Service returned an error.");
|
||||
}
|
||||
if (breakOnError) {
|
||||
if (methodContext.getMethodType() == MethodContext.EVENT) {
|
||||
methodContext.putEnv(simpleMethod.getEventResponseCodeName(), responseCode);
|
||||
} else {
|
||||
methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), responseCode);
|
||||
}
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "break-on-error set to \"true\", halting script execution. End call-service.");
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "End call-service.");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "Service ran successfully. End call-service.");
|
||||
}
|
||||
if (methodContext.getMethodType() == MethodContext.EVENT) {
|
||||
methodContext.putEnv(simpleMethod.getEventResponseCodeName(), responseCode);
|
||||
} else {
|
||||
methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), responseCode);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
aic.addServiceName(this.serviceNameFse.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<call-service ");
|
||||
sb.append("service-name=\"").append(this.serviceNameFse).append("\" ");
|
||||
if (!this.inMapFma.isEmpty()) {
|
||||
sb.append("in-map-name=\"").append(this.inMapFma).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <call-service> element.
|
||||
*/
|
||||
public static final class CallServiceFactory implements Factory<CallService> {
|
||||
@Override
|
||||
public CallService createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new CallService(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "call-service";
|
||||
}
|
||||
}
|
||||
|
||||
private final class ResultToField {
|
||||
private final FlexibleMapAccessor<Object> fieldFma;
|
||||
private final FlexibleMapAccessor<Object> resultFma;
|
||||
|
||||
private ResultToField(Element element) {
|
||||
resultFma = FlexibleMapAccessor.getInstance(element.getAttribute("result-name"));
|
||||
String fieldAttribute = element.getAttribute("field");
|
||||
if (fieldAttribute.isEmpty()) {
|
||||
fieldFma = resultFma;
|
||||
} else {
|
||||
fieldFma = FlexibleMapAccessor.getInstance(fieldAttribute);
|
||||
}
|
||||
}
|
||||
|
||||
private void exec(MethodContext methodContext, Map<String, Object> resultMap) {
|
||||
fieldFma.put(methodContext.getEnvMap(), resultFma.get(resultMap));
|
||||
}
|
||||
}
|
||||
|
||||
private final class ResultToRequest {
|
||||
private final FlexibleMapAccessor<Object> resultFma;
|
||||
private final FlexibleServletAccessor<Object> requestFsa;
|
||||
|
||||
private ResultToRequest(Element element) {
|
||||
requestFsa = new FlexibleServletAccessor<Object>(element.getAttribute("request-name"), element.getAttribute("result-name"));
|
||||
resultFma =FlexibleMapAccessor.getInstance(element.getAttribute("result-name"));
|
||||
}
|
||||
|
||||
private void exec(MethodContext methodContext, Map<String, Object> resultMap) {
|
||||
requestFsa.put(methodContext.getRequest(), resultFma.get(resultMap), methodContext.getEnvMap());
|
||||
}
|
||||
}
|
||||
|
||||
private final class ResultToResult {
|
||||
private final FlexibleMapAccessor<Object> resultFma;
|
||||
private final FlexibleMapAccessor<Object> serviceResultFma;
|
||||
|
||||
private ResultToResult(Element element) {
|
||||
resultFma = FlexibleMapAccessor.getInstance(element.getAttribute("result-name"));
|
||||
String serviceResultAttribute = element.getAttribute("service-result-name");
|
||||
if (serviceResultAttribute.isEmpty()) {
|
||||
serviceResultFma = resultFma;
|
||||
} else {
|
||||
serviceResultFma = FlexibleMapAccessor.getInstance(serviceResultAttribute);
|
||||
}
|
||||
}
|
||||
|
||||
private void exec(MethodContext methodContext, Map<String, Object> resultMap) {
|
||||
serviceResultFma.put(methodContext.getResults(), resultFma.get(resultMap));
|
||||
}
|
||||
}
|
||||
|
||||
private final class ResultToSession {
|
||||
private final FlexibleMapAccessor<Object> resultFma;
|
||||
private final FlexibleServletAccessor<Object> requestFsa;
|
||||
|
||||
private ResultToSession(Element element) {
|
||||
requestFsa = new FlexibleServletAccessor<Object>(element.getAttribute("session-name"), element.getAttribute("result-name"));
|
||||
resultFma =FlexibleMapAccessor.getInstance(element.getAttribute("result-name"));
|
||||
}
|
||||
|
||||
private void exec(MethodContext methodContext, Map<String, Object> resultMap) {
|
||||
requestFsa.put(methodContext.getRequest().getSession(), resultFma.get(resultMap), methodContext.getEnvMap());
|
||||
}
|
||||
}
|
||||
}
|
||||
+144
@@ -0,0 +1,144 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.callops;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.entity.GenericValue;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.ofbiz.service.GenericServiceException;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <call-service-asynch> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Ccallserviceasynch%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class CallServiceAsynch extends MethodOperation {
|
||||
|
||||
public static final String module = CallServiceAsynch.class.getName();
|
||||
|
||||
private final boolean includeUserLogin;
|
||||
private final FlexibleMapAccessor<Map<String, Object>> inMapFma;
|
||||
private final FlexibleStringExpander serviceNameFse;
|
||||
|
||||
public CallServiceAsynch(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "serviceName", "in-map-name", "include-user-login");
|
||||
MiniLangValidate.constantAttributes(simpleMethod, element, "include-user-login");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "service-name", "in-map-name");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "service-name");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
serviceNameFse = FlexibleStringExpander.getInstance(element.getAttribute("service-name"));
|
||||
inMapFma = FlexibleMapAccessor.getInstance(element.getAttribute("in-map-name"));
|
||||
includeUserLogin = !"false".equals(element.getAttribute("include-user-login"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "Begin call-service-asynch.");
|
||||
}
|
||||
String serviceName = serviceNameFse.expandString(methodContext.getEnvMap());
|
||||
Map<String, Object> inMap = inMapFma.get(methodContext.getEnvMap());
|
||||
if (inMap == null) {
|
||||
inMap = new HashMap<String, Object>();
|
||||
}
|
||||
if (includeUserLogin) {
|
||||
GenericValue userLogin = methodContext.getUserLogin();
|
||||
if (userLogin != null && inMap.get("userLogin") == null) {
|
||||
inMap.put("userLogin", userLogin);
|
||||
}
|
||||
}
|
||||
Locale locale = methodContext.getLocale();
|
||||
if (locale != null) {
|
||||
inMap.put("locale", locale);
|
||||
}
|
||||
try {
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "Invoking service \"" + serviceName + "\", IN attributes:", inMap.toString());
|
||||
}
|
||||
methodContext.getDispatcher().runAsync(serviceName, inMap);
|
||||
} catch (GenericServiceException e) {
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "Service engine threw an exception: " + e.getMessage() + ", halting script execution. End call-service-asynch.");
|
||||
}
|
||||
Debug.logError(e, module);
|
||||
String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [problem invoking the " + serviceName + " service: " + e.getMessage() + "]";
|
||||
if (methodContext.getMethodType() == MethodContext.EVENT) {
|
||||
methodContext.putEnv(simpleMethod.getEventErrorMessageName(), errMsg);
|
||||
methodContext.putEnv(simpleMethod.getEventResponseCodeName(), simpleMethod.getDefaultErrorCode());
|
||||
} else {
|
||||
methodContext.putEnv(simpleMethod.getServiceErrorMessageName(), errMsg);
|
||||
methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), simpleMethod.getDefaultErrorCode());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "End call-service-asynch.");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
aic.addServiceName(this.serviceNameFse.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<call-service-asynch ");
|
||||
sb.append("service-name=\"").append(this.serviceNameFse).append("\" ");
|
||||
if (!this.inMapFma.isEmpty()) {
|
||||
sb.append("in-map-name=\"").append(this.inMapFma).append("\" ");
|
||||
}
|
||||
if (!this.includeUserLogin) {
|
||||
sb.append("include-user-login=\"false\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <call-service-asynch> element.
|
||||
*/
|
||||
public static final class CallServiceAsynchFactory implements Factory<CallServiceAsynch> {
|
||||
@Override
|
||||
public CallServiceAsynch createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new CallServiceAsynch(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "call-service-asynch";
|
||||
}
|
||||
}
|
||||
}
|
||||
+140
@@ -0,0 +1,140 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.callops;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.ofbiz.base.util.UtilXml;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMapProcessor;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.ofbiz.minilang.operation.MapProcessor;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <call-map-processor> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Ccallmapprocessor%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class CallSimpleMapProcessor extends MethodOperation {
|
||||
|
||||
private final FlexibleMapAccessor<List<Object>> errorListFma;
|
||||
private final MapProcessor inlineMapProcessor;
|
||||
private final FlexibleMapAccessor<Map<String, Object>> inMapFma;
|
||||
private final FlexibleMapAccessor<Map<String, Object>> outMapFma;
|
||||
private final String processorName;
|
||||
private final String xmlResource;
|
||||
|
||||
public CallSimpleMapProcessor(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "processor-name", "xml-resource", "in-map-name", "out-map-name", "error-list-name");
|
||||
MiniLangValidate.constantAttributes(simpleMethod, element, "processor-name", "xml-resource", "error-list-name");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "in-map-name", "out-map-name");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "in-map-name", "out-map-name");
|
||||
MiniLangValidate.childElements(simpleMethod, element, "simple-map-processor");
|
||||
}
|
||||
processorName = element.getAttribute("processor-name");
|
||||
xmlResource = element.getAttribute("xml-resource");
|
||||
errorListFma = FlexibleMapAccessor.getInstance(MiniLangValidate.checkAttribute(element.getAttribute("error-list-name"), "error_list"));
|
||||
inMapFma = FlexibleMapAccessor.getInstance(element.getAttribute("in-map-name"));
|
||||
outMapFma = FlexibleMapAccessor.getInstance(element.getAttribute("out-map-name"));
|
||||
Element simpleMapProcessorElement = UtilXml.firstChildElement(element, "simple-map-processor");
|
||||
if (simpleMapProcessorElement != null) {
|
||||
inlineMapProcessor = new MapProcessor(simpleMapProcessorElement);
|
||||
} else {
|
||||
inlineMapProcessor = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
List<Object> messages = errorListFma.get(methodContext.getEnvMap());
|
||||
if (messages == null) {
|
||||
messages = new LinkedList<Object>();
|
||||
errorListFma.put(methodContext.getEnvMap(), messages);
|
||||
}
|
||||
Map<String, Object> inMap = inMapFma.get(methodContext.getEnvMap());
|
||||
if (inMap == null) {
|
||||
inMap = new HashMap<String, Object>();
|
||||
}
|
||||
Map<String, Object> outMap = outMapFma.get(methodContext.getEnvMap());
|
||||
if (outMap == null) {
|
||||
outMap = new HashMap<String, Object>();
|
||||
outMapFma.put(methodContext.getEnvMap(), outMap);
|
||||
}
|
||||
// run external map processor first
|
||||
if (!this.xmlResource.isEmpty() && !this.processorName.isEmpty()) {
|
||||
try {
|
||||
SimpleMapProcessor.runSimpleMapProcessor(xmlResource, processorName, inMap, outMap, messages, methodContext.getLocale(), methodContext.getLoader());
|
||||
} catch (MiniLangException e) {
|
||||
messages.add("Error running SimpleMapProcessor in XML file \"" + xmlResource + "\": " + e.toString());
|
||||
}
|
||||
}
|
||||
// run inline map processor last so it can override the external map processor
|
||||
if (inlineMapProcessor != null) {
|
||||
inlineMapProcessor.exec(inMap, outMap, messages, methodContext.getLocale(), methodContext.getLoader());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<call-map-processor ");
|
||||
if (!this.processorName.isEmpty()) {
|
||||
sb.append("processor-name=\"").append(this.processorName).append("\" ");
|
||||
}
|
||||
if (!this.xmlResource.isEmpty()) {
|
||||
sb.append("xml-resource=\"").append(this.xmlResource).append("\" ");
|
||||
}
|
||||
if (!this.inMapFma.isEmpty()) {
|
||||
sb.append("in-map-name=\"").append(this.inMapFma).append("\" ");
|
||||
}
|
||||
if (!this.outMapFma.isEmpty()) {
|
||||
sb.append("out-map-name=\"").append(this.outMapFma).append("\" ");
|
||||
}
|
||||
if (!"error_list".equals(errorListFma.toString())) {
|
||||
sb.append("error-list-name=\"").append(errorListFma).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <call-map-processor> element.
|
||||
*/
|
||||
public static final class CallSimpleMapProcessorFactory implements Factory<CallSimpleMapProcessor> {
|
||||
@Override
|
||||
public CallSimpleMapProcessor createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new CallSimpleMapProcessor(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "call-map-processor";
|
||||
}
|
||||
}
|
||||
}
|
||||
+235
@@ -0,0 +1,235 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.callops;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.ofbiz.base.location.FlexibleLocation;
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.UtilValidate;
|
||||
import org.ofbiz.base.util.UtilXml;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.ValidationException;
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <call-simple-method> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Ccallsimplemethod%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class CallSimpleMethod extends MethodOperation {
|
||||
|
||||
public static final String module = CallSimpleMethod.class.getName();
|
||||
|
||||
private final String methodName;
|
||||
private final String xmlResource;
|
||||
private final URL xmlURL;
|
||||
private final String scope;
|
||||
private final List<ResultToField> resultToFieldList;
|
||||
|
||||
public CallSimpleMethod(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "method-name", "xml-resource", "scope");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "method-name");
|
||||
MiniLangValidate.constantAttributes(simpleMethod, element, "method-name", "xml-resource", "scope");
|
||||
MiniLangValidate.childElements(simpleMethod, element, "result-to-field");
|
||||
}
|
||||
this.methodName = element.getAttribute("method-name");
|
||||
String xmlResourceAttribute = element.getAttribute("xml-resource");
|
||||
if (xmlResourceAttribute.isEmpty()) {
|
||||
xmlResourceAttribute = simpleMethod.getFromLocation();
|
||||
}
|
||||
this.xmlResource = xmlResourceAttribute;
|
||||
URL xmlURL = null;
|
||||
try {
|
||||
xmlURL = FlexibleLocation.resolveLocation(this.xmlResource);
|
||||
} catch (MalformedURLException e) {
|
||||
MiniLangValidate.handleError("Could not find SimpleMethod XML document in resource: " + this.xmlResource + "; error was: " + e.toString(), simpleMethod, element);
|
||||
}
|
||||
this.xmlURL = xmlURL;
|
||||
this.scope = element.getAttribute("scope");
|
||||
List<? extends Element> resultToFieldElements = UtilXml.childElementList(element, "result-to-field");
|
||||
if (UtilValidate.isNotEmpty(resultToFieldElements)) {
|
||||
if (!"function".equals(this.scope)) {
|
||||
MiniLangValidate.handleError("Inline scope cannot include <result-to-field> elements.", simpleMethod, element);
|
||||
}
|
||||
List<ResultToField> resultToFieldList = new ArrayList<ResultToField>(resultToFieldElements.size());
|
||||
for (Element resultToFieldElement : resultToFieldElements) {
|
||||
resultToFieldList.add(new ResultToField(resultToFieldElement, simpleMethod));
|
||||
}
|
||||
this.resultToFieldList = resultToFieldList;
|
||||
} else {
|
||||
this.resultToFieldList = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
if (UtilValidate.isEmpty(this.methodName)) {
|
||||
throw new MiniLangRuntimeException("method-name attribute is empty", this);
|
||||
}
|
||||
SimpleMethod simpleMethodToCall = SimpleMethod.getSimpleMethod(this.xmlURL, this.methodName);
|
||||
if (simpleMethodToCall == null) {
|
||||
throw new MiniLangRuntimeException("Could not find <simple-method name=\"" + this.methodName + "\"> in XML document " + this.xmlResource, this);
|
||||
}
|
||||
MethodContext localContext = methodContext;
|
||||
if ("function".equals(this.scope)) {
|
||||
Map<String, Object> localEnv = new HashMap<String, Object>();
|
||||
localEnv.putAll(methodContext.getEnvMap());
|
||||
localEnv.remove(this.simpleMethod.getEventResponseCodeName());
|
||||
localEnv.remove(this.simpleMethod.getServiceResponseMessageName());
|
||||
localContext = new MethodContext(localEnv, methodContext.getLoader(), methodContext.getMethodType());
|
||||
}
|
||||
String returnVal = simpleMethodToCall.exec(localContext);
|
||||
if (Debug.verboseOn())
|
||||
Debug.logVerbose("Called simple-method named [" + this.methodName + "] in resource [" + this.xmlResource + "], returnVal is [" + returnVal + "]", module);
|
||||
if (simpleMethodToCall.getDefaultErrorCode().equals(returnVal)) {
|
||||
if (methodContext.getMethodType() == MethodContext.EVENT) {
|
||||
methodContext.putEnv(simpleMethod.getEventResponseCodeName(), simpleMethod.getDefaultErrorCode());
|
||||
} else if (methodContext.getMethodType() == MethodContext.SERVICE) {
|
||||
methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), simpleMethod.getDefaultErrorCode());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (methodContext.getMethodType() == MethodContext.EVENT) {
|
||||
// FIXME: This doesn't make sense. We are comparing the called method's response code with this method's
|
||||
// response code. Since response codes are configurable per method, this code will fail.
|
||||
String responseCode = (String) localContext.getEnv(this.simpleMethod.getEventResponseCodeName());
|
||||
if (this.simpleMethod.getDefaultErrorCode().equals(responseCode)) {
|
||||
Debug.logWarning("Got error [" + responseCode + "] calling inline simple-method named [" + this.methodName + "] in resource [" + this.xmlResource + "], message is " + methodContext.getEnv(this.simpleMethod.getEventErrorMessageName()), module);
|
||||
return false;
|
||||
}
|
||||
} else if (methodContext.getMethodType() == MethodContext.SERVICE) {
|
||||
// FIXME: This doesn't make sense. We are comparing the called method's response message with this method's
|
||||
// response message. Since response messages are configurable per method, this code will fail.
|
||||
String responseMessage = (String) localContext.getEnv(this.simpleMethod.getServiceResponseMessageName());
|
||||
if (this.simpleMethod.getDefaultErrorCode().equals(responseMessage)) {
|
||||
Debug.logWarning("Got error [" + responseMessage + "] calling inline simple-method named [" + this.methodName + "] in resource [" + this.xmlResource + "], message is " + methodContext.getEnv(this.simpleMethod.getServiceErrorMessageName()) + ", and the error message list is: "
|
||||
+ methodContext.getEnv(this.simpleMethod.getServiceErrorMessageListName()), module);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
if ("function".equals(this.scope) && this.resultToFieldList != null) {
|
||||
Map<String, Object> results = localContext.getResults();
|
||||
if (results != null) {
|
||||
for (ResultToField resultToField : this.resultToFieldList) {
|
||||
resultToField.exec(methodContext.getEnvMap(), results);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
SimpleMethod simpleMethodToCall;
|
||||
try {
|
||||
simpleMethodToCall = SimpleMethod.getSimpleMethod(this.xmlURL, this.methodName);
|
||||
if (simpleMethodToCall != null) {
|
||||
if (!aic.hasVisited(simpleMethodToCall)) {
|
||||
aic.addSimpleMethod(simpleMethodToCall);
|
||||
simpleMethodToCall.gatherArtifactInfo(aic);
|
||||
}
|
||||
}
|
||||
} catch (MiniLangException e) {
|
||||
Debug.logWarning("Could not find <simple-method name=\"" + this.methodName + "\"> in XML document " + this.xmlResource + ": " + e.toString(), module);
|
||||
}
|
||||
}
|
||||
|
||||
public String getMethodName() {
|
||||
return this.methodName;
|
||||
}
|
||||
|
||||
public String getXmlResource() {
|
||||
return this.xmlResource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<call-simple-method ");
|
||||
if (this.methodName.length() > 0) {
|
||||
sb.append("method-name=\"").append(this.methodName).append("\" ");
|
||||
}
|
||||
if (this.xmlResource.length() > 0) {
|
||||
sb.append("xml-resource=\"").append(this.xmlResource).append("\" ");
|
||||
}
|
||||
if (this.scope.length() > 0) {
|
||||
sb.append("scope=\"").append(this.scope).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <call-simple-method> element.
|
||||
*/
|
||||
public static final class CallSimpleMethodFactory implements Factory<CallSimpleMethod> {
|
||||
@Override
|
||||
public CallSimpleMethod createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new CallSimpleMethod(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "call-simple-method";
|
||||
}
|
||||
}
|
||||
|
||||
private final class ResultToField {
|
||||
|
||||
private final FlexibleMapAccessor<Object> fieldFma;
|
||||
private final FlexibleMapAccessor<Object> resultNameFma;
|
||||
|
||||
private ResultToField(Element element, SimpleMethod simpleMethod) throws ValidationException {
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "result-name", "field");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "result-name");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "result-name", "field");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
this.resultNameFma = FlexibleMapAccessor.getInstance(element.getAttribute("result-name"));
|
||||
String fieldAttribute = element.getAttribute("field");
|
||||
if (fieldAttribute.length() == 0) {
|
||||
this.fieldFma = this.resultNameFma;
|
||||
} else {
|
||||
this.fieldFma = FlexibleMapAccessor.getInstance(fieldAttribute);
|
||||
}
|
||||
}
|
||||
|
||||
private void exec(Map<String, Object> context, Map<String, Object> results) throws MiniLangException {
|
||||
Object value = this.resultNameFma.get(results);
|
||||
if (value != null) {
|
||||
this.fieldFma.put(context, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+66
@@ -0,0 +1,66 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.callops;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import org.ofbiz.base.util.UtilProperties;
|
||||
import org.ofbiz.base.util.UtilXml;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Simple class to wrap messages that come either from a straight string or a properties file
|
||||
*/
|
||||
@SuppressWarnings("serial")
|
||||
public final class FlexibleMessage implements Serializable {
|
||||
|
||||
private final FlexibleStringExpander messageFse;
|
||||
private final String propertykey;
|
||||
private final String propertyResource;
|
||||
|
||||
public FlexibleMessage(Element element, String defaultProperty) {
|
||||
if (element != null) {
|
||||
String message = UtilXml.elementValue(element);
|
||||
if (message != null) {
|
||||
messageFse = FlexibleStringExpander.getInstance(message);
|
||||
propertykey = null;
|
||||
propertyResource = null;
|
||||
} else {
|
||||
messageFse = null;
|
||||
propertykey = MiniLangValidate.checkAttribute(element.getAttribute("property"), defaultProperty);
|
||||
propertyResource = MiniLangValidate.checkAttribute(element.getAttribute("resource"), "DefaultMessages");
|
||||
}
|
||||
} else {
|
||||
messageFse = null;
|
||||
propertykey = defaultProperty;
|
||||
propertyResource = "DefaultMessages";
|
||||
}
|
||||
}
|
||||
|
||||
public String getMessage(ClassLoader loader, MethodContext methodContext) {
|
||||
if (messageFse != null) {
|
||||
return messageFse.expandString(methodContext.getEnvMap());
|
||||
} else {
|
||||
return UtilProperties.getMessage(propertyResource, propertykey, methodContext.getEnvMap(), methodContext.getLocale());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.callops;
|
||||
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <return> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Creturn%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class Return extends MethodOperation {
|
||||
|
||||
private final FlexibleStringExpander responseCodeFse;
|
||||
|
||||
public Return(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "response-code");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
responseCodeFse = FlexibleStringExpander.getInstance(element.getAttribute("response-code"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
String responseCode = responseCodeFse.expandString(methodContext.getEnvMap());
|
||||
if (responseCode.isEmpty()) {
|
||||
responseCode = simpleMethod.getDefaultSuccessCode();
|
||||
}
|
||||
if (methodContext.getMethodType() == MethodContext.EVENT) {
|
||||
methodContext.putEnv(simpleMethod.getEventResponseCodeName(), responseCode);
|
||||
} else {
|
||||
methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), responseCode);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<return ");
|
||||
if (!"success".equals(responseCodeFse.getOriginal())) {
|
||||
sb.append("response-code=\"").append(responseCodeFse).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <return> element.
|
||||
*/
|
||||
public static final class ReturnFactory implements Factory<Return> {
|
||||
@Override
|
||||
public Return createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new Return(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "return";
|
||||
}
|
||||
}
|
||||
}
|
||||
+154
@@ -0,0 +1,154 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.callops;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangUtil;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.ofbiz.service.GenericServiceException;
|
||||
import org.ofbiz.service.ModelService;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <set-service-fields> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Csetservicefields%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class SetServiceFields extends MethodOperation {
|
||||
|
||||
public static final String module = SetServiceFields.class.getName();
|
||||
|
||||
// This method is needed only during the v1 to v2 transition
|
||||
private static boolean autoCorrect(Element element) {
|
||||
String errorListAttr = element.getAttribute("error-list-name");
|
||||
if (!errorListAttr.isEmpty()) {
|
||||
element.removeAttribute("error-list-name");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private final FlexibleMapAccessor<Map<String, ? extends Object>> mapFma;
|
||||
private final FlexibleStringExpander serviceNameFse;
|
||||
private final FlexibleMapAccessor<Map<String, Object>> toMapFma;
|
||||
private final String mode;
|
||||
|
||||
public SetServiceFields(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "service-name", "map", "to-map", "mode");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "service-name", "map", "to-map");
|
||||
MiniLangValidate.constantPlusExpressionAttributes(simpleMethod, element, "service-name");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "map", "to-map");
|
||||
MiniLangValidate.constantAttributes(simpleMethod, element, "mode");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
boolean elementModified = autoCorrect(element);
|
||||
if (elementModified && MiniLangUtil.autoCorrectOn()) {
|
||||
MiniLangUtil.flagDocumentAsCorrected(element);
|
||||
}
|
||||
serviceNameFse = FlexibleStringExpander.getInstance(element.getAttribute("service-name"));
|
||||
mapFma = FlexibleMapAccessor.getInstance(element.getAttribute("map"));
|
||||
toMapFma = FlexibleMapAccessor.getInstance(element.getAttribute("to-map"));
|
||||
mode = ModelService.OUT_PARAM.equals(element.getAttribute("mode")) ? ModelService.OUT_PARAM : ModelService.IN_PARAM;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
Map<String, ? extends Object> fromMap = mapFma.get(methodContext.getEnvMap());
|
||||
if (fromMap == null) {
|
||||
if (Debug.verboseOn()) {
|
||||
Debug.logVerbose("The from map in set-service-field was not found with name: " + mapFma, module);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
String serviceName = serviceNameFse.expandString(methodContext.getEnvMap());
|
||||
ModelService modelService = null;
|
||||
try {
|
||||
modelService = methodContext.getDispatcher().getDispatchContext().getModelService(serviceName);
|
||||
} catch (GenericServiceException e) {
|
||||
throw new MiniLangRuntimeException("Could not get service definition for service name \"" + serviceName + "\": " + e.getMessage(), this);
|
||||
}
|
||||
Map<String, Object> toMap = toMapFma.get(methodContext.getEnvMap());
|
||||
if (toMap == null) {
|
||||
toMap = new HashMap<String, Object>();
|
||||
toMapFma.put(methodContext.getEnvMap(), toMap);
|
||||
}
|
||||
List<Object> errorMessages = new LinkedList<Object>();
|
||||
Map<String, Object> validAttributes = modelService.makeValid(fromMap, mode, true, errorMessages, methodContext.getTimeZone(), methodContext.getLocale());
|
||||
if (errorMessages.size() > 0) {
|
||||
for (Object obj : errorMessages) {
|
||||
simpleMethod.addErrorMessage(methodContext, (String) obj);
|
||||
}
|
||||
throw new MiniLangRuntimeException("Errors encountered while setting service attributes for service name \"" + serviceName + "\"", this);
|
||||
}
|
||||
toMap.putAll(validAttributes);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
aic.addServiceName(this.serviceNameFse.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<set-service-fields ");
|
||||
if (!this.serviceNameFse.isEmpty()) {
|
||||
sb.append("service-name=\"").append(this.serviceNameFse).append("\" ");
|
||||
}
|
||||
if (!this.mapFma.isEmpty()) {
|
||||
sb.append("map=\"").append(this.mapFma).append("\" ");
|
||||
}
|
||||
if (!this.toMapFma.isEmpty()) {
|
||||
sb.append("to-map=\"").append(this.toMapFma).append("\" ");
|
||||
}
|
||||
sb.append("mode=\"").append(this.mode).append("\" ");
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <set-service-fields> element.
|
||||
*/
|
||||
public static final class SetServiceFieldsFactory implements Factory<SetServiceFields> {
|
||||
@Override
|
||||
public SetServiceFields createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new SetServiceFields(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "set-service-fields";
|
||||
}
|
||||
}
|
||||
}
|
||||
+193
@@ -0,0 +1,193 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.conditional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.util.UtilXml;
|
||||
import org.ofbiz.minilang.MiniLangElement;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <and>, <or>, <not>, and <xor> elements.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-Conditional%2FLoopingStatements">Mini-language Reference</a>
|
||||
*/
|
||||
public abstract class CombinedCondition extends MiniLangElement implements Conditional {
|
||||
|
||||
protected final List<Conditional> subConditions;
|
||||
|
||||
public CombinedCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
List<? extends Element> childElements = UtilXml.childElementList(element);
|
||||
if (MiniLangValidate.validationOn() && childElements.isEmpty()) {
|
||||
MiniLangValidate.handleError("No conditional elements.", simpleMethod, element);
|
||||
}
|
||||
List<Conditional> conditionalList = new ArrayList<Conditional>(childElements.size());
|
||||
for (Element conditionalElement : UtilXml.childElementList(element)) {
|
||||
conditionalList.add(ConditionalFactory.makeConditional(conditionalElement, simpleMethod));
|
||||
}
|
||||
this.subConditions = Collections.unmodifiableList(conditionalList);
|
||||
}
|
||||
|
||||
protected void prettyPrint(StringBuilder messageBuffer, MethodContext methodContext, String combineText) {
|
||||
messageBuffer.append("(");
|
||||
for (Conditional subCond : subConditions) {
|
||||
subCond.prettyPrint(messageBuffer, methodContext);
|
||||
messageBuffer.append(combineText);
|
||||
}
|
||||
messageBuffer.append(")");
|
||||
}
|
||||
|
||||
/**
|
||||
* A <and> element factory.
|
||||
*/
|
||||
public static final class AndConditionFactory extends ConditionalFactory<CombinedCondition> {
|
||||
@Override
|
||||
public CombinedCondition createCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new CombinedCondition(element, simpleMethod) {
|
||||
@Override
|
||||
public boolean checkCondition(MethodContext methodContext) throws MiniLangException {
|
||||
if (subConditions.size() == 0)
|
||||
return true;
|
||||
for (Conditional subCond : subConditions) {
|
||||
if (!subCond.checkCondition(methodContext)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public void prettyPrint(StringBuilder messageBuffer, MethodContext methodContext) {
|
||||
prettyPrint(messageBuffer, methodContext, " AND ");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "and";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A <not> element factory.
|
||||
*/
|
||||
public static final class NotConditionFactory extends ConditionalFactory<CombinedCondition> {
|
||||
@Override
|
||||
public CombinedCondition createCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new CombinedCondition(element, simpleMethod) {
|
||||
@Override
|
||||
public boolean checkCondition(MethodContext methodContext) throws MiniLangException {
|
||||
if (subConditions.size() == 0)
|
||||
return true;
|
||||
Conditional subCond = subConditions.get(0);
|
||||
return !subCond.checkCondition(methodContext);
|
||||
}
|
||||
@Override
|
||||
public void prettyPrint(StringBuilder messageBuffer, MethodContext methodContext) {
|
||||
messageBuffer.append("( NOT ");
|
||||
if (subConditions.size() > 0) {
|
||||
Conditional subCond = subConditions.get(0);
|
||||
subCond.prettyPrint(messageBuffer, methodContext);
|
||||
}
|
||||
messageBuffer.append(")");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "not";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A <or> element factory.
|
||||
*/
|
||||
public static final class OrConditionFactory extends ConditionalFactory<CombinedCondition> {
|
||||
@Override
|
||||
public CombinedCondition createCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new CombinedCondition(element, simpleMethod) {
|
||||
@Override
|
||||
public boolean checkCondition(MethodContext methodContext) throws MiniLangException {
|
||||
if (subConditions.size() == 0)
|
||||
return true;
|
||||
for (Conditional subCond : subConditions) {
|
||||
if (subCond.checkCondition(methodContext)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@Override
|
||||
public void prettyPrint(StringBuilder messageBuffer, MethodContext methodContext) {
|
||||
prettyPrint(messageBuffer, methodContext, " OR ");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "or";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A <xor> element factory.
|
||||
*/
|
||||
public static final class XorConditionFactory extends ConditionalFactory<CombinedCondition> {
|
||||
@Override
|
||||
public CombinedCondition createCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new CombinedCondition(element, simpleMethod) {
|
||||
@Override
|
||||
public boolean checkCondition(MethodContext methodContext) throws MiniLangException {
|
||||
if (subConditions.size() == 0)
|
||||
return true;
|
||||
boolean trueFound = false;
|
||||
for (Conditional subCond : subConditions) {
|
||||
if (subCond.checkCondition(methodContext)) {
|
||||
if (trueFound) {
|
||||
return false;
|
||||
} else {
|
||||
trueFound = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return trueFound;
|
||||
}
|
||||
@Override
|
||||
public void prettyPrint(StringBuilder messageBuffer, MethodContext methodContext) {
|
||||
prettyPrint(messageBuffer, methodContext, " XOR ");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "xor";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,291 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.conditional;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.ofbiz.base.util.Assert;
|
||||
import org.ofbiz.base.util.ObjectType;
|
||||
import org.ofbiz.base.util.UtilGenerics;
|
||||
import org.ofbiz.entity.GenericEntity;
|
||||
import org.ofbiz.minilang.MiniLangUtil;
|
||||
|
||||
/**
|
||||
* Implements a comparison - consisting of an l-value, an r-value, and a comparison operator.
|
||||
*/
|
||||
public abstract class Compare {
|
||||
|
||||
private static final Map<String, Compare> INSTANCE_MAP = createInstanceMap();
|
||||
|
||||
private static void assertValuesNotNull(Object lValue, Object rValue) {
|
||||
if (lValue == null) {
|
||||
throw new IllegalArgumentException("Cannot compare: l-value is null");
|
||||
}
|
||||
if (rValue == null) {
|
||||
throw new IllegalArgumentException("Cannot compare: r-value is null");
|
||||
}
|
||||
}
|
||||
|
||||
private static int compareBigDecimals(BigDecimal lBigDecimal, BigDecimal rBigDecimal) {
|
||||
// Developers: Do not change this. We are comparing two fixed-point decimal numbers,
|
||||
// not performing accounting calculations - so it is okay to specify the rounding mode.
|
||||
int decimals = lBigDecimal.scale();
|
||||
if (rBigDecimal.scale() < decimals) {
|
||||
lBigDecimal = lBigDecimal.setScale(rBigDecimal.scale(), RoundingMode.UP);
|
||||
} else if (decimals < rBigDecimal.scale()) {
|
||||
rBigDecimal = rBigDecimal.setScale(decimals, RoundingMode.UP);
|
||||
}
|
||||
return lBigDecimal.compareTo(rBigDecimal);
|
||||
}
|
||||
|
||||
private static Map<String, Compare> createInstanceMap() {
|
||||
Map<String, Compare> writableMap = new HashMap<String, Compare>(10);
|
||||
writableMap.put("contains", new CompareContains());
|
||||
writableMap.put("equals", new CompareEquals());
|
||||
writableMap.put("greater", new CompareGreater());
|
||||
writableMap.put("greater-equals", new CompareGreaterEquals());
|
||||
writableMap.put("is-empty", new CompareIsEmpty());
|
||||
writableMap.put("is-not-null", new CompareIsNotNull());
|
||||
writableMap.put("is-null", new CompareIsNull());
|
||||
writableMap.put("less", new CompareLess());
|
||||
writableMap.put("less-equals", new CompareLessEquals());
|
||||
writableMap.put("not-equals", new CompareNotEquals());
|
||||
return Collections.unmodifiableMap(writableMap);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a <code>Compare</code> instance for the specified operator.
|
||||
*
|
||||
* @param operator
|
||||
* @return A <code>Compare</code> instance for the specified operator
|
||||
*/
|
||||
public static Compare getInstance(String operator) {
|
||||
Assert.notNull("operator", operator);
|
||||
return INSTANCE_MAP.get(operator);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the result of this comparison.
|
||||
*
|
||||
* @param lValue The object being compared
|
||||
* @param rValue The object being compared to
|
||||
* @param type The Java class to be used in the comparison
|
||||
* @param format Optional format to be used in object type conversions
|
||||
* @param locale Optional locale to be used in object type conversions
|
||||
* @param timeZone Optional time zone to be used in object type conversions
|
||||
* @return The result of this comparison
|
||||
* @throws Exception
|
||||
*/
|
||||
public abstract boolean doCompare(Object lValue, Object rValue, Class<?> type, Locale locale, TimeZone timeZone, String format) throws Exception;
|
||||
|
||||
private static final class CompareContains extends Compare {
|
||||
|
||||
@Override
|
||||
public boolean doCompare(Object lValue, Object rValue, Class<?> type, Locale locale, TimeZone timeZone, String format) throws Exception {
|
||||
// The type parameter is ignored when using the contains operator, so no conversions will be performed.
|
||||
if (lValue == null || lValue == GenericEntity.NULL_FIELD) {
|
||||
return false;
|
||||
}
|
||||
if (lValue instanceof Collection) {
|
||||
Collection<Object> collection = UtilGenerics.checkCollection(lValue);
|
||||
return collection.contains(rValue);
|
||||
}
|
||||
if (lValue instanceof String && rValue instanceof String) {
|
||||
return ((String) lValue).contains((String) rValue);
|
||||
}
|
||||
throw new IllegalArgumentException("Cannot compare: l-value is not a collection");
|
||||
}
|
||||
}
|
||||
|
||||
private static final class CompareEquals extends Compare {
|
||||
|
||||
@Override
|
||||
public boolean doCompare(Object lValue, Object rValue, Class<?> type, Locale locale, TimeZone timeZone, String format) throws Exception {
|
||||
Object convertedLvalue = MiniLangUtil.convertType(lValue, type, locale, timeZone, format);
|
||||
Object convertedRvalue = MiniLangUtil.convertType(rValue, type, locale, timeZone, format);
|
||||
if (convertedLvalue == null) {
|
||||
return convertedRvalue == null;
|
||||
}
|
||||
if (convertedRvalue == null) {
|
||||
return false;
|
||||
}
|
||||
if (convertedLvalue instanceof BigDecimal &&
|
||||
convertedRvalue instanceof BigDecimal) {
|
||||
BigDecimal lBigDecimal = (BigDecimal) convertedLvalue;
|
||||
BigDecimal rBigDecimal = (BigDecimal) convertedRvalue;
|
||||
return compareBigDecimals(lBigDecimal, rBigDecimal) == 0;
|
||||
}
|
||||
if (convertedLvalue instanceof Comparable &&
|
||||
convertedRvalue instanceof Comparable) {
|
||||
Comparable<Object> comparable = UtilGenerics.cast(convertedLvalue);
|
||||
return comparable.compareTo(convertedRvalue) == 0;
|
||||
}
|
||||
return convertedLvalue.equals(convertedRvalue);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class CompareGreater extends Compare {
|
||||
|
||||
@Override
|
||||
public boolean doCompare(Object lValue, Object rValue, Class<?> type, Locale locale, TimeZone timeZone, String format) throws Exception {
|
||||
Object convertedLvalue = MiniLangUtil.convertType(lValue, type, locale, timeZone, format);
|
||||
Object convertedRvalue = MiniLangUtil.convertType(rValue, type, locale, timeZone, format);
|
||||
assertValuesNotNull(convertedLvalue, convertedRvalue);
|
||||
if (convertedLvalue instanceof BigDecimal &&
|
||||
convertedRvalue instanceof BigDecimal) {
|
||||
BigDecimal lBigDecimal = (BigDecimal) convertedLvalue;
|
||||
BigDecimal rBigDecimal = (BigDecimal) convertedRvalue;
|
||||
return compareBigDecimals(lBigDecimal, rBigDecimal) > 0;
|
||||
}
|
||||
if (convertedLvalue instanceof Comparable &&
|
||||
convertedRvalue instanceof Comparable) {
|
||||
Comparable<Object> comparable = UtilGenerics.cast(convertedLvalue);
|
||||
return comparable.compareTo(convertedRvalue) > 0;
|
||||
}
|
||||
throw new IllegalArgumentException("Cannot compare: l-value is not a comparable type");
|
||||
}
|
||||
}
|
||||
|
||||
private static final class CompareGreaterEquals extends Compare {
|
||||
|
||||
@Override
|
||||
public boolean doCompare(Object lValue, Object rValue, Class<?> type, Locale locale, TimeZone timeZone, String format) throws Exception {
|
||||
Object convertedLvalue = MiniLangUtil.convertType(lValue, type, locale, timeZone, format);
|
||||
Object convertedRvalue = MiniLangUtil.convertType(rValue, type, locale, timeZone, format);
|
||||
assertValuesNotNull(convertedLvalue, convertedRvalue);
|
||||
if (convertedLvalue instanceof BigDecimal &&
|
||||
convertedRvalue instanceof BigDecimal) {
|
||||
BigDecimal lBigDecimal = (BigDecimal) convertedLvalue;
|
||||
BigDecimal rBigDecimal = (BigDecimal) convertedRvalue;
|
||||
return compareBigDecimals(lBigDecimal, rBigDecimal) >= 0;
|
||||
}
|
||||
if (convertedLvalue instanceof Comparable &&
|
||||
convertedRvalue instanceof Comparable) {
|
||||
Comparable<Object> comparable = UtilGenerics.cast(convertedLvalue);
|
||||
return comparable.compareTo(convertedRvalue) >= 0;
|
||||
}
|
||||
throw new IllegalArgumentException("Cannot compare: l-value is not a comparable type");
|
||||
}
|
||||
}
|
||||
|
||||
private static final class CompareIsEmpty extends Compare {
|
||||
|
||||
@Override
|
||||
public boolean doCompare(Object lValue, Object rValue, Class<?> type, Locale locale, TimeZone timeZone, String format) throws Exception {
|
||||
Object convertedLvalue = MiniLangUtil.convertType(lValue, type, locale, timeZone, format);
|
||||
return ObjectType.isEmpty(convertedLvalue);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class CompareIsNotNull extends Compare {
|
||||
|
||||
@Override
|
||||
public boolean doCompare(Object lValue, Object rValue, Class<?> type, Locale locale, TimeZone timeZone, String format) throws Exception {
|
||||
Object convertedLvalue = MiniLangUtil.convertType(lValue, type, locale, timeZone, format);
|
||||
return convertedLvalue != null;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class CompareIsNull extends Compare {
|
||||
|
||||
@Override
|
||||
public boolean doCompare(Object lValue, Object rValue, Class<?> type, Locale locale, TimeZone timeZone, String format) throws Exception {
|
||||
Object convertedLvalue = MiniLangUtil.convertType(lValue, type, locale, timeZone, format);
|
||||
return convertedLvalue == null;
|
||||
}
|
||||
}
|
||||
|
||||
private static final class CompareLess extends Compare {
|
||||
|
||||
@Override
|
||||
public boolean doCompare(Object lValue, Object rValue, Class<?> type, Locale locale, TimeZone timeZone, String format) throws Exception {
|
||||
Object convertedLvalue = MiniLangUtil.convertType(lValue, type, locale, timeZone, format);
|
||||
Object convertedRvalue = MiniLangUtil.convertType(rValue, type, locale, timeZone, format);
|
||||
assertValuesNotNull(convertedLvalue, convertedRvalue);
|
||||
if (convertedLvalue instanceof BigDecimal &&
|
||||
convertedRvalue instanceof BigDecimal) {
|
||||
BigDecimal lBigDecimal = (BigDecimal) convertedLvalue;
|
||||
BigDecimal rBigDecimal = (BigDecimal) convertedRvalue;
|
||||
return compareBigDecimals(lBigDecimal, rBigDecimal) < 0;
|
||||
}
|
||||
if (convertedLvalue instanceof Comparable &&
|
||||
convertedRvalue instanceof Comparable) {
|
||||
Comparable<Object> comparable = UtilGenerics.cast(convertedLvalue);
|
||||
return comparable.compareTo(convertedRvalue) < 0;
|
||||
}
|
||||
throw new IllegalArgumentException("Cannot compare: l-value is not a comparable type");
|
||||
}
|
||||
}
|
||||
|
||||
private static final class CompareLessEquals extends Compare {
|
||||
|
||||
@Override
|
||||
public boolean doCompare(Object lValue, Object rValue, Class<?> type, Locale locale, TimeZone timeZone, String format) throws Exception {
|
||||
Object convertedLvalue = MiniLangUtil.convertType(lValue, type, locale, timeZone, format);
|
||||
Object convertedRvalue = MiniLangUtil.convertType(rValue, type, locale, timeZone, format);
|
||||
assertValuesNotNull(convertedLvalue, convertedRvalue);
|
||||
if (convertedLvalue instanceof BigDecimal &&
|
||||
convertedRvalue instanceof BigDecimal) {
|
||||
BigDecimal lBigDecimal = (BigDecimal) convertedLvalue;
|
||||
BigDecimal rBigDecimal = (BigDecimal) convertedRvalue;
|
||||
return compareBigDecimals(lBigDecimal, rBigDecimal) <= 0;
|
||||
}
|
||||
if (convertedLvalue instanceof Comparable &&
|
||||
convertedRvalue instanceof Comparable) {
|
||||
Comparable<Object> comparable = UtilGenerics.cast(convertedLvalue);
|
||||
return comparable.compareTo(convertedRvalue) <= 0;
|
||||
}
|
||||
throw new IllegalArgumentException("Cannot compare: l-value is not a comparable type");
|
||||
}
|
||||
}
|
||||
|
||||
private static final class CompareNotEquals extends Compare {
|
||||
|
||||
@Override
|
||||
public boolean doCompare(Object lValue, Object rValue, Class<?> type, Locale locale, TimeZone timeZone, String format) throws Exception {
|
||||
Object convertedLvalue = MiniLangUtil.convertType(lValue, type, locale, timeZone, format);
|
||||
Object convertedRvalue = MiniLangUtil.convertType(rValue, type, locale, timeZone, format);
|
||||
if (convertedLvalue == null) {
|
||||
return convertedRvalue != null;
|
||||
}
|
||||
if (convertedRvalue == null) {
|
||||
return true;
|
||||
}
|
||||
if (convertedLvalue instanceof BigDecimal &&
|
||||
convertedRvalue instanceof BigDecimal) {
|
||||
BigDecimal lBigDecimal = (BigDecimal) convertedLvalue;
|
||||
BigDecimal rBigDecimal = (BigDecimal) convertedRvalue;
|
||||
return compareBigDecimals(lBigDecimal, rBigDecimal) != 0;
|
||||
}
|
||||
if (convertedLvalue instanceof Comparable &&
|
||||
convertedRvalue instanceof Comparable) {
|
||||
Comparable<Object> comparable = UtilGenerics.cast(convertedLvalue);
|
||||
return comparable.compareTo(convertedRvalue) != 0;
|
||||
}
|
||||
return !convertedLvalue.equals(convertedRvalue);
|
||||
}
|
||||
}
|
||||
}
|
||||
+208
@@ -0,0 +1,208 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.conditional;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.ofbiz.base.util.ObjectType;
|
||||
import org.ofbiz.base.util.UtilValidate;
|
||||
import org.ofbiz.base.util.UtilXml;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangUtil;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <if-compare> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cifcompare%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class CompareCondition extends MethodOperation implements Conditional {
|
||||
|
||||
private final Compare compare;
|
||||
private final FlexibleMapAccessor<Object> fieldFma;
|
||||
private final FlexibleStringExpander formatFse;
|
||||
private final String operator;
|
||||
private final Class<?> targetClass;
|
||||
private final String type;
|
||||
private final FlexibleStringExpander valueFse;
|
||||
// Sub-operations are used only when this is a method operation.
|
||||
private final List<MethodOperation> elseSubOps;
|
||||
private final List<MethodOperation> subOps;
|
||||
|
||||
public CompareCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "field", "format", "operator", "type", "value");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "field", "operator", "value");
|
||||
MiniLangValidate.constantAttributes(simpleMethod, element, "operator", "type");
|
||||
MiniLangValidate.constantPlusExpressionAttributes(simpleMethod, element, "value");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "field");
|
||||
}
|
||||
this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field"));
|
||||
this.formatFse = FlexibleStringExpander.getInstance(element.getAttribute("format"));
|
||||
this.operator = element.getAttribute("operator");
|
||||
this.compare = Compare.getInstance(this.operator);
|
||||
if (this.compare == null) {
|
||||
MiniLangValidate.handleError("Invalid operator " + this.operator, simpleMethod, element);
|
||||
}
|
||||
this.type = element.getAttribute("type");
|
||||
Class<?> targetClass = null;
|
||||
if (!this.type.isEmpty()) {
|
||||
if ("contains".equals(this.operator)) {
|
||||
MiniLangValidate.handleError("Operator \"contains\" does not support type conversions (remove the type attribute).", simpleMethod, element);
|
||||
targetClass = Object.class;
|
||||
} else {
|
||||
try {
|
||||
targetClass = ObjectType.loadClass(this.type);
|
||||
} catch (ClassNotFoundException e) {
|
||||
MiniLangValidate.handleError("Invalid type " + this.type, simpleMethod, element);
|
||||
}
|
||||
}
|
||||
}
|
||||
this.targetClass = targetClass;
|
||||
this.valueFse = FlexibleStringExpander.getInstance(element.getAttribute("value"));
|
||||
Element childElement = UtilXml.firstChildElement(element);
|
||||
if (childElement != null && !"else".equals(childElement.getTagName())) {
|
||||
this.subOps = Collections.unmodifiableList(SimpleMethod.readOperations(element, simpleMethod));
|
||||
} else {
|
||||
this.subOps = null;
|
||||
}
|
||||
Element elseElement = UtilXml.firstChildElement(element, "else");
|
||||
if (elseElement != null) {
|
||||
this.elseSubOps = Collections.unmodifiableList(SimpleMethod.readOperations(elseElement, simpleMethod));
|
||||
} else {
|
||||
this.elseSubOps = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkCondition(MethodContext methodContext) throws MiniLangException {
|
||||
if (this.compare == null) {
|
||||
throw new MiniLangRuntimeException("Invalid operator \"" + this.operator + "\"", this);
|
||||
}
|
||||
Object fieldVal = fieldFma.get(methodContext.getEnvMap());
|
||||
Class<?> targetClass = this.targetClass;
|
||||
if (targetClass == null) {
|
||||
targetClass = MiniLangUtil.getObjectClassForConversion(fieldVal);
|
||||
}
|
||||
String value = valueFse.expandString(methodContext.getEnvMap());
|
||||
String format = formatFse.expandString(methodContext.getEnvMap());
|
||||
try {
|
||||
// We use en locale here so constant (literal) values are converted properly.
|
||||
return this.compare.doCompare(fieldVal, value, targetClass, Locale.ENGLISH, methodContext.getTimeZone(), format);
|
||||
} catch (Exception e) {
|
||||
simpleMethod.addErrorMessage(methodContext, e.getMessage());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
if (checkCondition(methodContext)) {
|
||||
if (this.subOps != null) {
|
||||
return SimpleMethod.runSubOps(subOps, methodContext);
|
||||
}
|
||||
} else {
|
||||
if (elseSubOps != null) {
|
||||
return SimpleMethod.runSubOps(elseSubOps, methodContext);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
if (this.subOps != null) {
|
||||
for (MethodOperation method : this.subOps) {
|
||||
method.gatherArtifactInfo(aic);
|
||||
}
|
||||
}
|
||||
if (this.elseSubOps != null) {
|
||||
for (MethodOperation method : this.elseSubOps) {
|
||||
method.gatherArtifactInfo(aic);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void prettyPrint(StringBuilder messageBuffer, MethodContext methodContext) {
|
||||
String value = valueFse.expandString(methodContext.getEnvMap());
|
||||
String format = formatFse.expandString(methodContext.getEnvMap());
|
||||
Object fieldVal = fieldFma.get(methodContext.getEnvMap());
|
||||
messageBuffer.append("[");
|
||||
messageBuffer.append(this.fieldFma);
|
||||
messageBuffer.append("=");
|
||||
messageBuffer.append(fieldVal);
|
||||
messageBuffer.append("] ");
|
||||
messageBuffer.append(operator);
|
||||
messageBuffer.append(" ");
|
||||
messageBuffer.append(value);
|
||||
messageBuffer.append(" as ");
|
||||
messageBuffer.append(type);
|
||||
if (UtilValidate.isNotEmpty(format)) {
|
||||
messageBuffer.append(":");
|
||||
messageBuffer.append(format);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<if-compare ");
|
||||
sb.append("field=\"").append(this.fieldFma).append("\" operator=\"").append(operator).append("\" ");
|
||||
if (!this.valueFse.isEmpty()) {
|
||||
sb.append("value=\"").append(this.valueFse).append("\" ");
|
||||
}
|
||||
if (!this.type.isEmpty()) {
|
||||
sb.append("type=\"").append(this.type).append("\" ");
|
||||
}
|
||||
if (!this.formatFse.isEmpty()) {
|
||||
sb.append("format=\"").append(this.formatFse).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A <if-compare> element factory.
|
||||
*/
|
||||
public static final class CompareConditionFactory extends ConditionalFactory<CompareCondition> implements Factory<CompareCondition> {
|
||||
@Override
|
||||
public CompareCondition createCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new CompareCondition(element, simpleMethod);
|
||||
}
|
||||
|
||||
public CompareCondition createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new CompareCondition(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "if-compare";
|
||||
}
|
||||
}
|
||||
}
|
||||
+219
@@ -0,0 +1,219 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.conditional;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.util.ObjectType;
|
||||
import org.ofbiz.base.util.UtilValidate;
|
||||
import org.ofbiz.base.util.UtilXml;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangUtil;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <if-compare-field> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cifcomparefield%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class CompareFieldCondition extends MethodOperation implements Conditional {
|
||||
|
||||
// This method is needed only during the v1 to v2 transition
|
||||
private static boolean autoCorrect(Element element) {
|
||||
// Correct missing to-field attribute
|
||||
String toFieldAttr = element.getAttribute("to-field");
|
||||
if (toFieldAttr.isEmpty()) {
|
||||
element.setAttribute("to-field", element.getAttribute("field"));
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private final Compare compare;
|
||||
private final FlexibleMapAccessor<Object> fieldFma;
|
||||
private final FlexibleStringExpander formatFse;
|
||||
private final String operator;
|
||||
private final FlexibleMapAccessor<Object> toFieldFma;
|
||||
private final Class<?> targetClass;
|
||||
private final String type;
|
||||
// Sub-operations are used only when this is a method operation.
|
||||
private final List<MethodOperation> elseSubOps;
|
||||
private final List<MethodOperation> subOps;
|
||||
|
||||
public CompareFieldCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "field", "format", "operator", "type", "to-field");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "field", "operator", "to-field");
|
||||
MiniLangValidate.constantAttributes(simpleMethod, element, "operator", "type");
|
||||
MiniLangValidate.constantPlusExpressionAttributes(simpleMethod, element, "format");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "field", "to-field");
|
||||
}
|
||||
boolean elementModified = autoCorrect(element);
|
||||
if (elementModified && MiniLangUtil.autoCorrectOn()) {
|
||||
MiniLangUtil.flagDocumentAsCorrected(element);
|
||||
}
|
||||
this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field"));
|
||||
this.formatFse = FlexibleStringExpander.getInstance(element.getAttribute("format"));
|
||||
this.operator = element.getAttribute("operator");
|
||||
this.compare = Compare.getInstance(this.operator);
|
||||
if (this.compare == null) {
|
||||
MiniLangValidate.handleError("Invalid operator " + this.operator, simpleMethod, element);
|
||||
}
|
||||
this.toFieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("to-field"));
|
||||
this.type = element.getAttribute("type");
|
||||
Class<?> targetClass = null;
|
||||
if (!this.type.isEmpty()) {
|
||||
try {
|
||||
targetClass = ObjectType.loadClass(this.type);
|
||||
} catch (ClassNotFoundException e) {
|
||||
MiniLangValidate.handleError("Invalid type " + this.type, simpleMethod, element);
|
||||
}
|
||||
}
|
||||
this.targetClass = targetClass;
|
||||
Element childElement = UtilXml.firstChildElement(element);
|
||||
if (childElement != null && !"else".equals(childElement.getTagName())) {
|
||||
this.subOps = Collections.unmodifiableList(SimpleMethod.readOperations(element, simpleMethod));
|
||||
} else {
|
||||
this.subOps = null;
|
||||
}
|
||||
Element elseElement = UtilXml.firstChildElement(element, "else");
|
||||
if (elseElement != null) {
|
||||
this.elseSubOps = Collections.unmodifiableList(SimpleMethod.readOperations(elseElement, simpleMethod));
|
||||
} else {
|
||||
this.elseSubOps = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkCondition(MethodContext methodContext) throws MiniLangException {
|
||||
if (this.compare == null) {
|
||||
throw new MiniLangRuntimeException("Invalid operator " + this.operator, this);
|
||||
}
|
||||
Object fieldVal = fieldFma.get(methodContext.getEnvMap());
|
||||
Object toFieldVal = toFieldFma.get(methodContext.getEnvMap());
|
||||
Class<?> targetClass = this.targetClass;
|
||||
if (targetClass == null) {
|
||||
targetClass = MiniLangUtil.getObjectClassForConversion(fieldVal);
|
||||
}
|
||||
String format = formatFse.expandString(methodContext.getEnvMap());
|
||||
try {
|
||||
return this.compare.doCompare(fieldVal, toFieldVal, targetClass, methodContext.getLocale(), methodContext.getTimeZone(), format);
|
||||
} catch (Exception e) {
|
||||
simpleMethod.addErrorMessage(methodContext, e.getMessage());
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
if (checkCondition(methodContext)) {
|
||||
if (this.subOps != null) {
|
||||
return SimpleMethod.runSubOps(subOps, methodContext);
|
||||
}
|
||||
} else {
|
||||
if (elseSubOps != null) {
|
||||
return SimpleMethod.runSubOps(elseSubOps, methodContext);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
if (this.subOps != null) {
|
||||
for (MethodOperation method : this.subOps) {
|
||||
method.gatherArtifactInfo(aic);
|
||||
}
|
||||
}
|
||||
if (this.elseSubOps != null) {
|
||||
for (MethodOperation method : this.elseSubOps) {
|
||||
method.gatherArtifactInfo(aic);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void prettyPrint(StringBuilder messageBuffer, MethodContext methodContext) {
|
||||
String format = formatFse.expandString(methodContext.getEnvMap());
|
||||
Object fieldVal = fieldFma.get(methodContext.getEnvMap());
|
||||
Object toFieldVal = toFieldFma.get(methodContext.getEnvMap());
|
||||
messageBuffer.append("[");
|
||||
messageBuffer.append(fieldFma);
|
||||
messageBuffer.append("=");
|
||||
messageBuffer.append(fieldVal);
|
||||
messageBuffer.append("] ");
|
||||
messageBuffer.append(operator);
|
||||
messageBuffer.append(" [");
|
||||
messageBuffer.append(toFieldFma);
|
||||
messageBuffer.append("=");
|
||||
messageBuffer.append(toFieldVal);
|
||||
messageBuffer.append("] ");
|
||||
messageBuffer.append(" as ");
|
||||
messageBuffer.append(type);
|
||||
if (UtilValidate.isNotEmpty(format)) {
|
||||
messageBuffer.append(":");
|
||||
messageBuffer.append(format);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<if-compare-field ");
|
||||
sb.append("field=\"").append(this.fieldFma).append("\" operator=\"").append(operator).append("\" ");
|
||||
if (!this.toFieldFma.isEmpty()) {
|
||||
sb.append("to-field=\"").append(this.toFieldFma).append("\" ");
|
||||
}
|
||||
if (!this.type.isEmpty()) {
|
||||
sb.append("type=\"").append(this.type).append("\" ");
|
||||
}
|
||||
if (!this.formatFse.isEmpty()) {
|
||||
sb.append("format=\"").append(this.formatFse).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A <if-compare-field> element factory.
|
||||
*/
|
||||
public static final class CompareFieldConditionFactory extends ConditionalFactory<CompareFieldCondition> implements Factory<CompareFieldCondition> {
|
||||
@Override
|
||||
public CompareFieldCondition createCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new CompareFieldCondition(element, simpleMethod);
|
||||
}
|
||||
|
||||
public CompareFieldCondition createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new CompareFieldCondition(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "if-compare-field";
|
||||
}
|
||||
}
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.conditional;
|
||||
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
|
||||
/**
|
||||
* Interface for all conditional elements under the <if> element.
|
||||
*/
|
||||
public interface Conditional {
|
||||
|
||||
public boolean checkCondition(MethodContext methodContext) throws MiniLangException;
|
||||
|
||||
public void prettyPrint(StringBuilder messageBuffer, MethodContext methodContext);
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.conditional;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.ServiceLoader;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.UtilGenerics;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* An abstract factory class for creating <if> element sub-element implementations.
|
||||
* <p>Mini-language can be extended to support additional condition elements
|
||||
* by extending this class to provide custom conditional element implementations.
|
||||
* </p>
|
||||
*/
|
||||
public abstract class ConditionalFactory<C extends Conditional> {
|
||||
|
||||
public static final String module = ConditionalFactory.class.getName();
|
||||
private static final Map<String, ConditionalFactory<?>> conditionalFactories;
|
||||
|
||||
static {
|
||||
Map<String, ConditionalFactory<?>> factories = new HashMap<String, ConditionalFactory<?>>();
|
||||
Iterator<ConditionalFactory<?>> it = UtilGenerics.cast(ServiceLoader.load(ConditionalFactory.class, ConditionalFactory.class.getClassLoader()).iterator());
|
||||
while (it.hasNext()) {
|
||||
ConditionalFactory<?> factory = it.next();
|
||||
factories.put(factory.getName(), factory);
|
||||
}
|
||||
conditionalFactories = Collections.unmodifiableMap(factories);
|
||||
}
|
||||
|
||||
public static Conditional makeConditional(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
String tagName = element.getTagName();
|
||||
ConditionalFactory<?> factory = conditionalFactories.get(tagName);
|
||||
if (factory != null) {
|
||||
return factory.createCondition(element, simpleMethod);
|
||||
} else {
|
||||
Debug.logWarning("Found an unknown if condition: " + tagName, module);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public abstract C createCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException;
|
||||
|
||||
public abstract String getName();
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.conditional;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.util.UtilXml;
|
||||
import org.ofbiz.minilang.MiniLangElement;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <else-if> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Celseif%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class ElseIf extends MiniLangElement {
|
||||
|
||||
private final Conditional condition;
|
||||
private final List<MethodOperation> thenSubOps;
|
||||
|
||||
public ElseIf(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.childElements(simpleMethod, element, "condition", "then");
|
||||
MiniLangValidate.requiredChildElements(simpleMethod, element, "condition", "then");
|
||||
}
|
||||
Element conditionElement = UtilXml.firstChildElement(element, "condition");
|
||||
Element conditionChildElement = UtilXml.firstChildElement(conditionElement);
|
||||
this.condition = ConditionalFactory.makeConditional(conditionChildElement, simpleMethod);
|
||||
Element thenElement = UtilXml.firstChildElement(element, "then");
|
||||
this.thenSubOps = Collections.unmodifiableList(SimpleMethod.readOperations(thenElement, simpleMethod));
|
||||
}
|
||||
|
||||
public boolean checkCondition(MethodContext methodContext) throws MiniLangException {
|
||||
return condition.checkCondition(methodContext);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
for (MethodOperation method : this.thenSubOps) {
|
||||
method.gatherArtifactInfo(aic);
|
||||
}
|
||||
}
|
||||
|
||||
public List<MethodOperation> getThenSubOps() {
|
||||
return this.thenSubOps;
|
||||
}
|
||||
|
||||
public boolean runSubOps(MethodContext methodContext) throws MiniLangException {
|
||||
return SimpleMethod.runSubOps(thenSubOps, methodContext);
|
||||
}
|
||||
}
|
||||
+137
@@ -0,0 +1,137 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.conditional;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.util.ObjectType;
|
||||
import org.ofbiz.base.util.UtilXml;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <if-empty> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cifempty%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class EmptyCondition extends MethodOperation implements Conditional {
|
||||
|
||||
public static final String module = EmptyCondition.class.getName();
|
||||
|
||||
private final FlexibleMapAccessor<Object> fieldFma;
|
||||
// Sub-operations are used only when this is a method operation.
|
||||
private final List<MethodOperation> elseSubOps;
|
||||
private final List<MethodOperation> subOps;
|
||||
|
||||
public EmptyCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "field");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "field");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "field");
|
||||
}
|
||||
this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field"));
|
||||
Element childElement = UtilXml.firstChildElement(element);
|
||||
if (childElement != null && !"else".equals(childElement.getTagName())) {
|
||||
this.subOps = Collections.unmodifiableList(SimpleMethod.readOperations(element, simpleMethod));
|
||||
} else {
|
||||
this.subOps = null;
|
||||
}
|
||||
Element elseElement = UtilXml.firstChildElement(element, "else");
|
||||
if (elseElement != null) {
|
||||
this.elseSubOps = Collections.unmodifiableList(SimpleMethod.readOperations(elseElement, simpleMethod));
|
||||
} else {
|
||||
this.elseSubOps = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkCondition(MethodContext methodContext) throws MiniLangException {
|
||||
return ObjectType.isEmpty(fieldFma.get(methodContext.getEnvMap()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
if (checkCondition(methodContext)) {
|
||||
if (this.subOps != null) {
|
||||
return SimpleMethod.runSubOps(subOps, methodContext);
|
||||
}
|
||||
} else {
|
||||
if (elseSubOps != null) {
|
||||
return SimpleMethod.runSubOps(elseSubOps, methodContext);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
if (this.subOps != null) {
|
||||
for (MethodOperation method : this.subOps) {
|
||||
method.gatherArtifactInfo(aic);
|
||||
}
|
||||
}
|
||||
if (this.elseSubOps != null) {
|
||||
for (MethodOperation method : this.elseSubOps) {
|
||||
method.gatherArtifactInfo(aic);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void prettyPrint(StringBuilder messageBuffer, MethodContext methodContext) {
|
||||
messageBuffer.append("empty[");
|
||||
messageBuffer.append(fieldFma);
|
||||
messageBuffer.append("=");
|
||||
messageBuffer.append(fieldFma.get(methodContext.getEnvMap()));
|
||||
messageBuffer.append("]");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<if-empty ");
|
||||
sb.append("field=\"").append(this.fieldFma).append("\"/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A <if-empty> element factory.
|
||||
*/
|
||||
public static final class EmptyConditionFactory extends ConditionalFactory<EmptyCondition> implements Factory<EmptyCondition> {
|
||||
@Override
|
||||
public EmptyCondition createCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new EmptyCondition(element, simpleMethod);
|
||||
}
|
||||
|
||||
public EmptyCondition createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new EmptyCondition(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "if-empty";
|
||||
}
|
||||
}
|
||||
}
|
||||
+163
@@ -0,0 +1,163 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.conditional;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.util.UtilValidate;
|
||||
import org.ofbiz.base.util.UtilXml;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.entity.GenericValue;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.ofbiz.security.Security;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <if-has-permission> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cifhaspermission%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class HasPermissionCondition extends MethodOperation implements Conditional {
|
||||
|
||||
private final FlexibleStringExpander actionFse;
|
||||
private final FlexibleStringExpander permissionFse;
|
||||
// Sub-operations are used only when this is a method operation.
|
||||
private final List<MethodOperation> elseSubOps;
|
||||
private final List<MethodOperation> subOps;
|
||||
|
||||
public HasPermissionCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "permission", "action");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "permission");
|
||||
MiniLangValidate.constantPlusExpressionAttributes(simpleMethod, element, "permission", "action");
|
||||
}
|
||||
this.permissionFse = FlexibleStringExpander.getInstance(element.getAttribute("permission"));
|
||||
this.actionFse = FlexibleStringExpander.getInstance(element.getAttribute("action"));
|
||||
Element childElement = UtilXml.firstChildElement(element);
|
||||
if (childElement != null && !"else".equals(childElement.getTagName())) {
|
||||
this.subOps = Collections.unmodifiableList(SimpleMethod.readOperations(element, simpleMethod));
|
||||
} else {
|
||||
this.subOps = null;
|
||||
}
|
||||
Element elseElement = UtilXml.firstChildElement(element, "else");
|
||||
if (elseElement != null) {
|
||||
this.elseSubOps = Collections.unmodifiableList(SimpleMethod.readOperations(elseElement, simpleMethod));
|
||||
} else {
|
||||
this.elseSubOps = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkCondition(MethodContext methodContext) throws MiniLangException {
|
||||
GenericValue userLogin = methodContext.getUserLogin();
|
||||
if (userLogin != null) {
|
||||
Security security = methodContext.getSecurity();
|
||||
String permission = permissionFse.expandString(methodContext.getEnvMap());
|
||||
String action = actionFse.expandString(methodContext.getEnvMap());
|
||||
if (!action.isEmpty()) {
|
||||
if (security.hasEntityPermission(permission, action, userLogin)) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
if (security.hasPermission(permission, userLogin)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
if (checkCondition(methodContext)) {
|
||||
if (this.subOps != null) {
|
||||
return SimpleMethod.runSubOps(subOps, methodContext);
|
||||
}
|
||||
} else {
|
||||
if (elseSubOps != null) {
|
||||
return SimpleMethod.runSubOps(elseSubOps, methodContext);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
if (this.subOps != null) {
|
||||
for (MethodOperation method : this.subOps) {
|
||||
method.gatherArtifactInfo(aic);
|
||||
}
|
||||
}
|
||||
if (this.elseSubOps != null) {
|
||||
for (MethodOperation method : this.elseSubOps) {
|
||||
method.gatherArtifactInfo(aic);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void prettyPrint(StringBuilder messageBuffer, MethodContext methodContext) {
|
||||
messageBuffer.append("has-permission[");
|
||||
messageBuffer.append(this.permissionFse);
|
||||
if (UtilValidate.isNotEmpty(this.actionFse)) {
|
||||
messageBuffer.append(":");
|
||||
messageBuffer.append(this.actionFse);
|
||||
}
|
||||
messageBuffer.append("]");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<if-has-permission ");
|
||||
if (!this.permissionFse.isEmpty()) {
|
||||
sb.append("permission=\"").append(this.permissionFse).append("\" ");
|
||||
}
|
||||
if (!this.actionFse.isEmpty()) {
|
||||
sb.append("action=\"").append(this.actionFse).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A <if-has-permission> element factory.
|
||||
*/
|
||||
public static final class HasPermissionConditionFactory extends ConditionalFactory<HasPermissionCondition> implements Factory<HasPermissionCondition> {
|
||||
@Override
|
||||
public HasPermissionCondition createCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new HasPermissionCondition(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public HasPermissionCondition createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new HasPermissionCondition(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "if-has-permission";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.conditional;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.util.UtilXml;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <if> element.
|
||||
*/
|
||||
public final class MasterIf extends MethodOperation {
|
||||
|
||||
private final Conditional condition;
|
||||
private final List<ElseIf> elseIfs;
|
||||
private final List<MethodOperation> elseSubOps;
|
||||
private final List<MethodOperation> thenSubOps;
|
||||
|
||||
public MasterIf(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.childElements(simpleMethod, element, "condition", "then", "else-if", "else");
|
||||
MiniLangValidate.requiredChildElements(simpleMethod, element, "condition", "then");
|
||||
}
|
||||
Element conditionElement = UtilXml.firstChildElement(element, "condition");
|
||||
Element conditionChildElement = UtilXml.firstChildElement(conditionElement);
|
||||
this.condition = ConditionalFactory.makeConditional(conditionChildElement, simpleMethod);
|
||||
Element thenElement = UtilXml.firstChildElement(element, "then");
|
||||
this.thenSubOps = Collections.unmodifiableList(SimpleMethod.readOperations(thenElement, simpleMethod));
|
||||
List<? extends Element> elseIfElements = UtilXml.childElementList(element, "else-if");
|
||||
if (elseIfElements.isEmpty()) {
|
||||
this.elseIfs = null;
|
||||
} else {
|
||||
List<ElseIf> elseIfs = new ArrayList<ElseIf>(elseIfElements.size());
|
||||
for (Element elseIfElement : elseIfElements) {
|
||||
elseIfs.add(new ElseIf(elseIfElement, simpleMethod));
|
||||
}
|
||||
this.elseIfs = Collections.unmodifiableList(elseIfs);
|
||||
}
|
||||
Element elseElement = UtilXml.firstChildElement(element, "else");
|
||||
if (elseElement == null) {
|
||||
this.elseSubOps = null;
|
||||
} else {
|
||||
this.elseSubOps = Collections.unmodifiableList(SimpleMethod.readOperations(elseElement, simpleMethod));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
// if conditions fails, always return true; if a sub-op returns false
|
||||
// return false and stop, otherwise return true
|
||||
// return true;
|
||||
// only run subOps if element is empty/null
|
||||
boolean runSubOps = condition.checkCondition(methodContext);
|
||||
if (runSubOps) {
|
||||
return SimpleMethod.runSubOps(thenSubOps, methodContext);
|
||||
} else {
|
||||
// try the else-ifs
|
||||
if (elseIfs != null) {
|
||||
for (ElseIf elseIf : elseIfs) {
|
||||
if (elseIf.checkCondition(methodContext)) {
|
||||
return elseIf.runSubOps(methodContext);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (elseSubOps != null) {
|
||||
return SimpleMethod.runSubOps(elseSubOps, methodContext);
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
for (MethodOperation method : this.thenSubOps) {
|
||||
method.gatherArtifactInfo(aic);
|
||||
}
|
||||
if (this.elseSubOps != null) {
|
||||
for (MethodOperation method : this.elseSubOps) {
|
||||
method.gatherArtifactInfo(aic);
|
||||
}
|
||||
}
|
||||
if (this.elseIfs != null) {
|
||||
for (ElseIf elseIf : elseIfs) {
|
||||
elseIf.gatherArtifactInfo(aic);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder messageBuf = new StringBuilder();
|
||||
this.condition.prettyPrint(messageBuf, null);
|
||||
return "<if><condition>" + messageBuf + "</condition></if>";
|
||||
}
|
||||
|
||||
/**
|
||||
* A <if> element factory.
|
||||
*/
|
||||
public static final class MasterIfFactory implements Factory<MasterIf> {
|
||||
public MasterIf createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new MasterIf(element, simpleMethod);
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return "if";
|
||||
}
|
||||
}
|
||||
}
|
||||
+183
@@ -0,0 +1,183 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.conditional;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.oro.text.regex.MalformedPatternException;
|
||||
import org.apache.oro.text.regex.Pattern;
|
||||
import org.apache.oro.text.regex.PatternMatcher;
|
||||
import org.apache.oro.text.regex.Perl5Matcher;
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.PatternFactory;
|
||||
import org.ofbiz.base.util.UtilXml;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangUtil;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <if-regexp> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{<ifregexp>}}">Mini-language Reference</a>
|
||||
*/
|
||||
public class RegexpCondition extends MethodOperation implements Conditional {
|
||||
|
||||
public static final String module = RegexpCondition.class.getName();
|
||||
|
||||
private final FlexibleMapAccessor<Object> fieldFma;
|
||||
private final FlexibleStringExpander exprFse;
|
||||
// Sub-operations are used only when this is a method operation.
|
||||
private final List<MethodOperation> elseSubOps;
|
||||
private final List<MethodOperation> subOps;
|
||||
|
||||
public RegexpCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "field", "expr");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "field", "expr");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "field");
|
||||
}
|
||||
this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field"));
|
||||
this.exprFse = FlexibleStringExpander.getInstance(element.getAttribute("expr"));
|
||||
Element childElement = UtilXml.firstChildElement(element);
|
||||
if (childElement != null && !"else".equals(childElement.getTagName())) {
|
||||
this.subOps = Collections.unmodifiableList(SimpleMethod.readOperations(element, simpleMethod));
|
||||
} else {
|
||||
this.subOps = null;
|
||||
}
|
||||
Element elseElement = UtilXml.firstChildElement(element, "else");
|
||||
if (elseElement != null) {
|
||||
this.elseSubOps = Collections.unmodifiableList(SimpleMethod.readOperations(elseElement, simpleMethod));
|
||||
} else {
|
||||
this.elseSubOps = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkCondition(MethodContext methodContext) throws MiniLangException {
|
||||
Object fieldVal = fieldFma.get(methodContext.getEnvMap());
|
||||
if (fieldVal == null) {
|
||||
fieldVal = "";
|
||||
} else if (!(fieldVal instanceof String)) {
|
||||
try {
|
||||
fieldVal = MiniLangUtil.convertType(fieldVal, String.class, methodContext.getLocale(), methodContext.getTimeZone(), null);
|
||||
} catch (Exception e) {
|
||||
throw new MiniLangRuntimeException(e, this);
|
||||
}
|
||||
}
|
||||
String regExp = exprFse.expandString(methodContext.getEnvMap());
|
||||
Pattern pattern = null;
|
||||
|
||||
try {
|
||||
pattern = PatternFactory.createOrGetPerl5CompiledPattern(regExp, true);
|
||||
} catch (MalformedPatternException e) {
|
||||
Debug.logError(e, "Regular Expression [" + regExp + "] is mal-formed: " + e.toString(), module);
|
||||
throw new MiniLangRuntimeException(e, this);
|
||||
}
|
||||
|
||||
PatternMatcher matcher = new Perl5Matcher();
|
||||
if (matcher.matches((String) fieldVal, pattern)) {
|
||||
//Debug.logInfo("The string [" + fieldVal + "] matched the pattern expr [" + pattern.getPattern() + "]", module);
|
||||
return true;
|
||||
} else {
|
||||
//Debug.logInfo("The string [" + fieldVal + "] did NOT match the pattern expr [" + pattern.getPattern() + "]", module);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
if (checkCondition(methodContext)) {
|
||||
if (this.subOps != null) {
|
||||
return SimpleMethod.runSubOps(subOps, methodContext);
|
||||
}
|
||||
} else {
|
||||
if (elseSubOps != null) {
|
||||
return SimpleMethod.runSubOps(elseSubOps, methodContext);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
if (this.subOps != null) {
|
||||
for (MethodOperation method : this.subOps) {
|
||||
method.gatherArtifactInfo(aic);
|
||||
}
|
||||
}
|
||||
if (this.elseSubOps != null) {
|
||||
for (MethodOperation method : this.elseSubOps) {
|
||||
method.gatherArtifactInfo(aic);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void prettyPrint(StringBuilder messageBuffer, MethodContext methodContext) {
|
||||
messageBuffer.append("regexp[");
|
||||
messageBuffer.append("[");
|
||||
messageBuffer.append(this.fieldFma);
|
||||
messageBuffer.append("=");
|
||||
messageBuffer.append(fieldFma.get(methodContext.getEnvMap()));
|
||||
messageBuffer.append("] matches ");
|
||||
messageBuffer.append(exprFse.expandString(methodContext.getEnvMap()));
|
||||
messageBuffer.append("]");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<if-regexp ");
|
||||
sb.append("field=\"").append(this.fieldFma).append("\" ");
|
||||
if (!this.exprFse.isEmpty()) {
|
||||
sb.append("expr=\"").append(this.exprFse).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A <if-regexp> element factory.
|
||||
*/
|
||||
public static final class RegexpConditionFactory extends ConditionalFactory<RegexpCondition> implements Factory<RegexpCondition> {
|
||||
@Override
|
||||
public RegexpCondition createCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new RegexpCondition(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public RegexpCondition createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new RegexpCondition(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "if-regexp";
|
||||
}
|
||||
}
|
||||
}
|
||||
+177
@@ -0,0 +1,177 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.conditional;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.util.UtilXml;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangUtil;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <if-validate-method> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cifvalidatemethod%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class ValidateMethodCondition extends MethodOperation implements Conditional {
|
||||
|
||||
public static final String module = ValidateMethodCondition.class.getName();
|
||||
private static final Class<?>[] paramTypes = new Class<?>[] { String.class };
|
||||
|
||||
private final String className;
|
||||
private final FlexibleMapAccessor<Object> fieldFma;
|
||||
private final String methodName;
|
||||
// Sub-operations are used only when this is a method operation.
|
||||
private final List<MethodOperation> elseSubOps;
|
||||
private final List<MethodOperation> subOps;
|
||||
|
||||
public ValidateMethodCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "field", "method", "class");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "field", "method");
|
||||
MiniLangValidate.constantAttributes(simpleMethod, element, "method", "class");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "field");
|
||||
}
|
||||
this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field"));
|
||||
this.methodName = element.getAttribute("method");
|
||||
this.className = MiniLangValidate.checkAttribute(element.getAttribute("class"), "org.ofbiz.base.util.UtilValidate");
|
||||
Element childElement = UtilXml.firstChildElement(element);
|
||||
if (childElement != null && !"else".equals(childElement.getTagName())) {
|
||||
this.subOps = Collections.unmodifiableList(SimpleMethod.readOperations(element, simpleMethod));
|
||||
} else {
|
||||
this.subOps = null;
|
||||
}
|
||||
Element elseElement = UtilXml.firstChildElement(element, "else");
|
||||
if (elseElement != null) {
|
||||
this.elseSubOps = Collections.unmodifiableList(SimpleMethod.readOperations(elseElement, simpleMethod));
|
||||
} else {
|
||||
this.elseSubOps = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean checkCondition(MethodContext methodContext) throws MiniLangException {
|
||||
Object fieldVal = fieldFma.get(methodContext.getEnvMap());
|
||||
if (fieldVal == null) {
|
||||
fieldVal = "";
|
||||
} else if (!(fieldVal instanceof String)) {
|
||||
try {
|
||||
fieldVal = MiniLangUtil.convertType(fieldVal, String.class, methodContext.getLocale(), methodContext.getTimeZone(), null);
|
||||
} catch (Exception e) {
|
||||
throw new MiniLangRuntimeException(e, this);
|
||||
}
|
||||
}
|
||||
Object[] params = new Object[] { fieldVal };
|
||||
try {
|
||||
Class<?> valClass = methodContext.getLoader().loadClass(className);
|
||||
Method valMethod = valClass.getMethod(methodName, paramTypes);
|
||||
Boolean resultBool = (Boolean) valMethod.invoke(null, params);
|
||||
return resultBool.booleanValue();
|
||||
} catch (Exception e) {
|
||||
throw new MiniLangRuntimeException(e, this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
if (checkCondition(methodContext)) {
|
||||
if (this.subOps != null) {
|
||||
return SimpleMethod.runSubOps(subOps, methodContext);
|
||||
}
|
||||
} else {
|
||||
if (elseSubOps != null) {
|
||||
return SimpleMethod.runSubOps(elseSubOps, methodContext);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
if (this.subOps != null) {
|
||||
for (MethodOperation method : this.subOps) {
|
||||
method.gatherArtifactInfo(aic);
|
||||
}
|
||||
}
|
||||
if (this.elseSubOps != null) {
|
||||
for (MethodOperation method : this.elseSubOps) {
|
||||
method.gatherArtifactInfo(aic);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void prettyPrint(StringBuilder messageBuffer, MethodContext methodContext) {
|
||||
messageBuffer.append("validate-method[");
|
||||
messageBuffer.append(className);
|
||||
messageBuffer.append(".");
|
||||
messageBuffer.append(methodName);
|
||||
messageBuffer.append("(");
|
||||
messageBuffer.append(this.fieldFma);
|
||||
if (methodContext != null) {
|
||||
messageBuffer.append("=");
|
||||
messageBuffer.append(fieldFma.get(methodContext.getEnvMap()));
|
||||
}
|
||||
messageBuffer.append(")]");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<if-validate-method ");
|
||||
sb.append("field=\"").append(this.fieldFma).append("\" ");
|
||||
if (!this.methodName.isEmpty()) {
|
||||
sb.append("methodName=\"").append(this.methodName).append("\" ");
|
||||
}
|
||||
if (!"org.ofbiz.base.util.UtilValidate".equals(this.className)) {
|
||||
sb.append("class=\"").append(this.className).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A <if-validate-method> element factory.
|
||||
*/
|
||||
public static final class ValidateMethodConditionFactory extends ConditionalFactory<ValidateMethodCondition> implements Factory<ValidateMethodCondition> {
|
||||
@Override
|
||||
public ValidateMethodCondition createCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new ValidateMethodCondition(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ValidateMethodCondition createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new ValidateMethodCondition(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "if-validate-method";
|
||||
}
|
||||
}
|
||||
}
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.entity.Delegator;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <clear-cache-line> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cclearcacheline%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class ClearCacheLine extends EntityOperation {
|
||||
|
||||
private final FlexibleStringExpander entityNameFse;
|
||||
private final FlexibleMapAccessor<Map<String, ? extends Object>> mapFma;
|
||||
|
||||
public ClearCacheLine(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "entity-name", "map", "delegator-name");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "entity-name");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "map", "delegator-name");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
entityNameFse = FlexibleStringExpander.getInstance(element.getAttribute("entity-name"));
|
||||
mapFma = FlexibleMapAccessor.getInstance(element.getAttribute("map"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
Delegator delegator = getDelegator(methodContext);
|
||||
String entityName = entityNameFse.expandString(methodContext.getEnvMap());
|
||||
Map<String, ? extends Object> fieldsMap = mapFma.get(methodContext.getEnvMap());
|
||||
if (fieldsMap == null) {
|
||||
delegator.clearCacheLine(entityName);
|
||||
} else {
|
||||
delegator.clearCacheLine(entityName, fieldsMap);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<clear-cache-line ");
|
||||
sb.append("entity-name=\"").append(this.entityNameFse).append("\" ");
|
||||
if (!this.mapFma.isEmpty()) {
|
||||
sb.append("map=\"").append(this.mapFma).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <clear-cache-line> element.
|
||||
*/
|
||||
public static final class ClearCacheLineFactory implements Factory<ClearCacheLine> {
|
||||
@Override
|
||||
public ClearCacheLine createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new ClearCacheLine(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "clear-cache-line";
|
||||
}
|
||||
}
|
||||
}
|
||||
+70
@@ -0,0 +1,70 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import org.ofbiz.entity.Delegator;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <clear-entity-caches> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cclearentitycaches%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class ClearEntityCaches extends EntityOperation {
|
||||
|
||||
public ClearEntityCaches(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "delegator-name");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "delegator-name");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
Delegator delegator = getDelegator(methodContext);
|
||||
delegator.clearAllCaches();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "<clear-entity-caches/>";
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <clear-entity-caches> element.
|
||||
*/
|
||||
public static final class ClearEntityCachesFactory implements Factory<ClearEntityCaches> {
|
||||
@Override
|
||||
public ClearEntityCaches createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new ClearEntityCaches(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "clear-entity-caches";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.entity.GenericValue;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <clone-value> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cclonevalue%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class CloneValue extends MethodOperation {
|
||||
|
||||
private final FlexibleMapAccessor<GenericValue> newValueFma;
|
||||
private final FlexibleMapAccessor<GenericValue> valueFma;
|
||||
|
||||
public CloneValue(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "value-field", "new-value-field");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "value-field", "new-value-field");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "value-field", "new-value-field");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
valueFma = FlexibleMapAccessor.getInstance(element.getAttribute("value-field"));
|
||||
newValueFma = FlexibleMapAccessor.getInstance(element.getAttribute("new-value-field"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
GenericValue value = valueFma.get(methodContext.getEnvMap());
|
||||
if (value != null) {
|
||||
newValueFma.put(methodContext.getEnvMap(), GenericValue.create(value));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<clone-value ");
|
||||
sb.append("value-field=\"").append(this.valueFma).append("\" ");
|
||||
sb.append("new-value-field=\"").append(this.newValueFma).append("\" ");
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <clone-value> element.
|
||||
*/
|
||||
public static final class CloneValueFactory implements Factory<CloneValue> {
|
||||
@Override
|
||||
public CloneValue createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new CloneValue(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "clone-value";
|
||||
}
|
||||
}
|
||||
}
|
||||
+112
@@ -0,0 +1,112 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.entity.GenericEntityException;
|
||||
import org.ofbiz.entity.GenericValue;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <create-value> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Ccreatevalue%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class CreateValue extends MethodOperation {
|
||||
|
||||
public static final String module = CreateValue.class.getName();
|
||||
|
||||
private final boolean createOrStore;
|
||||
@Deprecated
|
||||
private final boolean doCacheClear;
|
||||
private final FlexibleMapAccessor<GenericValue> valueFma;
|
||||
|
||||
public CreateValue(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "value-field", "do-cache-clear", "or-store");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "value-field");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "value-field");
|
||||
MiniLangValidate.constantAttributes(simpleMethod, element, "do-cache-clear", "or-store");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
valueFma = FlexibleMapAccessor.getInstance(element.getAttribute("value-field"));
|
||||
doCacheClear = !"false".equals(element.getAttribute("do-cache-clear"));
|
||||
createOrStore = "true".equals(element.getAttribute("or-store"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
GenericValue value = valueFma.get(methodContext.getEnvMap());
|
||||
if (value == null) {
|
||||
String errMsg = "In <create-value> the value \"" + valueFma + "\" was not found, not creating";
|
||||
Debug.logWarning(errMsg, module);
|
||||
simpleMethod.addErrorMessage(methodContext, errMsg);
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
if (createOrStore) {
|
||||
value.getDelegator().createOrStore(value);
|
||||
} else {
|
||||
value.getDelegator().create(value);
|
||||
}
|
||||
} catch (GenericEntityException e) {
|
||||
String errMsg = "Exception thrown while creating the \"" + valueFma +"\" GenericValue: " + e.getMessage();
|
||||
Debug.logWarning(e, errMsg, module);
|
||||
simpleMethod.addErrorMessage(methodContext, errMsg);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<create-value ");
|
||||
sb.append("value-field=\"").append(this.valueFma).append("\" ");
|
||||
if (!this.doCacheClear) {
|
||||
sb.append("do-cache-clear=\"false\"");
|
||||
}
|
||||
if (this.createOrStore) {
|
||||
sb.append("or-store=\"true\"");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <create-value> element.
|
||||
*/
|
||||
public static final class CreateValueFactory implements Factory<CreateValue> {
|
||||
@Override
|
||||
public CreateValue createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new CreateValue(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "create-value";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.GeneralException;
|
||||
import org.ofbiz.entity.Delegator;
|
||||
import org.ofbiz.entity.finder.ByAndFinder;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <entity-and> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Centityand%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class EntityAnd extends EntityOperation {
|
||||
|
||||
public static final String module = EntityAnd.class.getName();
|
||||
|
||||
private final ByAndFinder finder;
|
||||
|
||||
public EntityAnd(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "entity-name", "use-cache", "filter-by-date", "list", "distinct", "delegator-name");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "entity-name", "list");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "list");
|
||||
MiniLangValidate.childElements(simpleMethod, element, "field-map", "order-by", "limit-range", "limit-view", "use-iterator");
|
||||
MiniLangValidate.requiredChildElements(simpleMethod, element, "field-map");
|
||||
}
|
||||
this.finder = new ByAndFinder(element);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
try {
|
||||
Delegator delegator = getDelegator(methodContext);
|
||||
this.finder.runFind(methodContext.getEnvMap(), delegator);
|
||||
} catch (GeneralException e) {
|
||||
String errMsg = "Exception thrown while performing entity find: " + e.getMessage();
|
||||
Debug.logWarning(e, errMsg, module);
|
||||
simpleMethod.addErrorMessage(methodContext, errMsg);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
aic.addEntityName(this.finder.getEntityName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<entity-and ");
|
||||
sb.append("entity-name=\"").append(this.finder.getEntityName()).append("\" />");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <entity-and> element.
|
||||
*/
|
||||
public static final class EntityAndFactory implements Factory<EntityAnd> {
|
||||
@Override
|
||||
public EntityAnd createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new EntityAnd(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "entity-and";
|
||||
}
|
||||
}
|
||||
}
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.GeneralException;
|
||||
import org.ofbiz.entity.Delegator;
|
||||
import org.ofbiz.entity.finder.ByConditionFinder;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <entity-condition> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Centitycondition%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class EntityCondition extends EntityOperation {
|
||||
|
||||
public static final String module = EntityCondition.class.getName();
|
||||
|
||||
private final ByConditionFinder finder;
|
||||
|
||||
public EntityCondition(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "entity-name", "use-cache", "filter-by-date", "list", "distinct", "delegator-name");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "entity-name", "list");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "list", "delegator-name");
|
||||
MiniLangValidate.childElements(simpleMethod, element, "condition-expr", "condition-list", "condition-object", "having-condition-list", "select-field", "order-by", "limit-range", "limit-view", "use-iterator");
|
||||
MiniLangValidate.requireAnyChildElement(simpleMethod, element, "condition-expr", "condition-list", "condition-object");
|
||||
}
|
||||
this.finder = new ByConditionFinder(element);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
try {
|
||||
Delegator delegator = getDelegator(methodContext);
|
||||
finder.runFind(methodContext.getEnvMap(), delegator);
|
||||
} catch (GeneralException e) {
|
||||
String errMsg = "Exception thrown while performing entity find: " + e.getMessage();
|
||||
Debug.logWarning(e, errMsg, module);
|
||||
simpleMethod.addErrorMessage(methodContext, errMsg);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
aic.addEntityName(this.finder.getEntityName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<entity-condition ");
|
||||
sb.append("entity-name=\"").append(this.finder.getEntityName()).append("\" />");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static final class EntityConditionFactory implements Factory<EntityCondition> {
|
||||
@Override
|
||||
public EntityCondition createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new EntityCondition(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "entity-condition";
|
||||
}
|
||||
}
|
||||
}
|
||||
+146
@@ -0,0 +1,146 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.GeneralException;
|
||||
import org.ofbiz.base.util.UtilXml;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.entity.Delegator;
|
||||
import org.ofbiz.entity.condition.EntityCondition;
|
||||
import org.ofbiz.entity.finder.EntityFinderUtil.Condition;
|
||||
import org.ofbiz.entity.finder.EntityFinderUtil.ConditionExpr;
|
||||
import org.ofbiz.entity.finder.EntityFinderUtil.ConditionList;
|
||||
import org.ofbiz.entity.finder.EntityFinderUtil.ConditionObject;
|
||||
import org.ofbiz.entity.model.ModelEntity;
|
||||
import org.ofbiz.entity.util.EntityQuery;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <entity-count> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Centitycount%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class EntityCount extends EntityOperation {
|
||||
|
||||
public static final String module = EntityCount.class.getName();
|
||||
|
||||
private final FlexibleMapAccessor<Long> countFma;
|
||||
private final FlexibleStringExpander entityNameFse;
|
||||
private final Condition havingCondition;
|
||||
private final Condition whereCondition;
|
||||
|
||||
public EntityCount(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "entity-name", "count-field", "delegator-name");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "entity-name", "count-field");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "count-field", "delegator-name");
|
||||
MiniLangValidate.childElements(simpleMethod, element, "condition-expr", "condition-list", "condition-object", "having-condition-list");
|
||||
MiniLangValidate.requireAnyChildElement(simpleMethod, element, "condition-expr", "condition-list", "condition-object");
|
||||
}
|
||||
this.entityNameFse = FlexibleStringExpander.getInstance(element.getAttribute("entity-name"));
|
||||
this.countFma = FlexibleMapAccessor.getInstance(element.getAttribute("count-field"));
|
||||
int conditionElementCount = 0;
|
||||
Element conditionExprElement = UtilXml.firstChildElement(element, "condition-expr");
|
||||
conditionElementCount = conditionExprElement == null ? conditionElementCount : conditionElementCount++;
|
||||
Element conditionListElement = UtilXml.firstChildElement(element, "condition-list");
|
||||
conditionElementCount = conditionListElement == null ? conditionElementCount : conditionElementCount++;
|
||||
Element conditionObjectElement = UtilXml.firstChildElement(element, "condition-object");
|
||||
conditionElementCount = conditionObjectElement == null ? conditionElementCount : conditionElementCount++;
|
||||
if (conditionElementCount > 1) {
|
||||
MiniLangValidate.handleError("Element must include only one condition child element", simpleMethod, conditionObjectElement);
|
||||
}
|
||||
if (conditionExprElement != null) {
|
||||
this.whereCondition = new ConditionExpr(conditionExprElement);
|
||||
} else if (conditionListElement != null) {
|
||||
this.whereCondition = new ConditionList(conditionListElement);
|
||||
} else if (conditionObjectElement != null) {
|
||||
this.whereCondition = new ConditionObject(conditionObjectElement);
|
||||
} else {
|
||||
this.whereCondition = null;
|
||||
}
|
||||
Element havingConditionListElement = UtilXml.firstChildElement(element, "having-condition-list");
|
||||
if (havingConditionListElement != null) {
|
||||
this.havingCondition = new ConditionList(havingConditionListElement);
|
||||
} else {
|
||||
this.havingCondition = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
try {
|
||||
Delegator delegator = getDelegator(methodContext);
|
||||
String entityName = this.entityNameFse.expandString(methodContext.getEnvMap());
|
||||
ModelEntity modelEntity = delegator.getModelEntity(entityName);
|
||||
EntityCondition whereEntityCondition = null;
|
||||
if (this.whereCondition != null) {
|
||||
whereEntityCondition = this.whereCondition.createCondition(methodContext.getEnvMap(), modelEntity, delegator.getModelFieldTypeReader(modelEntity));
|
||||
}
|
||||
EntityCondition havingEntityCondition = null;
|
||||
if (this.havingCondition != null) {
|
||||
havingEntityCondition = this.havingCondition.createCondition(methodContext.getEnvMap(), modelEntity, delegator.getModelFieldTypeReader(modelEntity));
|
||||
}
|
||||
long count = EntityQuery.use(delegator).from(entityName).where(whereEntityCondition).having(havingEntityCondition).queryCount();
|
||||
this.countFma.put(methodContext.getEnvMap(), count);
|
||||
} catch (GeneralException e) {
|
||||
String errMsg = "Exception thrown while performing entity count: " + e.getMessage();
|
||||
Debug.logWarning(e, errMsg, module);
|
||||
simpleMethod.addErrorMessage(methodContext, errMsg);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
aic.addEntityName(entityNameFse.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<entity-count ");
|
||||
sb.append("entity-name=\"").append(this.entityNameFse).append("\" ");
|
||||
sb.append("count-field=\"").append(this.countFma).append("\" ");
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <entity-count> element.
|
||||
*/
|
||||
public static final class EntityCountFactory implements Factory<EntityCount> {
|
||||
@Override
|
||||
public EntityCount createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new EntityCount(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "entity-count";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,149 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.location.FlexibleLocation;
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.entity.Delegator;
|
||||
import org.ofbiz.entity.util.EntityDataAssert;
|
||||
import org.ofbiz.entity.util.EntitySaxReader;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <entity-data> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Centitydata%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class EntityData extends EntityOperation {
|
||||
|
||||
public static final String module = EntityData.class.getName();
|
||||
|
||||
private final FlexibleMapAccessor<List<Object>> errorListFma;
|
||||
private final FlexibleStringExpander locationFse;
|
||||
private final String mode;
|
||||
private final int timeout;
|
||||
|
||||
public EntityData(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "location", "timeout", "delegator-name", "error-list-name", "mode");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "location");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "delegator-name");
|
||||
MiniLangValidate.constantAttributes(simpleMethod, element, "timeout", "mode");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
locationFse = FlexibleStringExpander.getInstance(element.getAttribute("location"));
|
||||
mode = MiniLangValidate.checkAttribute(element.getAttribute("mode"), "load");
|
||||
String timeoutAttribute = element.getAttribute("timeout");
|
||||
if (!"load".equals(mode) && !timeoutAttribute.isEmpty()) {
|
||||
MiniLangValidate.handleError("timeout attribute is valid only when mode=\"load\".", simpleMethod, element);
|
||||
}
|
||||
int timeout = -1;
|
||||
if (!timeoutAttribute.isEmpty()) {
|
||||
try {
|
||||
timeout = Integer.parseInt(timeoutAttribute);
|
||||
} catch (NumberFormatException e) {
|
||||
MiniLangValidate.handleError("Exception thrown while parsing timeout attribute: " + e.getMessage(), simpleMethod, element);
|
||||
}
|
||||
}
|
||||
this.timeout = timeout;
|
||||
errorListFma = FlexibleMapAccessor.getInstance(MiniLangValidate.checkAttribute(element.getAttribute("error-list-name"), "error_list"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
List<Object> messages = errorListFma.get(methodContext.getEnvMap());
|
||||
if (messages == null) {
|
||||
messages = new LinkedList<Object>();
|
||||
errorListFma.put(methodContext.getEnvMap(), messages);
|
||||
}
|
||||
String location = this.locationFse.expandString(methodContext.getEnvMap());
|
||||
Delegator delegator = getDelegator(methodContext);
|
||||
URL dataUrl = null;
|
||||
try {
|
||||
dataUrl = FlexibleLocation.resolveLocation(location, methodContext.getLoader());
|
||||
} catch (MalformedURLException e) {
|
||||
messages.add("Could not find Entity Data document in resource: " + location + "; error was: " + e.toString());
|
||||
}
|
||||
if (dataUrl == null) {
|
||||
messages.add("Could not find Entity Data document in resource: " + location);
|
||||
}
|
||||
if ("assert".equals(mode)) {
|
||||
try {
|
||||
EntityDataAssert.assertData(dataUrl, delegator, messages);
|
||||
} catch (Exception e) {
|
||||
String xmlError = "Error checking/asserting XML Resource \"" + dataUrl.toExternalForm() + "\"; Error was: " + e.getMessage();
|
||||
messages.add(xmlError);
|
||||
Debug.logWarning(e, xmlError, module);
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
EntitySaxReader reader = null;
|
||||
if (timeout > 0) {
|
||||
reader = new EntitySaxReader(delegator, timeout);
|
||||
} else {
|
||||
reader = new EntitySaxReader(delegator);
|
||||
}
|
||||
reader.parse(dataUrl);
|
||||
} catch (Exception e) {
|
||||
String xmlError = "Error loading XML Resource \"" + dataUrl.toExternalForm() + "\"; Error was: " + e.getMessage();
|
||||
messages.add(xmlError);
|
||||
Debug.logWarning(e, xmlError, module);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<entity-data ");
|
||||
sb.append("location=\"").append(this.locationFse).append("\" ");
|
||||
sb.append("mode=\"").append(this.mode).append("\" ");
|
||||
sb.append("timeout=\"").append(this.timeout).append("\" ");
|
||||
sb.append("error-list-name=\"").append(this.errorListFma).append("\" ");
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <entity-data> element.
|
||||
*/
|
||||
public static final class EntityDataFactory implements Factory<EntityData> {
|
||||
@Override
|
||||
public EntityData createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new EntityData(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "entity-data";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.GeneralException;
|
||||
import org.ofbiz.entity.Delegator;
|
||||
import org.ofbiz.entity.finder.PrimaryKeyFinder;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <entity-one> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Centityone%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class EntityOne extends EntityOperation {
|
||||
|
||||
public static final String module = EntityOne.class.getName();
|
||||
|
||||
private final PrimaryKeyFinder finder;
|
||||
|
||||
public EntityOne(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "entity-name", "use-cache", "auto-field-map", "value-field", "delegator-name");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "entity-name", "value-field");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "value-field", "delegator-name");
|
||||
MiniLangValidate.childElements(simpleMethod, element, "field-map", "select-field");
|
||||
}
|
||||
this.finder = new PrimaryKeyFinder(element);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
try {
|
||||
Delegator delegator = getDelegator(methodContext);
|
||||
this.finder.runFind(methodContext.getEnvMap(), delegator);
|
||||
} catch (GeneralException e) {
|
||||
String errMsg = "Exception thrown while performing entity find: " + e.getMessage();
|
||||
Debug.logWarning(e, errMsg, module);
|
||||
simpleMethod.addErrorMessage(methodContext, errMsg);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
aic.addEntityName(this.finder.getEntityName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<entity-one ");
|
||||
sb.append("entity-name=\"").append(this.finder.getEntityName()).append("\" />");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <entity-one> element.
|
||||
*/
|
||||
public static final class EntityOneFactory implements Factory<EntityOne> {
|
||||
@Override
|
||||
public EntityOne createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new EntityOne(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "entity-one";
|
||||
}
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.entity.Delegator;
|
||||
import org.ofbiz.entity.DelegatorFactory;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* An abstract base class for entity operations.
|
||||
*/
|
||||
public abstract class EntityOperation extends MethodOperation {
|
||||
|
||||
private final FlexibleStringExpander delegatorNameFse;
|
||||
|
||||
public EntityOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
this.delegatorNameFse = FlexibleStringExpander.getInstance(element.getAttribute("delegator-name"));
|
||||
}
|
||||
|
||||
protected final Delegator getDelegator(MethodContext methodContext) {
|
||||
String delegatorName = delegatorNameFse.expandString(methodContext.getEnvMap());
|
||||
if (!delegatorName.isEmpty()) {
|
||||
return DelegatorFactory.getDelegator(delegatorName);
|
||||
}
|
||||
return methodContext.getDelegator();
|
||||
}
|
||||
}
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.entity.GenericEntity;
|
||||
import org.ofbiz.entity.util.EntityUtil;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <filter-list-by-and> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cfilterlistbyand%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class FilterListByAnd extends MethodOperation {
|
||||
|
||||
private final FlexibleMapAccessor<List<GenericEntity>> listFma;
|
||||
private final FlexibleMapAccessor<Map<String, ? extends Object>> mapFma;
|
||||
private final FlexibleMapAccessor<List<GenericEntity>> toListFma;
|
||||
|
||||
public FilterListByAnd(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "list", "map", "to-list");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "list", "map");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "list", "map", "to-list");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
listFma = FlexibleMapAccessor.getInstance(element.getAttribute("list"));
|
||||
String toListAttribute = element.getAttribute("to-list");
|
||||
if (toListAttribute.isEmpty()) {
|
||||
toListFma = listFma;
|
||||
} else {
|
||||
toListFma = FlexibleMapAccessor.getInstance(toListAttribute);
|
||||
}
|
||||
mapFma = FlexibleMapAccessor.getInstance(element.getAttribute("map"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
Map<String, ? extends Object> theMap = mapFma.get(methodContext.getEnvMap());
|
||||
toListFma.put(methodContext.getEnvMap(), EntityUtil.filterByAnd(listFma.get(methodContext.getEnvMap()), theMap));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<filter-list-by-and ");
|
||||
sb.append("list=\"").append(this.listFma).append("\" ");
|
||||
sb.append("map=\"").append(this.mapFma).append("\" ");
|
||||
sb.append("to-list=\"").append(this.toListFma).append("\" ");
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <filter-list-by-and> element.
|
||||
*/
|
||||
public static final class FilterListByAndFactory implements Factory<FilterListByAnd> {
|
||||
@Override
|
||||
public FilterListByAnd createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new FilterListByAnd(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "filter-list-by-and";
|
||||
}
|
||||
}
|
||||
}
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.util.UtilDateTime;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.entity.GenericEntity;
|
||||
import org.ofbiz.entity.util.EntityUtil;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <filter-list-by-date> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cfilterlistbydate%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class FilterListByDate extends MethodOperation {
|
||||
|
||||
private final FlexibleMapAccessor<List<GenericEntity>> listFma;
|
||||
private final FlexibleMapAccessor<List<GenericEntity>> toListFma;
|
||||
private final FlexibleMapAccessor<Timestamp> validDateFma;
|
||||
private final String fromFieldName;
|
||||
private final String thruFieldName;
|
||||
|
||||
public FilterListByDate(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "list", "to-list", "valid-date", "fromDate", "thruDate");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "list");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "list", "to-list", "valid-date");
|
||||
MiniLangValidate.constantAttributes(simpleMethod, element, "fromDate", "thruDate");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
listFma = FlexibleMapAccessor.getInstance(element.getAttribute("list"));
|
||||
String toListAttribute = element.getAttribute("to-list");
|
||||
if (toListAttribute.isEmpty()) {
|
||||
toListFma = listFma;
|
||||
} else {
|
||||
toListFma = FlexibleMapAccessor.getInstance(toListAttribute);
|
||||
}
|
||||
validDateFma = FlexibleMapAccessor.getInstance(element.getAttribute("valid-date"));
|
||||
fromFieldName = MiniLangValidate.checkAttribute(element.getAttribute("from-field-name"), "fromDate");
|
||||
thruFieldName = MiniLangValidate.checkAttribute(element.getAttribute("thru-field-name"), "thruDate");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
if (!validDateFma.isEmpty()) {
|
||||
toListFma.put(methodContext.getEnvMap(), EntityUtil.filterByDate(listFma.get(methodContext.getEnvMap()), validDateFma.get(methodContext.getEnvMap()), fromFieldName, thruFieldName, true));
|
||||
} else {
|
||||
toListFma.put(methodContext.getEnvMap(), EntityUtil.filterByDate(listFma.get(methodContext.getEnvMap()), UtilDateTime.nowTimestamp(), fromFieldName, thruFieldName, true));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<filter-list-by-date ");
|
||||
sb.append("list=\"").append(this.listFma).append("\" ");
|
||||
sb.append("to-list=\"").append(this.toListFma).append("\" ");
|
||||
sb.append("valid-date=\"").append(this.validDateFma).append("\" ");
|
||||
sb.append("from-field-name=\"").append(this.fromFieldName).append("\" ");
|
||||
sb.append("thru-field-name=\"").append(this.thruFieldName).append("\" ");
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <filter-list-by-date> element.
|
||||
*/
|
||||
public static final class FilterListByDateFactory implements Factory<FilterListByDate> {
|
||||
@Override
|
||||
public FilterListByDate createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new FilterListByDate(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "filter-list-by-date";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.UtilMisc;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.entity.Delegator;
|
||||
import org.ofbiz.entity.GenericEntityException;
|
||||
import org.ofbiz.entity.condition.EntityCondition;
|
||||
import org.ofbiz.entity.util.EntityQuery;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <find-by-and> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cfindbyand%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class FindByAnd extends EntityOperation {
|
||||
|
||||
public static final String module = FindByAnd.class.getName();
|
||||
|
||||
private final FlexibleStringExpander entityNameFse;
|
||||
private final FlexibleMapAccessor<Collection<String>> fieldsToSelectListFma;
|
||||
private final FlexibleMapAccessor<Object> listFma;
|
||||
private final FlexibleMapAccessor<Map<String, ? extends Object>> mapFma;
|
||||
private final FlexibleMapAccessor<List<String>> orderByListFma;
|
||||
private final FlexibleStringExpander useCacheFse;
|
||||
private final FlexibleStringExpander useIteratorFse;
|
||||
|
||||
public FindByAnd(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "entity-name", "use-cache", "fields-to-select-list", "use-iterator", "list", "map", "order-by-list", "delegator-name");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "entity-name", "list", "map");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "list", "map", "fields-to-select-list", "order-by-list", "delegator-name");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
entityNameFse = FlexibleStringExpander.getInstance(element.getAttribute("entity-name"));
|
||||
listFma = FlexibleMapAccessor.getInstance(element.getAttribute("list"));
|
||||
mapFma = FlexibleMapAccessor.getInstance(element.getAttribute("map"));
|
||||
orderByListFma = FlexibleMapAccessor.getInstance(element.getAttribute("order-by-list"));
|
||||
fieldsToSelectListFma = FlexibleMapAccessor.getInstance(element.getAttribute("fields-to-select-list"));
|
||||
useCacheFse = FlexibleStringExpander.getInstance(element.getAttribute("use-cache"));
|
||||
useIteratorFse = FlexibleStringExpander.getInstance(element.getAttribute("use-iterator"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
String entityName = entityNameFse.expandString(methodContext.getEnvMap());
|
||||
boolean useCache = "true".equals(useCacheFse.expandString(methodContext.getEnvMap()));
|
||||
boolean useIterator = "true".equals(useIteratorFse.expandString(methodContext.getEnvMap()));
|
||||
List<String> orderByNames = orderByListFma.get(methodContext.getEnvMap());
|
||||
Delegator delegator = getDelegator(methodContext);
|
||||
try {
|
||||
EntityCondition whereCond = null;
|
||||
Map<String, ? extends Object> fieldMap = mapFma.get(methodContext.getEnvMap());
|
||||
if (fieldMap != null) {
|
||||
whereCond = EntityCondition.makeCondition(fieldMap);
|
||||
}
|
||||
if (useIterator) {
|
||||
listFma.put(methodContext.getEnvMap(), EntityQuery.use(delegator)
|
||||
.select(UtilMisc.toSet(fieldsToSelectListFma.get(methodContext.getEnvMap())))
|
||||
.from(entityName)
|
||||
.where(whereCond)
|
||||
.orderBy(orderByNames)
|
||||
.queryList());
|
||||
} else {
|
||||
listFma.put(methodContext.getEnvMap(), EntityQuery.use(delegator)
|
||||
.select(UtilMisc.toSet(fieldsToSelectListFma.get(methodContext.getEnvMap())))
|
||||
.from(entityName)
|
||||
.where(whereCond)
|
||||
.orderBy(orderByNames)
|
||||
.cache(useCache)
|
||||
.queryList());
|
||||
}
|
||||
} catch (GenericEntityException e) {
|
||||
String errMsg = "Exception thrown while performing entity find: " + e.getMessage();
|
||||
Debug.logWarning(e, errMsg, module);
|
||||
simpleMethod.addErrorMessage(methodContext, errMsg);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
aic.addEntityName(entityNameFse.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<find-by-and ");
|
||||
sb.append("entity-name=\"").append(this.entityNameFse).append("\" ");
|
||||
sb.append("list=\"").append(this.listFma).append("\" ");
|
||||
sb.append("map=\"").append(this.mapFma).append("\" ");
|
||||
if (!orderByListFma.isEmpty()) {
|
||||
sb.append("order-by-list=\"").append(this.orderByListFma).append("\" ");
|
||||
}
|
||||
if (!fieldsToSelectListFma.isEmpty()) {
|
||||
sb.append("fields-to-select-list=\"").append(this.fieldsToSelectListFma).append("\" ");
|
||||
}
|
||||
if (!useCacheFse.isEmpty()) {
|
||||
sb.append("use-cache=\"").append(this.useCacheFse).append("\" ");
|
||||
}
|
||||
if (!useIteratorFse.isEmpty()) {
|
||||
sb.append("use-iterator=\"").append(this.useIteratorFse).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <find-by-and> element.
|
||||
*/
|
||||
public static final class FindByAndFactory implements Factory<FindByAnd> {
|
||||
@Override
|
||||
public FindByAnd createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new FindByAnd(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "find-by-and";
|
||||
}
|
||||
}
|
||||
}
|
||||
+138
@@ -0,0 +1,138 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.UtilMisc;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.entity.Delegator;
|
||||
import org.ofbiz.entity.GenericEntity;
|
||||
import org.ofbiz.entity.GenericEntityException;
|
||||
import org.ofbiz.entity.GenericValue;
|
||||
import org.ofbiz.entity.util.EntityQuery;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <find-by-primary-key> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cfindbyprimarykey%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class FindByPrimaryKey extends EntityOperation {
|
||||
|
||||
public static final String module = FindByPrimaryKey.class.getName();
|
||||
|
||||
private final FlexibleStringExpander entityNameFse;
|
||||
private final FlexibleMapAccessor<Collection<String>> fieldsToSelectListFma;
|
||||
private final FlexibleMapAccessor<Map<String, ? extends Object>> mapFma;
|
||||
private final FlexibleStringExpander useCacheFse;
|
||||
private final FlexibleMapAccessor<GenericValue> valueFma;
|
||||
|
||||
public FindByPrimaryKey(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "entity-name", "use-cache", "fields-to-select-list", "map", "value-field", "delegator-name");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "value-field", "map");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "value-field", "map", "fields-to-select-list", "delegator-name");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
valueFma = FlexibleMapAccessor.getInstance(element.getAttribute("value-field"));
|
||||
entityNameFse = FlexibleStringExpander.getInstance(element.getAttribute("entity-name"));
|
||||
mapFma = FlexibleMapAccessor.getInstance(element.getAttribute("map"));
|
||||
fieldsToSelectListFma = FlexibleMapAccessor.getInstance(element.getAttribute("fields-to-select-list"));
|
||||
useCacheFse = FlexibleStringExpander.getInstance(element.getAttribute("use-cache"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
String entityName = entityNameFse.expandString(methodContext.getEnvMap());
|
||||
boolean useCache = "true".equals(useCacheFse.expandString(methodContext.getEnvMap()));
|
||||
Delegator delegator = getDelegator(methodContext);
|
||||
Map<String, ? extends Object> inMap = mapFma.get(methodContext.getEnvMap());
|
||||
if (inMap == null) {
|
||||
throw new MiniLangRuntimeException("Primary key map \"" + mapFma + "\" not found", this);
|
||||
}
|
||||
if (entityName.isEmpty() && inMap instanceof GenericEntity) {
|
||||
GenericEntity inEntity = (GenericEntity) inMap;
|
||||
entityName = inEntity.getEntityName();
|
||||
}
|
||||
if (entityName.isEmpty()) {
|
||||
throw new MiniLangRuntimeException("Entity name not found", this);
|
||||
}
|
||||
Collection<String> fieldsToSelectList = fieldsToSelectListFma.get(methodContext.getEnvMap());
|
||||
try {
|
||||
if (fieldsToSelectList != null) {
|
||||
valueFma.put(methodContext.getEnvMap(), delegator.findByPrimaryKeyPartial(delegator.makePK(entityName, inMap), UtilMisc.toSet(fieldsToSelectList)));
|
||||
} else {
|
||||
valueFma.put(methodContext.getEnvMap(), EntityQuery.use(delegator).from(entityName).where(inMap).cache(useCache).queryOne());
|
||||
}
|
||||
} catch (GenericEntityException e) {
|
||||
String errMsg = "Exception thrown while performing entity find: " + e.getMessage();
|
||||
Debug.logWarning(e, errMsg, module);
|
||||
simpleMethod.addErrorMessage(methodContext, errMsg);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
aic.addEntityName(entityNameFse.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<find-by-primary-key ");
|
||||
sb.append("entity-name=\"").append(this.entityNameFse).append("\" ");
|
||||
sb.append("value-field=\"").append(this.valueFma).append("\" ");
|
||||
sb.append("map=\"").append(this.mapFma).append("\" ");
|
||||
if (!fieldsToSelectListFma.isEmpty()) {
|
||||
sb.append("fields-to-select-list=\"").append(this.fieldsToSelectListFma).append("\" ");
|
||||
}
|
||||
if (!useCacheFse.isEmpty()) {
|
||||
sb.append("use-cache=\"").append(this.useCacheFse).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <find-by-primary-key> element.
|
||||
*/
|
||||
public static final class FindByPrimaryKeyFactory implements Factory<FindByPrimaryKey> {
|
||||
@Override
|
||||
public FindByPrimaryKey createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new FindByPrimaryKey(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "find-by-primary-key";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.entity.GenericEntityException;
|
||||
import org.ofbiz.entity.GenericValue;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <get-related> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cgetrelated%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class GetRelated extends MethodOperation {
|
||||
|
||||
public static final String module = GetRelated.class.getName();
|
||||
|
||||
private final FlexibleMapAccessor<Object> listFma;
|
||||
private final FlexibleMapAccessor<Map<String, ? extends Object>> mapFma;
|
||||
private final FlexibleMapAccessor<List<String>> orderByListFma;
|
||||
private final FlexibleStringExpander relationNameFse;
|
||||
private final FlexibleStringExpander useCacheFse;
|
||||
private final FlexibleMapAccessor<GenericValue> valueFma;
|
||||
|
||||
public GetRelated(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "value-field", "relation-name", "list", "map", "order-by-list", "use-cache");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "value-field", "relation-name", "list");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "value-field", "list", "map", "order-by-list");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
valueFma = FlexibleMapAccessor.getInstance(element.getAttribute("value-field"));
|
||||
relationNameFse = FlexibleStringExpander.getInstance(element.getAttribute("relation-name"));
|
||||
listFma = FlexibleMapAccessor.getInstance(element.getAttribute("list"));
|
||||
mapFma = FlexibleMapAccessor.getInstance(element.getAttribute("map"));
|
||||
orderByListFma = FlexibleMapAccessor.getInstance(element.getAttribute("order-by-list"));
|
||||
useCacheFse = FlexibleStringExpander.getInstance(element.getAttribute("use-cache"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
GenericValue value = valueFma.get(methodContext.getEnvMap());
|
||||
if (value == null) {
|
||||
throw new MiniLangRuntimeException("Entity value not found with name: " + valueFma, this);
|
||||
}
|
||||
String relationName = relationNameFse.expandString(methodContext.getEnvMap());
|
||||
boolean useCache = "true".equals(useCacheFse.expandString(methodContext.getEnvMap()));
|
||||
List<String> orderByNames = orderByListFma.get(methodContext.getEnvMap());
|
||||
Map<String, ? extends Object> constraintMap = mapFma.get(methodContext.getEnvMap());
|
||||
try {
|
||||
listFma.put(methodContext.getEnvMap(), value.getRelated(relationName, constraintMap, orderByNames, useCache));
|
||||
} catch (GenericEntityException e) {
|
||||
String errMsg = "Exception thrown while finding related values: " + e.getMessage();
|
||||
Debug.logWarning(e, errMsg, module);
|
||||
simpleMethod.addErrorMessage(methodContext, errMsg);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
aic.addEntityName(relationNameFse.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<get-related ");
|
||||
sb.append("value-field=\"").append(this.valueFma).append("\" ");
|
||||
sb.append("relation-name=\"").append(this.relationNameFse).append("\" ");
|
||||
sb.append("list=\"").append(this.listFma).append("\" ");
|
||||
if (!mapFma.isEmpty()) {
|
||||
sb.append("map=\"").append(this.mapFma).append("\" ");
|
||||
}
|
||||
if (!orderByListFma.isEmpty()) {
|
||||
sb.append("order-by-list=\"").append(this.orderByListFma).append("\" ");
|
||||
}
|
||||
if (!useCacheFse.isEmpty()) {
|
||||
sb.append("use-cache=\"").append(this.useCacheFse).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <get-related> element.
|
||||
*/
|
||||
public static final class GetRelatedFactory implements Factory<GetRelated> {
|
||||
@Override
|
||||
public GetRelated createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new GetRelated(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "get-related";
|
||||
}
|
||||
}
|
||||
}
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.entity.GenericEntityException;
|
||||
import org.ofbiz.entity.GenericValue;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <get-related-one> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cgetrelatedone%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class GetRelatedOne extends MethodOperation {
|
||||
|
||||
public static final String module = GetRelatedOne.class.getName();
|
||||
|
||||
private final FlexibleStringExpander relationNameFse;
|
||||
private final FlexibleMapAccessor<GenericValue> toValueFma;
|
||||
private final FlexibleStringExpander useCacheFse;
|
||||
private final FlexibleMapAccessor<GenericValue> valueFma;
|
||||
|
||||
public GetRelatedOne(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "value-field", "relation-name", "to-value-field", "use-cache");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "value-field", "relation-name", "to-value-field");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "value-field", "to-value-field");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
valueFma = FlexibleMapAccessor.getInstance(element.getAttribute("value-field"));
|
||||
relationNameFse = FlexibleStringExpander.getInstance(element.getAttribute("relation-name"));
|
||||
toValueFma = FlexibleMapAccessor.getInstance(element.getAttribute("to-value-field"));
|
||||
useCacheFse = FlexibleStringExpander.getInstance(element.getAttribute("use-cache"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
GenericValue value = valueFma.get(methodContext.getEnvMap());
|
||||
if (value == null) {
|
||||
throw new MiniLangRuntimeException("Entity value not found with name: " + valueFma, this);
|
||||
}
|
||||
String relationName = relationNameFse.expandString(methodContext.getEnvMap());
|
||||
boolean useCache = "true".equals(useCacheFse.expandString(methodContext.getEnvMap()));
|
||||
try {
|
||||
toValueFma.put(methodContext.getEnvMap(), value.getRelatedOne(relationName, useCache));
|
||||
} catch (GenericEntityException e) {
|
||||
String errMsg = "Exception thrown while finding related value: " + e.getMessage();
|
||||
Debug.logWarning(e, errMsg, module);
|
||||
simpleMethod.addErrorMessage(methodContext, errMsg);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
aic.addEntityName(relationNameFse.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<get-related-one ");
|
||||
sb.append("value-field=\"").append(this.valueFma).append("\" ");
|
||||
sb.append("relation-name=\"").append(this.relationNameFse).append("\" ");
|
||||
sb.append("to-value-field=\"").append(this.toValueFma).append("\" ");
|
||||
if (!useCacheFse.isEmpty()) {
|
||||
sb.append("use-cache=\"").append(this.useCacheFse).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <get-related-one> element.
|
||||
*/
|
||||
public static final class GetRelatedOneFactory implements Factory<GetRelatedOne> {
|
||||
@Override
|
||||
public GetRelatedOne createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new GetRelatedOne(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "get-related-one";
|
||||
}
|
||||
}
|
||||
}
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.entity.GenericValue;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <make-next-seq-id> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cmakenextseqid%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class MakeNextSeqId extends MethodOperation {
|
||||
|
||||
public static final String module = MakeNextSeqId.class.getName();
|
||||
|
||||
private final FlexibleStringExpander incrementByFse;
|
||||
private final FlexibleStringExpander numericPaddingFse;
|
||||
private final FlexibleStringExpander seqFieldNameFse;
|
||||
private final FlexibleMapAccessor<GenericValue> valueFma;
|
||||
|
||||
public MakeNextSeqId(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "value-field", "seq-field-name", "increment-by", "numeric-padding");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "value-field", "seq-field-name");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "value-field");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
seqFieldNameFse = FlexibleStringExpander.getInstance(element.getAttribute("seq-field-name"));
|
||||
valueFma = FlexibleMapAccessor.getInstance(element.getAttribute("value-field"));
|
||||
numericPaddingFse = FlexibleStringExpander.getInstance(element.getAttribute("numeric-padding"));
|
||||
incrementByFse = FlexibleStringExpander.getInstance(element.getAttribute("increment-by"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
GenericValue value = valueFma.get(methodContext.getEnvMap());
|
||||
if (value == null) {
|
||||
throw new MiniLangRuntimeException("Entity value not found with name: " + valueFma, this);
|
||||
}
|
||||
String seqFieldName = seqFieldNameFse.expandString(methodContext.getEnvMap());
|
||||
String numericPaddingStr = numericPaddingFse.expandString(methodContext.getEnvMap());
|
||||
String incrementByStr = incrementByFse.expandString(methodContext.getEnvMap());
|
||||
int numericPadding = 5;
|
||||
if (!numericPaddingStr.isEmpty()) {
|
||||
try {
|
||||
numericPadding = Integer.parseInt(numericPaddingStr);
|
||||
} catch (Exception e) {
|
||||
throw new MiniLangRuntimeException("Invalid number in \"numeric-padding\" attribute", this);
|
||||
}
|
||||
}
|
||||
int incrementBy = 1;
|
||||
if (!incrementByStr.isEmpty()) {
|
||||
try {
|
||||
incrementBy = Integer.parseInt(incrementByStr);
|
||||
} catch (Exception e) {
|
||||
throw new MiniLangRuntimeException("Invalid number in \"increment-by\" attribute", this);
|
||||
}
|
||||
}
|
||||
value.getDelegator().setNextSubSeqId(value, seqFieldName, numericPadding, incrementBy);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<make-next-seq-id ");
|
||||
sb.append("value-field=\"").append(this.valueFma).append("\" ");
|
||||
sb.append("seq-field-name=\"").append(this.seqFieldNameFse).append("\" ");
|
||||
if (!incrementByFse.isEmpty()) {
|
||||
sb.append("increment-by=\"").append(this.incrementByFse).append("\" ");
|
||||
}
|
||||
if (!numericPaddingFse.isEmpty()) {
|
||||
sb.append("numeric-padding=\"").append(this.numericPaddingFse).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <make-next-seq-id> element.
|
||||
*/
|
||||
public static final class MakeNextSeqIdFactory implements Factory<MakeNextSeqId> {
|
||||
@Override
|
||||
public MakeNextSeqId createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new MakeNextSeqId(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "make-next-seq-id";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.entity.Delegator;
|
||||
import org.ofbiz.entity.GenericValue;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <make-value> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cmakevalue%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class MakeValue extends EntityOperation {
|
||||
|
||||
private final FlexibleStringExpander entityNameFse;
|
||||
private final FlexibleMapAccessor<Map<String, ? extends Object>> mapFma;
|
||||
private final FlexibleMapAccessor<GenericValue> valueFma;
|
||||
|
||||
public MakeValue(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "value-field", "entity-name", "map", "delegator-name");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "value-field", "entity-name");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "value-field", "map", "delegator-name");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
valueFma = FlexibleMapAccessor.getInstance(element.getAttribute("value-field"));
|
||||
entityNameFse = FlexibleStringExpander.getInstance(element.getAttribute("entity-name"));
|
||||
mapFma = FlexibleMapAccessor.getInstance(element.getAttribute("map"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
String entityName = entityNameFse.expandString(methodContext.getEnvMap());
|
||||
if (entityName.isEmpty()) {
|
||||
throw new MiniLangRuntimeException("Entity name not found.", this);
|
||||
}
|
||||
Delegator delegator = getDelegator(methodContext);
|
||||
valueFma.put(methodContext.getEnvMap(), delegator.makeValidValue(entityName, mapFma.get(methodContext.getEnvMap())));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
aic.addEntityName(entityNameFse.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<make-value ");
|
||||
sb.append("entity-name=\"").append(this.entityNameFse).append("\" ");
|
||||
sb.append("value-field=\"").append(this.valueFma).append("\" ");
|
||||
if (!mapFma.isEmpty()) {
|
||||
sb.append("map=\"").append(this.mapFma).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <make-value> element.
|
||||
*/
|
||||
public static final class MakeValueFactory implements Factory<MakeValue> {
|
||||
@Override
|
||||
public MakeValue createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new MakeValue(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "make-value";
|
||||
}
|
||||
}
|
||||
}
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.entity.GenericEntity;
|
||||
import org.ofbiz.entity.util.EntityUtil;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <order-value-list> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cordervaluelist%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class OrderValueList extends MethodOperation {
|
||||
|
||||
private final FlexibleMapAccessor<List<? extends GenericEntity>> listFma;
|
||||
private final FlexibleMapAccessor<List<String>> orderByListFma;
|
||||
private final FlexibleMapAccessor<List<? extends GenericEntity>> toListFma;
|
||||
|
||||
public OrderValueList(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "list", "order-by-list", "to-list");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "list", "order-by-list");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "list", "order-by-list", "to-list");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
listFma = FlexibleMapAccessor.getInstance(element.getAttribute("list"));
|
||||
orderByListFma = FlexibleMapAccessor.getInstance(element.getAttribute("order-by-list"));
|
||||
String toListAttribute = element.getAttribute("to-list");
|
||||
if (toListAttribute.isEmpty()) {
|
||||
toListFma = listFma;
|
||||
} else {
|
||||
toListFma = FlexibleMapAccessor.getInstance(toListAttribute);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
List<String> orderByList = orderByListFma.get(methodContext.getEnvMap());
|
||||
toListFma.put(methodContext.getEnvMap(), EntityUtil.orderBy(listFma.get(methodContext.getEnvMap()), orderByList));
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<order-value-list ");
|
||||
sb.append("list=\"").append(this.listFma).append("\" ");
|
||||
sb.append("order-by-list=\"").append(this.orderByListFma).append("\" ");
|
||||
sb.append("to-list=\"").append(this.toListFma).append("\" ");
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <order-value-list> element.
|
||||
*/
|
||||
public static final class OrderValueListFactory implements Factory<OrderValueList> {
|
||||
@Override
|
||||
public OrderValueList createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new OrderValueList(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "order-value-list";
|
||||
}
|
||||
}
|
||||
}
|
||||
+94
@@ -0,0 +1,94 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.entity.GenericEntityException;
|
||||
import org.ofbiz.entity.GenericValue;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <refresh-value> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Crefreshvalue%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class RefreshValue extends MethodOperation {
|
||||
|
||||
public static final String module = RemoveValue.class.getName();
|
||||
|
||||
private final FlexibleMapAccessor<GenericValue> valueFma;
|
||||
|
||||
public RefreshValue(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "value-field", "do-cache-clear");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "value-field");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "value-field");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
valueFma = FlexibleMapAccessor.getInstance(element.getAttribute("value-field"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
GenericValue value = valueFma.get(methodContext.getEnvMap());
|
||||
if (value == null) {
|
||||
throw new MiniLangRuntimeException("Entity value not found with name: " + valueFma, this);
|
||||
}
|
||||
try {
|
||||
value.getDelegator().refresh(value);
|
||||
} catch (GenericEntityException e) {
|
||||
String errMsg = "Exception thrown while refreshing value: " + e.getMessage();
|
||||
Debug.logWarning(e, errMsg, module);
|
||||
simpleMethod.addErrorMessage(methodContext, errMsg);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<refresh-value ");
|
||||
sb.append("value-field=\"").append(this.valueFma).append("\" ");
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <refresh-value> element.
|
||||
*/
|
||||
public static final class RefreshValueFactory implements Factory<RefreshValue> {
|
||||
@Override
|
||||
public RefreshValue createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new RefreshValue(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "refresh-value";
|
||||
}
|
||||
}
|
||||
}
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.entity.Delegator;
|
||||
import org.ofbiz.entity.GenericEntityException;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <remove-by-and> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cremovebyand%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class RemoveByAnd extends EntityOperation {
|
||||
|
||||
public static final String module = RemoveByAnd.class.getName();
|
||||
private final FlexibleStringExpander entityNameFse;
|
||||
private final FlexibleMapAccessor<Map<String, ? extends Object>> mapFma;
|
||||
|
||||
public RemoveByAnd(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "entity-name", "map", "do-cache-clear", "delegator-name");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "entity-name", "map");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "map", "delegator-name");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
entityNameFse = FlexibleStringExpander.getInstance(element.getAttribute("entity-name"));
|
||||
mapFma = FlexibleMapAccessor.getInstance(element.getAttribute("map"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
@Deprecated
|
||||
String entityName = entityNameFse.expandString(methodContext.getEnvMap());
|
||||
if (entityName.isEmpty()) {
|
||||
throw new MiniLangRuntimeException("Entity name not found.", this);
|
||||
}
|
||||
try {
|
||||
Delegator delegator = getDelegator(methodContext);
|
||||
delegator.removeByAnd(entityName, mapFma.get(methodContext.getEnvMap()));
|
||||
} catch (GenericEntityException e) {
|
||||
String errMsg = "Exception thrown while removing entities: " + e.getMessage();
|
||||
Debug.logWarning(e, errMsg, module);
|
||||
simpleMethod.addErrorMessage(methodContext, errMsg);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
aic.addEntityName(entityNameFse.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<remove-by-and ");
|
||||
sb.append("entity-name=\"").append(this.entityNameFse).append("\" ");
|
||||
sb.append("map=\"").append(this.mapFma).append("\" ");
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <remove-by-and> element.
|
||||
*/
|
||||
public static final class RemoveByAndFactory implements Factory<RemoveByAnd> {
|
||||
@Override
|
||||
public RemoveByAnd createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new RemoveByAnd(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "remove-by-and";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.entity.Delegator;
|
||||
import org.ofbiz.entity.GenericEntityException;
|
||||
import org.ofbiz.entity.GenericValue;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <remove-list> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cremovelist%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class RemoveList extends EntityOperation {
|
||||
|
||||
public static final String module = RemoveList.class.getName();
|
||||
private final FlexibleMapAccessor<List<GenericValue>> listFma;
|
||||
|
||||
public RemoveList(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "list", "do-cache-clear", "delegator-name");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "list");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "list", "delegator-name");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
listFma = FlexibleMapAccessor.getInstance(element.getAttribute("list"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
List<GenericValue> values = listFma.get(methodContext.getEnvMap());
|
||||
if (values == null) {
|
||||
throw new MiniLangRuntimeException("Entity value list not found with name: " + listFma, this);
|
||||
}
|
||||
try {
|
||||
Delegator delegator = getDelegator(methodContext);
|
||||
delegator.removeAll(values);
|
||||
} catch (GenericEntityException e) {
|
||||
String errMsg = "Exception thrown while removing entities: " + e.getMessage();
|
||||
Debug.logWarning(e, errMsg, module);
|
||||
simpleMethod.addErrorMessage(methodContext, errMsg);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<remove-list ");
|
||||
sb.append("list=\"").append(this.listFma).append("\" ");
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <remove-list> element.
|
||||
*/
|
||||
public static final class RemoveListFactory implements Factory<RemoveList> {
|
||||
@Override
|
||||
public RemoveList createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new RemoveList(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "remove-list";
|
||||
}
|
||||
}
|
||||
}
|
||||
+104
@@ -0,0 +1,104 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.entity.GenericEntityException;
|
||||
import org.ofbiz.entity.GenericValue;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <remove-related> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cremoverelated%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class RemoveRelated extends MethodOperation {
|
||||
|
||||
public static final String module = RemoveRelated.class.getName();
|
||||
private final FlexibleStringExpander relationNameFse;
|
||||
private final FlexibleMapAccessor<GenericValue> valueFma;
|
||||
|
||||
public RemoveRelated(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "value-field", "relation-name", "do-cache-clear");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "value-field", "relation-name");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "value-field");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
valueFma = FlexibleMapAccessor.getInstance(element.getAttribute("value-field"));
|
||||
relationNameFse = FlexibleStringExpander.getInstance(element.getAttribute("relation-name"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
GenericValue value = valueFma.get(methodContext.getEnvMap());
|
||||
if (value == null) {
|
||||
throw new MiniLangRuntimeException("Entity value not found with name: " + valueFma, this);
|
||||
}
|
||||
String relationName = relationNameFse.expandString(methodContext.getEnvMap());
|
||||
try {
|
||||
value.getDelegator().removeRelated(relationName, value);
|
||||
} catch (GenericEntityException e) {
|
||||
String errMsg = "Exception thrown while removing related entities: " + e.getMessage();
|
||||
Debug.logWarning(e, errMsg, module);
|
||||
simpleMethod.addErrorMessage(methodContext, errMsg);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
aic.addEntityName(relationNameFse.toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<remove-related ");
|
||||
sb.append("value-field=\"").append(this.valueFma).append("\" ");
|
||||
sb.append("relation-name=\"").append(this.relationNameFse).append("\" ");
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <remove-related> element.
|
||||
*/
|
||||
public static final class RemoveRelatedFactory implements Factory<RemoveRelated> {
|
||||
@Override
|
||||
public RemoveRelated createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new RemoveRelated(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "remove-related";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.entity.GenericEntityException;
|
||||
import org.ofbiz.entity.GenericValue;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <remove-value> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cremovevalue%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class RemoveValue extends MethodOperation {
|
||||
|
||||
public static final String module = RemoveValue.class.getName();
|
||||
private final FlexibleMapAccessor<GenericValue> valueFma;
|
||||
|
||||
public RemoveValue(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "value-field", "do-cache-clear");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "value-field");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "value-field");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
valueFma = FlexibleMapAccessor.getInstance(element.getAttribute("value-field"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
GenericValue value = valueFma.get(methodContext.getEnvMap());
|
||||
if (value == null) {
|
||||
throw new MiniLangRuntimeException("Entity value not found with name: " + valueFma, this);
|
||||
}
|
||||
try {
|
||||
value.getDelegator().removeValue(value);
|
||||
} catch (GenericEntityException e) {
|
||||
String errMsg = "Exception thrown while removing entity value: " + e.getMessage();
|
||||
Debug.logWarning(e, errMsg, module);
|
||||
simpleMethod.addErrorMessage(methodContext, errMsg);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<remove-value ");
|
||||
sb.append("value-field=\"").append(this.valueFma).append("\" ");
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <remove-value> element.
|
||||
*/
|
||||
public static final class RemoveValueFactory implements Factory<RemoveValue> {
|
||||
@Override
|
||||
public RemoveValue createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new RemoveValue(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "remove-value";
|
||||
}
|
||||
}
|
||||
}
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.entity.Delegator;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <sequenced-id> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Csequencedid%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class SequencedIdToEnv extends EntityOperation {
|
||||
|
||||
private final FlexibleMapAccessor<Object> fieldFma;
|
||||
private final boolean getLongOnly;
|
||||
private final FlexibleStringExpander sequenceNameFse;
|
||||
private final long staggerMax;
|
||||
|
||||
public SequencedIdToEnv(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "sequence-name", "field", "get-long-only", "stagger-max", "delegator-name");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "sequence-name", "field");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "field", "delegator-name");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
sequenceNameFse = FlexibleStringExpander.getInstance(element.getAttribute("sequence-name"));
|
||||
fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field"));
|
||||
getLongOnly = "true".equals(element.getAttribute("get-long-only"));
|
||||
long staggerMax = 1;
|
||||
String staggerMaxAttribute = element.getAttribute("stagger-max");
|
||||
if (!staggerMaxAttribute.isEmpty()) {
|
||||
try {
|
||||
staggerMax = Long.parseLong(staggerMaxAttribute);
|
||||
if (staggerMax < 1) {
|
||||
staggerMax = 1;
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
MiniLangValidate.handleError("Invalid stagger-max attribute value: " + e.getMessage(), simpleMethod, element);
|
||||
}
|
||||
}
|
||||
this.staggerMax = staggerMax;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
String seqName = sequenceNameFse.expandString(methodContext.getEnvMap());
|
||||
Delegator delegator = getDelegator(methodContext);
|
||||
if (getLongOnly) {
|
||||
fieldFma.put(methodContext.getEnvMap(), delegator.getNextSeqIdLong(seqName, staggerMax));
|
||||
} else {
|
||||
fieldFma.put(methodContext.getEnvMap(), delegator.getNextSeqId(seqName, staggerMax));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<sequenced-id ");
|
||||
sb.append("sequence-name=\"").append(this.sequenceNameFse).append("\" ");
|
||||
sb.append("field=\"").append(this.fieldFma).append("\" ");
|
||||
sb.append("stagger-max=\"").append(this.staggerMax).append("\" ");
|
||||
if (this.getLongOnly) {
|
||||
sb.append("get-long-only=\"true\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <sequenced-id> element.
|
||||
*/
|
||||
public static final class SequencedIdFactory implements Factory<SequencedIdToEnv> {
|
||||
@Override
|
||||
public SequencedIdToEnv createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new SequencedIdToEnv(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "sequenced-id";
|
||||
}
|
||||
}
|
||||
}
|
||||
+83
@@ -0,0 +1,83 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.entity.GenericValue;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <set-current-user-login> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Csetcurrentuserlogin%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class SetCurrentUserLogin extends MethodOperation {
|
||||
|
||||
private final FlexibleMapAccessor<GenericValue> valueFma;
|
||||
|
||||
public SetCurrentUserLogin(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.handleError("Deprecated - use the called service's userLogin IN attribute", simpleMethod, element);
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "value-field");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "value-field");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "value-field");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
valueFma = FlexibleMapAccessor.getInstance(element.getAttribute("value-field"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
GenericValue userLogin = valueFma.get(methodContext.getEnvMap());
|
||||
if (userLogin == null) {
|
||||
throw new MiniLangRuntimeException("Entity value not found with name: " + valueFma, this);
|
||||
}
|
||||
methodContext.setUserLogin(userLogin, this.simpleMethod.getUserLoginEnvName());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<set-current-user-login ");
|
||||
sb.append("value-field=\"").append(this.valueFma).append("\" />");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <set-current-user-login> element.
|
||||
*/
|
||||
public static final class SetCurrentUserLoginFactory implements Factory<SetCurrentUserLogin> {
|
||||
@Override
|
||||
public SetCurrentUserLogin createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new SetCurrentUserLogin(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "set-current-user-login";
|
||||
}
|
||||
}
|
||||
}
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.entity.GenericValue;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <set-nonpk-fields> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Csetnonpkfields%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class SetNonpkFields extends MethodOperation {
|
||||
|
||||
private final FlexibleMapAccessor<Map<String, ? extends Object>> mapFma;
|
||||
private final FlexibleStringExpander setIfNullFse;
|
||||
private final FlexibleMapAccessor<GenericValue> valueFma;
|
||||
|
||||
public SetNonpkFields(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "value-field", "set-if-null", "map");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "value-field", "map");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "value-field", "map");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
valueFma = FlexibleMapAccessor.getInstance(element.getAttribute("value-field"));
|
||||
setIfNullFse = FlexibleStringExpander.getInstance(element.getAttribute("set-if-null"));
|
||||
mapFma = FlexibleMapAccessor.getInstance(element.getAttribute("map"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
GenericValue value = valueFma.get(methodContext.getEnvMap());
|
||||
if (value == null) {
|
||||
throw new MiniLangRuntimeException("Entity value not found with name: " + valueFma, this);
|
||||
}
|
||||
Map<String, ? extends Object> theMap = mapFma.get(methodContext.getEnvMap());
|
||||
if (theMap == null) {
|
||||
throw new MiniLangRuntimeException("Map not found with name: " + mapFma, this);
|
||||
}
|
||||
boolean setIfNull = !"false".equals(setIfNullFse.expand(methodContext.getEnvMap()));
|
||||
value.setNonPKFields(theMap, setIfNull);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<set-nonpk-fields ");
|
||||
sb.append("value-field=\"").append(this.valueFma).append("\" ");
|
||||
sb.append("map=\"").append(this.mapFma).append("\" ");
|
||||
if (!setIfNullFse.isEmpty()) {
|
||||
sb.append("set-if-null=\"").append(this.setIfNullFse).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <set-nonpk-fields> element.
|
||||
*/
|
||||
public static final class SetNonpkFieldsFactory implements Factory<SetNonpkFields> {
|
||||
@Override
|
||||
public SetNonpkFields createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new SetNonpkFields(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "set-nonpk-fields";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.entity.GenericValue;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <set-pk-fields> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Csetpkfields%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class SetPkFields extends MethodOperation {
|
||||
|
||||
private final FlexibleMapAccessor<Map<String, ? extends Object>> mapFma;
|
||||
private final FlexibleStringExpander setIfNullFse;
|
||||
private final FlexibleMapAccessor<GenericValue> valueFma;
|
||||
|
||||
public SetPkFields(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "value-field", "set-if-null", "map");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "value-field", "map");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "value-field", "map");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
valueFma = FlexibleMapAccessor.getInstance(element.getAttribute("value-field"));
|
||||
setIfNullFse = FlexibleStringExpander.getInstance(element.getAttribute("set-if-null"));
|
||||
mapFma = FlexibleMapAccessor.getInstance(element.getAttribute("map"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
GenericValue value = valueFma.get(methodContext.getEnvMap());
|
||||
if (value == null) {
|
||||
throw new MiniLangRuntimeException("Entity value not found with name: " + valueFma, this);
|
||||
}
|
||||
Map<String, ? extends Object> theMap = mapFma.get(methodContext.getEnvMap());
|
||||
if (theMap == null) {
|
||||
throw new MiniLangRuntimeException("Map not found with name: " + mapFma, this);
|
||||
}
|
||||
boolean setIfNull = !"false".equals(setIfNullFse.expand(methodContext.getEnvMap()));
|
||||
value.setPKFields(theMap, setIfNull);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<set-pk-fields ");
|
||||
sb.append("value-field=\"").append(this.valueFma).append("\" ");
|
||||
sb.append("map=\"").append(this.mapFma).append("\" ");
|
||||
if (!setIfNullFse.isEmpty()) {
|
||||
sb.append("set-if-null=\"").append(this.setIfNullFse).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <set-pk-fields> element.
|
||||
*/
|
||||
public static final class SetPkFieldsFactory implements Factory<SetPkFields> {
|
||||
@Override
|
||||
public SetPkFields createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new SetPkFields(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "set-pk-fields";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.entity.Delegator;
|
||||
import org.ofbiz.entity.GenericEntityException;
|
||||
import org.ofbiz.entity.GenericValue;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <store-list> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cstorelist%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class StoreList extends EntityOperation {
|
||||
|
||||
public static final String module = StoreList.class.getName();
|
||||
private final FlexibleMapAccessor<List<GenericValue>> listFma;
|
||||
|
||||
public StoreList(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "list", "do-cache-clear", "delegator-name");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "list");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "list", "delegator-name");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
listFma = FlexibleMapAccessor.getInstance(element.getAttribute("list"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
List<GenericValue> values = listFma.get(methodContext.getEnvMap());
|
||||
if (values == null) {
|
||||
throw new MiniLangRuntimeException("Entity value list not found with name: " + listFma, this);
|
||||
}
|
||||
try {
|
||||
Delegator delegator = getDelegator(methodContext);
|
||||
delegator.storeAll(values);
|
||||
} catch (GenericEntityException e) {
|
||||
String errMsg = "Exception thrown while storing entities: " + e.getMessage();
|
||||
Debug.logWarning(e, errMsg, module);
|
||||
simpleMethod.addErrorMessage(methodContext, errMsg);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<store-list ");
|
||||
sb.append("list=\"").append(this.listFma).append("\" ");
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <store-list> element.
|
||||
*/
|
||||
public static final class StoreListFactory implements Factory<StoreList> {
|
||||
@Override
|
||||
public StoreList createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new StoreList(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "store-list";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.entity.GenericEntityException;
|
||||
import org.ofbiz.entity.GenericValue;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <store-value> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cstorevalue%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class StoreValue extends MethodOperation {
|
||||
|
||||
public static final String module = StoreValue.class.getName();
|
||||
private final FlexibleMapAccessor<GenericValue> valueFma;
|
||||
|
||||
public StoreValue(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "value-field", "do-cache-clear");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "value-field");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "value-field");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
valueFma = FlexibleMapAccessor.getInstance(element.getAttribute("value-field"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
GenericValue value = valueFma.get(methodContext.getEnvMap());
|
||||
if (value == null) {
|
||||
throw new MiniLangRuntimeException("Entity value not found with name: " + valueFma, this);
|
||||
}
|
||||
try {
|
||||
value.getDelegator().store(value);
|
||||
} catch (GenericEntityException e) {
|
||||
String errMsg = "Exception thrown while storing entity value: " + e.getMessage();
|
||||
Debug.logWarning(e, errMsg, module);
|
||||
simpleMethod.addErrorMessage(methodContext, errMsg);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<store-value ");
|
||||
sb.append("value-field=\"").append(this.valueFma).append("\" ");
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <store-value> element.
|
||||
*/
|
||||
public static final class StoreValueFactory implements Factory<StoreValue> {
|
||||
@Override
|
||||
public StoreValue createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new StoreValue(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "store-value";
|
||||
}
|
||||
}
|
||||
}
|
||||
+89
@@ -0,0 +1,89 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.entity.transaction.GenericTransactionException;
|
||||
import org.ofbiz.entity.transaction.TransactionUtil;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <transaction-begin> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Ctransactionbegin%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class TransactionBegin extends MethodOperation {
|
||||
|
||||
public static final String module = TransactionBegin.class.getName();
|
||||
|
||||
private final FlexibleMapAccessor<Boolean> beganTransactionFma;
|
||||
|
||||
public TransactionBegin(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "began-transaction-name");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "began-transaction-name");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
beganTransactionFma = FlexibleMapAccessor.getInstance(MiniLangValidate.checkAttribute(element.getAttribute("began-transaction-name"), "beganTransaction"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
boolean beganTransaction = false;
|
||||
try {
|
||||
beganTransaction = TransactionUtil.begin();
|
||||
} catch (GenericTransactionException e) {
|
||||
String errMsg = "Exception thrown while beginning transaction: " + e.getMessage();
|
||||
Debug.logWarning(e, errMsg, module);
|
||||
simpleMethod.addErrorMessage(methodContext, errMsg);
|
||||
return false;
|
||||
}
|
||||
beganTransactionFma.put(methodContext.getEnvMap(), beganTransaction);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<transaction-begin ");
|
||||
sb.append("began-transaction-name=\"").append(this.beganTransactionFma).append("\" />");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <transaction-begin> element.
|
||||
*/
|
||||
public static final class TransactionBeginFactory implements Factory<TransactionBegin> {
|
||||
@Override
|
||||
public TransactionBegin createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new TransactionBegin(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "transaction-begin";
|
||||
}
|
||||
}
|
||||
}
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.entity.transaction.GenericTransactionException;
|
||||
import org.ofbiz.entity.transaction.TransactionUtil;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <transaction-commit> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Ctransactioncommit%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class TransactionCommit extends MethodOperation {
|
||||
|
||||
public static final String module = TransactionCommit.class.getName();
|
||||
|
||||
private final FlexibleMapAccessor<Boolean> beganTransactionFma;
|
||||
|
||||
public TransactionCommit(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "began-transaction-name");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "began-transaction-name");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
beganTransactionFma = FlexibleMapAccessor.getInstance(MiniLangValidate.checkAttribute(element.getAttribute("began-transaction-name"), "beganTransaction"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
boolean beganTransaction = false;
|
||||
Boolean beganTransactionBoolean = beganTransactionFma.get(methodContext.getEnvMap());
|
||||
if (beganTransactionBoolean != null) {
|
||||
beganTransaction = beganTransactionBoolean.booleanValue();
|
||||
}
|
||||
try {
|
||||
TransactionUtil.commit(beganTransaction);
|
||||
} catch (GenericTransactionException e) {
|
||||
String errMsg = "Exception thrown while committing transaction: " + e.getMessage();
|
||||
Debug.logWarning(e, errMsg, module);
|
||||
simpleMethod.addErrorMessage(methodContext, errMsg);
|
||||
return false;
|
||||
}
|
||||
beganTransactionFma.remove(methodContext.getEnvMap());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<transaction-commit ");
|
||||
sb.append("began-transaction-name=\"").append(this.beganTransactionFma).append("\" />");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <transaction-commit> element.
|
||||
*/
|
||||
public static final class TransactionCommitFactory implements Factory<TransactionCommit> {
|
||||
@Override
|
||||
public TransactionCommit createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new TransactionCommit(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "transaction-commit";
|
||||
}
|
||||
}
|
||||
}
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.entityops;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.entity.transaction.GenericTransactionException;
|
||||
import org.ofbiz.entity.transaction.TransactionUtil;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <transaction-rollback> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Ctransactionrollback%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class TransactionRollback extends MethodOperation {
|
||||
|
||||
public static final String module = TransactionRollback.class.getName();
|
||||
|
||||
private final FlexibleMapAccessor<Boolean> beganTransactionFma;
|
||||
|
||||
public TransactionRollback(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "began-transaction-name");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "began-transaction-name");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
beganTransactionFma = FlexibleMapAccessor.getInstance(MiniLangValidate.checkAttribute(element.getAttribute("began-transaction-name"), "beganTransaction"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
boolean beganTransaction = false;
|
||||
Boolean beganTransactionBoolean = beganTransactionFma.get(methodContext.getEnvMap());
|
||||
if (beganTransactionBoolean != null) {
|
||||
beganTransaction = beganTransactionBoolean.booleanValue();
|
||||
}
|
||||
try {
|
||||
TransactionUtil.rollback(beganTransaction, "Explicit rollback in simple-method [" + this.simpleMethod.getShortDescription() + "]", null);
|
||||
} catch (GenericTransactionException e) {
|
||||
String errMsg = "Exception thrown while rolling back transaction: " + e.getMessage();
|
||||
Debug.logWarning(e, errMsg, module);
|
||||
simpleMethod.addErrorMessage(methodContext, errMsg);
|
||||
return false;
|
||||
}
|
||||
beganTransactionFma.remove(methodContext.getEnvMap());
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<transaction-rollback ");
|
||||
sb.append("began-transaction-name=\"").append(this.beganTransactionFma).append("\" />");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <transaction-rollback> element.
|
||||
*/
|
||||
public static final class TransactionRollbackFactory implements Factory<TransactionRollback> {
|
||||
@Override
|
||||
public TransactionRollback createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new TransactionRollback(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "transaction-rollback";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.envops;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MessageElement;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <add-error> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cadderror%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class AddError extends MethodOperation {
|
||||
|
||||
private final FlexibleMapAccessor<List<String>> errorListFma;
|
||||
private final MessageElement messageElement;
|
||||
|
||||
public AddError(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "error-list-name");
|
||||
MiniLangValidate.constantAttributes(simpleMethod, element, "error-list-name");
|
||||
MiniLangValidate.childElements(simpleMethod, element, "fail-message", "fail-property");
|
||||
MiniLangValidate.requireAnyChildElement(simpleMethod, element, "fail-message", "fail-property");
|
||||
}
|
||||
errorListFma = FlexibleMapAccessor.getInstance(MiniLangValidate.checkAttribute(element.getAttribute("error-list-name"), "error_list"));
|
||||
messageElement = MessageElement.fromParentElement(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
if (messageElement != null) {
|
||||
String message = messageElement.getMessage(methodContext);
|
||||
if (message != null) {
|
||||
List<String> messages = errorListFma.get(methodContext.getEnvMap());
|
||||
if (messages == null) {
|
||||
messages = new LinkedList<String>();
|
||||
errorListFma.put(methodContext.getEnvMap(), messages);
|
||||
}
|
||||
messages.add(message);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<add-error ");
|
||||
if (!"error_list".equals(this.errorListFma.getOriginalName())) {
|
||||
sb.append("error-list-name=\"").append(this.errorListFma).append("\"");
|
||||
}
|
||||
if (messageElement != null) {
|
||||
sb.append(">").append(messageElement).append("</add-error>");
|
||||
} else {
|
||||
sb.append("/>");
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <add-error> element.
|
||||
*/
|
||||
public static final class AddErrorFactory implements Factory<AddError> {
|
||||
@Override
|
||||
public AddError createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new AddError(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "add-error";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.envops;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.util.UtilXml;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.ofbiz.minilang.method.conditional.Conditional;
|
||||
import org.ofbiz.minilang.method.conditional.ConditionalFactory;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <assert> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cassert%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class Assert extends MethodOperation {
|
||||
|
||||
public static final String module = Assert.class.getName();
|
||||
|
||||
private final List<Conditional> conditionalList;
|
||||
private final FlexibleMapAccessor<List<Object>> errorListFma;
|
||||
private final FlexibleStringExpander titleExdr;
|
||||
|
||||
public Assert(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "title", "error-list-name");
|
||||
MiniLangValidate.constantAttributes(simpleMethod, element, "title", "error-list-name");
|
||||
}
|
||||
errorListFma = FlexibleMapAccessor.getInstance(MiniLangValidate.checkAttribute(element.getAttribute("error-list-name"), "error_list"));
|
||||
titleExdr = FlexibleStringExpander.getInstance(element.getAttribute("title"));
|
||||
List<? extends Element> childElements = UtilXml.childElementList(element);
|
||||
if (MiniLangValidate.validationOn() && childElements.isEmpty()) {
|
||||
MiniLangValidate.handleError("No conditional elements.", simpleMethod, element);
|
||||
}
|
||||
List<Conditional> conditionalList = new ArrayList<Conditional>(childElements.size());
|
||||
for (Element conditionalElement : UtilXml.childElementList(element)) {
|
||||
conditionalList.add(ConditionalFactory.makeConditional(conditionalElement, simpleMethod));
|
||||
}
|
||||
this.conditionalList = Collections.unmodifiableList(conditionalList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "Begin assert.");
|
||||
}
|
||||
List<Object> messages = errorListFma.get(methodContext.getEnvMap());
|
||||
String title = titleExdr.expandString(methodContext.getEnvMap());
|
||||
for (Conditional condition : conditionalList) {
|
||||
if (!condition.checkCondition(methodContext)) {
|
||||
StringBuilder messageBuffer = new StringBuilder("Assertion ");
|
||||
if (!title.isEmpty()) {
|
||||
messageBuffer.append("[");
|
||||
messageBuffer.append(title);
|
||||
messageBuffer.append("] ");
|
||||
}
|
||||
messageBuffer.append("failed: ");
|
||||
condition.prettyPrint(messageBuffer, methodContext);
|
||||
if (messages == null) {
|
||||
messages = new LinkedList<Object>();
|
||||
errorListFma.put(methodContext.getEnvMap(), messages);
|
||||
}
|
||||
messages.add(messageBuffer.toString());
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "Condition evaluated to false: " + condition + ", adding error message.");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "End assert.");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder messageBuf = new StringBuilder("<assert");
|
||||
if (!titleExdr.isEmpty()) {
|
||||
messageBuf.append(" title=\"");
|
||||
messageBuf.append(titleExdr);
|
||||
messageBuf.append("\"");
|
||||
}
|
||||
messageBuf.append(">");
|
||||
for (Conditional condition : conditionalList) {
|
||||
messageBuf.append(condition);
|
||||
}
|
||||
messageBuf.append("</assert>");
|
||||
return messageBuf.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <assert> element.
|
||||
*/
|
||||
public static final class AssertFactory implements Factory<Assert> {
|
||||
@Override
|
||||
public Assert createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new Assert(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "assert";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.envops;
|
||||
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <break> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cbreak%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public class Break extends MethodOperation {
|
||||
|
||||
public Break(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
throw new BreakElementException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "<break/>";
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class BreakElementException extends MiniLangException {
|
||||
|
||||
public BreakElementException() {
|
||||
super("<break> element encountered without enclosing loop");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
StringBuilder sb = new StringBuilder(super.getMessage());
|
||||
SimpleMethod method = getSimpleMethod();
|
||||
sb.append(" Method = ").append(method.getMethodName()).append(", File = ").append(method.getFromLocation());
|
||||
sb.append(", Element = <break>, Line ").append(getLineNumber());
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <break> element.
|
||||
*/
|
||||
public static final class BreakFactory implements Factory<Break> {
|
||||
@Override
|
||||
public Break createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new Break(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "break";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.envops;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <check-errors> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Ccheckerrors%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class CheckErrors extends MethodOperation {
|
||||
|
||||
private final FlexibleStringExpander errorCodeFse;
|
||||
private final FlexibleStringExpander errorListNameFse;
|
||||
|
||||
public CheckErrors(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "error-code", "error-list-name");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
this.errorCodeFse = FlexibleStringExpander.getInstance(element.getAttribute("error-code"));
|
||||
this.errorListNameFse = FlexibleStringExpander.getInstance(MiniLangValidate.checkAttribute(element.getAttribute("error-list-name"), "error_list"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "Begin check-errors.");
|
||||
}
|
||||
List<Object> messages = methodContext.getEnv(this.errorListNameFse.expandString(methodContext.getEnvMap()));
|
||||
if (messages != null && messages.size() > 0) {
|
||||
if (methodContext.getMethodType() == MethodContext.EVENT) {
|
||||
methodContext.putEnv(simpleMethod.getEventErrorMessageListName(), messages);
|
||||
methodContext.putEnv(this.simpleMethod.getEventResponseCodeName(), getErrorCode(methodContext));
|
||||
} else {
|
||||
methodContext.putEnv(simpleMethod.getServiceErrorMessageListName(), messages);
|
||||
methodContext.putEnv(this.simpleMethod.getServiceResponseMessageName(), getErrorCode(methodContext));
|
||||
}
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "Found error messages. Setting error status and halting script execution.");
|
||||
outputTraceMessage(methodContext, "End check-errors.");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
if (methodContext.isTraceOn()) {
|
||||
outputTraceMessage(methodContext, "No error messages found. Continuing script execution.");
|
||||
outputTraceMessage(methodContext, "End check-errors.");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private String getErrorCode(MethodContext methodContext) {
|
||||
String errorCode = this.errorCodeFse.expandString(methodContext.getEnvMap());
|
||||
if (errorCode.isEmpty()) {
|
||||
errorCode = this.simpleMethod.getDefaultErrorCode();
|
||||
}
|
||||
return errorCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<check-errors ");
|
||||
if (!this.errorCodeFse.isEmpty()) {
|
||||
sb.append("error-code=\"").append(this.errorCodeFse).append("\" ");
|
||||
}
|
||||
if (!"error_list".equals(this.errorListNameFse.getOriginal())) {
|
||||
sb.append("error-list-name=\"").append(this.errorListNameFse).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <check-errors> element.
|
||||
*/
|
||||
public static final class CheckErrorsFactory implements Factory<CheckErrors> {
|
||||
@Override
|
||||
public CheckErrors createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new CheckErrors(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "check-errors";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.envops;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.util.UtilProperties;
|
||||
import org.ofbiz.base.util.UtilValidate;
|
||||
import org.ofbiz.base.util.UtilXml;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <check-id> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Ccheckid%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class CheckId extends MethodOperation {
|
||||
|
||||
public static final String module = CheckId.class.getName();
|
||||
|
||||
private final FlexibleMapAccessor<List<String>> errorListFma;
|
||||
private final FlexibleMapAccessor<Object> fieldFma;
|
||||
private final FlexibleStringExpander messageFse;
|
||||
private final String propertykey;
|
||||
private final String propertyResource;
|
||||
|
||||
public CheckId(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "field", "error-list-name");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "field");
|
||||
MiniLangValidate.constantAttributes(simpleMethod, element, "error-list-name");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "field");
|
||||
MiniLangValidate.childElements(simpleMethod, element, "fail-message", "fail-property");
|
||||
}
|
||||
this.errorListFma = FlexibleMapAccessor.getInstance(MiniLangValidate.checkAttribute(element.getAttribute("error-list-name"), "error_list"));
|
||||
this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field"));
|
||||
Element childElement = UtilXml.firstChildElement(element, "fail-message");
|
||||
if (childElement != null) {
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, childElement, "message");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, childElement, "message");
|
||||
MiniLangValidate.constantPlusExpressionAttributes(simpleMethod, childElement, "message");
|
||||
}
|
||||
this.messageFse = FlexibleStringExpander.getInstance(childElement.getAttribute("message"));
|
||||
this.propertykey = null;
|
||||
this.propertyResource = null;
|
||||
} else {
|
||||
childElement = UtilXml.firstChildElement(element, "fail-property");
|
||||
if (childElement != null) {
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, childElement, "property", "resource");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, childElement, "property", "resource");
|
||||
MiniLangValidate.constantAttributes(simpleMethod, childElement, "property", "resource");
|
||||
}
|
||||
this.messageFse = FlexibleStringExpander.getInstance(null);
|
||||
this.propertykey = childElement.getAttribute("property");
|
||||
this.propertyResource = childElement.getAttribute("resource");
|
||||
} else {
|
||||
this.messageFse = FlexibleStringExpander.getInstance(null);
|
||||
this.propertykey = null;
|
||||
this.propertyResource = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
String errorMsg = null;
|
||||
Object fieldVal = fieldFma.get(methodContext.getEnvMap());
|
||||
if (fieldVal == null) {
|
||||
errorMsg = "field \"" + fieldFma + "\" is null";
|
||||
} else {
|
||||
errorMsg = UtilValidate.checkValidDatabaseId(fieldVal.toString());
|
||||
}
|
||||
if (errorMsg != null) {
|
||||
String message = null;
|
||||
if (!this.messageFse.isEmpty()) {
|
||||
message = this.messageFse.expandString(methodContext.getEnvMap());
|
||||
} else if (this.propertyResource != null) {
|
||||
message = UtilProperties.getMessage(this.propertyResource, this.propertykey, methodContext.getEnvMap(), methodContext.getLocale());
|
||||
}
|
||||
if (message != null) {
|
||||
List<String> messages = errorListFma.get(methodContext.getEnvMap());
|
||||
if (messages == null) {
|
||||
messages = new LinkedList<String>();
|
||||
}
|
||||
errorListFma.put(methodContext.getEnvMap(), messages);
|
||||
messages.add(message.concat(": ").concat(errorMsg));
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<check-id ");
|
||||
if (!this.fieldFma.isEmpty()) {
|
||||
sb.append("field=\"").append(this.fieldFma).append("\" ");
|
||||
}
|
||||
if (!"error_list".equals(this.errorListFma.getOriginalName())) {
|
||||
sb.append("error-list-name=\"").append(this.errorListFma).append("\" ");
|
||||
}
|
||||
sb.append(">");
|
||||
if (!this.messageFse.isEmpty()) {
|
||||
sb.append("<fail-message message=\"").append(this.messageFse).append("\" />");
|
||||
}
|
||||
if (this.propertykey != null) {
|
||||
sb.append("<fail-property property=\"").append(this.propertykey).append(" resource=\"").append(this.propertyResource).append("\" />");
|
||||
}
|
||||
sb.append("</check-id>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <check-id> element.
|
||||
*/
|
||||
public static final class CheckIdFactory implements Factory<CheckId> {
|
||||
@Override
|
||||
public CheckId createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new CheckId(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "check-id";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.envops;
|
||||
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <clear-field> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{<clearfield>}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class ClearField extends MethodOperation {
|
||||
|
||||
private final FlexibleMapAccessor<Object> fieldFma;
|
||||
|
||||
public ClearField(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "field");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "field");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "field");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
fieldFma.put(methodContext.getEnvMap(), null);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<set ");
|
||||
sb.append("field=\"").append(this.fieldFma).append("\" />");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <clear-field> element.
|
||||
*/
|
||||
public static final class ClearFieldFactory implements Factory<ClearField> {
|
||||
@Override
|
||||
public ClearField createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new ClearField(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "clear-field";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.envops;
|
||||
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <continue> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Ccontinue%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public class Continue extends MethodOperation {
|
||||
|
||||
public Continue(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
throw new ContinueElementException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "<continue/>";
|
||||
}
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class ContinueElementException extends MiniLangException {
|
||||
|
||||
public ContinueElementException() {
|
||||
super("<continue> element encountered without enclosing loop");
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getMessage() {
|
||||
StringBuilder sb = new StringBuilder(super.getMessage());
|
||||
SimpleMethod method = getSimpleMethod();
|
||||
sb.append(" Method = ").append(method.getMethodName()).append(", File = ").append(method.getFromLocation());
|
||||
sb.append(", Element = <continue>, Line ").append(getLineNumber());
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <continue> element.
|
||||
*/
|
||||
public static final class ContinueFactory implements Factory<Continue> {
|
||||
@Override
|
||||
public Continue createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new Continue(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "continue";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.envops;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.ObjectType;
|
||||
import org.ofbiz.base.util.UtilXml;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.FieldObject;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodObject;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.ofbiz.minilang.method.StringObject;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <create-object> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{<createobject>}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class CreateObject extends MethodOperation {
|
||||
|
||||
public static final String module = CreateObject.class.getName();
|
||||
|
||||
private final String className;
|
||||
private final Class<?> targetClass;
|
||||
private final FlexibleMapAccessor<Object> fieldFma;
|
||||
private final List<MethodObject<?>> parameters;
|
||||
|
||||
public CreateObject(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.handleError("<create-object> element is deprecated (use <script>)", simpleMethod, element);
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "class-name", "field");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "field");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "class-name", "field");
|
||||
MiniLangValidate.childElements(simpleMethod, element, "string", "field");
|
||||
}
|
||||
className = element.getAttribute("class-name");
|
||||
Class<?> targetClass = null;
|
||||
try {
|
||||
targetClass = ObjectType.loadClass(this.className);
|
||||
} catch (ClassNotFoundException e) {
|
||||
MiniLangValidate.handleError("Class not found with name " + this.className, simpleMethod, element);
|
||||
}
|
||||
this.targetClass = targetClass;
|
||||
fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field"));
|
||||
List<? extends Element> parameterElements = UtilXml.childElementList(element);
|
||||
if (parameterElements.size() > 0) {
|
||||
ArrayList<MethodObject<?>> parameterList = new ArrayList<MethodObject<?>>(parameterElements.size());
|
||||
for (Element parameterElement : parameterElements) {
|
||||
if ("string".equals(parameterElement.getNodeName())) {
|
||||
parameterList.add(new StringObject(parameterElement, simpleMethod));
|
||||
} else if ("field".equals(parameterElement.getNodeName())) {
|
||||
parameterList.add(new FieldObject<Object>(parameterElement, simpleMethod));
|
||||
}
|
||||
}
|
||||
parameterList.trimToSize();
|
||||
this.parameters = Collections.unmodifiableList(parameterList);
|
||||
} else {
|
||||
this.parameters = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
Object[] args = null;
|
||||
Class<?>[] parameterTypes = null;
|
||||
if (parameters != null) {
|
||||
args = new Object[parameters.size()];
|
||||
parameterTypes = new Class<?>[parameters.size()];
|
||||
int i = 0;
|
||||
for (MethodObject<?> methodObjectDef : parameters) {
|
||||
args[i] = methodObjectDef.getObject(methodContext);
|
||||
Class<?> typeClass = null;
|
||||
try {
|
||||
typeClass = methodObjectDef.getTypeClass(methodContext);
|
||||
} catch (ClassNotFoundException e) {
|
||||
String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [Parameter type not found with name " + methodObjectDef.getTypeName() + "]";
|
||||
Debug.logWarning(e, errMsg, module);
|
||||
simpleMethod.addErrorMessage(methodContext, errMsg);
|
||||
return false;
|
||||
}
|
||||
parameterTypes[i] = typeClass;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
try {
|
||||
Constructor<?> constructor = targetClass.getConstructor(parameterTypes);
|
||||
fieldFma.put(methodContext.getEnvMap(),constructor.newInstance(args));
|
||||
} catch (Exception e) {
|
||||
throw new MiniLangRuntimeException(e, this);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<create-object ");
|
||||
sb.append("class-name=\"").append(this.className).append("\" ");
|
||||
sb.append("field=\"").append(this.fieldFma).append("\" />");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <create-object> element.
|
||||
*/
|
||||
public static final class CreateObjectFactory implements Factory<CreateObject> {
|
||||
@Override
|
||||
public CreateObject createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new CreateObject(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "create-object";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.envops;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <field-to-list> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cfieldtolist%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class FieldToList extends MethodOperation {
|
||||
|
||||
private final FlexibleMapAccessor<Object> fieldFma;
|
||||
private final FlexibleMapAccessor<List<Object>> listFma;
|
||||
|
||||
public FieldToList(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.handleError("<field-to-list> element is deprecated (use <set>)", simpleMethod, element);
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "field", "list");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "field", "list");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "field", "list");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field"));
|
||||
listFma = FlexibleMapAccessor.getInstance(element.getAttribute("list"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
Object fieldVal = fieldFma.get(methodContext.getEnvMap());
|
||||
if (fieldVal != null) {
|
||||
List<Object> toList = listFma.get(methodContext.getEnvMap());
|
||||
if (toList == null) {
|
||||
toList = new LinkedList<Object>();
|
||||
listFma.put(methodContext.getEnvMap(), toList);
|
||||
}
|
||||
toList.add(fieldVal);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<field-to-list ");
|
||||
sb.append("field=\"").append(this.fieldFma).append("\" ");
|
||||
sb.append("list=\"").append(this.listFma).append("\" />");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <field-to-list> element.
|
||||
*/
|
||||
public static final class FieldToListFactory implements Factory<FieldToList> {
|
||||
@Override
|
||||
public FieldToList createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new FieldToList(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "field-to-list";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.envops;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.util.UtilValidate;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <first-from-list> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cfirstfromlist%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class FirstFromList extends MethodOperation {
|
||||
|
||||
private final FlexibleMapAccessor<Object> entryFma;
|
||||
private final FlexibleMapAccessor<List<Object>> listFma;
|
||||
|
||||
public FirstFromList(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.handleError("<first-from-list> element is deprecated (use <set>)", simpleMethod, element);
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "entry", "list");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "entry", "list");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "entry", "list");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
entryFma = FlexibleMapAccessor.getInstance(element.getAttribute("entry"));
|
||||
listFma = FlexibleMapAccessor.getInstance(element.getAttribute("list"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
List<? extends Object> theList = listFma.get(methodContext.getEnvMap());
|
||||
if (UtilValidate.isEmpty(theList)) {
|
||||
entryFma.put(methodContext.getEnvMap(), null);
|
||||
} else {
|
||||
entryFma.put(methodContext.getEnvMap(), theList.get(0));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<first-from-list ");
|
||||
sb.append("entry=\"").append(this.entryFma).append("\" ");
|
||||
sb.append("list=\"").append(this.listFma).append("\" />");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <first-from-list> element.
|
||||
*/
|
||||
public static final class FirstFromListFactory implements Factory<FirstFromList> {
|
||||
@Override
|
||||
public FirstFromList createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new FirstFromList(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "first-from-list";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,202 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.envops;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.UtilGenerics;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.entity.GenericEntityException;
|
||||
import org.ofbiz.entity.GenericValue;
|
||||
import org.ofbiz.entity.util.EntityListIterator;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.ofbiz.minilang.method.envops.Break.BreakElementException;
|
||||
import org.ofbiz.minilang.method.envops.Continue.ContinueElementException;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <iterate> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Citerate%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class Iterate extends MethodOperation {
|
||||
|
||||
public static final String module = Iterate.class.getName();
|
||||
|
||||
private final FlexibleMapAccessor<Object> entryFma;
|
||||
private final FlexibleMapAccessor<Object> listFma;
|
||||
private final List<MethodOperation> subOps;
|
||||
|
||||
public Iterate(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "entry", "list");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "entry", "list");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "entry", "list");
|
||||
}
|
||||
this.entryFma = FlexibleMapAccessor.getInstance(element.getAttribute("entry"));
|
||||
this.listFma = FlexibleMapAccessor.getInstance(element.getAttribute("list"));
|
||||
this.subOps = Collections.unmodifiableList(SimpleMethod.readOperations(element, simpleMethod));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
if (listFma.isEmpty()) {
|
||||
if (Debug.verboseOn())
|
||||
Debug.logVerbose("Collection not found, doing nothing: " + this, module);
|
||||
return true;
|
||||
}
|
||||
Object oldEntryValue = entryFma.get(methodContext.getEnvMap());
|
||||
Object objList = listFma.get(methodContext.getEnvMap());
|
||||
if (objList instanceof EntityListIterator) {
|
||||
EntityListIterator eli = (EntityListIterator) objList;
|
||||
GenericValue theEntry;
|
||||
try {
|
||||
while ((theEntry = eli.next()) != null) {
|
||||
entryFma.put(methodContext.getEnvMap(), theEntry);
|
||||
try {
|
||||
for (MethodOperation methodOperation : subOps) {
|
||||
if (!methodOperation.exec(methodContext)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} catch (MiniLangException e) {
|
||||
if (e instanceof BreakElementException) {
|
||||
break;
|
||||
}
|
||||
if (e instanceof ContinueElementException) {
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
eli.close();
|
||||
} catch (GenericEntityException e) {
|
||||
throw new MiniLangRuntimeException("Error closing entityListIterator: " + e.getMessage(), this);
|
||||
}
|
||||
}
|
||||
} else if (objList instanceof Collection<?>) {
|
||||
Collection<Object> theCollection = UtilGenerics.checkCollection(objList);
|
||||
if (theCollection.size() == 0) {
|
||||
if (Debug.verboseOn())
|
||||
Debug.logVerbose("Collection has zero entries, doing nothing: " + this, module);
|
||||
return true;
|
||||
}
|
||||
for (Object theEntry : theCollection) {
|
||||
entryFma.put(methodContext.getEnvMap(), theEntry);
|
||||
try {
|
||||
for (MethodOperation methodOperation : subOps) {
|
||||
if (!methodOperation.exec(methodContext)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} catch (MiniLangException e) {
|
||||
if (e instanceof BreakElementException) {
|
||||
break;
|
||||
}
|
||||
if (e instanceof ContinueElementException) {
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
} else if (objList instanceof Iterator<?>) {
|
||||
Iterator<Object> theIterator = UtilGenerics.cast(objList);
|
||||
if (!theIterator.hasNext()) {
|
||||
if (Debug.verboseOn())
|
||||
Debug.logVerbose("Iterator has zero entries, doing nothing: " + this, module);
|
||||
return true;
|
||||
}
|
||||
while (theIterator.hasNext()) {
|
||||
Object theEntry = theIterator.next();
|
||||
entryFma.put(methodContext.getEnvMap(), theEntry);
|
||||
try {
|
||||
for (MethodOperation methodOperation : subOps) {
|
||||
if (!methodOperation.exec(methodContext)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} catch (MiniLangException e) {
|
||||
if (e instanceof BreakElementException) {
|
||||
break;
|
||||
}
|
||||
if (e instanceof ContinueElementException) {
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if (Debug.verboseOn()) {
|
||||
Debug.logVerbose("Cannot iterate over a " + objList == null ? "null object" : objList.getClass().getName()
|
||||
+ ", doing nothing: " + this, module);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
entryFma.put(methodContext.getEnvMap(), oldEntryValue);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
for (MethodOperation method : this.subOps) {
|
||||
method.gatherArtifactInfo(aic);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<iterate ");
|
||||
if (!this.entryFma.isEmpty()) {
|
||||
sb.append("entry=\"").append(this.entryFma).append("\" ");
|
||||
}
|
||||
if (!this.listFma.isEmpty()) {
|
||||
sb.append("list=\"").append(this.listFma).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <iterate> element.
|
||||
*/
|
||||
public static final class IterateFactory implements Factory<Iterate> {
|
||||
@Override
|
||||
public Iterate createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new Iterate(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "iterate";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.envops;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.ofbiz.minilang.method.envops.Break.BreakElementException;
|
||||
import org.ofbiz.minilang.method.envops.Continue.ContinueElementException;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <iterate-map> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Citeratemap%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class IterateMap extends MethodOperation {
|
||||
|
||||
public static final String module = IterateMap.class.getName();
|
||||
|
||||
private final FlexibleMapAccessor<Object> keyFma;
|
||||
private final FlexibleMapAccessor<Map<? extends Object, ? extends Object>> mapFma;
|
||||
private final List<MethodOperation> subOps;
|
||||
private final FlexibleMapAccessor<Object> valueFma;
|
||||
|
||||
public IterateMap(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "key", "map", "value");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "key", "map", "value");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "key", "map", "value");
|
||||
}
|
||||
this.keyFma = FlexibleMapAccessor.getInstance(element.getAttribute("key"));
|
||||
this.mapFma = FlexibleMapAccessor.getInstance(element.getAttribute("map"));
|
||||
this.valueFma = FlexibleMapAccessor.getInstance(element.getAttribute("value"));
|
||||
this.subOps = Collections.unmodifiableList(SimpleMethod.readOperations(element, simpleMethod));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
if (mapFma.isEmpty()) {
|
||||
throw new MiniLangRuntimeException("No map specified.", this);
|
||||
}
|
||||
Object oldKey = keyFma.get(methodContext.getEnvMap());
|
||||
Object oldValue = valueFma.get(methodContext.getEnvMap());
|
||||
if (oldKey != null) {
|
||||
if (Debug.verboseOn())
|
||||
Debug.logVerbose("In iterate-map the key had a non-null value before entering the loop for the operation: " + this, module);
|
||||
}
|
||||
if (oldValue != null) {
|
||||
if (Debug.verboseOn())
|
||||
Debug.logVerbose("In iterate-map the value had a non-null value before entering the loop for the operation: " + this, module);
|
||||
}
|
||||
Map<? extends Object, ? extends Object> theMap = mapFma.get(methodContext.getEnvMap());
|
||||
if (theMap == null) {
|
||||
if (Debug.verboseOn())
|
||||
Debug.logVerbose("Map not found with name " + mapFma + ", doing nothing: " + this, module);
|
||||
return true;
|
||||
}
|
||||
if (theMap.size() == 0) {
|
||||
if (Debug.verboseOn())
|
||||
Debug.logVerbose("Map with name " + mapFma + " has zero entries, doing nothing: " + this, module);
|
||||
return true;
|
||||
}
|
||||
for (Map.Entry<? extends Object, ? extends Object> theEntry : theMap.entrySet()) {
|
||||
keyFma.put(methodContext.getEnvMap(), theEntry.getKey());
|
||||
valueFma.put(methodContext.getEnvMap(), theEntry.getValue());
|
||||
try {
|
||||
for (MethodOperation methodOperation : subOps) {
|
||||
if (!methodOperation.exec(methodContext)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} catch (MiniLangException e) {
|
||||
if (e instanceof BreakElementException) {
|
||||
break;
|
||||
}
|
||||
if (e instanceof ContinueElementException) {
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
for (MethodOperation method : this.subOps) {
|
||||
method.gatherArtifactInfo(aic);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<iterate-map ");
|
||||
if (!this.mapFma.isEmpty()) {
|
||||
sb.append("map=\"").append(this.mapFma).append("\" ");
|
||||
}
|
||||
if (!this.keyFma.isEmpty()) {
|
||||
sb.append("key=\"").append(this.keyFma).append("\" ");
|
||||
}
|
||||
if (!this.valueFma.isEmpty()) {
|
||||
sb.append("value=\"").append(this.valueFma).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <iterate-map> element.
|
||||
*/
|
||||
public static final class IterateMapFactory implements Factory<IterateMap> {
|
||||
@Override
|
||||
public IterateMap createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new IterateMap(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "iterate-map";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.envops;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <list-to-list> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Clisttolist%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class ListToList extends MethodOperation {
|
||||
|
||||
private final FlexibleMapAccessor<List<Object>> listFma;
|
||||
private final FlexibleMapAccessor<List<Object>> toListFma;
|
||||
|
||||
public ListToList(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "to-list", "list");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "to-list", "list");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "to-list", "list");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
toListFma = FlexibleMapAccessor.getInstance(element.getAttribute("to-list"));
|
||||
listFma = FlexibleMapAccessor.getInstance(element.getAttribute("list"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
List<Object> fromList = listFma.get(methodContext.getEnvMap());
|
||||
if (fromList != null) {
|
||||
List<Object> toList = toListFma.get(methodContext.getEnvMap());
|
||||
if (toList == null) {
|
||||
toList = new LinkedList<Object>();
|
||||
toListFma.put(methodContext.getEnvMap(), toList);
|
||||
}
|
||||
toList.addAll(fromList);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<list-to-list ");
|
||||
sb.append("to-list=\"").append(this.toListFma).append("\" ");
|
||||
sb.append("list=\"").append(this.listFma).append("\" />");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <list-to-list> element.
|
||||
*/
|
||||
public static final class ListToListFactory implements Factory<ListToList> {
|
||||
@Override
|
||||
public ListToList createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new ListToList(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "list-to-list";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.envops;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.ofbiz.minilang.method.envops.Break.BreakElementException;
|
||||
import org.ofbiz.minilang.method.envops.Continue.ContinueElementException;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <loop> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cloop%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class Loop extends MethodOperation {
|
||||
|
||||
public static final String module = Loop.class.getName();
|
||||
|
||||
private final FlexibleStringExpander countFse;
|
||||
private final FlexibleMapAccessor<Integer> fieldFma;
|
||||
private final List<MethodOperation> subOps;
|
||||
|
||||
public Loop(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "count", "field");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "count");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "count", "field");
|
||||
}
|
||||
this.countFse = FlexibleStringExpander.getInstance(element.getAttribute("count"));
|
||||
this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field"));
|
||||
this.subOps = Collections.unmodifiableList(SimpleMethod.readOperations(element, simpleMethod));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
String countStr = this.countFse.expandString(methodContext.getEnvMap());
|
||||
int count = 0;
|
||||
try {
|
||||
count = Double.valueOf(countStr).intValue();
|
||||
} catch (NumberFormatException e) {
|
||||
throw new MiniLangRuntimeException("Error while converting \"" + countStr + "\" to a number: " + e.getMessage(), this);
|
||||
}
|
||||
if (count < 0) {
|
||||
throw new MiniLangRuntimeException("Unable to execute loop operation because the count is negative: " + countStr, this);
|
||||
}
|
||||
for (int i = 0; i < count; i++) {
|
||||
this.fieldFma.put(methodContext.getEnvMap(), i);
|
||||
try {
|
||||
for (MethodOperation methodOperation : subOps) {
|
||||
if (!methodOperation.exec(methodContext)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} catch (MiniLangException e) {
|
||||
if (e instanceof BreakElementException) {
|
||||
break;
|
||||
}
|
||||
if (e instanceof ContinueElementException) {
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
for (MethodOperation method : this.subOps) {
|
||||
method.gatherArtifactInfo(aic);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<loop ");
|
||||
if (!this.countFse.isEmpty()) {
|
||||
sb.append("count=\"").append(this.countFse).append("\" ");
|
||||
}
|
||||
if (!this.fieldFma.isEmpty()) {
|
||||
sb.append("field=\"").append(this.fieldFma).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <loop> element.
|
||||
*/
|
||||
public static final class LoopFactory implements Factory<Loop> {
|
||||
@Override
|
||||
public Loop createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new Loop(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "loop";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.envops;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <map-to-map> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cmaptomap%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class MapToMap extends MethodOperation {
|
||||
|
||||
private final FlexibleMapAccessor<Map<String, Object>> mapFma;
|
||||
private final FlexibleMapAccessor<Map<String, Object>> toMapFma;
|
||||
|
||||
public MapToMap(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "to-map", "map");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "map");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "to-map", "map");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
mapFma = FlexibleMapAccessor.getInstance(element.getAttribute("map"));
|
||||
toMapFma = FlexibleMapAccessor.getInstance(element.getAttribute("to-map"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
Map<String, Object> fromMap = mapFma.get(methodContext.getEnvMap());
|
||||
if (fromMap != null) {
|
||||
if (!toMapFma.isEmpty()) {
|
||||
Map<String, Object> toMap = toMapFma.get(methodContext.getEnvMap());
|
||||
if (toMap == null) {
|
||||
toMap = new HashMap<String, Object>();
|
||||
toMapFma.put(methodContext.getEnvMap(), toMap);
|
||||
}
|
||||
toMap.putAll(fromMap);
|
||||
} else {
|
||||
methodContext.putAllEnv(fromMap);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<map-to-map ");
|
||||
sb.append("map=\"").append(this.mapFma).append("\" ");
|
||||
if (!toMapFma.isEmpty()) {
|
||||
sb.append("to-map=\"").append(this.toMapFma).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <map-to-map> element.
|
||||
*/
|
||||
public static final class MapToMapFactory implements Factory<MapToMap> {
|
||||
@Override
|
||||
public MapToMap createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new MapToMap(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "map-to-map";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.envops;
|
||||
|
||||
import org.ofbiz.base.conversion.ConversionException;
|
||||
import org.ofbiz.base.conversion.Converter;
|
||||
import org.ofbiz.base.conversion.Converters;
|
||||
import org.ofbiz.base.util.ObjectType;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangUtil;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.ValidationException;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <now>, <now-date-to-env>, and <now-timestamp> elements.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cnow%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class Now extends MethodOperation {
|
||||
|
||||
// This method is needed only during the v1 to v2 transition
|
||||
private static boolean autoCorrect(Element element) {
|
||||
String tagName = element.getTagName();
|
||||
if ("now-date-to-env".equals(tagName) || "now-timestamp".equals(tagName)) {
|
||||
Document doc = element.getOwnerDocument();
|
||||
Element newElement = doc.createElement("now");
|
||||
newElement.setAttribute("field", element.getAttribute("field"));
|
||||
if ("now-date-to-env".equals(tagName)) {
|
||||
element.setAttribute("type", "java.sql.Date");
|
||||
newElement.setAttribute("type", "java.sql.Date");
|
||||
}
|
||||
element.getParentNode().replaceChild(newElement, element);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private final FlexibleMapAccessor<Object> fieldFma;
|
||||
private final String type;
|
||||
private final Converter<Long, ? extends Object> converter;
|
||||
|
||||
public Now(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
String tagName = element.getTagName();
|
||||
if ("now-date-to-env".equals(tagName) || "now-timestamp".equals(tagName)) {
|
||||
MiniLangValidate.handleError("Deprecated - use <now>", simpleMethod, element);
|
||||
}
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "field", "type");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "field");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "field");
|
||||
MiniLangValidate.constantAttributes(simpleMethod, element, "type");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
boolean elementModified = autoCorrect(element);
|
||||
if (elementModified && MiniLangUtil.autoCorrectOn()) {
|
||||
MiniLangUtil.flagDocumentAsCorrected(element);
|
||||
}
|
||||
this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field"));
|
||||
this.type = element.getAttribute("type");
|
||||
Class<?> targetClass = null;
|
||||
try {
|
||||
if (!this.type.isEmpty()) {
|
||||
targetClass = ObjectType.loadClass(this.type);
|
||||
}
|
||||
if (targetClass == null) {
|
||||
targetClass = java.sql.Timestamp.class;
|
||||
}
|
||||
this.converter = Converters.getConverter(Long.class, targetClass);
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new ValidationException(e.getMessage(), simpleMethod, element);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
try {
|
||||
this.fieldFma.put(methodContext.getEnvMap(), this.converter.convert(System.currentTimeMillis()));
|
||||
} catch (ConversionException e) {
|
||||
throw new MiniLangRuntimeException(e.getMessage(), this);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<now ");
|
||||
if (!this.fieldFma.isEmpty()) {
|
||||
sb.append("field=\"").append(this.fieldFma).append("\" ");
|
||||
}
|
||||
if (!this.type.isEmpty()) {
|
||||
sb.append("type=\"").append(this.type).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* A factory for the <now> element.
|
||||
*/
|
||||
public static final class NowFactory implements Factory<Now> {
|
||||
@Override
|
||||
public Now createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new Now(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "now";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <now-date-to-env> element.
|
||||
*/
|
||||
public static final class NowDateToEnvFactory implements Factory<Now> {
|
||||
@Override
|
||||
public Now createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new Now(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "now-date-to-env";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <now-timestamp> element.
|
||||
*/
|
||||
public static final class NowTimestampFactory implements Factory<Now> {
|
||||
@Override
|
||||
public Now createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new Now(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "now-timestamp";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.envops;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.ofbiz.base.util.UtilXml;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.collections.MapComparator;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <order-map-list> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cordermaplist%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class OrderMapList extends MethodOperation {
|
||||
|
||||
private final FlexibleMapAccessor<List<Map<Object, Object>>> listFma;
|
||||
private final MapComparator mc;
|
||||
|
||||
public OrderMapList(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "list");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "list");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "list");
|
||||
MiniLangValidate.childElements(simpleMethod, element, "order-by");
|
||||
MiniLangValidate.requiredChildElements(simpleMethod, element, "order-by");
|
||||
}
|
||||
listFma = FlexibleMapAccessor.getInstance(element.getAttribute("list"));
|
||||
List<? extends Element> orderByElements = UtilXml.childElementList(element, "order-by");
|
||||
if (orderByElements.size() > 0) {
|
||||
ArrayList<FlexibleMapAccessor<String>> orderByList = new ArrayList<FlexibleMapAccessor<String>>(orderByElements.size());
|
||||
for (Element orderByElement : orderByElements) {
|
||||
FlexibleMapAccessor<String> fma = FlexibleMapAccessor.getInstance(orderByElement.getAttribute("field"));
|
||||
orderByList.add(fma);
|
||||
}
|
||||
mc = new MapComparator(orderByList);
|
||||
} else {
|
||||
mc = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
if (mc == null) {
|
||||
throw new MiniLangRuntimeException("order-by sub-elements not found.", this);
|
||||
}
|
||||
List<Map<Object, Object>> orderList = listFma.get(methodContext.getEnvMap());
|
||||
if (orderList != null) {
|
||||
Collections.sort(orderList, mc);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<order-map-list ");
|
||||
sb.append("list=\"").append(this.listFma).append("\" />");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <order-map-list> element.
|
||||
*/
|
||||
public static final class OrderMapListFactory implements Factory<OrderMapList> {
|
||||
@Override
|
||||
public OrderMapList createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new OrderMapList(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "order-map-list";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,335 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.envops;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.util.Locale;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.ObjectType;
|
||||
import org.ofbiz.base.util.Scriptlet;
|
||||
import org.ofbiz.base.util.StringUtil;
|
||||
import org.ofbiz.base.util.UtilDateTime;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangUtil;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
import com.ibm.icu.util.Calendar;
|
||||
|
||||
/**
|
||||
* Implements the <set-calendar> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Csetcalendar%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class SetCalendar extends MethodOperation {
|
||||
|
||||
public static final String module = SetCalendar.class.getName();
|
||||
|
||||
// This method is needed only during the v1 to v2 transition
|
||||
private static boolean autoCorrect(Element element) {
|
||||
boolean elementModified = false;
|
||||
// Correct deprecated default-value attribute
|
||||
String defaultAttr = element.getAttribute("default-value");
|
||||
if (defaultAttr.length() > 0) {
|
||||
element.setAttribute("default", defaultAttr);
|
||||
element.removeAttribute("default-value");
|
||||
elementModified = true;
|
||||
}
|
||||
// Correct deprecated from-field attribute
|
||||
String fromAttr = element.getAttribute("from-field");
|
||||
if (fromAttr.length() > 0) {
|
||||
element.setAttribute("from", fromAttr);
|
||||
element.removeAttribute("from-field");
|
||||
elementModified = true;
|
||||
}
|
||||
// Correct value attribute expression that belongs in from attribute
|
||||
String valueAttr = element.getAttribute("value").trim();
|
||||
if (valueAttr.startsWith("${") && valueAttr.endsWith("}")) {
|
||||
valueAttr = valueAttr.substring(2, valueAttr.length() - 1);
|
||||
if (!valueAttr.contains("${")) {
|
||||
element.setAttribute("from", valueAttr);
|
||||
element.removeAttribute("value");
|
||||
elementModified = true;
|
||||
}
|
||||
}
|
||||
return elementModified;
|
||||
}
|
||||
|
||||
// Fix for some Java versions that throw an exception when the String includes a "+".
|
||||
private static int parseInt(String intStr) {
|
||||
return Integer.parseInt(intStr.replace("+", ""));
|
||||
}
|
||||
|
||||
private final FlexibleStringExpander daysFse;
|
||||
private final FlexibleStringExpander defaultFse;
|
||||
private final FlexibleMapAccessor<Object> fieldFma;
|
||||
private final FlexibleMapAccessor<Object> fromFma;
|
||||
private final FlexibleStringExpander hoursFse;
|
||||
private final FlexibleStringExpander localeFse;
|
||||
private final FlexibleStringExpander millisFse;
|
||||
private final FlexibleStringExpander minutesFse;
|
||||
private final FlexibleStringExpander monthsFse;
|
||||
private final FlexibleStringExpander periodAlignEnd;
|
||||
private final FlexibleStringExpander periodAlignStart;
|
||||
private final FlexibleStringExpander secondsFse;
|
||||
private final boolean setIfNull;
|
||||
private final Scriptlet scriptlet;
|
||||
private final FlexibleStringExpander timeZoneFse;
|
||||
private final FlexibleStringExpander valueFse;
|
||||
private final FlexibleStringExpander yearsFse;
|
||||
|
||||
public SetCalendar(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.deprecatedAttribute(simpleMethod, element, "from-field", "replace with \"from\"");
|
||||
MiniLangValidate.deprecatedAttribute(simpleMethod, element, "default-value", "replace with \"default\"");
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "field", "from-field", "from", "value", "default-value", "default", "set-if-null",
|
||||
"years", "months", "days", "hours", "minutes", "seconds", "millis", "period-align-start", "period-align-end", "locale", "time-zone");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "field");
|
||||
MiniLangValidate.requireAnyAttribute(simpleMethod, element, "from", "value");
|
||||
MiniLangValidate.constantPlusExpressionAttributes(simpleMethod, element, "value");
|
||||
MiniLangValidate.constantAttributes(simpleMethod, element, "set-if-null");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "field", "from", "from-field");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
boolean elementModified = autoCorrect(element);
|
||||
if (elementModified && MiniLangUtil.autoCorrectOn()) {
|
||||
MiniLangUtil.flagDocumentAsCorrected(element);
|
||||
}
|
||||
this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field"));
|
||||
String fromAttribute = element.getAttribute("from");
|
||||
if (MiniLangUtil.containsScript(fromAttribute)) {
|
||||
this.scriptlet = new Scriptlet(StringUtil.convertOperatorSubstitutions(fromAttribute));
|
||||
this.fromFma = FlexibleMapAccessor.getInstance(null);
|
||||
} else {
|
||||
this.scriptlet = null;
|
||||
this.fromFma = FlexibleMapAccessor.getInstance(fromAttribute);
|
||||
}
|
||||
this.valueFse = FlexibleStringExpander.getInstance(element.getAttribute("value"));
|
||||
if (!fromAttribute.isEmpty() && !this.valueFse.isEmpty()) {
|
||||
throw new IllegalArgumentException("Cannot include both a from attribute and a value attribute in a <set-calendar> element.");
|
||||
}
|
||||
this.defaultFse = FlexibleStringExpander.getInstance(element.getAttribute("default"));
|
||||
this.setIfNull = "true".equals(element.getAttribute("set-if-null"));
|
||||
this.yearsFse = FlexibleStringExpander.getInstance(element.getAttribute("years"));
|
||||
this.monthsFse = FlexibleStringExpander.getInstance(element.getAttribute("months"));
|
||||
this.daysFse = FlexibleStringExpander.getInstance(element.getAttribute("days"));
|
||||
this.hoursFse = FlexibleStringExpander.getInstance(element.getAttribute("hours"));
|
||||
this.minutesFse = FlexibleStringExpander.getInstance(element.getAttribute("minutes"));
|
||||
this.secondsFse = FlexibleStringExpander.getInstance(element.getAttribute("seconds"));
|
||||
this.millisFse = FlexibleStringExpander.getInstance(element.getAttribute("millis"));
|
||||
this.periodAlignStart = FlexibleStringExpander.getInstance(element.getAttribute("period-align-start"));
|
||||
this.periodAlignEnd = FlexibleStringExpander.getInstance(element.getAttribute("period-align-end"));
|
||||
this.localeFse = FlexibleStringExpander.getInstance(element.getAttribute("locale"));
|
||||
this.timeZoneFse = FlexibleStringExpander.getInstance(element.getAttribute("time-zone"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
Object newValue = null;
|
||||
if (this.scriptlet != null) {
|
||||
try {
|
||||
newValue = this.scriptlet.executeScript(methodContext.getEnvMap());
|
||||
} catch (Exception exc) {
|
||||
Debug.logWarning(exc, "Error evaluating scriptlet [" + this.scriptlet + "]: " + exc, module);
|
||||
}
|
||||
} else if (!this.fromFma.isEmpty()) {
|
||||
newValue = this.fromFma.get(methodContext.getEnvMap());
|
||||
} else if (!this.valueFse.isEmpty()) {
|
||||
newValue = this.valueFse.expand(methodContext.getEnvMap());
|
||||
}
|
||||
if (ObjectType.isEmpty(newValue) && !this.defaultFse.isEmpty()) {
|
||||
newValue = this.defaultFse.expand(methodContext.getEnvMap());
|
||||
}
|
||||
if (!setIfNull && newValue == null) {
|
||||
return true;
|
||||
}
|
||||
Locale locale = null;
|
||||
TimeZone timeZone = null;
|
||||
Timestamp fromStamp = null;
|
||||
int years = 0;
|
||||
int months = 0;
|
||||
int days = 0;
|
||||
int hours = 0;
|
||||
int minutes = 0;
|
||||
int seconds = 0;
|
||||
int millis = 0;
|
||||
try {
|
||||
if (!this.localeFse.isEmpty()) {
|
||||
locale = (Locale) ObjectType.simpleTypeConvert(this.localeFse.expand(methodContext.getEnvMap()), "Locale", null, null);
|
||||
}
|
||||
if (locale == null) {
|
||||
locale = methodContext.getLocale();
|
||||
}
|
||||
if (locale == null) {
|
||||
locale = Locale.getDefault();
|
||||
}
|
||||
if (!this.timeZoneFse.isEmpty()) {
|
||||
timeZone = (TimeZone) ObjectType.simpleTypeConvert(this.timeZoneFse.expand(methodContext.getEnvMap()), "TimeZone", null, null);
|
||||
}
|
||||
if (timeZone == null) {
|
||||
timeZone = methodContext.getTimeZone();
|
||||
}
|
||||
if (timeZone == null) {
|
||||
timeZone = TimeZone.getDefault();
|
||||
}
|
||||
fromStamp = (Timestamp) MiniLangUtil.convertType(newValue, java.sql.Timestamp.class, locale, timeZone, UtilDateTime.getDateTimeFormat());
|
||||
if (!this.yearsFse.isEmpty()) {
|
||||
years= parseInt(this.yearsFse.expandString(methodContext.getEnvMap()));
|
||||
}
|
||||
if (!this.monthsFse.isEmpty()) {
|
||||
months = parseInt(this.monthsFse.expandString(methodContext.getEnvMap()));
|
||||
}
|
||||
if (!this.daysFse.isEmpty()) {
|
||||
days = parseInt(this.daysFse.expandString(methodContext.getEnvMap()));
|
||||
}
|
||||
if (!this.hoursFse.isEmpty()) {
|
||||
hours = parseInt(this.hoursFse.expandString(methodContext.getEnvMap()));
|
||||
}
|
||||
if (!this.minutesFse.isEmpty()) {
|
||||
minutes = parseInt(this.minutesFse.expandString(methodContext.getEnvMap()));
|
||||
}
|
||||
if (!this.secondsFse.isEmpty()) {
|
||||
seconds = parseInt(this.secondsFse.expandString(methodContext.getEnvMap()));
|
||||
}
|
||||
if (!this.millisFse.isEmpty()) {
|
||||
millis = parseInt(this.millisFse.expandString(methodContext.getEnvMap()));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new MiniLangRuntimeException("Exception thrown while parsing attributes: " + e.getMessage(), this);
|
||||
}
|
||||
Calendar cal = UtilDateTime.toCalendar(fromStamp, timeZone, locale);
|
||||
cal.add(Calendar.MILLISECOND, millis);
|
||||
cal.add(Calendar.SECOND, seconds);
|
||||
cal.add(Calendar.MINUTE, minutes);
|
||||
cal.add(Calendar.HOUR, hours);
|
||||
cal.add(Calendar.DAY_OF_MONTH, days);
|
||||
cal.add(Calendar.MONTH, months);
|
||||
cal.add(Calendar.YEAR, years);
|
||||
Timestamp toStamp = new Timestamp(cal.getTimeInMillis());
|
||||
if (!periodAlignStart.isEmpty()) {
|
||||
String period = periodAlignStart.expandString(methodContext.getEnvMap());
|
||||
if ("day".equals(period)) {
|
||||
toStamp = UtilDateTime.getDayStart(toStamp, 0, timeZone, locale);
|
||||
} else if ("week".equals(period)) {
|
||||
toStamp = UtilDateTime.getWeekStart(toStamp, 0, timeZone, locale);
|
||||
} else if ("month".equals(period)) {
|
||||
toStamp = UtilDateTime.getMonthStart(toStamp, 0, timeZone, locale);
|
||||
} else if ("year".equals(period)) {
|
||||
toStamp = UtilDateTime.getYearStart(toStamp, 0, timeZone, locale);
|
||||
} else {
|
||||
throw new MiniLangRuntimeException("Invalid period-align-start attribute value: " + period, this);
|
||||
}
|
||||
} else if (!periodAlignEnd.isEmpty()) {
|
||||
String period = periodAlignEnd.expandString(methodContext.getEnvMap());
|
||||
if ("day".equals(period)) {
|
||||
toStamp = UtilDateTime.getDayEnd(toStamp, timeZone, locale);
|
||||
} else if ("week".equals(period)) {
|
||||
toStamp = UtilDateTime.getWeekEnd(toStamp, timeZone, locale);
|
||||
} else if ("month".equals(period)) {
|
||||
toStamp = UtilDateTime.getMonthEnd(toStamp, timeZone, locale);
|
||||
} else if ("year".equals(period)) {
|
||||
toStamp = UtilDateTime.getYearEnd(toStamp, timeZone, locale);
|
||||
} else {
|
||||
throw new MiniLangRuntimeException("Invalid period-align-end attribute value: " + period, this);
|
||||
}
|
||||
}
|
||||
this.fieldFma.put(methodContext.getEnvMap(), toStamp);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<set-calendar ");
|
||||
sb.append("field=\"").append(this.fieldFma).append("\" ");
|
||||
if (!this.fromFma.isEmpty()) {
|
||||
sb.append("from=\"").append(this.fromFma).append("\" ");
|
||||
}
|
||||
if (this.scriptlet != null) {
|
||||
sb.append("from=\"").append(this.scriptlet).append("\" ");
|
||||
}
|
||||
if (!this.valueFse.isEmpty()) {
|
||||
sb.append("value=\"").append(this.valueFse).append("\" ");
|
||||
}
|
||||
if (!this.defaultFse.isEmpty()) {
|
||||
sb.append("default=\"").append(this.defaultFse).append("\" ");
|
||||
}
|
||||
if (!this.yearsFse.isEmpty()) {
|
||||
sb.append("years=\"").append(this.yearsFse).append("\" ");
|
||||
}
|
||||
if (!this.monthsFse.isEmpty()) {
|
||||
sb.append("months=\"").append(this.monthsFse).append("\" ");
|
||||
}
|
||||
if (!this.daysFse.isEmpty()) {
|
||||
sb.append("days=\"").append(this.daysFse).append("\" ");
|
||||
}
|
||||
if (!this.hoursFse.isEmpty()) {
|
||||
sb.append("hours=\"").append(this.hoursFse).append("\" ");
|
||||
}
|
||||
if (!this.minutesFse.isEmpty()) {
|
||||
sb.append("minutes=\"").append(this.minutesFse).append("\" ");
|
||||
}
|
||||
if (!this.secondsFse.isEmpty()) {
|
||||
sb.append("seconds=\"").append(this.secondsFse).append("\" ");
|
||||
}
|
||||
if (!this.millisFse.isEmpty()) {
|
||||
sb.append("millis=\"").append(this.millisFse).append("\" ");
|
||||
}
|
||||
if (!this.periodAlignStart.isEmpty()) {
|
||||
sb.append("period-align-start=\"").append(this.localeFse).append("\" ");
|
||||
}
|
||||
if (!this.periodAlignEnd.isEmpty()) {
|
||||
sb.append("period-align-end=\"").append(this.localeFse).append("\" ");
|
||||
}
|
||||
if (!this.localeFse.isEmpty()) {
|
||||
sb.append("locale=\"").append(this.localeFse).append("\" ");
|
||||
}
|
||||
if (!this.timeZoneFse.isEmpty()) {
|
||||
sb.append("time-zone=\"").append(this.timeZoneFse).append("\" ");
|
||||
}
|
||||
if (this.setIfNull) {
|
||||
sb.append("set-if-null=\"true\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <set-calendar> element.
|
||||
*/
|
||||
public static final class SetCalendarFactory implements Factory<SetCalendar> {
|
||||
@Override
|
||||
public SetCalendar createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new SetCalendar(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "set-calendar";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,248 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.envops;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Locale;
|
||||
|
||||
import org.ofbiz.base.util.Debug;
|
||||
import org.ofbiz.base.util.ObjectType;
|
||||
import org.ofbiz.base.util.Scriptlet;
|
||||
import org.ofbiz.base.util.StringUtil;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangUtil;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <set> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cset%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class SetOperation extends MethodOperation {
|
||||
|
||||
public static final String module = SetOperation.class.getName();
|
||||
|
||||
// This method is needed only during the v1 to v2 transition
|
||||
private static boolean autoCorrect(Element element) {
|
||||
boolean elementModified = false;
|
||||
// Correct deprecated default-value attribute
|
||||
String defaultAttr = element.getAttribute("default-value");
|
||||
if (defaultAttr.length() > 0) {
|
||||
element.setAttribute("default", defaultAttr);
|
||||
element.removeAttribute("default-value");
|
||||
elementModified = true;
|
||||
}
|
||||
// Correct deprecated from-field attribute
|
||||
String fromAttr = element.getAttribute("from-field");
|
||||
if (fromAttr.length() > 0) {
|
||||
element.setAttribute("from", fromAttr);
|
||||
element.removeAttribute("from-field");
|
||||
elementModified = true;
|
||||
}
|
||||
// Correct value attribute expression that belongs in from attribute
|
||||
String valueAttr = element.getAttribute("value").trim();
|
||||
if (valueAttr.startsWith("${") && valueAttr.endsWith("}")) {
|
||||
valueAttr = valueAttr.substring(2, valueAttr.length() - 1);
|
||||
if (!valueAttr.contains("${")) {
|
||||
element.setAttribute("from", valueAttr);
|
||||
element.removeAttribute("value");
|
||||
elementModified = true;
|
||||
}
|
||||
}
|
||||
return elementModified;
|
||||
}
|
||||
|
||||
private final FlexibleStringExpander defaultFse;
|
||||
private final FlexibleStringExpander formatFse;
|
||||
private final FlexibleMapAccessor<Object> fieldFma;
|
||||
private final FlexibleMapAccessor<Object> fromFma;
|
||||
private final Scriptlet scriptlet;
|
||||
private final boolean setIfEmpty;
|
||||
private final boolean setIfNull;
|
||||
private final Class<?> targetClass;
|
||||
private final String type;
|
||||
private final FlexibleStringExpander valueFse;
|
||||
|
||||
public SetOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.deprecatedAttribute(simpleMethod, element, "from-field", "replace with \"from\"");
|
||||
MiniLangValidate.deprecatedAttribute(simpleMethod, element, "default-value", "replace with \"default\"");
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "field", "from-field", "from", "value", "default-value", "default", "format", "type", "set-if-null", "set-if-empty");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "field");
|
||||
MiniLangValidate.requireAnyAttribute(simpleMethod, element, "from-field", "from", "value");
|
||||
MiniLangValidate.constantPlusExpressionAttributes(simpleMethod, element, "value");
|
||||
MiniLangValidate.constantAttributes(simpleMethod, element, "type", "set-if-null", "set-if-empty");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "field");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
boolean elementModified = autoCorrect(element);
|
||||
if (elementModified && MiniLangUtil.autoCorrectOn()) {
|
||||
MiniLangUtil.flagDocumentAsCorrected(element);
|
||||
}
|
||||
this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field"));
|
||||
String fromAttribute = element.getAttribute("from");
|
||||
if (MiniLangUtil.containsScript(fromAttribute)) {
|
||||
this.scriptlet = new Scriptlet(StringUtil.convertOperatorSubstitutions(fromAttribute));
|
||||
this.fromFma = FlexibleMapAccessor.getInstance(null);
|
||||
} else {
|
||||
this.scriptlet = null;
|
||||
this.fromFma = FlexibleMapAccessor.getInstance(fromAttribute);
|
||||
}
|
||||
this.valueFse = FlexibleStringExpander.getInstance(element.getAttribute("value"));
|
||||
this.defaultFse = FlexibleStringExpander.getInstance(element.getAttribute("default"));
|
||||
this.formatFse = FlexibleStringExpander.getInstance(element.getAttribute("format"));
|
||||
this.type = element.getAttribute("type");
|
||||
Class<?> targetClass = null;
|
||||
if (!this.type.isEmpty() && !"NewList".equals(this.type) && !"NewMap".equals(this.type)) {
|
||||
try {
|
||||
targetClass = ObjectType.loadClass(this.type);
|
||||
} catch (ClassNotFoundException e) {
|
||||
MiniLangValidate.handleError("Invalid type " + this.type, simpleMethod, element);
|
||||
}
|
||||
}
|
||||
this.targetClass = targetClass;
|
||||
this.setIfNull = "true".equals(element.getAttribute("set-if-null")); // default to false, anything but true is false
|
||||
this.setIfEmpty = !"false".equals(element.getAttribute("set-if-empty")); // default to true, anything but false is true
|
||||
if (!fromAttribute.isEmpty() && !this.valueFse.isEmpty()) {
|
||||
throw new IllegalArgumentException("Cannot include both a from attribute and a value attribute in a <set> element.");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
boolean isConstant = false;
|
||||
Object newValue = null;
|
||||
if (this.scriptlet != null) {
|
||||
try {
|
||||
newValue = this.scriptlet.executeScript(methodContext.getEnvMap());
|
||||
} catch (Exception exc) {
|
||||
Debug.logWarning(exc, "Error evaluating scriptlet [" + this.scriptlet + "]: " + exc, module);
|
||||
}
|
||||
} else if (!this.fromFma.isEmpty()) {
|
||||
newValue = this.fromFma.get(methodContext.getEnvMap());
|
||||
if (Debug.verboseOn())
|
||||
Debug.logVerbose("In screen getting value for field from [" + this.fromFma.toString() + "]: " + newValue, module);
|
||||
} else if (!this.valueFse.isEmpty()) {
|
||||
newValue = this.valueFse.expand(methodContext.getEnvMap());
|
||||
isConstant = true;
|
||||
}
|
||||
// If newValue is still empty, use the default value
|
||||
if (ObjectType.isEmpty(newValue) && !this.defaultFse.isEmpty()) {
|
||||
newValue = this.defaultFse.expand(methodContext.getEnvMap());
|
||||
isConstant = true;
|
||||
}
|
||||
if (!setIfNull && newValue == null && !"NewMap".equals(this.type) && !"NewList".equals(this.type)) {
|
||||
if (Debug.verboseOn())
|
||||
Debug.logVerbose("Field value not found (null) with name [" + fromFma + "] and value [" + valueFse + "], and there was not default value, not setting field", module);
|
||||
return true;
|
||||
}
|
||||
if (!setIfEmpty && ObjectType.isEmpty(newValue)) {
|
||||
if (Debug.verboseOn())
|
||||
Debug.logVerbose("Field value not found (empty) with name [" + fromFma + "] and value [" + valueFse + "], and there was not default value, not setting field", module);
|
||||
return true;
|
||||
}
|
||||
if (this.type.length() > 0) {
|
||||
if ("NewMap".equals(this.type)) {
|
||||
newValue = new HashMap<String, Object>();
|
||||
} else if ("NewList".equals(this.type)) {
|
||||
newValue = new LinkedList<Object>();
|
||||
} else {
|
||||
try {
|
||||
String format = null;
|
||||
if (!this.formatFse.isEmpty()) {
|
||||
format = this.formatFse.expandString(methodContext.getEnvMap());
|
||||
}
|
||||
Class<?> targetClass = this.targetClass;
|
||||
if (targetClass == null) {
|
||||
targetClass = MiniLangUtil.getObjectClassForConversion(newValue);
|
||||
}
|
||||
if (isConstant) {
|
||||
// We use en locale here so constant (literal) values are converted properly.
|
||||
newValue = MiniLangUtil.convertType(newValue, targetClass, Locale.ENGLISH, methodContext.getTimeZone(), format);
|
||||
} else {
|
||||
newValue = MiniLangUtil.convertType(newValue, targetClass, methodContext.getLocale(), methodContext.getTimeZone(), format);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
String errMsg = "Could not convert field value for the field: [" + this.fieldFma.toString() + "] to the [" + this.type + "] type for the value [" + newValue + "]: " + e.getMessage();
|
||||
Debug.logWarning(e, errMsg, module);
|
||||
this.simpleMethod.addErrorMessage(methodContext, errMsg);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (Debug.verboseOn())
|
||||
Debug.logVerbose("Setting field [" + this.fieldFma.toString() + "] to value: " + newValue, module);
|
||||
this.fieldFma.put(methodContext.getEnvMap(), newValue);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<set ");
|
||||
if (!this.fieldFma.isEmpty()) {
|
||||
sb.append("field=\"").append(this.fieldFma).append("\" ");
|
||||
}
|
||||
if (!this.fromFma.isEmpty()) {
|
||||
sb.append("from=\"").append(this.fromFma).append("\" ");
|
||||
}
|
||||
if (this.scriptlet != null) {
|
||||
sb.append("from=\"").append(this.scriptlet).append("\" ");
|
||||
}
|
||||
if (!this.valueFse.isEmpty()) {
|
||||
sb.append("value=\"").append(this.valueFse).append("\" ");
|
||||
}
|
||||
if (!this.defaultFse.isEmpty()) {
|
||||
sb.append("default=\"").append(this.defaultFse).append("\" ");
|
||||
}
|
||||
if (this.type.length() > 0) {
|
||||
sb.append("type=\"").append(this.type).append("\" ");
|
||||
}
|
||||
if (this.setIfNull) {
|
||||
sb.append("set-if-null=\"true\" ");
|
||||
}
|
||||
if (!this.setIfEmpty) {
|
||||
sb.append("set-if-empty=\"false\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <set> element.
|
||||
*/
|
||||
public static final class SetOperationFactory implements Factory<SetOperation> {
|
||||
@Override
|
||||
public SetOperation createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new SetOperation(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "set";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.envops;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <string-append> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cstringappend%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class StringAppend extends MethodOperation {
|
||||
|
||||
private final FlexibleMapAccessor<List<? extends Object>> argListFma;
|
||||
private final FlexibleMapAccessor<String> fieldFma;
|
||||
private final FlexibleStringExpander prefixFse;
|
||||
private final FlexibleStringExpander stringFse;
|
||||
private final FlexibleStringExpander suffixFse;
|
||||
|
||||
public StringAppend(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "field", "arg-list", "prefix", "string", "suffix");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "field", "string");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "field", "arg-list");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
argListFma = FlexibleMapAccessor.getInstance(element.getAttribute("arg-list"));
|
||||
fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field"));
|
||||
prefixFse = FlexibleStringExpander.getInstance(element.getAttribute("prefix"));
|
||||
stringFse = FlexibleStringExpander.getInstance(element.getAttribute("string"));
|
||||
suffixFse = FlexibleStringExpander.getInstance(element.getAttribute("suffix"));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
String value = stringFse.expandString(methodContext.getEnvMap());
|
||||
List<? extends Object> argList = argListFma.get(methodContext.getEnvMap());
|
||||
if (argList != null) {
|
||||
try {
|
||||
value = MessageFormat.format(value, argList.toArray());
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new MiniLangRuntimeException("Exception thrown while formatting the string attribute: " + e.getMessage(), this);
|
||||
}
|
||||
}
|
||||
if (!value.isEmpty()) {
|
||||
String prefixValue = prefixFse.expandString(methodContext.getEnvMap());
|
||||
String suffixValue = suffixFse.expandString(methodContext.getEnvMap());
|
||||
StringBuilder newValue = new StringBuilder();
|
||||
String oldValue = fieldFma.get(methodContext.getEnvMap());
|
||||
if (oldValue != null) {
|
||||
newValue.append(oldValue);
|
||||
}
|
||||
newValue.append(prefixValue).append(value).append(suffixValue);
|
||||
fieldFma.put(methodContext.getEnvMap(), newValue.toString());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<string-append ");
|
||||
sb.append("field=\"").append(this.fieldFma).append("\" ");
|
||||
sb.append("string=\"").append(this.stringFse).append("\" ");
|
||||
if (!this.argListFma.isEmpty()) {
|
||||
sb.append("arg-list=\"").append(this.argListFma).append("\" ");
|
||||
}
|
||||
if (!this.prefixFse.isEmpty()) {
|
||||
sb.append("prefix=\"").append(this.prefixFse).append("\" ");
|
||||
}
|
||||
if (!this.suffixFse.isEmpty()) {
|
||||
sb.append("suffix=\"").append(this.suffixFse).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <string-append> element.
|
||||
*/
|
||||
public static final class StringAppendFactory implements Factory<StringAppend> {
|
||||
@Override
|
||||
public StringAppend createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new StringAppend(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "string-append";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.envops;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.util.MessageString;
|
||||
import org.ofbiz.base.util.UtilValidate;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <string-to-list> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cstringtolist%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class StringToList extends MethodOperation {
|
||||
|
||||
private final FlexibleMapAccessor<List<? extends Object>> argListFma;
|
||||
private final FlexibleMapAccessor<List<Object>> listFma;
|
||||
private final String messageFieldName;
|
||||
private final FlexibleStringExpander stringFse;
|
||||
|
||||
public StringToList(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.handleError("<string-to-list> element is deprecated (use <set>)", simpleMethod, element);
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "list", "arg-list", "string", "message-field");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "list", "string");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "list", "arg-list");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
stringFse = FlexibleStringExpander.getInstance(element.getAttribute("string"));
|
||||
listFma = FlexibleMapAccessor.getInstance(element.getAttribute("list"));
|
||||
argListFma = FlexibleMapAccessor.getInstance(element.getAttribute("arg-list"));
|
||||
messageFieldName = element.getAttribute("message-field");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
String valueStr = stringFse.expandString(methodContext.getEnvMap());
|
||||
List<? extends Object> argList = argListFma.get(methodContext.getEnvMap());
|
||||
if (argList != null) {
|
||||
try {
|
||||
valueStr = MessageFormat.format(valueStr, argList.toArray());
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new MiniLangRuntimeException("Exception thrown while formatting the string attribute: " + e.getMessage(), this);
|
||||
}
|
||||
}
|
||||
Object value;
|
||||
if (UtilValidate.isNotEmpty(this.messageFieldName)) {
|
||||
value = new MessageString(valueStr, this.messageFieldName, true);
|
||||
} else {
|
||||
value = valueStr;
|
||||
}
|
||||
List<Object> toList = listFma.get(methodContext.getEnvMap());
|
||||
if (toList == null) {
|
||||
toList = new LinkedList<Object>();
|
||||
listFma.put(methodContext.getEnvMap(), toList);
|
||||
}
|
||||
toList.add(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<string-to-list ");
|
||||
sb.append("string=\"").append(this.stringFse).append("\" ");
|
||||
sb.append("list=\"").append(this.listFma).append("\" ");
|
||||
if (!this.argListFma.isEmpty()) {
|
||||
sb.append("arg-list=\"").append(this.argListFma).append("\" ");
|
||||
}
|
||||
if (!this.messageFieldName.isEmpty()) {
|
||||
sb.append("message-field=\"").append(this.messageFieldName).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <string-to-list> element.
|
||||
*/
|
||||
public static final class StringToListFactory implements Factory<StringToList> {
|
||||
@Override
|
||||
public StringToList createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new StringToList(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "string-to-list";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.envops;
|
||||
|
||||
import org.ofbiz.base.util.StringUtil;
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangRuntimeException;
|
||||
import org.ofbiz.minilang.MiniLangUtil;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <to-string> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Ctostring%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class ToString extends MethodOperation {
|
||||
|
||||
private final FlexibleMapAccessor<Object> fieldFma;
|
||||
private final String format;
|
||||
private final Integer numericPadding;
|
||||
|
||||
public ToString(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.handleError("<to-string> element is deprecated (use <set>)", simpleMethod, element);
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "field", "format", "numeric-padding");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "field");
|
||||
MiniLangValidate.constantAttributes(simpleMethod, element, "format", "numeric-padding");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "field");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field"));
|
||||
format = element.getAttribute("format");
|
||||
Integer numericPadding = null;
|
||||
String npAttribute = element.getAttribute("numeric-padding");
|
||||
if (!npAttribute.isEmpty()) {
|
||||
try {
|
||||
numericPadding = Integer.valueOf(npAttribute);
|
||||
} catch (Exception e) {
|
||||
MiniLangValidate.handleError("Exception thrown while parsing numeric-padding attribute: " + e.getMessage(), simpleMethod, element);
|
||||
}
|
||||
}
|
||||
this.numericPadding = numericPadding;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
Object value = fieldFma.get(methodContext.getEnvMap());
|
||||
if (value != null) {
|
||||
try {
|
||||
if (!format.isEmpty()) {
|
||||
value = MiniLangUtil.convertType(value, String.class, methodContext.getLocale(), methodContext.getTimeZone(), format);
|
||||
} else {
|
||||
value = value.toString();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new MiniLangRuntimeException("Exception thrown while converting field to a string: " + e.getMessage(), this);
|
||||
}
|
||||
if (this.numericPadding != null) {
|
||||
value = StringUtil.padNumberString(value.toString(), this.numericPadding.intValue());
|
||||
}
|
||||
fieldFma.put(methodContext.getEnvMap(), value);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<to-string ");
|
||||
sb.append("field=\"").append(this.fieldFma).append("\" ");
|
||||
if (!this.format.isEmpty()) {
|
||||
sb.append("format=\"").append(this.format).append("\" ");
|
||||
}
|
||||
if (numericPadding != null) {
|
||||
sb.append("numeric-padding=\"").append(this.numericPadding).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <to-string> element.
|
||||
*/
|
||||
public static final class ToStringFactory implements Factory<ToString> {
|
||||
@Override
|
||||
public ToString createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new ToString(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "to-string";
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.envops;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.util.UtilXml;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.artifact.ArtifactInfoContext;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.ofbiz.minilang.method.conditional.Conditional;
|
||||
import org.ofbiz.minilang.method.conditional.ConditionalFactory;
|
||||
import org.ofbiz.minilang.method.envops.Break.BreakElementException;
|
||||
import org.ofbiz.minilang.method.envops.Continue.ContinueElementException;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <while> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cwhile%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class While extends MethodOperation {
|
||||
|
||||
private final Conditional condition;
|
||||
private final List<MethodOperation> thenSubOps;
|
||||
|
||||
public While(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.childElements(simpleMethod, element, "condition", "then");
|
||||
MiniLangValidate.requiredChildElements(simpleMethod, element, "condition", "then");
|
||||
}
|
||||
Element conditionElement = UtilXml.firstChildElement(element, "condition");
|
||||
Element conditionChildElement = UtilXml.firstChildElement(conditionElement);
|
||||
this.condition = ConditionalFactory.makeConditional(conditionChildElement, simpleMethod);
|
||||
Element thenElement = UtilXml.firstChildElement(element, "then");
|
||||
this.thenSubOps = Collections.unmodifiableList(SimpleMethod.readOperations(thenElement, simpleMethod));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
while (condition.checkCondition(methodContext)) {
|
||||
try {
|
||||
for (MethodOperation methodOperation : thenSubOps) {
|
||||
if (!methodOperation.exec(methodContext)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
} catch (MiniLangException e) {
|
||||
if (e instanceof BreakElementException) {
|
||||
break;
|
||||
}
|
||||
if (e instanceof ContinueElementException) {
|
||||
continue;
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void gatherArtifactInfo(ArtifactInfoContext aic) {
|
||||
for (MethodOperation method : this.thenSubOps) {
|
||||
method.gatherArtifactInfo(aic);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder messageBuf = new StringBuilder();
|
||||
this.condition.prettyPrint(messageBuf, null);
|
||||
return "<while><condition>" + messageBuf + "</condition></while>";
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <while> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cwhile%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public static final class WhileFactory implements Factory<While> {
|
||||
@Override
|
||||
public While createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new While(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "while";
|
||||
}
|
||||
}
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.eventops;
|
||||
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <field-to-request> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cfieldtorequest%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class FieldToRequest extends MethodOperation {
|
||||
|
||||
private final FlexibleMapAccessor<Object> fieldFma;
|
||||
private final FlexibleStringExpander attributeNameFse;
|
||||
|
||||
public FieldToRequest(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "field", "request-name");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "field");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "field");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field"));
|
||||
String attributeName = element.getAttribute("request-name");
|
||||
if (!attributeName.isEmpty()) {
|
||||
this.attributeNameFse = FlexibleStringExpander.getInstance(attributeName);
|
||||
} else {
|
||||
this.attributeNameFse = FlexibleStringExpander.getInstance(this.fieldFma.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
if (methodContext.getMethodType() == MethodContext.EVENT) {
|
||||
Object fieldVal = fieldFma.get(methodContext.getEnvMap());
|
||||
if (fieldVal != null) {
|
||||
String attributeName = attributeNameFse.expandString(methodContext.getEnvMap());
|
||||
if (!attributeName.isEmpty()) {
|
||||
methodContext.getRequest().setAttribute(attributeName, fieldVal);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<field-to-request ");
|
||||
sb.append("field=\"").append(this.fieldFma).append("\" ");
|
||||
if (!this.attributeNameFse.isEmpty()) {
|
||||
sb.append("request-name=\"").append(this.attributeNameFse).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <field-to-request> element.
|
||||
*/
|
||||
public static final class FieldToRequestFactory implements Factory<FieldToRequest> {
|
||||
@Override
|
||||
public FieldToRequest createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new FieldToRequest(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "field-to-request";
|
||||
}
|
||||
}
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.eventops;
|
||||
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <field-to-session> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Cfieldtosession%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class FieldToSession extends MethodOperation {
|
||||
|
||||
private final FlexibleMapAccessor<Object> fieldFma;
|
||||
private final FlexibleStringExpander attributeNameFse;
|
||||
|
||||
public FieldToSession(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "field", "session-name");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "field");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "field");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field"));
|
||||
String attributeName = element.getAttribute("session-name");
|
||||
if (!attributeName.isEmpty()) {
|
||||
this.attributeNameFse = FlexibleStringExpander.getInstance(attributeName);
|
||||
} else {
|
||||
this.attributeNameFse = FlexibleStringExpander.getInstance(this.fieldFma.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
if (methodContext.getMethodType() == MethodContext.EVENT) {
|
||||
Object fieldVal = fieldFma.get(methodContext.getEnvMap());
|
||||
if (fieldVal != null) {
|
||||
String attributeName = attributeNameFse.expandString(methodContext.getEnvMap());
|
||||
if (!attributeName.isEmpty()) {
|
||||
methodContext.getRequest().getSession().setAttribute(attributeName, fieldVal);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<field-to-session ");
|
||||
sb.append("field=\"").append(this.fieldFma).append("\" ");
|
||||
if (!this.attributeNameFse.isEmpty()) {
|
||||
sb.append("session-name=\"").append(this.attributeNameFse).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <field-to-session> element.
|
||||
*/
|
||||
public static final class FieldToSessionFactory implements Factory<FieldToSession> {
|
||||
@Override
|
||||
public FieldToSession createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new FieldToSession(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "field-to-session";
|
||||
}
|
||||
}
|
||||
}
|
||||
+122
@@ -0,0 +1,122 @@
|
||||
/*******************************************************************************
|
||||
* Licensed to the Apache Software Foundation (ASF) under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. The ASF licenses this file
|
||||
* to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*******************************************************************************/
|
||||
package org.ofbiz.minilang.method.eventops;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.ofbiz.base.util.collections.FlexibleMapAccessor;
|
||||
import org.ofbiz.base.util.string.FlexibleStringExpander;
|
||||
import org.ofbiz.minilang.MiniLangException;
|
||||
import org.ofbiz.minilang.MiniLangUtil;
|
||||
import org.ofbiz.minilang.MiniLangValidate;
|
||||
import org.ofbiz.minilang.SimpleMethod;
|
||||
import org.ofbiz.minilang.method.MethodContext;
|
||||
import org.ofbiz.minilang.method.MethodOperation;
|
||||
import org.w3c.dom.Element;
|
||||
|
||||
/**
|
||||
* Implements the <request-parameters-to-list> element.
|
||||
*
|
||||
* @see <a href="https://cwiki.apache.org/confluence/display/OFBADMIN/Mini-language+Reference#Mini-languageReference-{{%3Crequestparameterstolist%3E}}">Mini-language Reference</a>
|
||||
*/
|
||||
public final class RequestParametersToList extends MethodOperation {
|
||||
|
||||
// This method is needed only during the v1 to v2 transition
|
||||
private static boolean autoCorrect(Element element) {
|
||||
// Correct deprecated list-name attribute
|
||||
String listAttr = element.getAttribute("list-name");
|
||||
if (listAttr.length() > 0) {
|
||||
element.setAttribute("list", listAttr);
|
||||
element.removeAttribute("list-name");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private final FlexibleMapAccessor<List<String>> listFma;
|
||||
private final FlexibleStringExpander parameterNameFse;
|
||||
|
||||
public RequestParametersToList(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
super(element, simpleMethod);
|
||||
if (MiniLangValidate.validationOn()) {
|
||||
MiniLangValidate.deprecatedAttribute(simpleMethod, element, "list-name", "replace with \"list\"");
|
||||
MiniLangValidate.attributeNames(simpleMethod, element, "list", "request-name");
|
||||
MiniLangValidate.requiredAttributes(simpleMethod, element, "request-name");
|
||||
MiniLangValidate.expressionAttributes(simpleMethod, element, "list");
|
||||
MiniLangValidate.noChildElements(simpleMethod, element);
|
||||
}
|
||||
boolean elementModified = autoCorrect(element);
|
||||
if (elementModified && MiniLangUtil.autoCorrectOn()) {
|
||||
MiniLangUtil.flagDocumentAsCorrected(element);
|
||||
}
|
||||
this.parameterNameFse = FlexibleStringExpander.getInstance(element.getAttribute("request-name"));
|
||||
String listAttribute = element.getAttribute("list");
|
||||
if (!listAttribute.isEmpty()) {
|
||||
this.listFma = FlexibleMapAccessor.getInstance(listAttribute);
|
||||
} else {
|
||||
this.listFma = FlexibleMapAccessor.getInstance(parameterNameFse.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean exec(MethodContext methodContext) throws MiniLangException {
|
||||
if (methodContext.getMethodType() == MethodContext.EVENT) {
|
||||
String parameterName = parameterNameFse.expandString(methodContext.getEnvMap());
|
||||
String[] parameterValues = methodContext.getRequest().getParameterValues(parameterName);
|
||||
if (parameterValues != null) {
|
||||
List<String> valueList = listFma.get(methodContext.getEnvMap());
|
||||
if (valueList == null) {
|
||||
valueList = new LinkedList<String>();
|
||||
listFma.put(methodContext.getEnvMap(), valueList);
|
||||
}
|
||||
for (int i = 0; i < parameterValues.length; i++) {
|
||||
valueList.add(parameterValues[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder("<request-parameters-to-list ");
|
||||
sb.append("request-name=\"").append(this.parameterNameFse).append("\" ");
|
||||
if (!this.listFma.isEmpty()) {
|
||||
sb.append("list=\"").append(this.listFma).append("\" ");
|
||||
}
|
||||
sb.append("/>");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* A factory for the <request-parameters-to-list> element.
|
||||
*/
|
||||
public static final class RequestParametersToListFactory implements Factory<RequestParametersToList> {
|
||||
@Override
|
||||
public RequestParametersToList createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException {
|
||||
return new RequestParametersToList(element, simpleMethod);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "request-parameters-to-list";
|
||||
}
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user