PayRateManagerImpl.java :  » J2EE » Enhydra-Demos » projectmanagement » business » timewage » Java Open Source

Java Open Source » J2EE » Enhydra Demos 
Enhydra Demos » projectmanagement » business » timewage » PayRateManagerImpl.java
package projectmanagement.business.timewage;

import projectmanagement.business.employee.*;
import projectmanagement.business.project.*;
import projectmanagement.business.ProjectManagementBusinessException;
import projectmanagement.data.timewage.*;
import com.lutris.appserver.server.sql.DatabaseManagerException;
import com.lutris.appserver.server.sql.ObjectId;
import com.lutris.appserver.server.sql.ObjectIdException;
import com.lutris.dods.builder.generator.query.*;
import com.lutris.appserver.server.Enhydra;
import com.lutris.logging.*;

import java.sql.Date;
import java.util.HashMap;
import java.util.Iterator;

import projectmanagement.spec.timewage.*;
import projectmanagement.spec.employee.*;
import projectmanagement.spec.project.*;
/**
 * Used to find the instance of PayRate.
 *
 * @author Sasa Bojanic
 * @version 1.0
 */
public class PayRateManagerImpl implements PayRateManager {

   /**
    * The getAllPayRates method performs a database query to
    * return all <CODE>PayRate</CODE> objects representing the
    * row in the <CODE>PayRates</CODE> table.
    * @return all the pay rates, or null if there are no any.
    * @exception ProjectManagementBusinessException
    *    if there is a problem retrieving pay rate information.
    */
    public  PayRate[] getAllPayRates()
         throws ProjectManagementBusinessException {
      try {
         PayRateQuery query = new PayRateQuery();

         PayRateDO[] foundPayRates = query.getDOArray();
         if(foundPayRates.length != 0) {
            PayRateImpl[] cs=new PayRateImpl[foundPayRates.length];
            for (int i=0; i<foundPayRates.length; i++) {
               cs[i]=new PayRateImpl(foundPayRates[i]);
            }
            return cs;
         } else {
            return null;
         }
      } catch(NonUniqueQueryException ex) {
         Enhydra.getLogChannel().write(Logger.DEBUG,
            "Non-unique pay rate found in database: "+ex.getMessage());
         throw new ProjectManagementBusinessException("Non unique pay rate found");
      } catch(DataObjectException ex) {
         throw new ProjectManagementBusinessException("Database error retrieving pay rates: ", ex);
      } /*catch(QueryException ex) {
         throw new ProjectManagementBusinessException("Query exception retrieving pay rates: ", ex);
      }*/
   }

   /**
    * The getAllPayRatesForEmployee method performs a database query to
    * return all <CODE>PayRate</CODE> objects representing the
    * row in the <CODE>PayRates</CODE> table that belongs to the
    * employee with given ID.
    * @param employeeID        the employee id
    * @param distinctOnEmployProjectPair if true returns only one payrate
    *                                    for [employee,project] pair
    * @return all the pay rates for given employee (it could be distinct on
    *         poject parameter, or not)
    * @exception ProjectManagementBusinessException
    *    if there is a problem retrieving pay rate information.
    */
    public  PayRate[] getAllPayRatesForEmployee(String employeeID, boolean distinctOnEmployProjectPair)
    throws ProjectManagementBusinessException {
        try {
           EmployeeManagerImpl employeeManager=new EmployeeManagerImpl();
           EmployeeImpl emp=(EmployeeImpl)employeeManager.findEmployeeByID(employeeID);
           
            if (emp==null) {
                throw new ProjectManagementBusinessException("The employee for given ID can't be found");
            }
            PayRateQuery query = new PayRateQuery();
            query.setQueryEmployee(emp.getDO());

            PayRateDO[] foundPayRates = query.getDOArray();
//System.out.println("distinctOnEmployProjectPair = "+distinctOnEmployProjectPair);
            if(foundPayRates.length != 0) {
                if (distinctOnEmployProjectPair) {
                    HashMap distinctPayRate = new HashMap();
                    for (int i=0; i<foundPayRates.length; i++) {
                        String key = foundPayRates[i].getEmployee().getOId()+"."+foundPayRates[i].getProject().getOId();
                        distinctPayRate.put(key,foundPayRates[i]);
                    }
                    PayRateImpl[] prs=new PayRateImpl[distinctPayRate.size()];
                    Iterator iter = distinctPayRate.values().iterator();
                    for (int i = 0; iter.hasNext(); i++ )
                        prs[i]=new PayRateImpl((PayRateDO)iter.next());
                    return prs;
                } else {
                    PayRateImpl[] prs=new PayRateImpl[foundPayRates.length];
                    for (int i=0; i<foundPayRates.length; i++)
                        prs[i]=new PayRateImpl(foundPayRates[i]);
                    return prs;
                }
            } else {
                return null;
            }
        } catch(NonUniqueQueryException ex) {
            Enhydra.getLogChannel().write(Logger.DEBUG,
            "Non-unique pay rate found in database: "+ex.getMessage());
            throw new ProjectManagementBusinessException("Non unique pay rate found");
        } catch(DataObjectException ex) {
            throw new ProjectManagementBusinessException("Database error retrieving pay rates: ", ex);
        } catch(QueryException ex) {
            throw new ProjectManagementBusinessException("Query exception retrieving pay rates: ", ex);
        }
    }

