001: /*
002: * ====================================================================
003: * JAFFA - Java Application Framework For All
004: *
005: * Copyright (C) 2002 JAFFA Development Group
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: * Redistribution and use of this software and associated documentation ("Software"),
022: * with or without modification, are permitted provided that the following conditions are met:
023: * 1. Redistributions of source code must retain copyright statements and notices.
024: * Redistributions must also contain a copy of this document.
025: * 2. Redistributions in binary form must reproduce the above copyright notice,
026: * this list of conditions and the following disclaimer in the documentation
027: * and/or other materials provided with the distribution.
028: * 3. The name "JAFFA" must not be used to endorse or promote products derived from
029: * this Software without prior written permission. For written permission,
030: * please contact mail to: jaffagroup@yahoo.com.
031: * 4. Products derived from this Software may not be called "JAFFA" nor may "JAFFA"
032: * appear in their names without prior written permission.
033: * 5. Due credit should be given to the JAFFA Project (http://jaffa.sourceforge.net).
034: *
035: * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: */
049:
050: package org.jaffa.components.codehelper.tx;
051:
052: import org.apache.log4j.Logger;
053: import org.jaffa.components.codehelper.ICodeHelper;
054: import org.jaffa.exceptions.ApplicationExceptions;
055: import org.jaffa.exceptions.FrameworkException;
056: import org.jaffa.components.codehelper.dto.CodeHelperInDto;
057: import org.jaffa.components.codehelper.dto.CodeHelperInElementDto;
058: import org.jaffa.components.codehelper.dto.CodeHelperOutDto;
059: import org.jaffa.components.codehelper.dto.CodeHelperOutElementDto;
060: import org.jaffa.components.codehelper.dto.CodeHelperOutCodeDto;
061: import org.jaffa.components.codehelper.dto.CriteriaElementDto;
062: import org.jaffa.components.finder.*;
063: import org.jaffa.persistence.UOW;
064: import org.jaffa.persistence.Criteria;
065: import org.jaffa.util.BeanHelper;
066: import java.util.Iterator;
067: import org.jaffa.persistence.AtomicCriteria;
068:
069: /** This is the default implementation of the ICodeHelper interface. It will retrieve codes for each domainClassName passed in the input.
070: * It will use reflection to determine the fields to return. Additional criteria can also be passed in the input, to restrict the output.
071: * For complex queries, create custom implementations of the interface, rather than making this generic implementation complex !!!
072: *
073: * @author GautamJ
074: */
075: public class CodeHelperTx implements ICodeHelper {
076:
077: private static final Logger log = Logger
078: .getLogger(CodeHelperTx.class);
079: private static final String DEFAULT_APPEND_BEGIN_MARKER = " (";
080: private static final String DEFAULT_APPEND_END_MARKER = ")";
081:
082: /**
083: * This should be invoked, when done with the helper. This will ensure that all the acquired resources are released.
084: */
085: public void destroy() {
086: // do nothing
087: }
088:
089: /**
090: * Retrieves the Codes for the specified domainClassName passed in the input.
091: * @param input The input dto, containing a list of domainClass to retrieve.
092: * @return The Codes for each domainClassName passed in the input.
093: * @throws ApplicationExceptions This will be thrown if any invalid data is passed.
094: * @throws FrameworkException Indicates some system error.
095: */
096: public CodeHelperOutDto getCodes(CodeHelperInDto input)
097: throws ApplicationExceptions, FrameworkException {
098: UOW uow = null;
099: try {
100: // Print Debug Information for the input
101: if (log.isDebugEnabled()) {
102: log.debug("Input: "
103: + (input != null ? input.toString() : null));
104: }
105:
106: // Create the output
107: CodeHelperOutDto output = new CodeHelperOutDto();
108:
109: // create the UOW
110: uow = new UOW();
111:
112: if (input != null
113: && input.getCodeHelperInElementDtoCount() > 0) {
114: CodeHelperInElementDto[] inputElements = input
115: .getCodeHelperInElementDtos();
116: for (int i = 0; i < inputElements.length; i++)
117: output
118: .addCodeHelperOutElementDto(processInputElement(
119: uow, inputElements[i]));
120: }
121:
122: // Print Debug Information for the output
123: if (log.isDebugEnabled()) {
124: log.debug("Output: "
125: + (output != null ? output.toString() : null));
126: }
127: return output;
128: } finally {
129: if (uow != null)
130: uow.rollback();
131: }
132: }
133:
134: private CodeHelperOutElementDto processInputElement(UOW uow,
135: CodeHelperInElementDto inputElement)
136: throws ApplicationExceptions, FrameworkException {
137: Criteria c = new Criteria();
138: c.setTable(inputElement.getDomainClassName());
139: if (inputElement.getCriteriaFieldCount() > 0) {
140: CriteriaElementDto[] criteriaFields = inputElement
141: .getCriteriaFields();
142: for (int i = 0; i < criteriaFields.length; i++) {
143: CriteriaElementDto criteriaField = criteriaFields[i];
144: addCriteria(criteriaField, c);
145: }
146: }
147: c.addOrderBy(inputElement.getCodeFieldName(),
148: Criteria.ORDER_BY_ASC);
149:
150: Iterator i = uow.query(c).iterator();
151: CodeHelperOutElementDto outputElement = new CodeHelperOutElementDto();
152: outputElement.setCode(inputElement.getCode());
153: outputElement.setDomainClassName(inputElement
154: .getDomainClassName());
155: try {
156: while (i.hasNext()) {
157: Object domainObject = i.next();
158: Object code = BeanHelper.getField(domainObject,
159: inputElement.getCodeFieldName());
160: Object description = BeanHelper.getField(domainObject,
161: inputElement.getDescriptionFieldName());
162:
163: CodeHelperOutCodeDto codeDto = new CodeHelperOutCodeDto();
164: codeDto.setCode(code);
165: codeDto.setDescription(formatDescription(inputElement,
166: code, description));
167: outputElement.addCodeHelperOutCodeDto(codeDto);
168: }
169: } catch (NoSuchMethodException e) {
170: String str = "The CodeHelperTx could not introspect the Domain Class "
171: + inputElement.getDomainClassName()
172: + " for the code/description field";
173: log.warn(str, e);
174: }
175: return outputElement;
176: }
177:
178: private void addCriteria(CriteriaElementDto field, Criteria criteria) {
179: if (field != null) {
180: String name = field.getFieldName();
181: String operator = field.getCriteria() != null ? field
182: .getCriteria().getOperator() : null;
183: Object[] values = field.getCriteria() != null ? field
184: .getCriteria().returnValuesAsObjectArray() : null;
185: if (operator != null
186: && operator
187: .equals(CriteriaField.RELATIONAL_IS_NULL))
188: criteria.addCriteria(name, Criteria.RELATIONAL_IS_NULL);
189: else if (operator != null
190: && operator
191: .equals(CriteriaField.RELATIONAL_IS_NOT_NULL))
192: criteria.addCriteria(name,
193: Criteria.RELATIONAL_IS_NOT_NULL);
194: else if (values.length > 0) {
195: if (operator == null
196: || operator
197: .equals(CriteriaField.RELATIONAL_EQUALS))
198: criteria.addCriteria(name,
199: Criteria.RELATIONAL_EQUALS, values[0]);
200: else if (operator
201: .equals(CriteriaField.RELATIONAL_NOT_EQUALS))
202: criteria.addCriteria(name,
203: Criteria.RELATIONAL_NOT_EQUALS, values[0]);
204: else if (operator
205: .equals(CriteriaField.RELATIONAL_GREATER_THAN))
206: criteria
207: .addCriteria(name,
208: Criteria.RELATIONAL_GREATER_THAN,
209: values[0]);
210: else if (operator
211: .equals(CriteriaField.RELATIONAL_SMALLER_THAN))
212: criteria
213: .addCriteria(name,
214: Criteria.RELATIONAL_SMALLER_THAN,
215: values[0]);
216: else if (operator
217: .equals(CriteriaField.RELATIONAL_GREATER_THAN_EQUAL_TO))
218: criteria.addCriteria(name,
219: Criteria.RELATIONAL_GREATER_THAN_EQUAL_TO,
220: values[0]);
221: else if (operator
222: .equals(CriteriaField.RELATIONAL_SMALLER_THAN_EQUAL_TO))
223: criteria.addCriteria(name,
224: Criteria.RELATIONAL_SMALLER_THAN_EQUAL_TO,
225: values[0]);
226: else if (operator
227: .equals(CriteriaField.RELATIONAL_BEGINS_WITH))
228: criteria.addCriteria(name,
229: Criteria.RELATIONAL_BEGINS_WITH, values[0]);
230: else if (operator
231: .equals(CriteriaField.RELATIONAL_ENDS_WITH))
232: criteria.addCriteria(name,
233: Criteria.RELATIONAL_ENDS_WITH, values[0]);
234: else if (operator.equals(CriteriaField.RELATIONAL_LIKE))
235: criteria.addCriteria(name,
236: Criteria.RELATIONAL_LIKE, values[0]);
237: else if (operator
238: .equals(CriteriaField.RELATIONAL_BETWEEN)) {
239: criteria.addCriteria(name,
240: Criteria.RELATIONAL_GREATER_THAN_EQUAL_TO,
241: values[0]);
242: if (values.length > 1)
243: criteria
244: .addCriteria(
245: name,
246: Criteria.RELATIONAL_SMALLER_THAN_EQUAL_TO,
247: values[1]);
248: } else if (operator.equals(CriteriaField.RELATIONAL_IN)) {
249: if (values.length == 1) {
250: criteria.addCriteria(name,
251: Criteria.RELATIONAL_EQUALS, values[0]);
252: } else {
253: AtomicCriteria atomic = new AtomicCriteria();
254: atomic.addCriteria(name,
255: Criteria.RELATIONAL_EQUALS, values[0]);
256: for (int i = 1; i < values.length; i++)
257: atomic.addOrCriteria(name,
258: Criteria.RELATIONAL_EQUALS,
259: values[i]);
260: criteria.addAtomic(atomic);
261: }
262: }
263: }
264: }
265: }
266:
267: /** This method is used to format the description field.
268: * It returns the description as is, if the 'appendCodeAndDescription' flag is false.
269: * Otherwise, it appends the code and description, using the markers specified in the inputElement.
270: * If no markers are specified, then the format will be 'code (description)'.
271: * @param inputElement The input to the ICodeHelper.
272: * @param code The code value.
273: * @param description The description for the code.
274: * @return a formatted description.
275: */
276: public static Object formatDescription(
277: CodeHelperInElementDto inputElement, Object code,
278: Object description) {
279: Object out = null;
280: if (inputElement.isAppendCodeAndDescription()) {
281: String beginMarker = null;
282: String endMarker = null;
283: if (inputElement.getAppendBeginMarker() != null)
284: beginMarker = inputElement.getAppendBeginMarker();
285: if (inputElement.getAppendEndMarker() != null)
286: endMarker = inputElement.getAppendEndMarker();
287: if (inputElement.getAppendBeginMarker() == null
288: && inputElement.getAppendEndMarker() == null) {
289: beginMarker = DEFAULT_APPEND_BEGIN_MARKER;
290: endMarker = DEFAULT_APPEND_END_MARKER;
291: }
292:
293: StringBuffer buf = new StringBuffer();
294: if (code != null)
295: buf.append(code);
296: if (beginMarker != null)
297: buf.append(beginMarker);
298: if (description != null)
299: buf.append(description);
300: if (endMarker != null)
301: buf.append(endMarker);
302: out = buf.toString();
303: } else {
304: out = description;
305: }
306: return out;
307: }
308:
309: /** Test stub
310: * @param args arguments
311: */
312: public static void main(String[] args) {
313: try {
314: org.apache.log4j.BasicConfigurator.configure();
315: CodeHelperTx tx = new CodeHelperTx();
316: int testCode = 1;
317:
318: switch (testCode) {
319: case 1: {
320: // test getCode
321: CodeHelperInDto in = new CodeHelperInDto();
322: CodeHelperInElementDto element1 = new CodeHelperInElementDto();
323: element1
324: .setDomainClassName("org.example.applications.app1.modules.request.domain.ReqRemarks");
325: element1.setCodeFieldName("RequestId");
326: element1.setDescriptionFieldName("remarks");
327: //element1.addCriteriaField(new StringCriteriaField("RequestId", "In", new String[] {"REQ02658", "REQ02659", "REQ00645"}));
328: in.addCodeHelperInElementDto(element1);
329:
330: tx.getCodes(in);
331: }
332: break;
333: }
334: } catch (ApplicationExceptions e) {
335: for (Iterator itr = e.iterator(); itr.hasNext();)
336: ((Exception) itr.next()).printStackTrace();
337: } catch (Exception e) {
338: e.printStackTrace();
339: }
340: }
341: }
|