01: /*
02: * $Id: SequencedIdToEnv.java,v 1.2 2004/01/18 11:36:28 jonesde Exp $
03: *
04: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
05: *
06: * Permission is hereby granted, free of charge, to any person obtaining a
07: * copy of this software and associated documentation files (the "Software"),
08: * to deal in the Software without restriction, including without limitation
09: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10: * and/or sell copies of the Software, and to permit persons to whom the
11: * Software is furnished to do so, subject to the following conditions:
12: *
13: * The above copyright notice and this permission notice shall be included
14: * in all copies or substantial portions of the Software.
15: *
16: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23: */
24: package org.ofbiz.minilang.method.entityops;
25:
26: import org.ofbiz.base.util.UtilValidate;
27: import org.ofbiz.minilang.SimpleMethod;
28: import org.ofbiz.minilang.method.ContextAccessor;
29: import org.ofbiz.minilang.method.MethodContext;
30: import org.ofbiz.minilang.method.MethodOperation;
31: import org.w3c.dom.Element;
32:
33: /**
34: * Gets a sequenced ID from the delegator and puts it in the env
35: *
36: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
37: * @version $Revision: 1.2 $
38: * @since 2.0
39: */
40: public class SequencedIdToEnv extends MethodOperation {
41:
42: String seqName;
43: ContextAccessor envAcsr;
44: boolean toString;
45: long staggerMax = 1;
46:
47: public SequencedIdToEnv(Element element, SimpleMethod simpleMethod) {
48: super (element, simpleMethod);
49: seqName = element.getAttribute("sequence-name");
50: envAcsr = new ContextAccessor(element.getAttribute("env-name"));
51: // default false, anything but true is false
52: toString = "true".equals(element.getAttribute("to-string"));
53: String staggerMaxStr = element.getAttribute("stagger-max");
54: if (UtilValidate.isNotEmpty(staggerMaxStr)) {
55: try {
56: this .staggerMax = Long.parseLong(staggerMaxStr);
57: if (this .staggerMax < 1) {
58: this .staggerMax = 1;
59: }
60: } catch (NumberFormatException e) {
61: this .staggerMax = 1;
62: }
63: }
64: }
65:
66: public boolean exec(MethodContext methodContext) {
67: String seqName = methodContext.expandString(this .seqName);
68: Long seqId = methodContext.getDelegator().getNextSeqId(seqName,
69: staggerMax);
70:
71: if (toString) {
72: envAcsr.put(methodContext, seqId.toString());
73: } else {
74: envAcsr.put(methodContext, seqId);
75: }
76: return true;
77: }
78: }
|