View Javadoc

1   // Created on 24.11.2003
2   package com.pnpconsult.zeiterfassung.actions.user;
3   
4   import java.io.Serializable;
5   import java.text.DateFormat;
6   import java.text.ParseException;
7   import java.util.Date;
8   import java.util.Iterator;
9   import java.util.List;
10  import java.util.SortedSet;
11  
12  import javax.servlet.http.HttpServletRequest;
13  
14  import org.apache.commons.lang.StringUtils;
15  import org.apache.commons.logging.Log;
16  import org.apache.commons.logging.LogFactory;
17  import org.apache.struts.action.ActionErrors;
18  import org.apache.struts.action.ActionMapping;
19  
20  import com.pnpconsult.zeiterfassung.actions.EditForm;
21  import com.pnpconsult.zeiterfassung.helper.ActivityManager;
22  import com.pnpconsult.zeiterfassung.helper.ProjectManager;
23  import com.pnpconsult.zeiterfassung.helper.UserManager;
24  import com.pnpconsult.zeiterfassung.model.Activity;
25  import com.pnpconsult.zeiterfassung.model.Project;
26  import com.pnpconsult.zeiterfassung.model.User;
27  import com.pnpconsult.zeiterfassung.model.UserEntry;
28  import com.pnpconsult.zeiterfassung.model.UserProject;
29  import com.pnpconsult.zeiterfassung.util.ActionErrorUtils;
30  import com.pnpconsult.zeiterfassung.util.DateUtils;
31  import com.pnpconsult.zeiterfassung.util.NumberUtils;
32  import com.pnpconsult.zeiterfassung.util.SimpleExpressionParser;
33  
34  /***
35   * @author <a href="mailto:powerpete@users.sf.net">M. Petersen </a>
36   * @version $Id: InputForm.java,v 1.12 2004/06/25 09:08:49 powerpete Exp $
37   * 
38   * @struts.form name="inputForm"
39   */
40  public class InputForm extends EditForm
41  {
42  
43      private static final Log LOG = LogFactory.getLog(InputForm.class);
44      private List activities;
45      private long activityId;
46      private boolean allProjects;
47      private float billingFactor;
48      private Date date;
49      private long entryId;
50      private float hours;
51      private String involved;
52      private String involvedSetSelection;
53      private String partner;
54      private String partnerSetSelection;
55      private String information;
56  
57      private ProjectEntries projectEntries;
58      private long projectId;
59      private SortedSet projects = new UnarchivedProjectSet();
60      private String submit;
61      private Date targetDate;
62  
63      private Date targetDateListSelection;
64      private User user;
65      private boolean billingFactorFormatError;
66      private boolean dateError;
67      private boolean dateFormatError;
68      private boolean hoursError;
69      private boolean hoursFormatError;
70      private boolean targetDateFormatError;
71  
72      public String getInformation()
73      {
74          return information;
75      }
76  
77      public void setInformation(String information)
78      {
79          this.information = information;
80      }
81  
82      protected Serializable dataObjectKey()
83      {
84          return new Long(entryId);
85      }
86  
87      protected Class dataObjectType()
88      {
89          return UserEntry.class;
90      }
91  
92      public List getActivities()
93      {
94          return activities;
95      }
96  
97      public long getActivityId()
98      {
99          return activityId;
100     }
101 
102     public String getBillingFactor()
103     {
104         if (billingFactor == -1)
105         {
106             return "";
107         }
108         return NumberUtils.formatShort(billingFactor);
109     }
110 
111     public String getDate()
112     {
113         if (date == null)
114         {
115             return "";
116         }
117         return DateFormat.getDateInstance().format(date);
118     }
119 
120     public long getEntryId()
121     {
122         return entryId;
123     }
124 
125     public String getHours()
126     {
127         if (hours == -1)
128         {
129             return "";
130         }
131         return NumberUtils.formatShort(hours);
132     }
133 
134     public String getInvolved()
135     {
136         return involved;
137     }
138 
139     public SortedSet getInvolvedSet()
140     {
141         return getProjectEntries().getInvolvedSet();
142     }
143 
144     public String getInvolvedSetSelection()
145     {
146         return involvedSetSelection;
147     }
148 
149     public String getPartner()
150     {
151         return partner;
152     }
153 
154     public SortedSet getPartnerSet()
155     {
156         return getProjectEntries().getPartnerSet();
157     }
158 
159     public String getPartnerSetSelection()
160     {
161         return partnerSetSelection;
162     }
163 
164     private ProjectEntries getProjectEntries()
165     {
166         if (projectEntries == null)
167         {
168             projectEntries = new ProjectEntries(projectId);
169         }
170         return projectEntries;
171     }
172 
173     public String getProjectId()
174     {
175         return String.valueOf(projectId);
176     }
177 
178     public SortedSet getProjects()
179     {
180         return projects;
181     }
182 
183     public String getSubmit()
184     {
185         return submit;
186     }
187 
188     public String getTargetDate()
189     {
190         if (targetDate == null)
191         {
192             return "";
193         }
194         return DateFormat.getDateInstance().format(targetDate);
195     }
196 
197     public List getTargetDateList()
198     {
199         return getProjectEntries().getTargetDateStringList();
200     }
201 
202     public String getTargetDateListSelection()
203     {
204         if (targetDateListSelection == null)
205         {
206             return "";
207         }
208         return DateFormat.getDateInstance().format(targetDateListSelection);
209     }
210 
211     public User getUser()
212     {
213         return user;
214     }
215 
216     public boolean isAllProjects()
217     {
218         return allProjects;
219     }
220 
221     public boolean isCheckTargetDate()
222     {
223         return !getProjectEntries().containsTargetDate(targetDate);
224     }
225 
226     public boolean isCheckInvolved()
227     {
228         return !getProjectEntries().containsInvolved(involved);
229     }
230 
231     public boolean isCheckPartner()
232     {
233         return !getProjectEntries().containsPartner(partner);
234     }
235 
236     public boolean needsCheck()
237     {
238         if (isCheckTargetDate())
239         {
240             return true;
241         }
242         if (isCheckInvolved())
243         {
244             return true;
245         }
246         if (isCheckPartner())
247         {
248             return true;
249         }
250         return false;
251     }
252 
253     protected Object newDataObject()
254     {
255         return new UserEntry();
256     }
257 
258     protected void readFromDataObject(Object obj)
259     {
260         UserEntry entry = (UserEntry) obj;
261         Activity activity = entry.getActivity();
262         activityId = activity == null ? -1 : activity.getId();
263         Project project = entry.getProject();
264         projectId = project == null ? -1 : project.getId();
265         billingFactor = entry.getBillingFactor();
266         date = entry.getDate();
267         hours = entry.getHours();
268         involved = entry.getInvolved();
269         partner = entry.getPartner();
270         targetDate = entry.getTargetDate();
271         information = entry.getInformation();
272 
273         setAllProjects(!isNew);
274     }
275 
276     public void reset(ActionMapping mapping, HttpServletRequest request)
277     {
278         if (submit != null && submit.equals("Abbrechen"))
279         {
280             return;
281         }
282         if (request.getRequestURI().endsWith("checkSubmit.do"))
283         {
284             return;
285         }
286         try
287         {
288             String login = request.getUserPrincipal().getName();
289             user = new UserManager().load(login);
290             projects.clear();
291             for (Iterator it = user.getUserProjects().iterator(); it.hasNext();)
292             {
293                 UserProject userProject = (UserProject) it.next();
294                 projects.add(userProject.getProject());
295             }
296             activities = new ActivityManager().loadNotArchived();
297             clear();
298         }
299         catch (Exception e)
300         {
301             LOG.fatal("Error resetting InputForm.", e);
302         }
303     }
304 
305     /***
306      * Clears all fields of this form.
307      */
308     public void clear()
309     {
310         date = date == null ? new Date() : date;
311         targetDate = null;
312         //activityId = -1;
313         allProjects = false;
314         billingFactor = 1;
315         entryId = -1;
316         hours = -1;
317         involved = null;
318         partner = null;
319         //projectId = -1;
320         partnerSetSelection = null;
321         involvedSetSelection = null;
322         targetDateListSelection = null;
323         information = null;
324         setNew(true);
325     }
326 
327     public void setActivities(List activities)
328     {
329         this.activities = activities;
330     }
331 
332     public void setActivityId(long activityId)
333     {
334         this.activityId = activityId;
335     }
336 
337     public void setAllProjects(boolean allProjects)
338     {
339         this.allProjects = allProjects;
340         if (allProjects)
341         {
342             try
343             {
344                 projects.clear();
345                 projects.addAll(new ProjectManager().loadAll());
346             }
347             catch (Exception e)
348             {
349                 LOG.fatal("Error loading all projects.", e);
350             }
351         }
352     }
353 
354     public void setBillingFactor(String billingFactor)
355     {
356         try
357         {
358             this.billingFactor = (float) SimpleExpressionParser.parse(billingFactor);
359             billingFactorFormatError = false;
360         }
361         catch (ParseException e)
362         {
363             billingFactorFormatError = true;
364         }
365     }
366 
367     public void setDate(String date)
368     {
369         if ("".equals(date))
370         {
371             dateError = true;
372             this.date = null;
373             return;
374         }
375         try
376         {
377             this.date = DateUtils.parse(date);
378             dateError = false;
379             dateFormatError = false;
380         }
381         catch (ParseException e)
382         {
383             dateFormatError = true;
384             this.date = null;
385         }
386     }
387 
388     public void setEntryId(long id)
389     {
390         this.entryId = id;
391     }
392 
393     public void setHours(String hours)
394     {
395         if ("".equals(hours))
396         {
397             hoursError = true;
398             return;
399         }
400         try
401         {
402             this.hours = (float) SimpleExpressionParser.parse(hours);
403             hoursError = false;
404             hoursFormatError = false;
405         }
406         catch (ParseException e)
407         {
408             hoursFormatError = true;
409             this.hours = 0;
410         }
411     }
412 
413     public void setInvolved(String involved)
414     {
415         this.involved = involved;
416     }
417 
418     public void setInvolvedSetSelection(String involvedSetSelection)
419     {
420         this.involvedSetSelection = involvedSetSelection;
421         if (!StringUtils.isEmpty(involvedSetSelection))
422         {
423             involved = involvedSetSelection;
424         }
425     }
426 
427     public void setPartner(String partner)
428     {
429         this.partner = partner;
430     }
431 
432     public void setPartnerSetSelection(String partnerSetSelection)
433     {
434         this.partnerSetSelection = partnerSetSelection;
435         if (!StringUtils.isEmpty(partnerSetSelection))
436         {
437             partner = partnerSetSelection;
438         }
439     }
440 
441     public void setProjectId(String projectId)
442     {
443         this.projectId = Long.parseLong(projectId);
444         projectEntries = null;
445     }
446 
447     public void setProjects(SortedSet projects)
448     {
449         this.projects = projects;
450     }
451 
452     public void setSubmit(String submit)
453     {
454         this.submit = submit;
455         setAllProjects("Alle Projekte".equals(submit));
456     }
457 
458     public void setTargetDate(String targetDate)
459     {
460         if ("".equals(targetDate))
461         {
462             this.targetDate = null;
463             targetDateFormatError = false;
464             return;
465         }
466         try
467         {
468             this.targetDate = DateUtils.parse(targetDate);
469             targetDateFormatError = false;
470         }
471         catch (ParseException e)
472         {
473             targetDateFormatError = true;
474             this.targetDate = null;
475         }
476     }
477 
478     public void setTargetDateListSelection(String targetDateListSelection)
479     {
480         if ("".equals(targetDateListSelection))
481         {
482             this.targetDateListSelection = null;
483             return;
484         }
485         try
486         {
487             this.targetDateListSelection = DateFormat.getDateInstance().parse(
488                 targetDateListSelection);
489             this.targetDate = this.targetDateListSelection;
490         }
491         catch (ParseException e)
492         {
493             LOG.error("Error while parsing targetDateListSelection: "
494                 + targetDateListSelection
495                 + " "
496                 + e);
497             this.targetDateListSelection = null;
498         }
499     }
500 
501     public ActionErrors validate(
502         ActionMapping mapping,
503         HttpServletRequest request)
504     {
505         if (isAllProjects())
506         {
507             return null;
508         }
509         ActionErrors errors = new ActionErrors();
510         if ("Sichern".equals(submit))
511         {
512             return errors;
513         }
514         if (billingFactorFormatError)
515         {
516             ActionErrorUtils.add(
517                 errors,
518                 "errors.secure.user.input.billingFactor_format_error");
519         }
520         if (dateError)
521         {
522             ActionErrorUtils.add(errors, "errors.secure.user.input.date");
523         }
524         if (dateFormatError)
525         {
526             ActionErrorUtils.add(
527                 errors,
528                 "errors.secure.user.input.date_format_error");
529         }
530         if (hoursError)
531         {
532             ActionErrorUtils.add(errors, "errors.secure.user.input.hours");
533         }
534         if (hoursFormatError)
535         {
536             ActionErrorUtils.add(
537                 errors,
538                 "errors.secure.user.input.hours_format_error");
539         }
540         if (targetDateFormatError)
541         {
542             ActionErrorUtils.add(
543                 errors,
544                 "errors.secure.user.input.targetDate_format_error");
545         }
546         if ("Alle Projekte".equals(submit))
547         {
548             ActionErrorUtils.add(errors, "errors.secure.user.input.allProjects");
549         }
550         return errors;
551     }
552 
553     protected void writeToDataObject(Object obj)
554     {
555         UserEntry entry = (UserEntry) obj;
556         entry.setActivity(new Activity(activityId));
557         entry.setBillingFactor(billingFactor);
558         entry.setDate(date);
559         entry.setHours(hours);
560         entry.setInformation(information);
561         entry.setInvolved(involved);
562         entry.setPartner(partner);
563         entry.setProject(new Project(projectId));
564         entry.setTargetDate(targetDate);
565         entry.setUser(user);
566         LOG.info("Entry written");
567         clear();
568         LOG.info("Form cleared.");
569     }
570 }