KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > projectmanagement > business > timewage > PayRateManagerImpl


1 package projectmanagement.business.timewage;
2
3 import projectmanagement.business.employee.*;
4 import projectmanagement.business.project.*;
5 import projectmanagement.business.ProjectManagementBusinessException;
6 import projectmanagement.data.timewage.*;
7 import com.lutris.appserver.server.sql.DatabaseManagerException;
8 import com.lutris.appserver.server.sql.ObjectId;
9 import com.lutris.appserver.server.sql.ObjectIdException;
10 import com.lutris.dods.builder.generator.query.*;
11 import com.lutris.appserver.server.Enhydra;
12 import com.lutris.logging.*;
13
14 import java.sql.Date JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.Iterator JavaDoc;
17
18 import projectmanagement.spec.timewage.*;
19 import projectmanagement.spec.employee.*;
20 import projectmanagement.spec.project.*;
21 /**
22  * Used to find the instance of PayRate.
23  *
24  * @author Sasa Bojanic
25  * @version 1.0
26  */

27 public class PayRateManagerImpl implements PayRateManager {
28
29    /**
30     * The getAllPayRates method performs a database query to
31     * return all <CODE>PayRate</CODE> objects representing the
32     * row in the <CODE>PayRates</CODE> table.
33     * @return all the pay rates, or null if there are no any.
34     * @exception ProjectManagementBusinessException
35     * if there is a problem retrieving pay rate information.
36     */

37     public PayRate[] getAllPayRates()
38          throws ProjectManagementBusinessException {
39       try {
40          PayRateQuery query = new PayRateQuery();
41
42          PayRateDO[] foundPayRates = query.getDOArray();
43          if(foundPayRates.length != 0) {
44             PayRateImpl[] cs=new PayRateImpl[foundPayRates.length];
45             for (int i=0; i<foundPayRates.length; i++) {
46                cs[i]=new PayRateImpl(foundPayRates[i]);
47             }
48             return cs;
49          } else {
50             return null;
51          }
52       } catch(NonUniqueQueryException ex) {
53          Enhydra.getLogChannel().write(Logger.DEBUG,
54             "Non-unique pay rate found in database: "+ex.getMessage());
55          throw new ProjectManagementBusinessException("Non unique pay rate found");
56       } catch(DataObjectException ex) {
57          throw new ProjectManagementBusinessException("Database error retrieving pay rates: ", ex);
58       } /*catch(QueryException ex) {
59          throw new ProjectManagementBusinessException("Query exception retrieving pay rates: ", ex);
60       }*/

61    }
62
63    /**
64     * The getAllPayRatesForEmployee method performs a database query to
65     * return all <CODE>PayRate</CODE> objects representing the
66     * row in the <CODE>PayRates</CODE> table that belongs to the
67     * employee with given ID.
68     * @param employeeID the employee id
69     * @param distinctOnEmployProjectPair if true returns only one payrate
70     * for [employee,project] pair
71     * @return all the pay rates for given employee (it could be distinct on
72     * poject parameter, or not)
73     * @exception ProjectManagementBusinessException
74     * if there is a problem retrieving pay rate information.
75     */

76     public PayRate[] getAllPayRatesForEmployee(String JavaDoc employeeID, boolean distinctOnEmployProjectPair)
77     throws ProjectManagementBusinessException {
78         try {
79            EmployeeManagerImpl employeeManager=new EmployeeManagerImpl();
80            EmployeeImpl emp=(EmployeeImpl)employeeManager.findEmployeeByID(employeeID);
81            
82             if (emp==null) {
83                 throw new ProjectManagementBusinessException("The employee for given ID can't be found");
84             }
85             PayRateQuery query = new PayRateQuery();
86             query.setQueryEmployee(emp.getDO());
87
88             PayRateDO[] foundPayRates = query.getDOArray();
89 //System.out.println("distinctOnEmployProjectPair = "+distinctOnEmployProjectPair);
90
if(foundPayRates.length != 0) {
91                 if (distinctOnEmployProjectPair) {
92                     HashMap JavaDoc distinctPayRate = new HashMap JavaDoc();
93                     for (int i=0; i<foundPayRates.length; i++) {
94                         String JavaDoc key = foundPayRates[i].getEmployee().getOId()+"."+foundPayRates[i].getProject().getOId();
95                         distinctPayRate.put(key,foundPayRates[i]);
96                     }
97                     PayRateImpl[] prs=new PayRateImpl[distinctPayRate.size()];
98                     Iterator JavaDoc iter = distinctPayRate.values().iterator();
99                     for (int i = 0; iter.hasNext(); i++ )
100                         prs[i]=new PayRateImpl((PayRateDO)iter.next());
101                     return prs;
102                 } else {
103                     PayRateImpl[] prs=new PayRateImpl[foundPayRates.length];
104                     for (int i=0; i<foundPayRates.length; i++)
105                         prs[i]=new PayRateImpl(foundPayRates[i]);
106                     return prs;
107                 }
108             } else {
109                 return null;
110             }
111         } catch(NonUniqueQueryException ex) {
112             Enhydra.getLogChannel().write(Logger.DEBUG,
113             "Non-unique pay rate found in database: "+ex.getMessage());
114             throw new ProjectManagementBusinessException("Non unique pay rate found");
115         } catch(DataObjectException ex) {
116             throw new ProjectManagementBusinessException("Database error retrieving pay rates: ", ex);
117         } catch(QueryException ex) {
118             throw new ProjectManagementBusinessException("Query exception retrieving pay rates: ", ex);
119         }
120     }
121
122    /**
123     * The getAllPayRatesForEmployeeProjectPair method performs a database
124     * query to return all <CODE>PayRate</CODE> objects representing the
125     * row in the <CODE>PayRates</CODE> table that belongs to the
126     * employee with given ID, and project with a given ID.
127     * @param employeeID the employee id
128     * @param projectID the project id
129     * @return all the pay rates for given employee-project pair
130     * @exception ProjectManagementBusinessException
131     * if there is a problem retrieving pay rate information.
132     */

133     public PayRate[] getAllPayRatesForEmployeeProjectPair (String JavaDoc employeeID,String JavaDoc projectID)
134          throws ProjectManagementBusinessException {
135         try {
136             PayRateQuery query = new PayRateQuery();
137             if (employeeID != null || projectID != null) {
138                 QueryBuilder mainQuery=query.getQueryBuilder();
139                 if (employeeID != null) {
140                  
141            EmployeeManagerImpl employeeManager=new EmployeeManagerImpl();
142            EmployeeImpl employee=(EmployeeImpl) employeeManager.findEmployeeByID(employeeID);
143                  
144                     mainQuery.addWhere(PayRateDO.Employee,employee.getDO(),QueryBuilder.EQUAL);
145                 }
146                 if (projectID != null) {
147                   
148                 ProjectManagerImpl projectManager = new ProjectManagerImpl();
149                 Project project = projectManager.findProjectByID(projectID);
150                   
151                     mainQuery.addWhere(PayRateDO.Project,((ProjectImpl)project).getDO(),QueryBuilder.EQUAL);
152                 }
153             }
154             PayRateDO[] foundPayRates = query.getDOArray();
155             if(foundPayRates.length != 0) {
156                 PayRateImpl[] pr=new PayRateImpl[foundPayRates.length];
157                 for (int i=0; i<foundPayRates.length; i++) {
158                     pr[i]=new PayRateImpl(foundPayRates[i]);
159                 }
160                 return pr;
161             } else {
162                 return null;
163             }
164         } catch(NonUniqueQueryException ex) {
165             Enhydra.getLogChannel().write(Logger.DEBUG,
166             "Non-unique pay rate found in database: "+ex.getMessage());
167             throw new ProjectManagementBusinessException("Non unique pay rate found");
168         } catch(DataObjectException ex) {
169             throw new ProjectManagementBusinessException("Database error retrieving pay rates: ", ex);
170         }/* catch(QueryException ex) {
171             throw new ProjectManagementBusinessException("Query exception retrieving pay rates: ", ex);
172         }*/

173     }
174
175    /**
176     * The findPayRateByID method performs a database query to
177     * return a <CODE>PayRate</CODE> object
178     * representing the row in the <CODE>pay rate</CODE> table
179     * that matches the object id.
180     *
181     * @param id, the object id of the pay rate table.
182     * @return
183     * the pay rate. null if there isn't a pay rate associated
184     * the id
185     * @exception ProjectManagementBusinessException
186     * if there is a problem retrieving pay rate information.
187     */

188    public PayRate findPayRateByID(String JavaDoc id)
189          throws ProjectManagementBusinessException {
190       PayRateImpl thePayRate = null;
191
192       try {
193          PayRateQuery query = new PayRateQuery();
194          //set query
195
query.setQueryOId(new ObjectId(id));
196          // Throw an exception if more than one user by this name is found
197
query.requireUniqueInstance();
198          PayRateDO thePayRateDO = query.getNextDO();
199          thePayRate = new PayRateImpl(thePayRateDO);
200          return thePayRate;
201       } catch(Exception JavaDoc ex) {
202          throw new ProjectManagementBusinessException("Exception in findPayRateByID()", ex);
203       }
204    }
205
206     /**
207      * The findPayRateByEmployeeProjectAndDate method performs a database query to
208      * return a <CODE>PayRate</CODE> object
209      * representing the row in the <CODE>pay rate</CODE> table
210      * that matches the employee id, project id and has the closest lower or equal
211      * date to the given one.
212      *
213      * @param employeeID the employee id
214      * @param projectID the project id
215      * @param workingDate the date that [employee,project] pair record has to
216      * be closest to (lower or equal)
217      * @return
218      * the pay rate. null if there isn't a pay rate associated
219      * the id
220      * @exception ProjectManagementBusinessException
221      * if there is a problem retrieving pay rate information.
222      */

223     public PayRate findPayRateByEmployeeProjectAndDate(String JavaDoc employeeID, String JavaDoc projectID, Date JavaDoc workingDate)
224     throws ProjectManagementBusinessException {
225
226       PayRate thePayRate = null;
227
228       try {
229          EmployeeManagerImpl employeeManager=new EmployeeManagerImpl();
230          Employee emp= employeeManager.findEmployeeByID(employeeID);
231          
232          if (emp==null) {
233             throw new ProjectManagementBusinessException("The employee for given ID can't be found");
234          }
235          ProjectManagerImpl projectManager = new ProjectManagerImpl();
236          Project proj = projectManager.findProjectByID(projectID);
237          
238          if (proj==null) {
239             throw new ProjectManagementBusinessException("The project for given ID can't be found");
240          }
241
242          PayRateQuery query = new PayRateQuery();
243          //set query
244
query.setQueryEmployee(((EmployeeImpl)emp).getDO());
245          query.getQueryBuilder().addWhere(PayRateDO.Project,((ProjectImpl)proj).getDO(),QueryBuilder.EQUAL);
246          query.getQueryBuilder().addOrderByColumn(PayRateDO.ValidFrom,"DESC");
247
248          // searching for pay rate nearest to the given date
249
PayRateDO[] foundPayRates = query.getDOArray();
250          if(foundPayRates!=null && foundPayRates.length != 0) {
251 //System.out.println("There is "+foundPayRates.length+" prs");
252
for (int i=0; i<foundPayRates.length; i++) {
253                PayRateDO pr=foundPayRates[i];
254 //System.out.println("Comp "+pr.getValidFrom()+" to "+workingDate);
255
if (pr.getValidFrom().compareTo(workingDate)<=0) {
256                   thePayRate=new PayRateImpl(pr);
257                   break;
258                }
259             }
260          }
261          /*PayRateDO foundPayRate = query.getNextDO();
262          if( foundPayRate != null)
263              thePayRate=new PayRate(query.getNextDO());*/

264
265          return thePayRate;
266       } catch(Exception JavaDoc ex) {
267          throw new ProjectManagementBusinessException("Exception in findPayRateByEmployeeProjectAndDate()", ex);
268       }
269    }
270 public PayRate getPayRate()
271        throws ProjectManagementBusinessException{
272      return new PayRateImpl();
273     }
274 }
275
Popular Tags