View Javadoc

1   // Created on 14.11.2003
2   package com.pnpconsult.zeiterfassung.model;
3   
4   import java.text.DateFormat;
5   import java.util.Date;
6   
7   import org.apache.commons.lang.builder.CompareToBuilder;
8   import org.apache.commons.logging.Log;
9   import org.apache.commons.logging.LogFactory;
10  
11  import com.pnpconsult.zeiterfassung.util.Description;
12  
13  /***
14   * @author <a href="mailto:powerpete@users.sf.net">M. Petersen </a>
15   * @version $Id: AbstractUserEntry.java,v 1.10 2004/07/21 22:13:11 powerpete Exp $
16   */
17  public abstract class AbstractUserEntry extends AbstractBaseEntry
18  {
19      private static final Log LOG = LogFactory.getLog(AbstractUserEntry.class);
20      private Activity activity;
21      private String partner;
22      private String involved;
23      private float billingFactor = 1.0F;
24      private Date date;
25      private String information;
26  
27      /***
28       * @return Returns the activity.
29       * 
30       * @hibernate.many-to-one column="activityId"
31       */
32      public Activity getActivity()
33      {
34          return activity;
35      }
36  
37      /***
38       * @param activity
39       *            The activity to set.
40       */
41      public void setActivity(Activity activity)
42      {
43          this.activity = activity;
44      }
45  
46      /***
47       * @return Returns the billingFactor.
48       * 
49       * @hibernate.property
50       */
51      public float getBillingFactor()
52      {
53          return billingFactor;
54      }
55  
56      /***
57       * @param billingFactor
58       *            The billingFactor to set.
59       */
60      public void setBillingFactor(float billingFactor)
61      {
62          this.billingFactor = billingFactor;
63      }
64  
65      /***
66       * @return Returns the date.
67       * 
68       * @hibernate.property type="date"
69       */
70      public Date getDate()
71      {
72          return date;
73      }
74  
75      public String getFormattedDate()
76      {
77          return DateFormat.getDateInstance().format(date);
78      }
79  
80      /***
81       * @param date
82       *            The date to set.
83       */
84      public void setDate(Date date)
85      {
86          this.date = date;
87      }
88  
89      /***
90       * @return Returns the involved.
91       * 
92       * @hibernate.property
93       */
94      public String getInvolved()
95      {
96          return involved;
97      }
98  
99      /***
100      * @param involved
101      *            The involved to set.
102      */
103     public void setInvolved(String involved)
104     {
105         this.involved = involved;
106     }
107 
108     /***
109      * @return Returns the partner.
110      * 
111      * @hibernate.property
112      */
113     public String getPartner()
114     {
115         return partner;
116     }
117 
118     /***
119      * @param partner
120      *            The partner to set.
121      */
122     public void setPartner(String partner)
123     {
124         this.partner = partner;
125     }
126 
127     /***
128      * Returns the description constructed from the <tt>partner</tt>,
129      * <tt>involved</tt>,<tt>targetDate</tt> and <tt>information</tt>
130      * fields.
131      * 
132      * @return The description.
133      */
134     protected Description buildDescription()
135     {
136         return buildDescription(false, true);
137     }
138 
139     private Description buildDescription(
140         boolean includeAcivity,
141         boolean includeTargetDate)
142     {
143         Description description = new Description();
144         if (includeAcivity)
145         {
146             description.append(activity);
147         }
148         description.append(", ", "Gesprächspartner: ", partner, null).append(
149             ", ",
150             null,
151             involved,
152             null);
153         if (includeTargetDate)
154         {
155             description.append(", ", "Termin: ", getTargetDate(), null);
156         }
157         description.append(", ", null, information, null);
158         return description;
159     }
160 
161     /***
162      * @return Returns the information.
163      * 
164      * @hibernate.property
165      */
166     public String getInformation()
167     {
168         return information;
169     }
170 
171     /***
172      * @param information
173      *            The information to set.
174      */
175     public void setInformation(String information)
176     {
177         this.information = information;
178     }
179 
180     public float computeHourlyRate()
181     {
182         User user = getUser();
183         float rate;
184         if (user == null)
185         {
186             LOG.error("User is null: " + getId() + "@" + toString());
187             return 0;
188         }
189         else
190         {
191             rate = user.getRate();
192         }
193 
194         UserProject userProject = user.getUserProject(getProject());
195         if (userProject == null)
196         {
197             LOG.error("UserProject is null: " + getId() + "@" + toString());
198         }
199         else
200         {
201             rate *= userProject.getBillingFactor();
202         }
203 
204         if (activity == null)
205         {
206             LOG.error("Activity is null: " + getId() + "@" + toString());
207             return 0;
208         }
209         else
210         {
211             rate *= activity.getBillingFactor();
212         }
213         rate *= billingFactor;
214 
215         return rate;
216     }
217 
218     public float getRate()
219     {
220         return computeHourlyRate();
221     }
222 
223     /***
224      * Compares two {@link AbstractUserEntry}s, so that they appear in the
225      * following order:
226      * <ol>
227      * <li>User name
228      * <li>Date
229      * <li>Project
230      * <li>Activity
231      * <li>Target date
232      * <li>Information
233      * <li>Hours
234      * <li>...
235      * </ol>
236      */
237     public int compareTo(Object o)
238     {
239         AbstractUserEntry e = (AbstractUserEntry) o;
240 
241         return new CompareToBuilder().append(getUser(), e.getUser()).append(
242             e.getDate(),
243             getDate()).append(getProject(), e.getProject()).append(
244             getActivity(),
245             e.getActivity()).append(getTargetDate(), e.getTargetDate()).append(
246             getInformation(),
247             e.getInformation()).append(getHours(), e.getHours()).append(
248             getId(),
249             e.getId()).toComparison();
250     }
251 
252     public String getBillDescription()
253     {
254         return buildDescription(true, false).toString();
255     }
256 
257     public String getUserDescription()
258     {
259         return toString();
260     }
261 }