diff --git a/framework/minilang/dtd/simple-methods-v2.xsd b/framework/minilang/dtd/simple-methods-v2.xsd index 7215568b3a..da8a3c4d88 100644 --- a/framework/minilang/dtd/simple-methods-v2.xsd +++ b/framework/minilang/dtd/simple-methods-v2.xsd @@ -57,6 +57,22 @@ under the License. + + + + The name of the field to set. The target of the assignment. + <br/><br/> + Required. Attribute type: expression. + + + + + + + + + + @@ -1341,15 +1357,7 @@ under the License. - - - - The name of the field to set. The target of the assignment. - <br/><br/> - Required. Attribute type: expression. - - - + @@ -4416,43 +4424,52 @@ under the License. - + - The now-timestamp tag creates a java.sql.Timestamp object with the current date and time in it and puts it in a field in the method environment. + Sets a field to the current system time. - + + + + + The field data type. Defaults to java.sql.Timestamp. + + + + + + + + + + + + + + + + + + Deprecated - use <now>. + + + + - - - - - The name of the method environment field the timestamp will be put in. - - - - - The now-date-to-env tag creates a java.sql.Date object with the current date in it and puts it in a field in the method environment. + Deprecated - use <now>. - - + + + - - - - - The name of the method environment field the date will be put in. - - - - diff --git a/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation$Factory b/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation$Factory index 14ff462140..8b5cc7f722 100644 --- a/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation$Factory +++ b/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation$Factory @@ -49,7 +49,6 @@ 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.NowDateToEnv$NowDateToEnvFactory -org.ofbiz.minilang.method.entityops.NowTimestampToEnv$NowTimestampToEnvFactory org.ofbiz.minilang.method.entityops.NowTimestampToEnv$NowTimestampFactory org.ofbiz.minilang.method.entityops.OrderValueList$OrderValueListFactory org.ofbiz.minilang.method.entityops.RefreshValue$RefreshValueFactory @@ -77,6 +76,7 @@ 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.OrderMapList$OrderMapListFactory org.ofbiz.minilang.method.envops.SetCalendar$SetCalendarFactory org.ofbiz.minilang.method.envops.SetOperation$SetOperationFactory diff --git a/framework/minilang/src/org/ofbiz/minilang/method/entityops/NowDateToEnv.java b/framework/minilang/src/org/ofbiz/minilang/method/entityops/NowDateToEnv.java index 98bd3554a2..143e38ec36 100644 --- a/framework/minilang/src/org/ofbiz/minilang/method/entityops/NowDateToEnv.java +++ b/framework/minilang/src/org/ofbiz/minilang/method/entityops/NowDateToEnv.java @@ -18,51 +18,57 @@ *******************************************************************************/ package org.ofbiz.minilang.method.entityops; +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.ContextAccessor; import org.ofbiz.minilang.method.MethodContext; import org.ofbiz.minilang.method.MethodOperation; import org.w3c.dom.Element; /** - * Creates a java.sql.Date with the current date and puts it in the env + * Creates a java.sql.Timestamp object that is set to the current system time. */ public class NowDateToEnv extends MethodOperation { - ContextAccessor envAcsr; + private final FlexibleMapAccessor fieldFma; public NowDateToEnv(Element element, SimpleMethod simpleMethod) throws MiniLangException { super(element, simpleMethod); - envAcsr = new ContextAccessor(element.getAttribute("field"), element.getAttribute("env-name")); + if (MiniLangValidate.validationOn()) { + MiniLangValidate.handleError("Deprecated - use ", simpleMethod, element); + MiniLangValidate.attributeNames(simpleMethod, element, "field"); + MiniLangValidate.requiredAttributes(simpleMethod, element, "field"); + MiniLangValidate.noChildElements(simpleMethod, element); + } + this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field")); } @Override public boolean exec(MethodContext methodContext) throws MiniLangException { - envAcsr.put(methodContext, new java.sql.Date(System.currentTimeMillis())); + this.fieldFma.put(methodContext.getEnvMap(), new java.sql.Date(System.currentTimeMillis())); return true; } @Override public String expandedString(MethodContext methodContext) { - // TODO: something more than a stub/dummy - return this.rawString(); + return FlexibleStringExpander.expandString(toString(), methodContext.getEnvMap()); } @Override public String rawString() { - // TODO: something more than the empty tag - return ""; + return toString(); } - public static final class NowDateFactory implements Factory { - public NowDateToEnv createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException { - return new NowDateToEnv(element, simpleMethod); - } - - public String getName() { - return "now-date"; + @Override + public String toString() { + StringBuilder sb = new StringBuilder(""); + return sb.toString(); } public static final class NowDateToEnvFactory implements Factory { diff --git a/framework/minilang/src/org/ofbiz/minilang/method/entityops/NowTimestampToEnv.java b/framework/minilang/src/org/ofbiz/minilang/method/entityops/NowTimestampToEnv.java index 3a901e22a9..dd4ca2afa0 100644 --- a/framework/minilang/src/org/ofbiz/minilang/method/entityops/NowTimestampToEnv.java +++ b/framework/minilang/src/org/ofbiz/minilang/method/entityops/NowTimestampToEnv.java @@ -18,41 +18,57 @@ *******************************************************************************/ package org.ofbiz.minilang.method.entityops; +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.ContextAccessor; import org.ofbiz.minilang.method.MethodContext; import org.ofbiz.minilang.method.MethodOperation; import org.w3c.dom.Element; /** - * Creates a java.sql.Timestamp with the current date/time in it and puts it in the env + * Creates a java.sql.Timestamp object that is set to the current system time. */ -public class NowTimestampToEnv extends MethodOperation { +public final class NowTimestampToEnv extends MethodOperation { - ContextAccessor envAcsr; + private final FlexibleMapAccessor fieldFma; public NowTimestampToEnv(Element element, SimpleMethod simpleMethod) throws MiniLangException { super(element, simpleMethod); - envAcsr = new ContextAccessor(element.getAttribute("field"), element.getAttribute("env-name")); + if (MiniLangValidate.validationOn()) { + // MiniLangValidate.handleError("Deprecated - use ", simpleMethod, element); + MiniLangValidate.attributeNames(simpleMethod, element, "field"); + MiniLangValidate.requiredAttributes(simpleMethod, element, "field"); + MiniLangValidate.noChildElements(simpleMethod, element); + } + this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field")); } @Override public boolean exec(MethodContext methodContext) throws MiniLangException { - envAcsr.put(methodContext, new java.sql.Timestamp(System.currentTimeMillis())); + this.fieldFma.put(methodContext.getEnvMap(), new java.sql.Timestamp(System.currentTimeMillis())); return true; } @Override public String expandedString(MethodContext methodContext) { - // TODO: something more than a stub/dummy - return this.rawString(); + return FlexibleStringExpander.expandString(toString(), methodContext.getEnvMap()); } @Override public String rawString() { - // TODO: something more than the empty tag - return ""; + return toString(); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(""); + return sb.toString(); } public static final class NowTimestampFactory implements Factory { @@ -64,14 +80,4 @@ public class NowTimestampToEnv extends MethodOperation { return "now-timestamp"; } } - - public static final class NowTimestampToEnvFactory implements Factory { - public NowTimestampToEnv createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException { - return new NowTimestampToEnv(element, simpleMethod); - } - - public String getName() { - return "now-timestamp-to-env"; - } - } } diff --git a/framework/minilang/src/org/ofbiz/minilang/method/envops/Now.java b/framework/minilang/src/org/ofbiz/minilang/method/envops/Now.java new file mode 100644 index 0000000000..ca51bc21f1 --- /dev/null +++ b/framework/minilang/src/org/ofbiz/minilang/method/envops/Now.java @@ -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 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.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.ValidationException; +import org.ofbiz.minilang.method.MethodContext; +import org.ofbiz.minilang.method.MethodOperation; +import org.w3c.dom.Element; + +/** + * Sets a field to the current system time. + */ +public final class Now extends MethodOperation { + + private final FlexibleMapAccessor fieldFma; + private final String type; + private final Converter converter; + + @SuppressWarnings("unchecked") + public Now(Element element, SimpleMethod simpleMethod) throws MiniLangException { + super(element, simpleMethod); + if (MiniLangValidate.validationOn()) { + MiniLangValidate.attributeNames(simpleMethod, element, "field", "type"); + MiniLangValidate.requiredAttributes(simpleMethod, element, "field"); + MiniLangValidate.noChildElements(simpleMethod, element); + } + this.fieldFma = FlexibleMapAccessor.getInstance(element.getAttribute("field")); + this.type = element.getAttribute("type"); + Class targetClass = null; + try { + if (this.type.length() > 0) { + targetClass = ObjectType.loadClass(this.type); + } + if (targetClass == null) { + targetClass = java.sql.Timestamp.class; + } + this.converter = (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 expandedString(MethodContext methodContext) { + return FlexibleStringExpander.expandString(toString(), methodContext.getEnvMap()); + } + + @Override + public String rawString() { + return toString(); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(" 0) { + sb.append("type=\"").append(this.type).append("\" "); + } + sb.append("/>"); + return sb.toString(); + } + + + public static final class NowFactory implements Factory { + public Now createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException { + return new Now(element, simpleMethod); + } + + public String getName() { + return "now"; + } + } +}