1
2 package com.pnpconsult.zeiterfassung.actions.admin;
3
4 import java.io.Serializable;
5 import java.text.ParseException;
6 import java.util.Date;
7
8 import javax.servlet.http.HttpServletRequest;
9
10 import org.apache.commons.lang.StringUtils;
11 import org.apache.struts.action.ActionErrors;
12 import org.apache.struts.action.ActionMapping;
13
14 import com.pnpconsult.zeiterfassung.actions.EditForm;
15 import com.pnpconsult.zeiterfassung.model.Customer;
16 import com.pnpconsult.zeiterfassung.model.Project;
17 import com.pnpconsult.zeiterfassung.util.ActionErrorUtils;
18 import com.pnpconsult.zeiterfassung.util.DateUtils;
19 import com.pnpconsult.zeiterfassung.util.NumberUtils;
20 import com.pnpconsult.zeiterfassung.util.SimpleExpressionParser;
21
22 /***
23 * @author <a href="mailto:powerpete@users.sf.net">M. Petersen</a>
24 * @version $Id: EditProjectForm.java,v 1.7 2004/05/28 21:21:25 powerpete Exp $
25 *
26 * @struts.form name="editProjectForm"
27 */
28 public class EditProjectForm extends EditForm
29 {
30 private long id;
31 private String number;
32 private String name;
33 private String currency;
34 private Customer customer;
35 private float[] taxValue = new float[3];
36 private Date[] taxDate = new Date[3];
37 private boolean taxDateFormatError;
38 private boolean taxValueFormatError;
39 private boolean newProject;
40
41 public void setNewProject(boolean newProject)
42 {
43 super.setNew(newProject);
44 this.newProject = newProject;
45 }
46
47 public boolean isNewProject()
48 {
49 return newProject;
50 }
51
52 public ActionErrors validate(
53 ActionMapping mapping,
54 HttpServletRequest request)
55 {
56 ActionErrors errors = new ActionErrors();
57 if (StringUtils.isBlank(name))
58 {
59 ActionErrorUtils.add(errors, "errors.secure.admin.project.name");
60 }
61 if (taxDateFormatError)
62 {
63 ActionErrorUtils.add(errors, "errors.secure.admin.project.taxDate_format_error");
64 }
65 if (taxValueFormatError)
66 {
67 ActionErrorUtils.add(errors, "errors.secure.admin.project.taxValue_format_error");
68 }
69 return errors;
70 }
71
72 public void writeToDataObject(Object obj)
73 {
74 Project project = (Project) obj;
75 project.setNumber(number);
76 project.setCustomer(customer);
77 project.setCurrency(currency);
78 project.setName(name);
79 project.setTaxValue0(taxValue[0]);
80 project.setTaxDate0(taxDate[0]);
81 project.setTaxValue1(taxValue[1]);
82 project.setTaxDate1(taxDate[1]);
83 project.setTaxValue2(taxValue[2]);
84 project.setTaxDate2(taxDate[2]);
85 }
86
87 public void readFromDataObject(Object obj)
88 {
89 Project project = (Project) obj;
90 number = project.getNumber();
91 currency = project.getCurrency();
92 customer = project.getCustomer();
93 name = project.getName();
94 taxValue[0] = project.getTaxValue0();
95 taxDate[0] = project.getTaxDate0();
96 taxValue[1] = project.getTaxValue1();
97 taxDate[1] = project.getTaxDate1();
98 taxValue[2] = project.getTaxValue2();
99 taxDate[2] = project.getTaxDate2();
100 }
101
102 public long getId()
103 {
104 return id;
105 }
106
107 public void setId(long id)
108 {
109 this.id = id;
110 }
111
112 /***
113 * @return Returns the name.
114 */
115 public String getName()
116 {
117 return name;
118 }
119
120 /***
121 * @param name The name to set.
122 */
123 public void setName(String name)
124 {
125 this.name = name;
126 }
127
128 /***
129 * @return Returns the currency.
130 */
131 public String getCurrency()
132 {
133 return currency;
134 }
135
136 /***
137 * @param currency The currency to set.
138 */
139 public void setCurrency(String currency)
140 {
141 this.currency = currency;
142 }
143
144 /***
145 * @return Returns the customerName.
146 */
147 public String getCustomerName()
148 {
149 return customer == null ? "" : customer.getName();
150 }
151
152 public void setCustomerName(String customerName)
153 {
154 if (customer == null)
155 {
156 customer = new Customer();
157 }
158 customer.setName(customerName);
159 }
160
161 /***
162 * @return Returns the number.
163 */
164 public String getNumber()
165 {
166 return number;
167 }
168
169 /***
170 * @param number The number to set.
171 */
172 public void setNumber(String number)
173 {
174 this.number = number;
175 }
176
177 public String getTaxValue0()
178 {
179 return NumberUtils.formatShort(taxValue[0]);
180 }
181
182 public String getTaxDate0()
183 {
184 return DateUtils.format(taxDate[0]);
185 }
186
187 public void setTaxValue0(String taxValue)
188 {
189 try
190 {
191 this.taxValue[0] = (float) SimpleExpressionParser.parse(taxValue);
192 taxValueFormatError = false;
193 }
194 catch (ParseException e)
195 {
196 taxValueFormatError = true && taxValueFormatError;
197 }
198 }
199
200 public void setTaxDate0(String taxDate)
201 {
202 try
203 {
204 this.taxDate[0] = DateUtils.parse(taxDate);
205 taxDateFormatError = false;
206 }
207 catch (ParseException e)
208 {
209 taxDateFormatError = true && taxDateFormatError;
210 }
211 }
212
213 public String getTaxValue1()
214 {
215 return NumberUtils.formatShort(taxValue[1]);
216 }
217
218 public String getTaxDate1()
219 {
220 return DateUtils.format(taxDate[1]);
221 }
222
223 public void setTaxValue1(String taxValue)
224 {
225 try
226 {
227 this.taxValue[1] = (float) SimpleExpressionParser.parse(taxValue);
228 taxValueFormatError = false;
229 }
230 catch (ParseException e)
231 {
232 taxValueFormatError = true && taxValueFormatError;
233 }
234 }
235
236 public void setTaxDate1(String taxDate)
237 {
238 try
239 {
240 this.taxDate[1] = DateUtils.parse(taxDate);
241 taxDateFormatError = false;
242 }
243 catch (ParseException e)
244 {
245 taxDateFormatError = true && taxDateFormatError;
246 }
247 }
248
249 /***
250 * @hibernate.property
251 */
252 public String getTaxValue2()
253 {
254 return NumberUtils.formatShort(taxValue[2]);
255 }
256
257 /***
258 * @hibernate.property type="date"
259 */
260 public String getTaxDate2()
261 {
262 return DateUtils.format(taxDate[2]);
263 }
264
265 public void setTaxValue2(String taxValue)
266 {
267 try
268 {
269 this.taxValue[2] = (float) SimpleExpressionParser.parse(taxValue);
270 taxValueFormatError = false;
271 }
272 catch (ParseException e)
273 {
274 taxValueFormatError = true && taxValueFormatError;
275 }
276 }
277
278 public void setTaxDate2(String taxDate)
279 {
280 try
281 {
282 this.taxDate[2] = DateUtils.parse(taxDate);
283 taxDateFormatError = false;
284 }
285 catch (ParseException e)
286 {
287 taxDateFormatError = true && taxDateFormatError;
288 }
289 }
290
291 /***
292 * @return Returns the customerId.
293 */
294 public long getCustomerId()
295 {
296 return customer == null ? -1 : customer.getId();
297 }
298
299 /***
300 * @param customerId The customerId to set.
301 */
302 public void setCustomerId(long customerId)
303 {
304 if (customer == null)
305 {
306 customer = new Customer();
307 }
308 customer.setId(customerId);
309 }
310
311 /***
312 * @see com.pnpconsult.zeiterfassung.actions.EditForm#newDataObject()
313 */
314 protected Object newDataObject()
315 {
316 return new Project();
317 }
318
319 /***
320 * @see com.pnpconsult.zeiterfassung.actions.EditForm#dataObjectType()
321 */
322 protected Class dataObjectType()
323 {
324 return Project.class;
325 }
326
327 /***
328 * @see com.pnpconsult.zeiterfassung.actions.EditForm#dataObjectKey()
329 */
330 protected Serializable dataObjectKey()
331 {
332 return new Long(id);
333 }
334
335 /***
336 * @see org.apache.struts.action.ActionForm#reset(org.apache.struts.action.ActionMapping, javax.servlet.http.HttpServletRequest)
337 */
338 public void reset(ActionMapping mapping, HttpServletRequest request)
339 {
340 taxDateFormatError = true;
341 taxValueFormatError = true;
342 }
343 }