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   import java.util.Iterator;
8   import java.util.List;
9   
10  import javax.servlet.http.HttpServletRequest;
11  
12  import net.sf.hibernate.HibernateException;
13  
14  import org.apache.commons.logging.Log;
15  import org.apache.commons.logging.LogFactory;
16  import org.apache.struts.action.ActionErrors;
17  import org.apache.struts.action.ActionMapping;
18  
19  import com.pnpconsult.zeiterfassung.actions.SubmitActionForm;
20  import com.pnpconsult.zeiterfassung.helper.ActivityManager;
21  import com.pnpconsult.zeiterfassung.helper.ProjectManager;
22  import com.pnpconsult.zeiterfassung.helper.UserEntryManager;
23  import com.pnpconsult.zeiterfassung.helper.UserManager;
24  import com.pnpconsult.zeiterfassung.model.UserEntry;
25  import com.pnpconsult.zeiterfassung.util.ActionErrorUtils;
26  import com.pnpconsult.zeiterfassung.util.DateUtils;
27  import com.pnpconsult.zeiterfassung.util.NumberUtils;
28  import com.pnpconsult.zeiterfassung.util.SimpleExpressionParser;
29  
30  /***
31   * @author <a href="mailto:powerpete@users.sf.net">M. Petersen </a>
32   * @version $Id: EditUserEntryRowForm.java,v 1.2 2004/06/09 19:26:40 powerpete
33   *          Exp $
34   * 
35   * @struts.form name="editUserEntryRowForm"
36   */
37  public class EditUserEntryRowForm extends SubmitActionForm
38  {
39      private static final Log LOG = LogFactory.getLog(EditUserEntryRowForm.class);
40      private int index;
41      private long[] activityId;
42      private float[] billingFactor;
43      private Date[] date;
44      private float[] hours;
45      private long[] id;
46      private String[] information;
47      private String[] involved;
48      private String[] partner;
49      private long[] projectId;
50      private Date[] targetDate;
51      private String[] userLogin;
52      private boolean[] delete;
53      private boolean billingFactorFormatError;
54      private boolean dateFormatError;
55      private boolean targetDateFormatError;
56      private boolean hoursFormatError;
57      private List activities;
58      private List projects;
59      private List users;
60      private String newline;
61  
62      public void reset(ActionMapping mapping, HttpServletRequest request)
63      {
64          super.reset(mapping, request);
65          newline = null;
66          try
67          {
68              activities = new ActivityManager().loadNotArchived();
69              projects = new ProjectManager().loadNotArchived();
70              users = new UserManager().loadNotArchived();
71          }
72          catch (HibernateException e)
73          {
74              LOG.fatal("Error while accessing database.", e);
75          }
76      }
77  
78      public ActionErrors validate(
79          ActionMapping mapping,
80          HttpServletRequest request)
81      {
82          if (userHasNotClicked() || userHasClickedCancel())
83          {
84              return null;
85          }
86          ActionErrors errors = new ActionErrors();
87          if (billingFactorFormatError)
88          {
89              ActionErrorUtils.add(
90                  errors,
91                  "secure.manager.editUserEntryRow.billingFactor_format_error");
92          }
93          if (dateFormatError)
94          {
95              ActionErrorUtils.add(
96                  errors,
97                  "secure.manager.editUserEntryRow.date_format_error");
98          }
99          if (hoursFormatError)
100         {
101             ActionErrorUtils.add(
102                 errors,
103                 "secure.manager.editUserEntryRow.hours_format_error");
104         }
105         if (targetDateFormatError)
106         {
107             ActionErrorUtils.add(
108                 errors,
109                 "secure.manager.editUserEntryRow.targetDate_format_error");
110         }
111         return errors;
112     }
113 
114     public int getIndex()
115     {
116         return index;
117     }
118 
119     public void setIndex(int index)
120     {
121         this.index = index;
122     }
123 
124     void setEntries(Collection entries)
125     {
126         int size = entries.size();
127         activityId = new long[size];
128         billingFactor = new float[size];
129         date = new Date[size];
130         hours = new float[size];
131         id = new long[size];
132         information = new String[size];
133         involved = new String[size];
134         partner = new String[size];
135         projectId = new long[size];
136         targetDate = new Date[size];
137         userLogin = new String[size];
138         delete = new boolean[size];
139         int i = 0;
140         for (Iterator it = entries.iterator(); it.hasNext();)
141         {
142             UserEntry entry = (UserEntry) it.next();
143             activityId[i] = entry.getActivity().getId();
144             billingFactor[i] = entry.getBillingFactor();
145             date[i] = entry.getDate();
146             hours[i] = entry.getHours();
147             id[i] = entry.getId();
148             information[i] = entry.getInformation();
149             involved[i] = entry.getInvolved();
150             partner[i] = entry.getPartner();
151             projectId[i] = entry.getProject().getId();
152             targetDate[i] = entry.getTargetDate();
153             userLogin[i] = entry.getUser().getLogin();
154             i++;
155         }
156     }
157 
158     void updateEntries() throws HibernateException
159     {
160         int size = id.length;
161         for (int i = 0; i < size; i++)
162         {
163             UserEntryManager userEntryManager = new UserEntryManager();
164             if (delete[i])
165             {
166                 userEntryManager.delete(id[i]);
167             }
168             else
169             {
170                 UserEntry entry = userEntryManager.open(id[i]);
171                 if (entry.getBackupUserEntry() == null)
172                 {
173                     userEntryManager.createBackup(entry);
174                 }
175                 entry.setActivity(new ActivityManager().load(
176                     activityId[i]));
177                 entry.setBillingFactor(billingFactor[i]);
178                 entry.setDate(date[i]);
179                 entry.setHours(hours[i]);
180                 entry.setInformation(information[i]);
181                 entry.setInvolved(involved[i]);
182                 entry.setPartner(partner[i]);
183                 entry.setProject(new ProjectManager()
184                     .load(projectId[i]));
185                 entry.setTargetDate(targetDate[i]);
186                 entry.setUser(new UserManager().load(userLogin[i]));
187                 userEntryManager.update(entry);
188             }
189         }
190     }
191 
192     public long getActivityId(int index)
193     {
194         return activityId[index];
195     }
196 
197     public void setActivityId(int index, long activityId)
198     {
199         this.activityId[index] = activityId;
200     }
201 
202     public String getBillingFactor(int index)
203     {
204         return NumberUtils.formatShort(billingFactor[index]);
205     }
206 
207     public void setBillingFactor(int index, String billingFactor)
208     {
209         try
210         {
211             this.billingFactor[index] = (float) SimpleExpressionParser.parse(billingFactor);
212         }
213         catch (ParseException e)
214         {
215             billingFactorFormatError = true;
216         }
217     }
218 
219     public String getDate(int index)
220     {
221         return DateUtils.format(date[index]);
222     }
223 
224     public void setDate(int index, String date)
225     {
226         try
227         {
228             this.date[index] = DateUtils.parse(date);
229         }
230         catch (ParseException e)
231         {
232             dateFormatError = true;
233         }
234     }
235 
236     public String getHours(int index)
237     {
238         return NumberUtils.formatShort(hours[index]);
239     }
240 
241     public void setHours(int index, String hours)
242     {
243         try
244         {
245             this.hours[index] = (float) SimpleExpressionParser.parse(hours);
246         }
247         catch (ParseException e)
248         {
249             hoursFormatError = true;
250         }
251     }
252 
253     public long getId(int index)
254     {
255         return id[index];
256     }
257 
258     public void setId(int index, long id)
259     {
260         this.id[index] = id;
261     }
262 
263     public String getInformation(int index)
264     {
265         return information[index];
266     }
267 
268     public void setInformation(int index, String information)
269     {
270         this.information[index] = information;
271     }
272 
273     public String getInvolved(int index)
274     {
275         return involved[index];
276     }
277 
278     public void setInvolved(int index, String involved)
279     {
280         this.involved[index] = involved;
281     }
282 
283     public String getPartner(int index)
284     {
285         return partner[index];
286     }
287 
288     public void setPartner(int index, String partner)
289     {
290         this.partner[index] = partner;
291     }
292 
293     public long getProjectId(int index)
294     {
295         return projectId[index];
296     }
297 
298     public void setProjectId(int index, long projectId)
299     {
300         this.projectId[index] = projectId;
301     }
302 
303     public String getTargetDate(int index)
304     {
305         return DateUtils.format(targetDate[index]);
306     }
307 
308     public void setTargetDate(int index, String targetDate)
309     {
310         try
311         {
312             this.targetDate[index] = DateUtils.parse(targetDate);
313         }
314         catch (ParseException e)
315         {
316             targetDateFormatError = true;
317         }
318     }
319 
320     public String getUserLogin(int index)
321     {
322         return userLogin[index];
323     }
324 
325     public void setUserLogin(int index, String userLogin)
326     {
327         this.userLogin[index] = userLogin;
328     }
329 
330     public boolean getDelete(int index)
331     {
332         return delete[index];
333     }
334 
335     public void setDelete(int index, boolean delete)
336     {
337         this.delete[index] = delete;
338     }
339 
340     public List getActivities()
341     {
342         return activities;
343     }
344 
345     public int getNumberOfEntries()
346     {
347         // Take one of the arrays. They should all have the same size.
348         return activityId == null ? 0 : activityId.length;
349     }
350 
351     public List getProjects()
352     {
353         return projects;
354     }
355 
356     public List getUsers()
357     {
358         return users;
359     }
360     
361     public void setNewline(String newline)
362     {
363         this.newline = newline;
364     }
365     
366     public boolean userHasClickedNewline()
367     {
368         return newline != null;
369     }
370     
371     public boolean userHasNotClicked()
372     {
373         return super.userHasNotClicked() && !userHasClickedNewline();
374     }
375 }