001: /*
002: * $Id: MakeNextSeqId.java,v 1.4 2003/11/28 18:48:46 jonesde Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.minilang.method.entityops;
025:
026: import java.util.Iterator;
027: import java.util.List;
028:
029: import org.ofbiz.base.util.Debug;
030: import org.ofbiz.base.util.UtilFormatOut;
031: import org.ofbiz.base.util.UtilMisc;
032: import org.ofbiz.base.util.UtilValidate;
033: import org.ofbiz.entity.GenericValue;
034: import org.ofbiz.minilang.SimpleMethod;
035: import org.ofbiz.minilang.method.ContextAccessor;
036: import org.ofbiz.minilang.method.MethodContext;
037: import org.ofbiz.minilang.method.MethodOperation;
038: import org.w3c.dom.Element;
039:
040: /**
041: * Gets a sequenced ID from the delegator and puts it in the env
042: *
043: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
044: * @version $Revision: 1.4 $
045: * @since 2.0
046: */
047: public class MakeNextSeqId extends MethodOperation {
048:
049: public static final String module = MakeNextSeqId.class.getName();
050:
051: String seqFieldName;
052: ContextAccessor valueAcsr;
053: String numericPaddingStr;
054: String incrementByStr;
055:
056: public MakeNextSeqId(Element element, SimpleMethod simpleMethod) {
057: super (element, simpleMethod);
058: seqFieldName = element.getAttribute("seq-field-name");
059: valueAcsr = new ContextAccessor(element
060: .getAttribute("value-name"));
061:
062: numericPaddingStr = element.getAttribute("numeric-padding");
063: incrementByStr = element.getAttribute("increment-by");
064: }
065:
066: public boolean exec(MethodContext methodContext) {
067: String seqFieldName = methodContext
068: .expandString(this .seqFieldName);
069: String numericPaddingStr = methodContext
070: .expandString(this .numericPaddingStr);
071: String incrementByStr = methodContext
072: .expandString(this .incrementByStr);
073: int numericPadding = 5;
074: int incrementBy = 1;
075: try {
076: if (UtilValidate.isNotEmpty(numericPaddingStr)) {
077: numericPadding = Integer.parseInt(numericPaddingStr);
078: }
079: } catch (Exception e) {
080: Debug.logError(e, "numeric-padding format invalid for ["
081: + numericPaddingStr + "]", module);
082: }
083: try {
084: if (UtilValidate.isNotEmpty(incrementByStr)) {
085: incrementBy = Integer.parseInt(incrementByStr);
086: }
087: } catch (Exception e) {
088: Debug.logError(e, "increment-by format invalid for ["
089: + incrementByStr + "]", module);
090: }
091:
092: GenericValue value = (GenericValue) valueAcsr
093: .get(methodContext);
094: if (UtilValidate.isEmpty(value.getString(seqFieldName))) {
095: value.remove(seqFieldName);
096: GenericValue lookupValue = methodContext.getDelegator()
097: .makeValue(value.getEntityName(), null);
098: lookupValue.setPKFields(value);
099: try {
100: // get values in reverse order
101: List allValues = methodContext.getDelegator()
102: .findByAnd(value.getEntityName(), lookupValue,
103: UtilMisc.toList("-" + seqFieldName));
104: //Debug.logInfo("Get existing values from entity " + value.getEntityName() + " with lookupValue: " + lookupValue + ", and the seqFieldName: " + seqFieldName + ", and the results are: " + allValues, module);
105: Iterator allValueIter = allValues.iterator();
106: Integer seqVal = null;
107: while (seqVal == null && allValueIter.hasNext()) {
108: GenericValue first = (GenericValue) allValueIter
109: .next();
110: String currentSeqId = null;
111: if (first != null) {
112: currentSeqId = first.getString(seqFieldName);
113: }
114: try {
115: if (currentSeqId != null) {
116: seqVal = Integer.valueOf(currentSeqId);
117: }
118: } catch (Exception e) {
119: seqVal = null;
120: Debug.logWarning(
121: "Error converting last SeqId to a number: "
122: + e.toString(), module);
123: }
124: }
125:
126: String newSeqId = null;
127: if (seqVal != null) {
128: newSeqId = UtilFormatOut.formatPaddedNumber(seqVal
129: .intValue()
130: + incrementBy, numericPadding);
131: } else {
132: newSeqId = UtilFormatOut.formatPaddedNumber(1,
133: numericPadding);
134: }
135:
136: value.set(seqFieldName, newSeqId);
137: } catch (Exception e) {
138: Debug.logError(e, "Error making next seqId", module);
139: return true;
140: }
141: }
142:
143: return true;
144: }
145: }
|