View Javadoc

1   // Created on 24.11.2003
2   package com.pnpconsult.zeiterfassung.actions.manager;
3   
4   import java.text.ParseException;
5   import java.util.Collection;
6   import java.util.Date;
7   
8   import javax.servlet.http.HttpServletRequest;
9   
10  import net.sf.hibernate.HibernateException;
11  
12  import org.apache.commons.lang.StringUtils;
13  import org.apache.commons.logging.Log;
14  import org.apache.commons.logging.LogFactory;
15  import org.apache.struts.action.ActionErrors;
16  import org.apache.struts.action.ActionMapping;
17  
18  import com.pnpconsult.zeiterfassung.actions.SubmitActionForm;
19  import com.pnpconsult.zeiterfassung.helper.UserManager;
20  import com.pnpconsult.zeiterfassung.model.BillEntry;
21  import com.pnpconsult.zeiterfassung.model.User;
22  import com.pnpconsult.zeiterfassung.util.ActionErrorUtils;
23  import com.pnpconsult.zeiterfassung.util.DateUtils;
24  import com.pnpconsult.zeiterfassung.util.NumberUtils;
25  import com.pnpconsult.zeiterfassung.util.SimpleExpressionParser;
26  
27  /***
28   * @author <a href="mailto:powerpete@users.sf.net">M. Petersen </a>
29   * @version $Id: EditBillEntryRowForm.java,v 1.2 2004/06/09 19:26:40 powerpete
30   *          Exp $
31   * 
32   * @struts.form name="editBillEntryRowForm"
33   */
34  public class EditBillEntryRowForm extends SubmitActionForm
35  {
36      private static final Log LOG = LogFactory.getLog(EditBillEntryRowForm.class);
37      private int index;
38      private User user;
39      private float hours;
40      private float rate;
41      private Date targetDate;
42      private String description;
43      private boolean hoursFormatError;
44      private boolean rateFormatError;
45      private boolean targetDateFormatError;
46      private long billEntryId;
47      private Collection users;
48  
49      public void reset(ActionMapping mapping, HttpServletRequest request)
50      {
51          super.reset(mapping, request);
52          try
53          {
54              users = new UserManager().loadNotArchived();
55          }
56          catch (HibernateException e)
57          {
58              LOG.fatal(e.getMessage(), e);
59          }
60          targetDateFormatError = false;
61      }
62  
63      public ActionErrors validate(
64          ActionMapping mapping,
65          HttpServletRequest request)
66      {
67          if (userHasNotClicked() || userHasClickedCancel())
68          {
69              return null;
70          }
71          if (userHasClickedDelete())
72          {
73              return null;
74          }
75          ActionErrors errors = new ActionErrors();
76          if (hoursFormatError)
77          {
78              ActionErrorUtils.add(
79                  errors,
80                  "errors.secure.manager.editBillEntryRow.hoursFormatError_format_error");
81          }
82          if (rateFormatError)
83          {
84              ActionErrorUtils.add(
85                  errors,
86                  "errors.secure.manager.editBillEntryRow.rateFormatError_format_error");
87          }
88          if (targetDateFormatError)
89          {
90              ActionErrorUtils.add(
91                  errors,
92                  "errors.secure.manager.editBillEntryRow.targetDateFormatError_format_error");
93          }
94          if (StringUtils.isBlank(description))
95          {
96              ActionErrorUtils.add(
97                  errors,
98                  "errors.secure.manager.editBillEntryRow.description");
99          }
100         return errors;
101     }
102 
103     public BillEntry getBillEntry()
104     {
105         BillEntry entry = new BillEntry();
106         entry.setId(billEntryId);
107         entry.setDescription(description);
108         entry.setHours(hours);
109         entry.setRate(rate);
110         entry.setTargetDate(targetDate);
111         entry.setUser(user);
112         return entry;
113     }
114 
115     public void setBillEntry(BillEntry entry)
116     {
117         billEntryId = entry.getId();
118         description = entry.getDescription();
119         hours = entry.getHours();
120         rate = entry.getRate();
121         targetDate = entry.getTargetDate();
122         user = entry.getUser();
123     }
124 
125     public int getIndex()
126     {
127         return index;
128     }
129 
130     public void setIndex(int index)
131     {
132         this.index = index;
133     }
134 
135     public String getHours()
136     {
137         return NumberUtils.formatShort(hours);
138     }
139 
140     public void setHours(String hours)
141     {
142         try
143         {
144             this.hours = (float) SimpleExpressionParser.parse(hours);
145             hoursFormatError = false;
146         }
147         catch (ParseException e)
148         {
149             hoursFormatError = true;
150         }
151     }
152 
153     public String getRate()
154     {
155         return NumberUtils.formatShort(rate);
156     }
157 
158     public void setRate(String rate)
159     {
160         try
161         {
162             this.rate = (float) SimpleExpressionParser.parse(rate);
163             rateFormatError = false;
164         }
165         catch (ParseException e)
166         {
167             rateFormatError = true;
168         }
169     }
170 
171     public String getTargetDate()
172     {
173         return DateUtils.format(targetDate);
174     }
175 
176     public void setTargetDate(String targetDate)
177     {
178         try
179         {
180             this.targetDate = DateUtils.parse(targetDate);
181             targetDateFormatError = false;
182         }
183         catch (ParseException e)
184         {
185             targetDateFormatError = true;
186         }
187     }
188 
189     public String getUserLogin()
190     {
191         return user == null ? null : user.getLogin();
192     }
193 
194     public void setUserLogin(String userLogin)
195     {
196         try
197         {
198             this.user = new UserManager().load(userLogin);
199         }
200         catch (Exception e)
201         {
202             e.printStackTrace();
203         }
204     }
205 
206     public String getDescription()
207     {
208         return description;
209     }
210 
211     public void setDescription(String description)
212     {
213         this.description = description;
214     }
215 
216     public long getBillEntryId()
217     {
218         return billEntryId;
219     }
220 
221     public void setBillEntryId(long billEntryId)
222     {
223         this.billEntryId = billEntryId;
224     }
225 
226     public Collection getUsers()
227     {
228         return users;
229     }
230 }