   /**
    * The getAllPayRatesForEmployeeProjectPair method performs a database
    * query to return all <CODE>PayRate</CODE> objects representing the
    * row in the <CODE>PayRates</CODE> table that belongs to the
    * employee with given ID, and project with a given ID.
    * @param employeeID        the employee id
    * @param projectID         the project id
    * @return all the pay rates for given employee-project pair
    * @exception ProjectManagementBusinessException
    *    if there is a problem retrieving pay rate information.
    */
    public  PayRate[] getAllPayRatesForEmployeeProjectPair (String employeeID,String projectID)
         throws ProjectManagementBusinessException {
        try {
            PayRateQuery query = new PayRateQuery();
            if (employeeID != null || projectID != null) {
                QueryBuilder mainQuery=query.getQueryBuilder();
                if (employeeID != null) {
                 
           EmployeeManagerImpl employeeManager=new EmployeeManagerImpl();
           EmployeeImpl employee=(EmployeeImpl) employeeManager.findEmployeeByID(employeeID);   
                 
                    mainQuery.addWhere(PayRateDO.Employee,employee.getDO(),QueryBuilder.EQUAL);
                }
                if (projectID != null) {
                  
                ProjectManagerImpl projectManager = new ProjectManagerImpl();
                Project project = projectManager.findProjectByID(projectID);
                  
                    mainQuery.addWhere(PayRateDO.Project,((ProjectImpl)project).getDO(),QueryBuilder.EQUAL);
                }
            }
            PayRateDO[] foundPayRates = query.getDOArray();
            if(foundPayRates.length != 0) {
                PayRateImpl[] pr=new PayRateImpl[foundPayRates.length];
                for (int i=0; i<foundPayRates.length; i++) {
                    pr[i]=new PayRateImpl(foundPayRates[i]);
                }
                return pr;
            } else {
                return null;
            }
        } catch(NonUniqueQueryException ex) {
            Enhydra.getLogChannel().write(Logger.DEBUG,
            "Non-unique pay rate found in database: "+ex.getMessage());
            throw new ProjectManagementBusinessException("Non unique pay rate found");
        } catch(DataObjectException ex) {
            throw new ProjectManagementBusinessException("Database error retrieving pay rates: ", ex);
        }/* catch(QueryException ex) {
            throw new ProjectManagementBusinessException("Query exception retrieving pay rates: ", ex);
        }*/
    }

   /**
    * The findPayRateByID method performs a database query to
    * return a <CODE>PayRate</CODE> object
    * representing the row in the <CODE>pay rate</CODE> table
    * that matches the object id.
    *
    * @param id, the object id of the pay rate table.
    * @return
    *    the pay rate. null if there isn't a pay rate associated
    *    the id
    * @exception ProjectManagementBusinessException
    *    if there is a problem retrieving pay rate information.
    */
   public  PayRate findPayRateByID(String id)
         throws ProjectManagementBusinessException {
      PayRateImpl thePayRate = null;

      try {
         PayRateQuery query = new PayRateQuery();
         //set query
         query.setQueryOId(new ObjectId(id));
         // Throw an exception if more than one user by this name is found
         query.requireUniqueInstance();
         PayRateDO thePayRateDO = query.getNextDO();
         thePayRate = new PayRateImpl(thePayRateDO);
         return thePayRate;
      } catch(Exception ex) {
         throw new ProjectManagementBusinessException("Exception in findPayRateByID()", ex);
      }
   }

    /**
     * The findPayRateByEmployeeProjectAndDate method performs a database query to
     * return a <CODE>PayRate</CODE> object
     * representing the row in the <CODE>pay rate</CODE> table
     * that matches the employee id, project id and has the closest lower or equal
     * date to the given one.
     *
     * @param employeeID        the employee id
     * @param projectID         the project id
     * @param workingDate       the date that [employee,project] pair record has to
     *                          be closest to (lower or equal)
     * @return
     *    the pay rate. null if there isn't a pay rate associated
     *    the id
     * @exception ProjectManagementBusinessException
     *    if there is a problem retrieving pay rate information.
     */
    public  PayRate findPayRateByEmployeeProjectAndDate(String employeeID, String projectID, Date workingDate)
    throws ProjectManagementBusinessException {

      PayRate thePayRate = null;

      try {
         EmployeeManagerImpl employeeManager=new EmployeeManagerImpl();
         Employee emp= employeeManager.findEmployeeByID(employeeID);   
         
         if (emp==null) {
            throw new ProjectManagementBusinessException("The employee for given ID can't be found");
         }
         ProjectManagerImpl projectManager = new ProjectManagerImpl();
         Project proj = projectManager.findProjectByID(projectID);
         
         if (proj==null) {
            throw new ProjectManagementBusinessException("The project for given ID can't be found");
         }

         PayRateQuery query = new PayRateQuery();
         //set query
         query.setQueryEmployee(((EmployeeImpl)emp).getDO());
         query.getQueryBuilder().addWhere(PayRateDO.Project,((ProjectImpl)proj).getDO(),QueryBuilder.EQUAL);
         query.getQueryBuilder().addOrderByColumn(PayRateDO.ValidFrom,"DESC");

         // searching for pay rate nearest to the given date
         PayRateDO[] foundPayRates = query.getDOArray();
         if(foundPayRates!=null && foundPayRates.length != 0) {
//System.out.println("There is "+foundPayRates.length+" prs");
            for (int i=0; i<foundPayRates.length; i++) {
               PayRateDO pr=foundPayRates[i];
//System.out.println("Comp "+pr.getValidFrom()+" to "+workingDate);
               if (pr.getValidFrom().compareTo(workingDate)<=0) {
                  thePayRate=new PayRateImpl(pr);
                  break;
               }
            }
         }
         /*PayRateDO foundPayRate = query.getNextDO();
         if( foundPayRate != null)
             thePayRate=new PayRate(query.getNextDO());*/

         return thePayRate;
      } catch(Exception ex) {
         throw new ProjectManagementBusinessException("Exception in findPayRateByEmployeeProjectAndDate()", ex);
      }
   }
public PayRate getPayRate() 
       throws ProjectManagementBusinessException{
     return new PayRateImpl();  
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.