KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > ejb > client > models > IssueFieldModel


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

18
19 package cowsultants.itracker.ejb.client.models;
20
21 import java.text.*;
22 import java.util.*;
23
24 import cowsultants.itracker.ejb.client.exceptions.*;
25 import cowsultants.itracker.ejb.client.resources.*;
26 import cowsultants.itracker.ejb.client.util.*;
27
28 public class IssueFieldModel extends GenericModel {
29     private CustomFieldModel customField;
30     private Integer JavaDoc customFieldId = new Integer JavaDoc(-1);
31
32     private String JavaDoc stringValue;
33     private int intValue;
34     private Date dateValue;
35
36     private Integer JavaDoc issueId;
37
38     public IssueFieldModel() {
39     }
40
41     public IssueFieldModel(CustomFieldModel field) {
42         this.customField = field;
43         this.customFieldId = field.getId();
44     }
45
46     public IssueFieldModel(CustomFieldModel field, Integer JavaDoc issueId) {
47         this(field);
48         this.issueId = issueId;
49     }
50
51     /**
52       * Gets the current field id.
53       */

54     public Integer JavaDoc getCustomFieldId() {
55         return (customField != null ? customField.getId() : customFieldId);
56     }
57
58     /**
59       * Sets the current field id.
60       */

61     public void setCustomFieldId(Integer JavaDoc value) {
62         customFieldId = value;
63     }
64
65     public CustomFieldModel getCustomField() {
66         return customField;
67     }
68
69     public void setCustomField(CustomFieldModel value) {
70         customField = value;
71     }
72
73     /**
74       * Gets the custom field value as a String.
75       * @param locale the locale used for any string formatting
76       * @return the current value of this field
77       */

78     public String JavaDoc getValue(Locale locale) {
79         return getValue(ITrackerResources.getBundle(locale), locale);
80     }
81
82     /**
83       * Gets the custom field value as a String.
84       * @param bundle a resource bundle to use for any string formatting
85       * @param locale a locale to use for any string formatting
86       * @return the current value of this field
87       */

88     public String JavaDoc getValue(ResourceBundle bundle, Locale locale) {
89         if(customField == null) {
90             return "";
91         }
92
93         if(customField.getFieldType() == CustomFieldUtilities.TYPE_INTEGER) {
94             return Integer.toString(getIntValue());
95         } else if(customField.getFieldType() == CustomFieldUtilities.TYPE_DATE) {
96             try {
97                 if(customField.getDateFormat() != CustomFieldUtilities.DATE_FORMAT_UNKNOWN) {
98                     SimpleDateFormat sdf = new SimpleDateFormat(bundle.getString("itracker.dateformat." + customField.getDateFormat()), locale);
99                     return sdf.format(getDateValue());
100                 }
101             } catch(Exception JavaDoc e) {
102                 Logger.logDebug("Error parsing date value.", e);
103             }
104         } else {
105             return (getStringValue() == null ? "" : getStringValue());
106         }
107         return "";
108     }
109
110     /**
111       * Sets the custom field value. Takes a string and then converts the value to the
112       * appropriate type based on the defined field type.
113       * @param value the value to set this field to as a string
114       * @param locale the locale used for any string formatting
115       * @throws IssueException represents an error formatting or parsing the value
116       */

117     public void setValue(String JavaDoc value, Locale locale) throws IssueException {
118         setValue(value, locale, ITrackerResources.getBundle(locale));
119     }
120
121     /**
122       * Sets the custom field value. Takes a string and then converts the value to the
123       * appropriate type based on the defined field type.
124       * @param value the value to set this field to as a string
125       * @param locale the locale used for any string formatting
126       * @param bundle the ResourceBundle used for any string formatting
127       * @throws IssueException represents an error formatting or parsing the value
128       */

129     public void setValue(String JavaDoc value, Locale locale, ResourceBundle bundle) throws IssueException {
130         if(value != null) {
131             if(customField.getFieldType() == CustomFieldUtilities.TYPE_INTEGER) {
132                 try {
133                     setIntValue(Integer.parseInt(value));
134                 } catch(NumberFormatException JavaDoc nfe) {
135                     throw new IssueException("Invalid integer.", IssueException.TYPE_CF_PARSE_NUM);
136                 }
137             } else if(customField.getFieldType() == CustomFieldUtilities.TYPE_DATE) {
138                 try {
139                     if(customField.getDateFormat() != CustomFieldUtilities.DATE_FORMAT_UNKNOWN) {
140                         SimpleDateFormat sdf = new SimpleDateFormat(bundle.getString("itracker.dateformat." + customField.getDateFormat()), locale);
141                         Date dateValue = sdf.parse(value);
142                         if(dateValue != null) {
143                             setDateValue(dateValue);
144                         } else {
145                             Logger.logDebug("Unable to set custom field date. Date value was null. Data was '" + value + "'");
146                             throw new IssueException("Invalid date.", IssueException.TYPE_CF_PARSE_DATE);
147                         }
148                     }
149                 } catch(Exception JavaDoc e) {
150                     Logger.logDebug("Unable to set custom field date. Exception: " + e.getMessage() + ". Data was '" + value + "'");
151                     throw new IssueException("Invalid date format.", IssueException.TYPE_CF_PARSE_DATE);
152                 }
153             } else {
154                 setStringValue(value);
155             }
156         }
157     }
158
159     public String JavaDoc getStringValue() {
160         return stringValue;
161     }
162
163     public void setStringValue(String JavaDoc value) {
164         stringValue = value;
165     }
166
167     public int getIntValue() {
168         return intValue;
169     }
170
171     public void setIntValue(int value) {
172         intValue = value;
173     }
174
175     public Date getDateValue() {
176         return dateValue;
177     }
178
179     public void setDateValue(Date value) {
180         dateValue = value;
181     }
182
183     public Integer JavaDoc getIssueId() {
184         return issueId;
185     }
186
187     public void setIssueId(Integer JavaDoc value) {
188         issueId = value;
189     }
190 }
191
Popular Tags