001: package projectmanagement.business.timewage;
002:
003: import projectmanagement.business.employee.*;
004: import projectmanagement.business.project.*;
005: import projectmanagement.business.ProjectManagementBusinessException;
006: import projectmanagement.data.timewage.*;
007: import com.lutris.appserver.server.sql.DatabaseManagerException;
008: import com.lutris.appserver.server.sql.ObjectId;
009: import com.lutris.appserver.server.sql.ObjectIdException;
010: import com.lutris.dods.builder.generator.query.*;
011: import com.lutris.appserver.server.Enhydra;
012: import com.lutris.logging.*;
013:
014: import java.sql.Date;
015: import java.util.HashMap;
016: import java.util.Iterator;
017:
018: import projectmanagement.spec.timewage.*;
019: import projectmanagement.spec.employee.*;
020: import projectmanagement.spec.project.*;
021:
022: /**
023: * Used to find the instance of PayRate.
024: *
025: * @author Sasa Bojanic
026: * @version 1.0
027: */
028: public class PayRateManagerImpl implements PayRateManager {
029:
030: /**
031: * The getAllPayRates method performs a database query to
032: * return all <CODE>PayRate</CODE> objects representing the
033: * row in the <CODE>PayRates</CODE> table.
034: * @return all the pay rates, or null if there are no any.
035: * @exception ProjectManagementBusinessException
036: * if there is a problem retrieving pay rate information.
037: */
038: public PayRate[] getAllPayRates()
039: throws ProjectManagementBusinessException {
040: try {
041: PayRateQuery query = new PayRateQuery();
042:
043: PayRateDO[] foundPayRates = query.getDOArray();
044: if (foundPayRates.length != 0) {
045: PayRateImpl[] cs = new PayRateImpl[foundPayRates.length];
046: for (int i = 0; i < foundPayRates.length; i++) {
047: cs[i] = new PayRateImpl(foundPayRates[i]);
048: }
049: return cs;
050: } else {
051: return null;
052: }
053: } catch (NonUniqueQueryException ex) {
054: Enhydra.getLogChannel().write(
055: Logger.DEBUG,
056: "Non-unique pay rate found in database: "
057: + ex.getMessage());
058: throw new ProjectManagementBusinessException(
059: "Non unique pay rate found");
060: } catch (DataObjectException ex) {
061: throw new ProjectManagementBusinessException(
062: "Database error retrieving pay rates: ", ex);
063: } /*catch(QueryException ex) {
064: throw new ProjectManagementBusinessException("Query exception retrieving pay rates: ", ex);
065: }*/
066: }
067:
068: /**
069: * The getAllPayRatesForEmployee method performs a database query to
070: * return all <CODE>PayRate</CODE> objects representing the
071: * row in the <CODE>PayRates</CODE> table that belongs to the
072: * employee with given ID.
073: * @param employeeID the employee id
074: * @param distinctOnEmployProjectPair if true returns only one payrate
075: * for [employee,project] pair
076: * @return all the pay rates for given employee (it could be distinct on
077: * poject parameter, or not)
078: * @exception ProjectManagementBusinessException
079: * if there is a problem retrieving pay rate information.
080: */
081: public PayRate[] getAllPayRatesForEmployee(String employeeID,
082: boolean distinctOnEmployProjectPair)
083: throws ProjectManagementBusinessException {
084: try {
085: EmployeeManagerImpl employeeManager = new EmployeeManagerImpl();
086: EmployeeImpl emp = (EmployeeImpl) employeeManager
087: .findEmployeeByID(employeeID);
088:
089: if (emp == null) {
090: throw new ProjectManagementBusinessException(
091: "The employee for given ID can't be found");
092: }
093: PayRateQuery query = new PayRateQuery();
094: query.setQueryEmployee(emp.getDO());
095:
096: PayRateDO[] foundPayRates = query.getDOArray();
097: //System.out.println("distinctOnEmployProjectPair = "+distinctOnEmployProjectPair);
098: if (foundPayRates.length != 0) {
099: if (distinctOnEmployProjectPair) {
100: HashMap distinctPayRate = new HashMap();
101: for (int i = 0; i < foundPayRates.length; i++) {
102: String key = foundPayRates[i].getEmployee()
103: .getOId()
104: + "."
105: + foundPayRates[i].getProject()
106: .getOId();
107: distinctPayRate.put(key, foundPayRates[i]);
108: }
109: PayRateImpl[] prs = new PayRateImpl[distinctPayRate
110: .size()];
111: Iterator iter = distinctPayRate.values().iterator();
112: for (int i = 0; iter.hasNext(); i++)
113: prs[i] = new PayRateImpl((PayRateDO) iter
114: .next());
115: return prs;
116: } else {
117: PayRateImpl[] prs = new PayRateImpl[foundPayRates.length];
118: for (int i = 0; i < foundPayRates.length; i++)
119: prs[i] = new PayRateImpl(foundPayRates[i]);
120: return prs;
121: }
122: } else {
123: return null;
124: }
125: } catch (NonUniqueQueryException ex) {
126: Enhydra.getLogChannel().write(
127: Logger.DEBUG,
128: "Non-unique pay rate found in database: "
129: + ex.getMessage());
130: throw new ProjectManagementBusinessException(
131: "Non unique pay rate found");
132: } catch (DataObjectException ex) {
133: throw new ProjectManagementBusinessException(
134: "Database error retrieving pay rates: ", ex);
135: } catch (QueryException ex) {
136: throw new ProjectManagementBusinessException(
137: "Query exception retrieving pay rates: ", ex);
138: }
139: }
140:
141: /**
142: * The getAllPayRatesForEmployeeProjectPair method performs a database
143: * query to return all <CODE>PayRate</CODE> objects representing the
144: * row in the <CODE>PayRates</CODE> table that belongs to the
145: * employee with given ID, and project with a given ID.
146: * @param employeeID the employee id
147: * @param projectID the project id
148: * @return all the pay rates for given employee-project pair
149: * @exception ProjectManagementBusinessException
150: * if there is a problem retrieving pay rate information.
151: */
152: public PayRate[] getAllPayRatesForEmployeeProjectPair(
153: String employeeID, String projectID)
154: throws ProjectManagementBusinessException {
155: try {
156: PayRateQuery query = new PayRateQuery();
157: if (employeeID != null || projectID != null) {
158: QueryBuilder mainQuery = query.getQueryBuilder();
159: if (employeeID != null) {
160:
161: EmployeeManagerImpl employeeManager = new EmployeeManagerImpl();
162: EmployeeImpl employee = (EmployeeImpl) employeeManager
163: .findEmployeeByID(employeeID);
164:
165: mainQuery.addWhere(PayRateDO.Employee, employee
166: .getDO(), QueryBuilder.EQUAL);
167: }
168: if (projectID != null) {
169:
170: ProjectManagerImpl projectManager = new ProjectManagerImpl();
171: Project project = projectManager
172: .findProjectByID(projectID);
173:
174: mainQuery.addWhere(PayRateDO.Project,
175: ((ProjectImpl) project).getDO(),
176: QueryBuilder.EQUAL);
177: }
178: }
179: PayRateDO[] foundPayRates = query.getDOArray();
180: if (foundPayRates.length != 0) {
181: PayRateImpl[] pr = new PayRateImpl[foundPayRates.length];
182: for (int i = 0; i < foundPayRates.length; i++) {
183: pr[i] = new PayRateImpl(foundPayRates[i]);
184: }
185: return pr;
186: } else {
187: return null;
188: }
189: } catch (NonUniqueQueryException ex) {
190: Enhydra.getLogChannel().write(
191: Logger.DEBUG,
192: "Non-unique pay rate found in database: "
193: + ex.getMessage());
194: throw new ProjectManagementBusinessException(
195: "Non unique pay rate found");
196: } catch (DataObjectException ex) {
197: throw new ProjectManagementBusinessException(
198: "Database error retrieving pay rates: ", ex);
199: }/* catch(QueryException ex) {
200: throw new ProjectManagementBusinessException("Query exception retrieving pay rates: ", ex);
201: }*/
202: }
203:
204: /**
205: * The findPayRateByID method performs a database query to
206: * return a <CODE>PayRate</CODE> object
207: * representing the row in the <CODE>pay rate</CODE> table
208: * that matches the object id.
209: *
210: * @param id, the object id of the pay rate table.
211: * @return
212: * the pay rate. null if there isn't a pay rate associated
213: * the id
214: * @exception ProjectManagementBusinessException
215: * if there is a problem retrieving pay rate information.
216: */
217: public PayRate findPayRateByID(String id)
218: throws ProjectManagementBusinessException {
219: PayRateImpl thePayRate = null;
220:
221: try {
222: PayRateQuery query = new PayRateQuery();
223: //set query
224: query.setQueryOId(new ObjectId(id));
225: // Throw an exception if more than one user by this name is found
226: query.requireUniqueInstance();
227: PayRateDO thePayRateDO = query.getNextDO();
228: thePayRate = new PayRateImpl(thePayRateDO);
229: return thePayRate;
230: } catch (Exception ex) {
231: throw new ProjectManagementBusinessException(
232: "Exception in findPayRateByID()", ex);
233: }
234: }
235:
236: /**
237: * The findPayRateByEmployeeProjectAndDate method performs a database query to
238: * return a <CODE>PayRate</CODE> object
239: * representing the row in the <CODE>pay rate</CODE> table
240: * that matches the employee id, project id and has the closest lower or equal
241: * date to the given one.
242: *
243: * @param employeeID the employee id
244: * @param projectID the project id
245: * @param workingDate the date that [employee,project] pair record has to
246: * be closest to (lower or equal)
247: * @return
248: * the pay rate. null if there isn't a pay rate associated
249: * the id
250: * @exception ProjectManagementBusinessException
251: * if there is a problem retrieving pay rate information.
252: */
253: public PayRate findPayRateByEmployeeProjectAndDate(
254: String employeeID, String projectID, Date workingDate)
255: throws ProjectManagementBusinessException {
256:
257: PayRate thePayRate = null;
258:
259: try {
260: EmployeeManagerImpl employeeManager = new EmployeeManagerImpl();
261: Employee emp = employeeManager.findEmployeeByID(employeeID);
262:
263: if (emp == null) {
264: throw new ProjectManagementBusinessException(
265: "The employee for given ID can't be found");
266: }
267: ProjectManagerImpl projectManager = new ProjectManagerImpl();
268: Project proj = projectManager.findProjectByID(projectID);
269:
270: if (proj == null) {
271: throw new ProjectManagementBusinessException(
272: "The project for given ID can't be found");
273: }
274:
275: PayRateQuery query = new PayRateQuery();
276: //set query
277: query.setQueryEmployee(((EmployeeImpl) emp).getDO());
278: query.getQueryBuilder().addWhere(PayRateDO.Project,
279: ((ProjectImpl) proj).getDO(), QueryBuilder.EQUAL);
280: query.getQueryBuilder().addOrderByColumn(
281: PayRateDO.ValidFrom, "DESC");
282:
283: // searching for pay rate nearest to the given date
284: PayRateDO[] foundPayRates = query.getDOArray();
285: if (foundPayRates != null && foundPayRates.length != 0) {
286: //System.out.println("There is "+foundPayRates.length+" prs");
287: for (int i = 0; i < foundPayRates.length; i++) {
288: PayRateDO pr = foundPayRates[i];
289: //System.out.println("Comp "+pr.getValidFrom()+" to "+workingDate);
290: if (pr.getValidFrom().compareTo(workingDate) <= 0) {
291: thePayRate = new PayRateImpl(pr);
292: break;
293: }
294: }
295: }
296: /*PayRateDO foundPayRate = query.getNextDO();
297: if( foundPayRate != null)
298: thePayRate=new PayRate(query.getNextDO());*/
299:
300: return thePayRate;
301: } catch (Exception ex) {
302: throw new ProjectManagementBusinessException(
303: "Exception in findPayRateByEmployeeProjectAndDate()",
304: ex);
305: }
306: }
307:
308: public PayRate getPayRate()
309: throws ProjectManagementBusinessException {
310: return new PayRateImpl();
311: }
312: }
|