com.rta.vsd.data.service.impl.VehicleDataServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.rta.vsd.data.service.impl.VehicleDataServiceImpl.java

Source

/**<pre> 
 *==========================================================================
 *
 * Copyright: (C) IBM Corporation 2010 -- IBM Internal Use Only
 *
 *==========================================================================
 *
 *    FILE: VehicleDataServiceImpl.java
 *    CREATOR: Eldon Barrows
 *    DEPT: GBS PAK
 *    DATE: 01/05/2011
 *
 *-PURPOSE-----------------------------------------------------------------
 * This is the implementation for IVehicleDataService
 * 
 *-------------------------------------------------------------------------
 *
 *
 *-CHANGE LOG--------------------------------------------------------------
 * 01/05/2011 Eldon Initial coding.         
 *==========================================================================
 * </pre> */

package com.rta.vsd.data.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.NonUniqueResultException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.ProjectionList;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Property;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.AliasToBeanResultTransformer;

import com.rta.vsd.data.DataServiceContext;
import com.rta.vsd.data.service.IDataService;
import com.rta.vsd.data.service.IVehicleDataService;
import com.rta.vsd.dto.VsdVehicle;
import com.rta.vsd.dto.custom.PaginationParam;
import com.rta.vsd.dto.custom.VehiclePlate;
import com.rta.vsd.dto.custom.VehicleSearchCriteria;
import com.rta.vsd.exception.data.VSDDataAccessException;
import com.rta.vsd.exception.data.VSDDuplicateDataException;
import com.rta.vsd.exception.data.VSDOnDeleteRestrictException;
import com.rta.vsd.utility.Constant;
import com.rta.vsd.utility.VSDLogger;

/**
 * This is the implementation for IVehicleDataService
 * 
 * @author Eldon Barrows
 *
 */
public class VehicleDataServiceImpl extends DataServiceImpl implements IVehicleDataService {

    private static Logger logger = VSDLogger.init(VehicleDataServiceImpl.class.getName());

    /**
     * 
     * Saves vsdVehicle
     * 
     * @author Eldon Barrows
     * @param dsContext
     * @param retrieveArabicData
     * @param vsdVehicle
     * @return VsdVehicle
     * @throws VSDDataAccessException
     */
    public VsdVehicle saveVehicle(DataServiceContext dsContext, boolean retrieveArabicData, VsdVehicle vsdVehicle)
            throws VSDDataAccessException, VSDDuplicateDataException {
        logger.info("saveVehicle -- START");
        try {
            Session session = (Session) dsContext.getInternalContext();
            if (getVehicleByChassisNumber(dsContext, retrieveArabicData, vsdVehicle.getChassisNumber()) != null
                    || getVehicleByPlateNumberAndVehPlateCatCodeId(dsContext, retrieveArabicData,
                            vsdVehicle.getVehiclePlateNumber(),
                            vsdVehicle.getVsdVehPlateCatCode().getVehPlateCatCodeId()) != null) {
                throw new VSDDuplicateDataException("A vehicle with the chassis or plate details already exists.");
            } else {
                Long id = (Long) session.save(vsdVehicle);
                vsdVehicle.setVehicleId(id);
            }
            logger.info("saveVehicle -- ENDS");
            return vsdVehicle;
        } catch (VSDDuplicateDataException de) {
            logger.error("An error occured in saveVehicle()");
            throw de;
        } catch (Exception ex) {
            logger.error("An error occured in saveVehicle()");
            throw new VSDDataAccessException(ex.getMessage(), ex);
        }
    }

    /**
     * 
     * Gets the vehicle which has the plate number and vehPlateCatCodeId provided.
     * 
     * @author Eldon Barrows
     * @param dsContext
     * @param retrieveArabicData
     * @param vehiclePlateNumber
     * @param vehPlateCatCodeId
     * @return VsdVehicle
     * @throws VSDDataAccessException
     */
    private VsdVehicle getVehicleByPlateNumberAndVehPlateCatCodeId(DataServiceContext dsContext,
            boolean retrieveArabicData, String vehiclePlateNumber, Long vehPlateCatCodeId)
            throws VSDDataAccessException {
        logger.info("getVehicleByPlateNumberAndVehPlateCatCodeId -- START");
        try {
            Session session = (Session) dsContext.getInternalContext();
            VsdVehicle vsdVehicle = (VsdVehicle) session.createCriteria(VsdVehicle.class, "v")
                    .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("v.vehiclePlateNumber", vehiclePlateNumber).ignoreCase())
                    .createCriteria("v.vsdVehPlateCatCode", "vpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpcc.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("vpcc.vehPlateCatCodeId", vehPlateCatCodeId)).uniqueResult();
            logger.info("getVehicleByPlateNumberAndVehPlateCatCodeId -- END");
            return vsdVehicle;
        } catch (Exception ex) {
            logger.error("An error occured in getVehicleByPlateNumberAndVehPlateCatCodeId()");
            logger.error(ex);
            throw new VSDDataAccessException(ex.getMessage(), ex);
        }
    }

    /**
     * 
     * Updates vsdVehicle
     * 
     * @author Eldon Barrows
     * @param dsContext
     * @param retrieveArabicData
     * @param vsdVehicle
     * @return VsdVehicle
     * @throws VSDDataAccessException
     */
    public VsdVehicle updateVehicle(DataServiceContext dsContext, boolean retrieveArabicData, VsdVehicle vsdVehicle)
            throws VSDDataAccessException, VSDDuplicateDataException {
        logger.info("updateVehicle -- START");
        try {
            VsdVehicle vehicle = getVehicleByPlateNumberAndVehPlateCatCodeId(dsContext, retrieveArabicData,
                    vsdVehicle.getVehiclePlateNumber(), vsdVehicle.getVsdVehPlateCatCode().getVehPlateCatCodeId());
            if (vehicle != null && vehicle.getVehicleId() != vsdVehicle.getVehicleId()) {
                throw new VSDDuplicateDataException("A vehicle with the plate details already exists.");
            } else {
                vehicle = getVehicleByChassisNumber(dsContext, retrieveArabicData, vsdVehicle.getChassisNumber());
                if (vehicle != null && vehicle.getVehicleId() != vsdVehicle.getVehicleId()) {
                    throw new VSDDuplicateDataException("A vehicle with the chassis number already exists.");
                } else {
                    createDynamicUpdateQuery(vsdVehicle, dsContext).executeUpdate();
                }
            }
        } catch (VSDDuplicateDataException de) {
            logger.error("An error occured in updateVehicle()");
            throw de;
        } catch (Exception ex) {
            logger.error("An error occured in updateVehicle()");
            throw new VSDDataAccessException(ex.getMessage(), ex);
        }
        logger.info("updateVehicle -- ENDS");
        return vsdVehicle;
    }

    /**
     * 
     * deletes vsdVehicle
     * 
     * @author Eldon Barrows
     * @param dsContext
     * @param retrieveArabicData
     * @param vsdVehicle
     * @return boolean
     * @throws VSDDataAccessException
     */
    public boolean deleteVehicle(DataServiceContext dsContext, boolean retrieveArabicData, VsdVehicle vsdVehicle)
            throws VSDDataAccessException {
        logger.info("deleteVehicle -- START");
        try {
            softDelete(dsContext, vsdVehicle, Delete.RESTRICT);
        } catch (VSDOnDeleteRestrictException oDRex) {
            throw oDRex;
        } catch (Exception ex) {
            logger.error("An error occured in deleteVehicle()");
            throw new VSDDataAccessException(ex.getMessage(), ex);
        }
        logger.info("deleteVehicle -- ENDS");
        return true;
    }

