KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mdarad > framework > util > struts > criteria > MoneyCriterion


1 /*
2     Mdarad-Toolobox is a collection of tools for Architected RAD
3     (Rapid Application Development) based on an MDA approach.
4     The toolbox contains frameworks and generators for many environments
5     (JAVA, J2EE, Hibernate, .NET, C++, etc.) which allow to generate
6     applications from a design Model
7     Copyright (C) 2004-2005 Elapse Technologies Inc.
8
9     This library is free software; you can redistribute it and/or
10     modify it under the terms of the GNU General Public
11     License as published by the Free Software Foundation; either
12     version 2.1 of the License, or (at your option) any later version.
13
14     This library is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17     General Public License for more details.
18
19     You should have received a copy of the GNU General Public
20     License along with this library; if not, write to the Free Software
21     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */

23 package org.mdarad.framework.util.struts.criteria;
24
25 import java.util.Currency JavaDoc;
26 import java.util.Locale JavaDoc;
27
28 import org.dataisland.primitives.datatype.Money;
29
30 /**
31  * This classe representes a criterion that is used with "date" properties.
32  * It defines the operators used with a numeric criterion.
33  * @author Philippe Brouillette
34  * @version 1.0
35  */

36 public class MoneyCriterion extends NumericCriterion {
37
38     /**
39      * Constructor that takes all the needed properties to
40      * initialize a date criterion.
41      * @param name name of the criterion, this name is used as a key
42      * in the criterion map
43      * @param associatedEntity class type associated to this criterion
44      * @param property property used for the query criterion
45      * @param bundleName bundle name
46      * @param locale locale information
47      * @see FormCriterion#FormCriterion(String, CriterionProperty, String, Locale) FormCriterion
48      */

49     public MoneyCriterion(String JavaDoc name, Class JavaDoc associatedEntity, CriterionProperty property, String JavaDoc bundleName, Locale JavaDoc locale) {
50         super(name, associatedEntity, property, bundleName, locale);
51     }
52
53     /**
54      * Constructor that clones a query criterion. This constructor is used
55      * to instanciate a criterion in a search form when the form
56      * contains dynamic criteria.
57      * @param criterion query criterion that must be of
58      * type <code>DateCriterion</code>
59      * @see FormCriterion#FormCriterion(FormCriterion) FormCriterion
60      */

61     public MoneyCriterion(MoneyCriterion criterion) {
62         super(criterion);
63     }
64  
65     
66     /**
67      * Method that assigns the value of the criterion as a String. This method
68      * is needed for the user interface which uses only string values.
69      * @param value value to be assigned
70      * @see FormCriterion#setPropertyValue(java.lang.String)
71      */

72     public void setPropertyValue(String JavaDoc value) {
73         if (value != null && value.trim().length() > 0) {
74             //TODO: More intelligent system for money
75
String JavaDoc[] values = value.split(" ");
76
77             Money money = new Money();
78             Double JavaDoc amount = new Double JavaDoc(0.0d);
79             
80             //If values are found, otherwise use default
81
Currency JavaDoc currency = Currency.getInstance(Locale.getDefault());
82             if(values.length == 2) {
83                 amount = new Double JavaDoc(values[0]);
84                 currency = Currency.getInstance(values[1]);
85             } else if(value.trim().length() != 0) {
86                 amount = new Double JavaDoc(value);
87             }
88             money.setAmount(amount);
89             money.setCurrency(currency);
90             setValue(money);
91         }
92     }
93     
94     /**
95      * Method that returns the value as a string.
96      * @see FormCriterion#getPropertyValue()
97      */

98     public String JavaDoc getPropertyValue() {
99         if (getValue() != null) {
100             Money money = (Money) getValue();
101             return money.getAmount() + " " + money.getCurrency();
102         }
103         
104         return "";
105     }
106
107     /**
108      * Method that returns the object type of the criterion value.
109      *
110      * @see FormCriterion#getObjectType()
111      */

112     public Class JavaDoc getObjectType() {
113         return Money.class;
114     }
115 }
Popular Tags