01: /*******************************************************************************
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: *******************************************************************************/package org.ofbiz.minilang.method.entityops;
19:
20: import org.ofbiz.base.util.Debug;
21: import org.ofbiz.base.util.UtilValidate;
22: import org.ofbiz.entity.GenericValue;
23: import org.ofbiz.minilang.SimpleMethod;
24: import org.ofbiz.minilang.method.ContextAccessor;
25: import org.ofbiz.minilang.method.MethodContext;
26: import org.ofbiz.minilang.method.MethodOperation;
27: import org.w3c.dom.Element;
28:
29: /**
30: * Look at existing values for a sub-entity with a sequenced secondary ID, and get the highest plus 1
31: */
32: public class MakeNextSeqId extends MethodOperation {
33:
34: public static final String module = MakeNextSeqId.class.getName();
35:
36: String seqFieldName;
37: ContextAccessor valueAcsr;
38: String numericPaddingStr;
39: String incrementByStr;
40:
41: public MakeNextSeqId(Element element, SimpleMethod simpleMethod) {
42: super (element, simpleMethod);
43: seqFieldName = element.getAttribute("seq-field-name");
44: valueAcsr = new ContextAccessor(element
45: .getAttribute("value-name"));
46:
47: numericPaddingStr = element.getAttribute("numeric-padding");
48: incrementByStr = element.getAttribute("increment-by");
49: }
50:
51: public boolean exec(MethodContext methodContext) {
52: String seqFieldName = methodContext
53: .expandString(this .seqFieldName);
54: String numericPaddingStr = methodContext
55: .expandString(this .numericPaddingStr);
56: String incrementByStr = methodContext
57: .expandString(this .incrementByStr);
58: int numericPadding = 5;
59: int incrementBy = 1;
60: try {
61: if (UtilValidate.isNotEmpty(numericPaddingStr)) {
62: numericPadding = Integer.parseInt(numericPaddingStr);
63: }
64: } catch (Exception e) {
65: Debug.logError(e, "numeric-padding format invalid for ["
66: + numericPaddingStr + "]", module);
67: }
68: try {
69: if (UtilValidate.isNotEmpty(incrementByStr)) {
70: incrementBy = Integer.parseInt(incrementByStr);
71: }
72: } catch (Exception e) {
73: Debug.logError(e, "increment-by format invalid for ["
74: + incrementByStr + "]", module);
75: }
76:
77: GenericValue value = (GenericValue) valueAcsr
78: .get(methodContext);
79: methodContext.getDelegator().setNextSubSeqId(value,
80: seqFieldName, numericPadding, incrementBy);
81:
82: return true;
83: }
84:
85: public String rawString() {
86: // TODO: something more than the empty tag
87: return "<make-next-seq-id/>";
88: }
89:
90: public String expandedString(MethodContext methodContext) {
91: // TODO: something more than a stub/dummy
92: return this.rawString();
93: }
94: }
|