package com.vst.dao.hibernate;
import java.util.ArrayList;
import java.util.List;
import com.vst.dao.DefectVarityDao;
import com.vst.dao.util.RefferenceUtil;
import com.vst.model.DefectType;
import com.vst.model.DefectVarity;
import com.vst.model.Parameter;
public class DefectVarityDaoHibernate extends BaseDaoHibernate<DefectVarity> implements DefectVarityDao {
public boolean isUniqueVarity(DefectVarity defectVarity) {
List parameterList = defectVarity.getParameters();
String joinQuery = "";
String conditionQuery = "";
for (int i = 0; i < parameterList.size(); i++) {
Parameter parameter = (Parameter) parameterList.get(i);
joinQuery += " inner join fetch defectVarity.parameters as parameters" + i + " ";
conditionQuery += " AND parameters" + i + ".parameterId=" + parameter.getParameterId() + " ";
}
int count = super.getSession().createQuery("from DefectVarity defectVarity " + joinQuery + " where defectVarity.varityName=? AND defectVarity.material.materialId=? AND defectVarity.varityId<>?" + conditionQuery).setString(0, defectVarity.getVarityName()).setString(1, defectVarity.getMaterial().getMaterialId().toString()).setString(2, String.valueOf(defectVarity.getVarityId())).list().size();
return count == 0;
}
public List getConnectedRefferences(final String varityId) {
List defectTypeList = super.getSession().createQuery("from DefectType defectType inner join fetch defectType.varities as varities where varities.varityId=? AND defectType.defectTypeId>0)").setString(0, varityId).list();
List defectCategoryList = super.getSession().createQuery("from DefectCategory defectCategory where defectCategory.defectVarity.varityId=?").setString(0, varityId).list();
List constructionDefectList = super.getSession().createQuery("from ConstructionDefect constructionDefect where constructionDefect.defectVarity.varityId=?").setString(0, varityId).list();
List reasonList = super.getSession().createQuery("from Reason reason where reason.defectVarity.varityId=?").setString(0, varityId).list();
List referenceList = new ArrayList();
RefferenceUtil.makeURL(referenceList, "defectType", defectTypeList);
RefferenceUtil.makeURL(referenceList, "defectCategory", defectCategoryList);
RefferenceUtil.makeURL(referenceList, "constructionDefect", constructionDefectList);
RefferenceUtil.makeURL(referenceList, "reason", reasonList);
return referenceList;
}
public boolean isDefectVarityUsed(String varityId) {
Integer count1 = super.getSession().createQuery("from DefectType defectType inner join fetch defectType.varities as varities where varities.varityId=? AND defectType.defectTypeId>0)").setString(0, varityId).list().size();
Long count2 = (Long) super.getSession().createQuery("select count(*) from DefectCategory defectCategory where defectCategory.defectVarity.varityId=?").setString(0, varityId).uniqueResult();
Long count3 = (Long) super.getSession().createQuery("select count(*) from ConstructionDefect constructionDefect where constructionDefect.defectVarity.varityId=?").setString(0, varityId).uniqueResult();
Long count4 = (Long) super.getSession().createQuery("select count(*) from Reason reason where reason.defectVarity.varityId=?").setString(0, varityId).uniqueResult();
if (count1 == null) {
count1 = new Integer(0);
}
if (count2 == null) {
count2 = new Long(0);
}
if (count3 == null) {
count3 = new Long(0);
}
if (count4 == null) {
count4 = new Long(0);
}
if (count1.intValue() + count2.intValue() + count3.intValue() + count4.intValue() > 0) {
return true;
}
return false;
}
public List getDefectVaritiesByTypeAndConstruction(final String defectTypeId,final String constructionTypeId){
return super.getSession().createQuery("from DefectVarity defectVarity where defectVarity.varityId in (select varities.varityId from DefectType defectType join defectType.varities as varities join varities.constructionTypes as constructionTypes where defectType.defectTypeId=? AND constructionTypes.constructionTypeId=?)").setString(0,defectTypeId).setString(1,constructionTypeId).list();
}
public List getDefectVaritiesByType(final String defectTypeId) {
DefectType defectType = (DefectType) getHibernateTemplate().get(DefectType.class, new Integer(defectTypeId));
// evict(defectType);
// List varityList=defectType.getVarities();
// List resultList=new ArrayList();
// for (int i = 0; i < varityList.size(); i++) {
// DefectVarity defectVarity = (DefectVarity)varityList.get(i);
// evict(defectVarity);
// resultList.add(defectVarity);
// }
// return resultList;
System.out.println(defectType);
return defectType.getVarities();
}
/**
* @see com.vst.dao.DefectVarityDao#getDefectVaritys(com.vst.model.DefectVarity)
*/
public List getDefectVaritys(final DefectVarity defectVarity) {
return getHibernateTemplate().find("from DefectVarity");
/* Remove the line above and uncomment this code block if you want
to use Hibernate's Query by Example API.
if (defectVarity == null) {
return getHibernateTemplate().find("from DefectVarity");
} else {
// filter on properties set in the defectVarity
HibernateCallback callback = new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Example ex = Example.create(defectVarity).ignoreCase().enableLike(MatchMode.ANYWHERE);
return session.createCriteria(DefectVarity.class).add(ex).list();
}
};
return (List) getHibernateTemplate().execute(callback);
}*/
}
/**
* @see com.vst.dao.DefectVarityDao#getDefectVarity(Integer varityId)
*/
public DefectVarity getDefectVarity(final Integer varityId) {
DefectVarity defectVarity = (DefectVarity) getObject(varityId);
return defectVarity;
}
/**
* @see com.vst.dao.DefectVarityDao#saveDefectVarity(DefectVarity defectVarity)
*/
public void saveDefectVarity(final DefectVarity defectVarity) {
saveObject(defectVarity);
}
/**
* @see com.vst.dao.DefectVarityDao#removeDefectVarity(Integer varityId)
*/
public void removeDefectVarity(final Integer varityId) {
removeObject(varityId);
}
@Override
Class<DefectVarity> getServiceClass() {
return DefectVarity.class;
}
}