com.digitnexus.autoid.dao.AssetAndVehicleDaoImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.digitnexus.autoid.dao.AssetAndVehicleDaoImpl.java

Source

package com.digitnexus.autoid.dao;

import java.util.List;

import com.digitnexus.core.dao.BaseDaoImpl;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.digitnexus.autoid.dao.AssetAndVehicleDao;
import com.digitnexus.autoid.dataobject.Asset;
import com.digitnexus.autoid.dataobject.AssetLocation;
import com.digitnexus.autoid.dataobject.Location;
import com.digitnexus.autoid.dataobject.Vehicle;

@Repository
public class AssetAndVehicleDaoImpl extends BaseDaoImpl implements AssetAndVehicleDao {

    @Override
    public void updateAssetLocation(AssetLocation assetLocation) {
        Location location = assetLocation.getLocation();
        Asset asset = this.get(Asset.class, assetLocation.getAsset().getId());
        @SuppressWarnings("unchecked")
        List<AssetLocation> assetLocationList = this.find(" from AssetLocation where asset = ?", asset);
        if (assetLocationList != null && assetLocationList.size() > 0) {
            asset.setStatusCode(assetLocation.getAsset().getStatusCode());
            AssetLocation oldAssetLocation = assetLocationList.get(0);
            Location oldlocation = oldAssetLocation.getLocation();
            oldlocation.setAddrLine1(location.getAddrLine1());
            oldlocation.setLatCoor(location.getLatCoor());
            oldlocation.setLonCoor(location.getLonCoor());
            oldAssetLocation.setTypecode(assetLocation.getTypecode());
        }

    }

    @Override
    public void saveVehicle(Vehicle newVehicleRecord) {
        DetachedCriteria detVehicle = this.getDetachedCriteria(Vehicle.class);
        detVehicle.add(Restrictions.eq("typecode", newVehicleRecord.getTypecode()));
        detVehicle.addOrder(Order.asc("id"));
        // to fetch some basic information such as vin number , because the same type of vehicle have the same basic information
        List<Vehicle> vehicleList = this.findByCriteria(detVehicle, Vehicle.class, 0, 1);
        if (vehicleList != null && vehicleList.size() > 0) {
            Vehicle vehicle = vehicleList.get(0);
            newVehicleRecord.setAvailableInd(vehicle.getAvailableInd());
            newVehicleRecord.setClientID(vehicle.getClientID());
            newVehicleRecord.setLicenseNum(vehicle.getLicenseNum());
            newVehicleRecord.setRegistrationNum(vehicle.getRegistrationNum());
            newVehicleRecord.setDescription(vehicle.getDescription());
            newVehicleRecord.setRemark(vehicle.getRemark());
            newVehicleRecord.setVin(vehicle.getVin());
            this.save(newVehicleRecord);
        }
    }

    @Override
    public List<Vehicle> getCurrentVehicle(long id) {
        DetachedCriteria detVehicle = this.getDetachedCriteria(Vehicle.class);
        if (id != 0) {
            detVehicle.add(Restrictions.ge("id", id));
        }
        detVehicle.addOrder(Order.asc("id"));
        return this.findByCriteria(detVehicle, Vehicle.class);
    }

    @Override
    public int updateAssetStatus(Asset asset) {
        if (asset == null || asset.getTagId() == null) {
            return 0;
        }
        String hql = "update Asset a set a.statusCode = '" + asset.getStatusCode() + "' where a.tagId = '"
                + asset.getTagId() + "'";
        int count = this.getHibernateTemplate().bulkUpdate(hql);
        // TODO Auto-generated method stub
        return count;
    }

}