package com.vst.dao.hibernate;
import java.util.ArrayList;
import java.util.List;
import com.vst.dao.DefectZoneDao;
import com.vst.dao.util.RefferenceUtil;
import com.vst.model.DefectZone;
public class DefectZoneDaoHibernate extends BaseDaoHibernate<DefectZone> implements DefectZoneDao {
public boolean isUniqueDefectZone(DefectZone defectZone) {
int count = super.getSession().createQuery("from DefectZone defectZone where defectZone.defectZoneName=? AND defectZone.defectZoneId<>?").setString(0, defectZone.getDefectZoneName()).setString(1, String.valueOf(defectZone.getDefectZoneId())).list().size();
return count == 0;
}
public List getDefectZonesByConstructionType(final String constructionTypeId) {
// super.getSession().createQuery("from DefectZone defectZone where defectZone.defectZoneId in (select )")
// ConstructionType constructionType=(ConstructionType) super.getSession().get(ConstructionType.class, new Integer(constructionTypeId));
// evict(constructionType);
// List defectZones=constructionType.getDefectZones();
// for (int i = 0; i < defectZones.size(); i++) {
// DefectZone defectZone = (DefectZone)defectZones.get(i);
// evict(defectZone);
System.out.println("CTDZ" + constructionTypeId);
return super.getSession().createQuery("select constructionType.defectZones from ConstructionType constructionType where constructionType.constructionTypeId=?").setString(0,constructionTypeId).list();
}
public List getConnectedRefferences(final String defectZoneId) {
List constructionDefectList=super.getSession().createQuery("from ConstructionDefect constructionDefect where constructionDefect.defectZone.defectZoneId=?").setString(0, defectZoneId).list();
List constructionTypeList= super.getSession().createQuery("from ConstructionType constructionType inner join fetch constructionType.defectZones as defectZones where defectZones.defectZoneId=?").setString(0, defectZoneId).list();
List defectCategoryList = super.getSession().createQuery("from DefectCategory defectCategory where defectCategory.defectZone.defectZoneId=?").setString(0, defectZoneId).list();
List reasonList = super.getSession().createQuery("from Reason reason where reason.defectZone.defectZoneId=?").setString(0, defectZoneId).list();
List referenceList = new ArrayList();
RefferenceUtil.makeURL(referenceList, "constructionDefect", constructionDefectList);
RefferenceUtil.makeURL(referenceList, "constructionType", constructionTypeList);
RefferenceUtil.makeURL(referenceList, "defectCategory", defectCategoryList);
RefferenceUtil.makeURL(referenceList, "reason", reasonList);
return referenceList;
}
public boolean isDefectZoneUsed(String defectZoneId) {
Long count1 = (Long) super.getSession().createQuery("select count(*) from ConstructionDefect constructionDefect where constructionDefect.defectZone.defectZoneId=?").setString(0, defectZoneId).uniqueResult();
int count2 = super.getSession().createQuery("from ConstructionType constructionType inner join fetch constructionType.defectZones as defectZones where defectZones.defectZoneId=?").setString(0, defectZoneId).list().size();
Long count3 = (Long) super.getSession().createQuery("select count(*) from DefectCategory defectCategory where defectCategory.defectZone.defectZoneId=?").setString(0, defectZoneId).uniqueResult();
Long count4 = (Long) super.getSession().createQuery("select count(*) from Reason reason where reason.defectZone.defectZoneId=?").setString(0, defectZoneId).uniqueResult();
if (count3 == null) {
count3 = new Long(0);
}
if (count4 == null) {
count4 = new Long(0);
}
if (count1 == null) {
count1 = new Long(0);
}
if (count2 + count1.intValue() + count3.intValue() + count4.intValue() == 0) {
return false;
}
return true;
}
/**
* @see com.vst.dao.DefectZoneDao#getDefectZones(com.vst.model.DefectZone)
*/
public List getDefectZones(final DefectZone defectZone) {
return getHibernateTemplate().find("from DefectZone");
/* Remove the line above and uncomment this code block if you want
to use Hibernate's Query by Example API.
if (defectZone == null) {
return getHibernateTemplate().find("from DefectZone");
} else {
// filter on properties set in the defectZone
HibernateCallback callback = new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Example ex = Example.create(defectZone).ignoreCase().enableLike(MatchMode.ANYWHERE);
return session.createCriteria(DefectZone.class).add(ex).list();
}
};
return (List) getHibernateTemplate().execute(callback);
}*/
}
/**
* @see com.vst.dao.DefectZoneDao#getDefectZone(Integer defectZoneId)
*/
public DefectZone getDefectZone(final Integer defectZoneId) {
DefectZone defectZone = (DefectZone) getObject(defectZoneId);
return defectZone;
}
/**
* @see com.vst.dao.DefectZoneDao#saveDefectZone(DefectZone defectZone)
*/
public void saveDefectZone(final DefectZone defectZone) {
saveObject(defectZone);
}
/**
* @see com.vst.dao.DefectZoneDao#removeDefectZone(Integer defectZoneId)
*/
public void removeDefectZone(final Integer defectZoneId) {
removeObject(defectZoneId);
}
@Override
Class<DefectZone> getServiceClass() {
return DefectZone.class;
}
}