    /**
     * 
     * Get VsdVehicle by chassisNumber
     * 
     * @author Eldon Barrows
     * @param dsContext
     * @param retrieveArabicData
     * @param typeName
     * @return VsdVehicle
     * @throws VSDDataAccessException
     */
    public VsdVehicle getVehicleByChassisNumber(DataServiceContext dsContext, boolean retrieveArabicData,
            String chassisNumber) throws VSDDataAccessException, VSDDuplicateDataException {
        logger.info("getVehicleByChassisNumber -- START");
        try {
            Session session = (Session) dsContext.getInternalContext();
            VsdVehicle vsdVehicle = (VsdVehicle) session.createCriteria(VsdVehicle.class, "v")
                    .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("v.chassisNumber", chassisNumber).ignoreCase())
                    .createCriteria("v.vsdVehicleManufacturers", "vm", Criteria.LEFT_JOIN,
                            Restrictions.eq("vm.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleInsurances", "vi", Criteria.LEFT_JOIN,
                            Restrictions.eq("vi.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleCategory", "vc", Criteria.LEFT_JOIN,
                            Restrictions.eq("vc.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehPlateCatCode", "vpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpcc.isDeleted", IDataService.BOOL_FALSE))//child                                    
                    .createCriteria("vpcc.vsdVehPlateCatCode", "vpccc", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpccc.isDeleted", IDataService.BOOL_FALSE))//parent                                    
                    .createCriteria("vpccc.vsdCountry", "c", Criteria.LEFT_JOIN,
                            Restrictions.eq("c.isDeleted", IDataService.BOOL_FALSE))
                    .uniqueResult();
            logger.info("getVehicleByChassisNumber -- END");
            return vsdVehicle;
        } catch (NonUniqueResultException nEx) {
            logger.error("An error occured in getVehicleByChassisNumber");
            throw new VSDDuplicateDataException("getVehicleByChassisNumber() did not return a unique result");
        } catch (Exception ex) {
            logger.error("An error occured in getVehicleByChassisNumber()");
            throw new VSDDataAccessException(ex.getMessage(), ex);
        }
    }

    /**
     * 
     * Get VsdVehicle by vehiclePlateNumber
     * 
     * @author Eldon Barrows
     * @param dsContext
     * @param retrieveArabicData
     * @param vehiclePlateNumber
     * @return VsdVehicle
     * @throws VSDDataAccessException
     */
    public VsdVehicle getVehicleByPlateNumber(DataServiceContext dsContext, boolean retrieveArabicData,
            String vehiclePlateNumber) throws VSDDataAccessException {
        logger.info("getVehicleByPlateNumber -- START");
        try {
            Session session = (Session) dsContext.getInternalContext();
            VsdVehicle vsdVehicle = (VsdVehicle) session.createCriteria(VsdVehicle.class, "v")
                    .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("v.vehiclePlateNumber", vehiclePlateNumber).ignoreCase()).uniqueResult();
            logger.info("getVehicleByPlateNumber -- END");
            return vsdVehicle;
        } catch (Exception ex) {
            logger.error("An error occured in getVehicleByPlateNumber()");
            throw new VSDDataAccessException(ex.getMessage(), ex);
        }
    }

    /**
     * 
     * Get Vehicle Details By VehiclePlate. Along with vehicle vsdVehicleManufacturers, vsdVehicleInsurances, vsdVehicleOwner,
     *  vsdVehicleCategory (its parent and vsdCountry inside it) are also populated.
     * 
     * @author Tayyab
     * @param dsContext
     * @param retrieveArabicData
     * @param vehiclePlate
     * @return VsdVehicle
     * @throws VSDDataAccessException
     */
    public VsdVehicle getVehicleDetailsByVehiclePlate(DataServiceContext dsContext, boolean retrieveArabicData,
            VehiclePlate vehiclePlate) throws VSDDataAccessException {
        logger.info("getVehicleDetailsByVehiclePlate -- START");
        VsdVehicle vsdVehicle = null;
        try {
            Session session = (Session) dsContext.getInternalContext();
            vsdVehicle = (VsdVehicle) session.createCriteria(VsdVehicle.class, "v")
                    .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("v.vehiclePlateNumber", vehiclePlate.getPlateNumber()).ignoreCase())
                    .createCriteria("v.vsdVehicleManufacturers", "vm", Criteria.LEFT_JOIN,
                            Restrictions.eq("vm.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleInsurances", "vi", Criteria.LEFT_JOIN,
                            Restrictions.eq("vi.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleOwner", "vo", Criteria.LEFT_JOIN,
                            Restrictions.eq("vo.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleCategory", "vc", Criteria.LEFT_JOIN,
                            Restrictions.eq("vc.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehPlateCatCode", "vpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpcc.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("vpcc.categoryName", vehiclePlate.getPlateCode()).ignoreCase())
                    .createCriteria("vpcc.vsdVehPlateCatCode", "vpcc2", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpcc2.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("vpcc2.categoryName", vehiclePlate.getPlateCategory()).ignoreCase())
                    .createCriteria("vpcc2.vsdCountry", "c", Criteria.LEFT_JOIN,
                            Restrictions.eq("c.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("c.countryCode", vehiclePlate.getPlateSource()).ignoreCase())
                    .uniqueResult();
        } catch (Exception ex) {
            logger.error("An error occured in getVehicleDetailsByVehiclePlate()");
            throw new VSDDataAccessException(ex.getMessage(), ex);
        }
        logger.info("getVehicleDetailsByVehiclePlate -- END");
        return vsdVehicle;
    }

    /**
     * search vehicles.
     * 
     * @author Muhammad Attique
     * @param dsContext
     * @param retrieveArabicData
     * @param vehicleSearchCriteria
     * @return
     * @throws VSDDataAccessException
     */
    public List<VsdVehicle> searchVehicle(DataServiceContext dsContext, boolean retrieveArabicData,
            VehicleSearchCriteria vehicleSearchCriteria) throws VSDDataAccessException {
        logger.info("searchVehicle -- START");
        List<VsdVehicle> vehicles;
        try {
            Session session = (Session) dsContext.getInternalContext();
            Criteria criteria = session.createCriteria(VsdVehicle.class, "v")
                    .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehPlateCatCode", "vpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpcc.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("vpcc.vsdVehPlateCatCode", "vpcc2", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpcc2.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleOwner", "vo", Criteria.LEFT_JOIN,
                            Restrictions.eq("vo.isDeleted", IDataService.BOOL_FALSE));
            if (vehicleSearchCriteria.getPlateNumber() != null
                    && !vehicleSearchCriteria.getPlateNumber().equals(""))
                criteria.add(Restrictions.eq("v.vehiclePlateNumber", vehicleSearchCriteria.getPlateNumber())
                        .ignoreCase());

            if (vehicleSearchCriteria.getPlateCode() != null && !vehicleSearchCriteria.getPlateCode().equals(""))
                criteria.add(Restrictions.eq("vpcc.vehPlateCatCodeId", vehicleSearchCriteria.getPlateCode()));

            if (vehicleSearchCriteria.getPlateCategory() != null
                    && !vehicleSearchCriteria.getPlateCategory().equals(""))
                criteria.add(Restrictions.eq("vpcc2.vehPlateCatCodeId", vehicleSearchCriteria.getPlateCategory()));

            if (vehicleSearchCriteria.getOperatorName() != null
                    && !vehicleSearchCriteria.getOperatorName().equals(""))
                criteria.add(Restrictions.like("vo.ownerName", "%" + vehicleSearchCriteria.getOperatorName() + "%")
                        .ignoreCase());

            if (vehicleSearchCriteria.getOperatorNameA() != null
                    && !vehicleSearchCriteria.getOperatorNameA().equals(""))
                criteria.add(Restrictions
                        .like("vo.ownerNameA", "%" + vehicleSearchCriteria.getOperatorNameA() + "%").ignoreCase());

            vehicles = criteria.addOrder(Order.desc("v.vehicleId"))
                    .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();

        } catch (Exception ex) {
            logger.error("An error occured in searchVehicle()");
            throw new VSDDataAccessException(ex.getMessage(), ex);
        }
        logger.info("searchVehicle -- END");
        return vehicles;
    }

    /**
     * search vehicles.
     * 
     * @author Eldon Barrows
     * @param dsContext
     * @param retrieveArabicData
     * @param vehicleSearchCriteria
     * @param paginationValues
     * @return List<VsdVehicle>
     * @throws VSDDataAccessException
     */
    public List<VsdVehicle> searchVehicle(DataServiceContext dsContext, boolean retrieveArabicData,
            VehicleSearchCriteria vehicleSearchCriteria, PaginationParam paginationValues)
            throws VSDDataAccessException {
        logger.info("searchVehicle -- START");
        List<VsdVehicle> vehicles = null;
        try {
            Session session = (Session) dsContext.getInternalContext();
            ProjectionList projectionList = Projections.projectionList();
            projectionList.add(Projections.property("v.vehicleId"), "vehicleId");
            projectionList.add(Projections.property("vo.ownerName"), "chassisNumber");
            projectionList.add(Projections.property("vo.ownerNameA"), "chassisNumber");
            projectionList.add(Projections.property("v.vehiclePlateNumber"), "chassisNumber");
            Criteria criteria = session.createCriteria(VsdVehicle.class, "v")
                    .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehPlateCatCode", "vpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpcc.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("vpcc.vsdVehPlateCatCode", "vpcc2", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpcc2.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleOwner", "vo", Criteria.LEFT_JOIN,
                            Restrictions.eq("vo.isDeleted", IDataService.BOOL_FALSE));

            /*
             * if (vehicleSearchCriteria.getPlateNumber() != null && !vehicleSearchCriteria.getPlateNumber().equals(""))
               criteria.add(Restrictions.eq("v.vehiclePlateNumber", vehicleSearchCriteria.getPlateNumber()).ignoreCase());
            if (vehicleSearchCriteria.getPlateCode() != null && !vehicleSearchCriteria.getPlateCode().equals(""))
               criteria.add(Restrictions.eq("vpcc.categoryName", vehicleSearchCriteria.getPlateCode()).ignoreCase());
            if (vehicleSearchCriteria.getPlateCategory() != null && !vehicleSearchCriteria.getPlateCategory().equals(""))
               criteria.add(Restrictions.eq("vpcc2.categoryName", vehicleSearchCriteria.getPlateCategory()).ignoreCase());
            if (vehicleSearchCriteria.getOperatorName() != null && !vehicleSearchCriteria.getOperatorName().equals(""))
               criteria.add(Restrictions.eq("vo.ownerName", vehicleSearchCriteria.getOperatorName()).ignoreCase());
            if (vehicleSearchCriteria.getOperatorNameA() != null && !vehicleSearchCriteria.getOperatorNameA().equals(""))
               criteria.add(Restrictions.eq("vo.ownerNameA", vehicleSearchCriteria.getOperatorNameA()).ignoreCase());
            */
            if (vehicleSearchCriteria.getEmirateCode() != null
                    && !vehicleSearchCriteria.getEmirateCode().equals(""))
                criteria.add(
                        Restrictions.eq("v.vehicleCountry", vehicleSearchCriteria.getEmirateCode()).ignoreCase());

            if (vehicleSearchCriteria.getPlateNumber() != null
                    && !vehicleSearchCriteria.getPlateNumber().equals(""))
                criteria.add(Restrictions.eq("v.vehiclePlateNumber", vehicleSearchCriteria.getPlateNumber())
                        .ignoreCase());

            if (vehicleSearchCriteria.getPlateCode() != null && !vehicleSearchCriteria.getPlateCode().equals(""))
                criteria.add(Restrictions.eq("vpcc.vehPlateCatCodeId", vehicleSearchCriteria.getPlateCode()));

            if (vehicleSearchCriteria.getPlateCategory() != null
                    && !vehicleSearchCriteria.getPlateCategory().equals(""))
                criteria.add(Restrictions.eq("vpcc2.vehPlateCatCodeId", vehicleSearchCriteria.getPlateCategory()));

            if (vehicleSearchCriteria.getOperatorName() != null
                    && !vehicleSearchCriteria.getOperatorName().equals(""))
                criteria.add(Restrictions.like("vo.ownerName", "%" + vehicleSearchCriteria.getOperatorName() + "%")
                        .ignoreCase());

            if (vehicleSearchCriteria.getOperatorNameA() != null
                    && !vehicleSearchCriteria.getOperatorNameA().equals(""))
                criteria.add(Restrictions
                        .like("vo.ownerNameA", "%" + vehicleSearchCriteria.getOperatorNameA() + "%").ignoreCase());

            criteria.setProjection(Projections.distinct(projectionList));
            criteria.setResultTransformer(new AliasToBeanResultTransformer(VsdVehicle.class));
            criteria.setFirstResult(paginationValues.getFirstResult().intValue());
            criteria.setMaxResults(paginationValues.getFetchedSize().intValue());
            if (paginationValues.getPageLocale().equalsIgnoreCase(Constant.LOCALE_ENGLISH)) {
                criteria.addOrder(Order.asc("vo.ownerName").ignoreCase());
                criteria.addOrder(Order.asc("v.vehiclePlateNumber").ignoreCase());
            } else {
                criteria.addOrder(Order.asc("vo.ownerNameA").ignoreCase());
                criteria.addOrder(Order.asc("v.vehiclePlateNumber").ignoreCase());
            }

            List list = criteria.list();
            logger.debug("list.size() : " + list.size());
            if (list.size() == 0)
                return new ArrayList();
            Iterator iterator = list.iterator();
            ArrayList innerQueryList = new ArrayList<Long>();
            while (iterator.hasNext()) {
                VsdVehicle vehicle = (VsdVehicle) iterator.next();
                innerQueryList.add(vehicle.getVehicleId());
            }
            Criteria main = session.createCriteria(VsdVehicle.class, "v")
                    .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehPlateCatCode", "vpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpcc.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("vpcc.vsdVehPlateCatCode", "vpcc2", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpcc2.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleOwner", "vo", Criteria.LEFT_JOIN,
                            Restrictions.eq("vo.isDeleted", IDataService.BOOL_FALSE))
                    .add(Property.forName("v.vehicleId").in(innerQueryList));

            if (vehicleSearchCriteria.getEmirateCode() != null
                    && !vehicleSearchCriteria.getEmirateCode().equals(""))
                main.add(Restrictions.eq("v.vehicleCountry", vehicleSearchCriteria.getEmirateCode()).ignoreCase());

            if (vehicleSearchCriteria.getPlateNumber() != null
                    && !vehicleSearchCriteria.getPlateNumber().equals(""))
                main.add(Restrictions.eq("v.vehiclePlateNumber", vehicleSearchCriteria.getPlateNumber())
                        .ignoreCase());

            if (vehicleSearchCriteria.getPlateCode() != null && !vehicleSearchCriteria.getPlateCode().equals(""))
                main.add(Restrictions.eq("vpcc.vehPlateCatCodeId", vehicleSearchCriteria.getPlateCode()));

            if (vehicleSearchCriteria.getPlateCategory() != null
                    && !vehicleSearchCriteria.getPlateCategory().equals(""))
                main.add(Restrictions.eq("vpcc2.vehPlateCatCodeId", vehicleSearchCriteria.getPlateCategory()));

            if (vehicleSearchCriteria.getOperatorName() != null
                    && !vehicleSearchCriteria.getOperatorName().equals(""))
                main.add(Restrictions.like("vo.ownerName", "%" + vehicleSearchCriteria.getOperatorName() + "%")
                        .ignoreCase());

            if (vehicleSearchCriteria.getOperatorNameA() != null
                    && !vehicleSearchCriteria.getOperatorNameA().equals(""))
                main.add(Restrictions.like("vo.ownerNameA", "%" + vehicleSearchCriteria.getOperatorNameA() + "%")
                        .ignoreCase());

            if (paginationValues.getPageLocale().equalsIgnoreCase(Constant.LOCALE_ENGLISH)) {
                main.addOrder(Order.asc("vo.ownerName").ignoreCase());
                main.addOrder(Order.asc("v.vehiclePlateNumber").ignoreCase());
            } else {
                main.addOrder(Order.asc("vo.ownerNameA").ignoreCase());
                main.addOrder(Order.asc("v.vehiclePlateNumber").ignoreCase());
            }

            vehicles = main.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
            logger.info("searchVehicle -- END");
            return vehicles;
        } catch (Exception ex) {
            logger.error("An error occured in searchVehicle()");
            throw new VSDDataAccessException(ex.getMessage(), ex);
        }
    }

    /**
     * search vehicles.
     * 
     * @author Eldon Barrows
     * @param dsContext
     * @param retrieveArabicData
     * @param vehicleSearchCriteria
     * @return Long
     * @throws VSDDataAccessException
     */
    public Long getCountForSearchVehicle(DataServiceContext dsContext, boolean retrieveArabicData,
            VehicleSearchCriteria vehicleSearchCriteria) throws VSDDataAccessException {
        logger.info("getCountForSearchVehicle -- START");
        try {
            Session session = (Session) dsContext.getInternalContext();
            Criteria criteria = session.createCriteria(VsdVehicle.class, "v")
                    .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehPlateCatCode", "vpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpcc.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("vpcc.vsdVehPlateCatCode", "vpcc2", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpcc2.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleOwner", "vo", Criteria.LEFT_JOIN,
                            Restrictions.eq("vo.isDeleted", IDataService.BOOL_FALSE));

            if (vehicleSearchCriteria.getEmirateCode() != null
                    && !vehicleSearchCriteria.getEmirateCode().equals(""))
                criteria.add(
                        Restrictions.eq("v.vehicleCountry", vehicleSearchCriteria.getEmirateCode()).ignoreCase());

            if (vehicleSearchCriteria.getPlateNumber() != null
                    && !vehicleSearchCriteria.getPlateNumber().equals(""))
                criteria.add(Restrictions.eq("v.vehiclePlateNumber", vehicleSearchCriteria.getPlateNumber())
                        .ignoreCase());

            if (vehicleSearchCriteria.getPlateCode() != null && !vehicleSearchCriteria.getPlateCode().equals(""))
                criteria.add(Restrictions.eq("vpcc.vehPlateCatCodeId", vehicleSearchCriteria.getPlateCode()));

            if (vehicleSearchCriteria.getPlateCategory() != null
                    && !vehicleSearchCriteria.getPlateCategory().equals(""))
                criteria.add(Restrictions.eq("vpcc2.vehPlateCatCodeId", vehicleSearchCriteria.getPlateCategory()));

            if (vehicleSearchCriteria.getOperatorName() != null
                    && !vehicleSearchCriteria.getOperatorName().equals(""))
                criteria.add(Restrictions.like("vo.ownerName", "%" + vehicleSearchCriteria.getOperatorName() + "%")
                        .ignoreCase());

            if (vehicleSearchCriteria.getOperatorNameA() != null
                    && !vehicleSearchCriteria.getOperatorNameA().equals(""))
                criteria.add(Restrictions
                        .like("vo.ownerNameA", "%" + vehicleSearchCriteria.getOperatorNameA() + "%").ignoreCase());

            criteria.setProjection(Projections.countDistinct("v.vehicleId"));
            Long count = (Long) criteria.uniqueResult();
            logger.info("getCountForSearchVehicle -- END");
            return count;
        } catch (Exception ex) {
            logger.error("An error occured in getCountForSearchVehicle()");
            logger.error(ex);
            throw new VSDDataAccessException(ex.getMessage(), ex);
        }
    }

    /**
     * 
     * Get Vehicles By TradeLicenseNumber
     * populated DTOs are vsdVehPlateCatCode (twice), vsdCountry, vsdRRProfilings(vehicle's rr not owner's rr)
     * 
     * @author Tayyab
     * @param dsContext
     * @param retrieveArabicData
     * @param tradeLicenseNumber
     * @return VsdVehicle
     * @throws VSDDataAccessException
     */
    public List<VsdVehicle> getVehiclesByTradeLicenseNumber(DataServiceContext dsContext,
            boolean retrieveArabicData, String tradeLicenseNumber) throws VSDDataAccessException {
        logger.info("getVehiclesByTradeLicenseNumber -- START");
        List<VsdVehicle> vsdVehicles = null;
        try {
            Session session = (Session) dsContext.getInternalContext();

            vsdVehicles = session.createCriteria(VsdVehicle.class, "v")
                    .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleOwner", "vo", Criteria.LEFT_JOIN,
                            Restrictions.eq("vo.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("vo.tradeLicenseNumber", tradeLicenseNumber).ignoreCase())
                    .createCriteria("v.vsdRRProfilings", "rrp", Criteria.LEFT_JOIN,
                            Restrictions.eq("rrp.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehPlateCatCode", "vpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpcc.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("vpcc.vsdVehPlateCatCode", "pvpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("pvpcc.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("pvpcc.vsdCountry", "c", Criteria.LEFT_JOIN,
                            Restrictions.eq("c.isDeleted", IDataService.BOOL_FALSE))
                    .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
        } catch (Exception ex) {
            logger.error("An error occured in getVehiclesByTradeLicenseNumber()");
            throw new VSDDataAccessException(ex.getMessage(), ex);
        }
        logger.info("getVehiclesByTradeLicenseNumber -- END");
        return vsdVehicles;
    }

    /**
     * 
     * Get Vehicles By trafficFileNumber
     * populated DTOs are vsdVehPlateCatCode (twice), vsdVehicleManufacturers, vsdCountry, vsdRRProfilings(vehicle's rr not owner's rr)
     * 
     * @author Tayyab
     * @param dsContext
     * @param retrieveArabicData
     * @param trafficFileNumber
     * @return VsdVehicle
     * @throws VSDDataAccessException
     */
    public List<VsdVehicle> getVehiclesByTrafficFileNumber(DataServiceContext dsContext, boolean retrieveArabicData,
            String trafficFileNumber) throws VSDDataAccessException {
        logger.info("getVehiclesByTrafficFileNumber -- START");
        List<VsdVehicle> vsdVehicles = null;
        try {
            Session session = (Session) dsContext.getInternalContext();

            vsdVehicles = session.createCriteria(VsdVehicle.class, "v")
                    .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleManufacturers", "vm", Criteria.LEFT_JOIN,
                            Restrictions.eq("vm.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleOwner", "vo", Criteria.LEFT_JOIN,
                            Restrictions.eq("vo.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("vo.trafficFileNumber", trafficFileNumber).ignoreCase())
                    .createCriteria("rrp.vsdRiskRating", "rr", Criteria.LEFT_JOIN,
                            Restrictions.eq("rr.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehPlateCatCode", "vpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpcc.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("vpcc.vsdVehPlateCatCode", "pvpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("pvpcc.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("pvpcc.vsdCountry", "c", Criteria.LEFT_JOIN,
                            Restrictions.eq("c.isDeleted", IDataService.BOOL_FALSE))
                    //.addOrder(Order.desc("rrp.riskRatingScore"))
                    .createCriteria("v.vsdRRProfilings", "rrp", Criteria.LEFT_JOIN,
                            Restrictions.eq("rrp.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.sqlRestriction("1=1 Order by {alias}.RISK_RATING_SCORE DESC NULLS LAST"))
                    .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
        } catch (Exception ex) {
            logger.error("An error occured in getVehiclesByTrafficFileNumber()");
            throw new VSDDataAccessException(ex.getMessage(), ex);
        }
        logger.info("getVehiclesByTrafficFileNumber -- END");
        return vsdVehicles;
    }

    /**
     * 
     * Gets the count for getVehiclesByTrafficFileNumber
     * 
     * @author Eldon
     * @param dsContext
     * @param retrieveArabicData
     * @param trafficFileNumber
     * @return Long
     * @throws VSDDataAccessException
     */
    public Long getCountForGetVehiclesByTrafficFileNumber(DataServiceContext dsContext, boolean retrieveArabicData,
            String trafficFileNumber) throws VSDDataAccessException {
        logger.info("getCountForGetVehiclesByTrafficFileNumber -- START");
        try {
            Session session = (Session) dsContext.getInternalContext();
            Criteria crit = session.createCriteria(VsdVehicle.class, "v")
                    .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleManufacturers", "vm", Criteria.LEFT_JOIN,
                            Restrictions.eq("vm.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleOwner", "vo", Criteria.LEFT_JOIN,
                            Restrictions.eq("vo.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("vo.trafficFileNumber", trafficFileNumber).ignoreCase())
                    .createCriteria("rrp.vsdRiskRating", "rr", Criteria.LEFT_JOIN,
                            Restrictions.eq("rr.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehPlateCatCode", "vpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpcc.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("vpcc.vsdVehPlateCatCode", "pvpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("pvpcc.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("pvpcc.vsdCountry", "c", Criteria.LEFT_JOIN,
                            Restrictions.eq("c.isDeleted", IDataService.BOOL_FALSE))
                    //.addOrder(Order.desc("rrp.riskRatingScore"))
                    .createCriteria("v.vsdRRProfilings", "rrp", Criteria.LEFT_JOIN,
                            Restrictions.eq("rrp.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.sqlRestriction("1=1 Order by {alias}.RISK_RATING_SCORE DESC NULLS LAST"))
                    .setProjection(Projections.countDistinct("v.vehicleId"));
            Long count = (Long) crit.uniqueResult();
            logger.info("getCountForGetVehiclesByTrafficFileNumber -- END");
            return count;
        } catch (Exception ex) {
            logger.error("An error occured in getCountForGetVehiclesByTrafficFileNumber()");
            logger.error(ex);
            throw new VSDDataAccessException(ex.getMessage(), ex);
        }
    }

    /**
     * 
     * Gets a vehicle with its associated vehicle category owner and risk rating information by the id provided
     * 
     * @author Eldon Barrows
     * @param dsContext
     * @param retrieveArabicData
     * @param vehicleId
     * @return VsdVehicle
     * @throws VSDDataAccessException
     */
    public VsdVehicle getVehicleWithCategoryOwnerAndRiskRatingByVehicleId(DataServiceContext dsContext,
            boolean retrieveArabicData, Long vehicleId) throws VSDDataAccessException {
        logger.info("getVehicleWithCategoryOwnerAndRiskRatingByVehicleId -- START");
        VsdVehicle vehicle = null;
        try {
            Session session = (Session) dsContext.getInternalContext();
            vehicle = (VsdVehicle) session.createCriteria(VsdVehicle.class, "vehicle")
                    .add(Restrictions.eq("vehicle.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("vehicle.vehicleId", vehicleId))
                    .createCriteria("vehicle.vsdVehicleCategory", "vcChild", Criteria.LEFT_JOIN,
                            Restrictions.eq("vcChild.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("vcChild.vsdVehicleCategory", "vcParent", Criteria.LEFT_JOIN,
                            Restrictions.eq("vcParent.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("vehicle.vsdVehicleManufacturers", "m", Criteria.LEFT_JOIN,
                            Restrictions.eq("m.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("vehicle.vsdRRProfilings", "rrp", Criteria.LEFT_JOIN,
                            Restrictions.eq("rrp.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("rrp.vsdRiskRating", "rr", Criteria.LEFT_JOIN,
                            Restrictions.eq("rr.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("vehicle.vsdVehicleOwner", "vo", Criteria.LEFT_JOIN,
                            Restrictions.eq("vo.isDeleted", IDataService.BOOL_FALSE))
                    .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).uniqueResult();
        } catch (Exception ex) {
            logger.error("An error occured in getVehicleWithCategoryOwnerAndRiskRatingByVehicleId()");
            throw new VSDDataAccessException(ex.getMessage(), ex);
        }
        logger.info("getVehicleWithCategoryOwnerAndRiskRatingByVehicleId -- END");
        return vehicle;
    }

    /**
     * 
     * Get Vehicle By chassisNumber
     * populated DTOs are vsdVehicleManufacturers, vsdVehicleCategory
     * 
     * @author Tayyab
     * @param dsContext
     * @param retrieveArabicData
     * @param chassisNumber
     * @return VsdVehicle
     * @throws VSDDataAccessException
     */
    public VsdVehicle getVehicleWithManufacturerAndVehicleCategoryByChassisNumber(DataServiceContext dsContext,
            boolean retrieveArabicData, String chassisNumber) throws VSDDataAccessException {
        logger.info("getVehicleWithManufacturerAndVehicleCategoryByChassisNumber -- START");
        VsdVehicle vsdVehicle = null;
        try {
            Session session = (Session) dsContext.getInternalContext();

            vsdVehicle = (VsdVehicle) session.createCriteria(VsdVehicle.class, "v")
                    .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleManufacturers", "vm", Criteria.LEFT_JOIN,
                            Restrictions.eq("vm.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleCategory", "vc", Criteria.LEFT_JOIN,
                            Restrictions.eq("vc.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("v.chassisNumber", chassisNumber).ignoreCase()).uniqueResult();
        } catch (Exception ex) {
            logger.error("An error occured in getVehicleWithManufacturerAndVehicleCategoryByChassisNumber()");
            throw new VSDDataAccessException(ex.getMessage(), ex);
        }
        logger.info("getVehicleWithManufacturerAndVehicleCategoryByChassisNumber -- END");
        return vsdVehicle;
    }

    /**
     * 
     * Gets a list of vehicles having an owner with the provided trade license number. The manufacturer(VsdVehicleManufacturer), risk rating (VsdRRProfiling,VsdRiskRating) and plate category code(VsdVehPlateCatCode) details will also be populated
     * 
     * @author Eldon Barrows
     * @param dsContext
     * @param retrieveArabicData
     * @param tradeLicenseNumber
     * @return List<VsdVehicle>
     * @throws VSDDataAccessException
     */
    public List<VsdVehicle> getVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTradeLicenseNumber(
            DataServiceContext dsContext, boolean retrieveArabicData, String tradeLicenseNumber)
            throws VSDDataAccessException {
        logger.info("getVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTradeLicenseNumber -- START");
        List<VsdVehicle> vsdVehicles = null;
        try {
            Session session = (Session) dsContext.getInternalContext();

            vsdVehicles = session.createCriteria(VsdVehicle.class, "v")
                    .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleOwner", "vo", Criteria.LEFT_JOIN,
                            Restrictions.eq("vo.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("vo.tradeLicenseNumber", tradeLicenseNumber).ignoreCase())
                    .createCriteria("v.vsdRRProfilings", "rrp", Criteria.LEFT_JOIN,
                            Restrictions.eq("rrp.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("rrp.vsdRiskRating", "rr", Criteria.LEFT_JOIN,
                            Restrictions.eq("rr.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleManufacturers", "m", Criteria.LEFT_JOIN,
                            Restrictions.eq("m.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehPlateCatCode", "vpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpcc.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("vpcc.vsdVehPlateCatCode", "pvpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("pvpcc.isDeleted", IDataService.BOOL_FALSE))
                    .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
            logger.info("getVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTradeLicenseNumber -- END");
            return vsdVehicles;
        } catch (Exception ex) {
            logger.error(
                    "An error occured in getVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTradeLicenseNumber()");
            logger.error(ex);
            throw new VSDDataAccessException(ex.getMessage(), ex);
        }
    }

    /**
     * 
     * Gets a list of vehicles having an owner with the provided trade license number. The manufacturer(VsdVehicleManufacturer), risk rating (VsdRRProfiling,VsdRiskRating) and plate category code(VsdVehPlateCatCode) details will also be populated
     * 
     * @author Eldon Barrows
     * @param dsContext
     * @param retrieveArabicData
     * @param tradeLicenseNumber
     * @return List<VsdVehicle>
     * @throws VSDDataAccessException
     */
    public List<VsdVehicle> getVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTrficFileNumber(
            DataServiceContext dsContext, boolean retrieveArabicData, String traficFileNo)
            throws VSDDataAccessException {
        logger.info("getVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTradeLicenseNumber -- START");
        List<VsdVehicle> vsdVehicles = null;
        try {
            Session session = (Session) dsContext.getInternalContext();

            vsdVehicles = session.createCriteria(VsdVehicle.class, "v")
                    .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleOwner", "vo", Criteria.LEFT_JOIN,
                            Restrictions.eq("vo.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("vo.trafficFileNumber", traficFileNo).ignoreCase())
                    .createCriteria("v.vsdRRProfilings", "rrp", Criteria.LEFT_JOIN,
                            Restrictions.eq("rrp.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("rrp.vsdRiskRating", "rr", Criteria.LEFT_JOIN,
                            Restrictions.eq("rr.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleManufacturers", "m", Criteria.LEFT_JOIN,
                            Restrictions.eq("m.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehPlateCatCode", "vpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpcc.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("vpcc.vsdVehPlateCatCode", "pvpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("pvpcc.isDeleted", IDataService.BOOL_FALSE))
                    .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
            logger.info("getVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTradeLicenseNumber -- END");
            return vsdVehicles;
        } catch (Exception ex) {
            logger.error(
                    "An error occured in getVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTradeLicenseNumber()");
            logger.error(ex);
            throw new VSDDataAccessException(ex.getMessage(), ex);
        }
    }

    /**
     * 
     * Gets count for a list of vehicles having an owner with the provided trade license number. The manufacturer(VsdVehicleManufacturer), risk rating (VsdRRProfiling,VsdRiskRating) and plate category code(VsdVehPlateCatCode) details will also be populated
     * 
     * @author Eldon Barrows
     * @param dsContext
     * @param retrieveArabicData
     * @param Long
     * @throws VSDDataAccessException
     */
    public Long getCountForVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTrficFileNumber(
            DataServiceContext dsContext, boolean retrieveArabicData, String traficFileNo)
            throws VSDDataAccessException {
        logger.info(
                "getCountForVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTrficFileNumber -- START");
        Long count = null;
        try {
            Session session = (Session) dsContext.getInternalContext();
            count = (Long) session.createCriteria(VsdVehicle.class, "v")
                    .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleOwner", "vo", Criteria.LEFT_JOIN,
                            Restrictions.eq("vo.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("vo.trafficFileNumber", traficFileNo).ignoreCase())
                    .createCriteria("v.vsdRRProfilings", "rrp", Criteria.LEFT_JOIN,
                            Restrictions.eq("rrp.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("rrp.vsdRiskRating", "rr", Criteria.LEFT_JOIN,
                            Restrictions.eq("rr.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleManufacturers", "m", Criteria.LEFT_JOIN,
                            Restrictions.eq("m.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehPlateCatCode", "vpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpcc.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("vpcc.vsdVehPlateCatCode", "pvpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("pvpcc.isDeleted", IDataService.BOOL_FALSE))
                    .setProjection(Projections.countDistinct("v.vehicleId")).uniqueResult();
            logger.info(
                    "getCountForVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTrficFileNumber -- END");
            return count;
        } catch (Exception ex) {
            logger.error(
                    "An error occured in getCountForVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTrficFileNumber()");
            logger.error(ex);
            throw new VSDDataAccessException(ex.getMessage(), ex);
        }
    }

    public List<VsdVehicle> getVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTrficFileNumber(
            DataServiceContext dsContext, boolean retrieveArabicData, String traficFileNo,
            PaginationParam paginationValues) throws VSDDataAccessException {
        logger.info("getVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTradeLicenseNumber -- START");
        List<VsdVehicle> vehicles = null;
        try {
            Session session = (Session) dsContext.getInternalContext();
            ProjectionList projectionList = Projections.projectionList();
            projectionList.add(Projections.property("v.vehicleId"), "vehicleId");
            Criteria crit = session.createCriteria(VsdVehicle.class, "v")
                    .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleOwner", "vo", Criteria.LEFT_JOIN,
                            Restrictions.eq("vo.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("vo.trafficFileNumber", traficFileNo).ignoreCase())
                    .createCriteria("v.vsdRRProfilings", "rrp", Criteria.LEFT_JOIN,
                            Restrictions.eq("rrp.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("rrp.vsdRiskRating", "rr", Criteria.LEFT_JOIN,
                            Restrictions.eq("rr.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleManufacturers", "m", Criteria.LEFT_JOIN,
                            Restrictions.eq("m.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehPlateCatCode", "vpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpcc.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("vpcc.vsdVehPlateCatCode", "pvpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("pvpcc.isDeleted", IDataService.BOOL_FALSE))
                    .addOrder(Order.desc("v.vehicleId")).setProjection(Projections.distinct(projectionList));
            crit.setResultTransformer(new AliasToBeanResultTransformer(VsdVehicle.class));
            if (paginationValues.getFirstResult() != null && paginationValues.getFirstResult().longValue() != -1) {
                crit.setFirstResult(paginationValues.getFirstResult().intValue());
            }
            if (paginationValues.getFetchedSize() != null && paginationValues.getFetchedSize().longValue() != -1) {
                crit.setMaxResults(paginationValues.getFetchedSize().intValue());
            }
            List list = crit.list();
            logger.debug("list.size() : " + list.size());
            Set resultSet = new HashSet(list);
            logger.debug("resultSet.size() : " + resultSet.size());
            if (resultSet.size() == 0)
                return new ArrayList();
            Iterator iterator = resultSet.iterator();
            ArrayList innerQueryList = new ArrayList<Long>();
            while (iterator.hasNext()) {
                VsdVehicle vehicle = (VsdVehicle) iterator.next();
                innerQueryList.add(vehicle.getVehicleId());
            }
            Criteria main = session.createCriteria(VsdVehicle.class, "v")
                    .add(Property.forName("v.vehicleId").in(innerQueryList))
                    .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleOwner", "vo", Criteria.LEFT_JOIN,
                            Restrictions.eq("vo.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("vo.trafficFileNumber", traficFileNo).ignoreCase())
                    .createCriteria("v.vsdRRProfilings", "rrp", Criteria.LEFT_JOIN,
                            Restrictions.eq("rrp.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("rrp.vsdRiskRating", "rr", Criteria.LEFT_JOIN,
                            Restrictions.eq("rr.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleManufacturers", "m", Criteria.LEFT_JOIN,
                            Restrictions.eq("m.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehPlateCatCode", "vpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpcc.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("vpcc.vsdVehPlateCatCode", "pvpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("pvpcc.isDeleted", IDataService.BOOL_FALSE))
                    .addOrder(Order.desc("v.vehicleId"));
            vehicles = main.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
            logger.info("getVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTradeLicenseNumber -- END");
            return vehicles;
        } catch (Exception ex) {
            logger.error(
                    "An error occured in getVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTradeLicenseNumber()");
            throw new VSDDataAccessException(ex.getMessage(), ex);
        }
    }

    /**
     * 
     * Gets the count for getVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTrficFileNumber
     * 
     * @author Eldon Barrows
     * @param dsContext
     * @param retrieveArabicData
     * @param traficFileNo
     * @param paginationValues
     * @return Long
     * @throws VSDDataAccessException
     */
    public Long getCountForGetVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTrficFileNumber(
            DataServiceContext dsContext, boolean retrieveArabicData, String traficFileNo)
            throws VSDDataAccessException {
        logger.info(
                "getCountForVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTrficFileNumber -- START");
        try {
            Session session = (Session) dsContext.getInternalContext();
            Criteria crit = session.createCriteria(VsdVehicle.class, "v")
                    .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleOwner", "vo", Criteria.LEFT_JOIN,
                            Restrictions.eq("vo.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("vo.trafficFileNumber", traficFileNo).ignoreCase())
                    .createCriteria("v.vsdRRProfilings", "rrp", Criteria.LEFT_JOIN,
                            Restrictions.eq("rrp.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("rrp.vsdRiskRating", "rr", Criteria.LEFT_JOIN,
                            Restrictions.eq("rr.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleManufacturers", "m", Criteria.LEFT_JOIN,
                            Restrictions.eq("m.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehPlateCatCode", "vpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpcc.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("vpcc.vsdVehPlateCatCode", "pvpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("pvpcc.isDeleted", IDataService.BOOL_FALSE))
                    .setProjection(Projections.countDistinct("v.vehicleId"));
            Long count = (Long) crit.uniqueResult();
            logger.info(
                    "getCountForVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTrficFileNumber -- END");
            return count;
        } catch (Exception ex) {
            logger.error(
                    "An error occured in getCountForVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTrficFileNumber()");
            throw new VSDDataAccessException(ex.getMessage(), ex);
        }
    }

    /**
     * 
     * Gets a list of vehicles having an owner with the provided trade license number. The manufacturer(VsdVehicleManufaceturer), risk rating (VsdRRProfiling,VsdRiskRating) and plate category code(VsdVehPlateCatCode) details will also be populated
     * 
     * @author Eldon Barrows
     * @param dsContext
     * @param retrieveArabicData
     * @param tradeLicenseNumber
     * @param paginationValues
     * @return List<VsdVehicle>
     * @throws VSDDataAccessException
     */
    public List<VsdVehicle> getVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTradeLicenseNumber(
            DataServiceContext dsContext, boolean retrieveArabicData, String tradeLicenseNumber,
            PaginationParam paginationValues) throws VSDDataAccessException {
        logger.info("getVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTradeLicenseNumber -- START");
        List<VsdVehicle> vehicles = null;
        try {
            Session session = (Session) dsContext.getInternalContext();
            ProjectionList projectionList = Projections.projectionList();
            projectionList.add(Projections.property("v.vehicleId"), "vehicleId");
            Criteria crit = session.createCriteria(VsdVehicle.class, "v")
                    .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleOwner", "vo", Criteria.LEFT_JOIN,
                            Restrictions.eq("vo.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("vo.tradeLicenseNumber", tradeLicenseNumber).ignoreCase())
                    .createCriteria("v.vsdRRProfilings", "rrp", Criteria.LEFT_JOIN,
                            Restrictions.eq("rrp.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("rrp.vsdRiskRating", "rr", Criteria.LEFT_JOIN,
                            Restrictions.eq("rr.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleManufacturers", "m", Criteria.LEFT_JOIN,
                            Restrictions.eq("m.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehPlateCatCode", "vpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpcc.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("vpcc.vsdVehPlateCatCode", "pvpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("pvpcc.isDeleted", IDataService.BOOL_FALSE))
                    .addOrder(Order.desc("v.vehicleId")).setProjection(Projections.distinct(projectionList));
            crit.setResultTransformer(new AliasToBeanResultTransformer(VsdVehicle.class));
            if (paginationValues.getFirstResult() != null && paginationValues.getFirstResult().longValue() != -1) {
                crit.setFirstResult(paginationValues.getFirstResult().intValue());
            }
            if (paginationValues.getFetchedSize() != null && paginationValues.getFetchedSize().longValue() != -1) {
                crit.setMaxResults(paginationValues.getFetchedSize().intValue());
            }
            List list = crit.list();
            logger.debug("list.size() : " + list.size());
            Set resultSet = new HashSet(list);
            logger.debug("resultSet.size() : " + resultSet.size());
            if (resultSet.size() == 0)
                return new ArrayList();
            Iterator iterator = resultSet.iterator();
            ArrayList innerQueryList = new ArrayList<Long>();
            while (iterator.hasNext()) {
                VsdVehicle vehicle = (VsdVehicle) iterator.next();
                innerQueryList.add(vehicle.getVehicleId());
            }
            Criteria main = session.createCriteria(VsdVehicle.class, "v")
                    .add(Property.forName("v.vehicleId").in(innerQueryList))
                    .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleOwner", "vo", Criteria.LEFT_JOIN,
                            Restrictions.eq("vo.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("vo.tradeLicenseNumber", tradeLicenseNumber).ignoreCase())
                    .createCriteria("v.vsdRRProfilings", "rrp", Criteria.LEFT_JOIN,
                            Restrictions.eq("rrp.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("rrp.vsdRiskRating", "rr", Criteria.LEFT_JOIN,
                            Restrictions.eq("rr.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleManufacturers", "m", Criteria.LEFT_JOIN,
                            Restrictions.eq("m.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehPlateCatCode", "vpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpcc.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("vpcc.vsdVehPlateCatCode", "pvpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("pvpcc.isDeleted", IDataService.BOOL_FALSE))
                    .addOrder(Order.desc("v.vehicleId"));
            vehicles = main.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
            logger.info("getVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTradeLicenseNumber -- END");
            return vehicles;
        } catch (Exception ex) {
            logger.error(
                    "An error occured in getVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTradeLicenseNumber()");
            throw new VSDDataAccessException(ex.getMessage(), ex);
        }
    }

    /**
     * 
     * Gets the count for getVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTradeLicenseNumber 
     * 
     * @author Eldon Barrows
     * @param dsContext
     * @param retrieveArabicData
     * @param tradeLicenseNumber
     * @return Long
     * @throws VSDDataAccessException
     */
    public Long getCountForGetVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTradeLicenseNumber(
            DataServiceContext dsContext, boolean retrieveArabicData, String tradeLicenseNumber)
            throws VSDDataAccessException {
        logger.info(
                "getCountForGetVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTradeLicenseNumber -- START");

        try {
            Session session = (Session) dsContext.getInternalContext();
            Criteria crit = session.createCriteria(VsdVehicle.class, "v")
                    .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleOwner", "vo", Criteria.LEFT_JOIN,
                            Restrictions.eq("vo.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("vo.tradeLicenseNumber", tradeLicenseNumber).ignoreCase())
                    .createCriteria("v.vsdRRProfilings", "rrp", Criteria.LEFT_JOIN,
                            Restrictions.eq("rrp.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("rrp.vsdRiskRating", "rr", Criteria.LEFT_JOIN,
                            Restrictions.eq("rr.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleManufacturers", "m", Criteria.LEFT_JOIN,
                            Restrictions.eq("m.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehPlateCatCode", "vpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpcc.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("vpcc.vsdVehPlateCatCode", "pvpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("pvpcc.isDeleted", IDataService.BOOL_FALSE))
                    .addOrder(Order.desc("v.vehicleId")).setProjection(Projections.countDistinct("v.vehicleId"));
            Long count = (Long) crit.uniqueResult();
            logger.info(
                    "getCountForGetVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTradeLicenseNumber -- END");
            return count;
        } catch (Exception ex) {
            logger.error(
                    "An error occured in getCountForGetVehiclesWithRiskRatingManufacturerPlateCategoryCodeDetailsByTradeLicenseNumber()");
            throw new VSDDataAccessException(ex.getMessage(), ex);
        }
    }

    /**
     * 
     * Gets a list of vehicles by risk rating type code and profiling date
     * 
     * @author Eldon Barrows
     * @param dsContext
     * @param retrieveArabicData
     * @param riskRatingTypeCode
     * @param profilingDate
     * @return List<VsdVehicle>
     * @throws VSDDataAccessException
     */
    public List<VsdVehicle> getVehiclesByRiskRatingTypeCode(DataServiceContext dsContext,
            boolean retrieveArabicData, String riskRatingTypeCode, Date profilingDate)
            throws VSDDataAccessException {
        logger.info("getVehiclesByRiskRatingTypeCode -- START");
        List<VsdVehicle> vehicles = null;
        try {
            Calendar startDate = Calendar.getInstance();
            startDate.setTime(profilingDate);
            startDate.set(Calendar.HOUR_OF_DAY, 0);
            startDate.set(Calendar.MINUTE, 0);
            startDate.set(Calendar.SECOND, 0);
            startDate.set(Calendar.MILLISECOND, 0);

            Calendar endDate = Calendar.getInstance();
            endDate.setTime(profilingDate);
            endDate.set(Calendar.HOUR_OF_DAY, 23);
            endDate.set(Calendar.MINUTE, 59);
            endDate.set(Calendar.SECOND, 59);
            endDate.set(Calendar.MILLISECOND, 999);

            Session session = (Session) dsContext.getInternalContext();
            vehicles = session.createCriteria(VsdVehicle.class, "v")
                    .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdRRProfilings", "rrp", Criteria.LEFT_JOIN,
                            Restrictions.eq("rrp.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.between("rrp.createdTimestamp", startDate.getTime(), endDate.getTime()))
                    .createCriteria("rrp.vsdRiskRating", "rr", Criteria.LEFT_JOIN,
                            Restrictions.eq("rr.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("rr.vsdRiskRatingType", "rrt", Criteria.LEFT_JOIN,
                            Restrictions.eq("rrt.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("rrt.ratingTypeCode", riskRatingTypeCode))
                    .createCriteria("v.vsdVehicleOwner", "vo", Criteria.LEFT_JOIN,
                            Restrictions.eq("vo.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("vo.vsdVehOwnConDetails", "vocd", Criteria.LEFT_JOIN,
                            Restrictions.eq("vocd.isDeleted", IDataService.BOOL_FALSE))
                    .addOrder(Order.desc("rrp.createdTimestamp"))
                    .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
            logger.info("getVehiclesByRiskRatingTypeCode -- END");
            return vehicles;
        } catch (Exception ex) {
            logger.error("An error occured in getVehiclesByRiskRatingTypeCode()");
            logger.error(ex);
            throw new VSDDataAccessException(ex.getMessage(), ex);
        }
    }

    /**
     * 
     * Get Vehicle VehiclePlate
     * 
     * @author Tayyab
     * @param dsContext
     * @param retrieveArabicData
     * @param vehiclePlate
     * @return VsdVehicle
     * @throws VSDDataAccessException
     */
    public VsdVehicle getVehicleByVehiclePlate(DataServiceContext dsContext, boolean retrieveArabicData,
            VehiclePlate vehiclePlate) throws VSDDataAccessException {
        logger.info("getVehicleByVehiclePlate -- START");
        VsdVehicle vsdVehicle = null;
        try {
            Session session = (Session) dsContext.getInternalContext();
            vsdVehicle = (VsdVehicle) session.createCriteria(VsdVehicle.class, "v")
                    .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("v.vehiclePlateNumber", vehiclePlate.getPlateNumber()).ignoreCase())
                    .createCriteria("v.vsdVehPlateCatCode", "vpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpcc.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("vpcc.categoryName", vehiclePlate.getPlateCode()).ignoreCase())
                    .createCriteria("vpcc.vsdVehPlateCatCode", "vpcc2", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpcc2.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("vpcc2.categoryName", vehiclePlate.getPlateCategory()).ignoreCase())
                    .createCriteria("vpcc2.vsdCountry", "c", Criteria.LEFT_JOIN,
                            Restrictions.eq("c.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("c.countryCode", vehiclePlate.getPlateSource()).ignoreCase())
                    .uniqueResult();
        } catch (Exception ex) {
            logger.error("An error occured in getVehicleByVehiclePlate()");
            throw new VSDDataAccessException(ex.getMessage(), ex);
        }
        logger.info("getVehicleByVehiclePlate -- END");
        return vsdVehicle;
    }

    /**
     * 
     * get Vehicle With VehicleOwner By VehiclePlate
     * 
     * @author Tayyab
     * @param dsContext
     * @param retrieveArabicData
     * @param vehiclePlate
     * @return VsdVehicle
     * @throws VSDDataAccessException
     */
    public VsdVehicle getVehicleWithVehicleOwnerByVehiclePlate(DataServiceContext dsContext,
            boolean retrieveArabicData, VehiclePlate vehiclePlate) throws VSDDataAccessException {
        logger.info("getVehicleWithVehicleOwnerByVehiclePlate -- START");
        VsdVehicle vsdVehicle = null;
        try {
            Session session = (Session) dsContext.getInternalContext();
            vsdVehicle = (VsdVehicle) session.createCriteria(VsdVehicle.class, "v")
                    .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("v.vehiclePlateNumber", vehiclePlate.getPlateNumber()).ignoreCase())
                    .createCriteria("v.vsdVehicleOwner", "vo", Criteria.LEFT_JOIN,
                            Restrictions.eq("vo.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehPlateCatCode", "vpcc", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpcc.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("vpcc.categoryName", vehiclePlate.getPlateCode()).ignoreCase())
                    .createCriteria("vpcc.vsdVehPlateCatCode", "vpcc2", Criteria.LEFT_JOIN,
                            Restrictions.eq("vpcc2.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("vpcc2.categoryName", vehiclePlate.getPlateCategory()).ignoreCase())
                    .createCriteria("vpcc2.vsdCountry", "c", Criteria.LEFT_JOIN,
                            Restrictions.eq("c.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("c.countryCode", vehiclePlate.getPlateSource()).ignoreCase())
                    .uniqueResult();
        } catch (Exception ex) {
            logger.error("An error occured in getVehicleWithVehicleOwnerByVehiclePlate()");
            throw new VSDDataAccessException(ex.getMessage(), ex);
        }
        logger.info("getVehicleWithVehicleOwnerByVehiclePlate -- END");
        return vsdVehicle;
    }

    /**
     * 
     * Gets a list of vehicles by risk rating profiling date
     * 
     * @author Eldon Barrows
     * @param dsContext
     * @param retrieveArabicData
     * @param profilingDate
     * @return List<VsdVehicle>
     * @throws VSDDataAccessException
     */
    public List<VsdVehicle> getVehiclesByProfilingDate(DataServiceContext dsContext, boolean retrieveArabicData,
            Date profilingDate) throws VSDDataAccessException {
        logger.info("getVehiclesByProfilingDate -- START");
        List<VsdVehicle> vehicles = null;
        try {
            Calendar startDate = Calendar.getInstance();
            startDate.setTime(profilingDate);
            startDate.set(Calendar.HOUR_OF_DAY, 0);
            startDate.set(Calendar.MINUTE, 0);
            startDate.set(Calendar.SECOND, 0);
            startDate.set(Calendar.MILLISECOND, 0);

            Calendar endDate = Calendar.getInstance();
            endDate.setTime(profilingDate);
            endDate.set(Calendar.HOUR_OF_DAY, 23);
            endDate.set(Calendar.MINUTE, 59);
            endDate.set(Calendar.SECOND, 59);
            endDate.set(Calendar.MILLISECOND, 999);

            Session session = (Session) dsContext.getInternalContext();
            vehicles = session.createCriteria(VsdVehicle.class, "v")
                    .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdRRProfilings", "rrp", Criteria.LEFT_JOIN,
                            Restrictions.eq("rrp.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.between("rrp.createdTimestamp", startDate.getTime(), endDate.getTime()))
                    .createCriteria("rrp.vsdRiskRating", "rr", Criteria.LEFT_JOIN,
                            Restrictions.eq("rr.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("rr.vsdRiskRatingType", "rrt", Criteria.LEFT_JOIN,
                            Restrictions.eq("rrt.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleOwner", "vo", Criteria.LEFT_JOIN,
                            Restrictions.eq("vo.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("vo.vsdVehOwnConDetails", "vocd", Criteria.LEFT_JOIN,
                            Restrictions.eq("vocd.isDeleted", IDataService.BOOL_FALSE))
                    .addOrder(Order.desc("rrp.createdTimestamp"))
                    .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list();
            logger.info("getVehiclesByProfilingDate -- END");
            return vehicles;
        } catch (Exception ex) {
            logger.error("An error occured in getVehiclesByProfilingDate()");
            logger.error(ex);
            throw new VSDDataAccessException(ex.getMessage(), ex);
        }
    }

    /**
     * 
     * Get Vehicle and Risk Rating by vehicleId 
     * populated DTO are vsdVehicleOwnActivities, vsdVehicleActivity, vsdRRProfilings
     * 
     * @author Tayyab
     * @param dsContext
     * @param retrieveArabicData
     * @param tradeLicenseNumber
     * @return VsdVehicle
     * @throws VSDDataAccessException
     */
    public VsdVehicle getVehicleAndRRProfilingByVehicleId(DataServiceContext dsContext, boolean retrieveArabicData,
            Long vehicleId, Long rrProfilingId) throws VSDDataAccessException {
        logger.info("getVehicleAndRRProfilingByVehicleOwnerId -- START");
        VsdVehicle vsdVehicle = null;
        try {
            Session session = (Session) dsContext.getInternalContext();

            vsdVehicle = (VsdVehicle) session.createCriteria(VsdVehicle.class, "v")
                    .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("v.vehicleId", vehicleId))
                    .createCriteria("v.vsdVehicleCategory", "vcChild", Criteria.LEFT_JOIN,
                            Restrictions.eq("vcChild.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("vcChild.vsdVehicleCategory", "vcParent", Criteria.LEFT_JOIN,
                            Restrictions.eq("vcParent.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleManufacturers", "m", Criteria.LEFT_JOIN,
                            Restrictions.eq("m.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdVehicleOwner", "vo", Criteria.LEFT_JOIN,
                            Restrictions.eq("vo.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("v.vsdRRProfilings", "rrp", Criteria.LEFT_JOIN,
                            Restrictions.eq("rrp.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("rrp.RRProfilingId", rrProfilingId))
                    .createCriteria("rrp.vsdRiskRating", "rr", Criteria.LEFT_JOIN,
                            Restrictions.eq("rr.isDeleted", IDataService.BOOL_FALSE))
                    .addOrder(Order.desc("rrp.updatedTimestamp")).uniqueResult();
        } catch (Exception ex) {
            logger.error("An error occured in getVehicleAndRRProfilingByVehicleOwnerId()");
            throw new VSDDataAccessException(ex.getMessage(), ex);
        }
        logger.info("getVehicleAndRRProfilingByVehicleOwnerId -- END");
        return vsdVehicle;
    }

    /**
     * 
     * Gets the plate details for the vehicle provided
     * 
     * @author Eldon Barrows
     * @param dsContext
     * @param retrieveArabicData
     * @param vehicle
     * @return VehiclePlate
     * @throws VSDDataAccessException
     */
    public VehiclePlate getVehiclePlateByVehicle(DataServiceContext dsContext, boolean retrieveArabicData,
            VsdVehicle vehicle) throws VSDDataAccessException {
        logger.info("getVehiclePlateByVehicle -- START");
        VehiclePlate vehiclePlate = null;
        try {
            Session session = (Session) dsContext.getInternalContext();
            ProjectionList projectionList = Projections.projectionList();
            projectionList.add(Projections.property("v.vehiclePlateNumber"), "plateNumber");
            projectionList.add(Projections.property("cat.categoryName"), "plateCategory");
            projectionList.add(Projections.property("code.categoryName"), "plateCode");
            projectionList.add(Projections.property("country.countryCode"), "plateSource");
            vehiclePlate = (VehiclePlate) session.createCriteria(VsdVehicle.class, "v")
                    .add(Restrictions.eq("v.isDeleted", IDataService.BOOL_FALSE))
                    .add(Restrictions.eq("v.vehicleId", vehicle.getVehicleId()))
                    .createCriteria("v.vsdVehPlateCatCode", "code", Criteria.LEFT_JOIN,
                            Restrictions.eq("code.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("code.vsdVehPlateCatCode", "cat", Criteria.LEFT_JOIN,
                            Restrictions.eq("cat.isDeleted", IDataService.BOOL_FALSE))
                    .createCriteria("cat.vsdCountry", "country", Criteria.LEFT_JOIN,
                            Restrictions.eq("country.isDeleted", IDataService.BOOL_FALSE))
                    .setProjection(projectionList)
                    .setResultTransformer(new AliasToBeanResultTransformer(VehiclePlate.class)).uniqueResult();
            logger.info("getVehiclePlateByVehicle -- END");
            return vehiclePlate;
        } catch (Exception ex) {
            logger.error("An error occured in getVehiclePlateByVehicle()");
            logger.error(ex);
            throw new VSDDataAccessException(ex.getMessage(), ex);
        }
    }
}