data.dao.FeatureDao.java Source code

Java tutorial

Introduction

Here is the source code for data.dao.FeatureDao.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package data.dao;

import data.dao.parent.Dao;
import data.entity.Car;
import data.entity.CarCompletionOption;
import data.entity.Feature;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 *
 * @author bezdatiuzer
 */
@Repository("featureDao")
public class FeatureDao extends Dao<Feature> {

    @Autowired
    private CarCompletionOptionDao carCompletionOptionDao;

    @Override
    public Class getSupportedClass() {
        return Feature.class;
    }

    private List<Long> getCcoIdDict() {
        Criteria cr = currentSession().createCriteria(getSupportedClass());
        cr.setProjection(Projections.distinct(Projections.property("ccoId")));
        cr.addOrder(Order.asc("ccoId"));
        return cr.list();
    }

    public List<CarCompletionOption> getCcoDict() {
        List<Long> ccoIdList = getCcoIdDict();
        List<CarCompletionOption> ccoList = new ArrayList();
        for (Long oldId : ccoIdList) {
            CarCompletionOption cco = new CarCompletionOption();
            cco.setOldId(oldId);
            List<CarCompletionOption> supList = carCompletionOptionDao.find(cco);
            if (!supList.isEmpty()) {
                ccoList.add(supList.get(0));
            }
        }
        return ccoList;
    }

    public List<Long> getCmgDict() {
        Criteria cr = currentSession().createCriteria(getSupportedClass());
        cr.setProjection(Projections.distinct(Projections.property("cmgId")));
        cr.addOrder(Order.asc("cmgId"));
        return cr.list();
    }

    public List<Feature> getGroupOfFeatures(Object cmgId, Object ccoId) {
        Criteria cr = currentSession().createCriteria(getSupportedClass());
        if (cmgId == null && ccoId == null) {
            return getAllAsc("oldId");
        } else {
            if (cmgId != null) {
                if (!cmgId.equals(Long.valueOf("0"))) {
                    //cr.add(cmgId.equals("") ? Restrictions.isNull("cmgId") : Restrictions.eq("cmgId",cmgId));
                    cr.add(Restrictions.eq("cmgId", cmgId));
                } else {
                    cr.add(Restrictions.isNull("cmgId"));
                }
            }
            if (ccoId != null) {
                //cr.add(ccoId.equals("") ? Restrictions.isNull("ccoId") : Restrictions.eq("ccoId",ccoId));
                if (!ccoId.equals(Long.valueOf("0"))) {
                    cr.add(Restrictions.eq("ccoId", ccoId));
                } else {
                    cr.add(Restrictions.isNull("ccoId"));
                }
            }
        }
        return cr.list();
    }

    public List<Feature> getByCmgId(Long cmgId) {
        Criteria cr = currentSession().createCriteria(getSupportedClass());
        cr.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        cr.add(Restrictions.eq("cmgId", cmgId));
        //cr.addOrder(Order.asc("cmgId"));
        return cr.list();
    }

    /*public List<Feature> getByCar(Car car){
    List<Car> carList = new ArrayList();
    carList.add(car);
    Criteria cr = currentSession().createCriteria(getSupportedClass());
    cr.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
    Criterion cmgCrit = Restrictions.eq("cmgId", car.getCmsqId());
    Criterion carCrit = Restrictions.eq("cars", carList);
    cr.add(Restrictions.or(cmgCrit, carCrit));
    return cr.list();
    }*/

    public List<Feature> getByCar(Car car) {
        String sql = "SELECT distinct f.* FROM car_to_feature ctf LEFT JOIN feature f ON ctf.feature_id=f.feature_id WHERE ctf.car_id=:carId or f.car_model_generation_id=:carCmgId";
        Query query = currentSession().createSQLQuery(sql).addEntity("feature", getSupportedClass())
                .setParameter("carId", car.getId()).setParameter("carCmgId", car.getCmgqId());
        return query.list();

        /*String hql = "select f from Feature f left join cars where f.cmgId ";
        Query query = currentSession().createQuery(hql);
            
        return query.list();*/

        /*Criteria cr = currentSession().createCriteria(getSupportedClass());
        cr.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
        Criterion cmgCrit = Restrictions.eq("cmgId", car.getCmsqId());
        Criterion carCrit = Restrictions.eq("cars", carList);
        cr.add(Restrictions.or(cmgCrit, carCrit));
        return cr.list();*/
    }

}