package com.vst.dao.hibernate;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import com.vst.dao.*;
import com.vst.model.ConstructionDefect;
import com.vst.model.ConstructionDefectZone;
import com.vst.model.ConstructionExample;
import com.vst.model.DefectParameter;
import org.springframework.orm.ObjectRetrievalFailureException;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
public class ConstructionDefectDaoHibernate extends BaseDaoHibernate<ConstructionDefect> implements ConstructionDefectDao {
DefectTypeDao defectTypeDao;
DangerCategoryDao dangerCategoryDao;
DefectZoneDao defectZoneDao;
DefectVarityDao defectVarityDao;
ReasonDao reasonDao;
public void setReasonDao(ReasonDao reasonDao) {
this.reasonDao = reasonDao;
}
public void setDefectVarityDao(DefectVarityDao defectVarityDao) {
this.defectVarityDao = defectVarityDao;
}
public void setDangerCategoryDao(DangerCategoryDao dangerCategoryDao) {
this.dangerCategoryDao = dangerCategoryDao;
}
public void setDefectZoneDao(DefectZoneDao defectZoneDao) {
this.defectZoneDao = defectZoneDao;
}
public void setDefectTypeDao(DefectTypeDao defectTypeDao) {
this.defectTypeDao = defectTypeDao;
}
public String getDefectConstructionNumber(String exampleId){
try {
ConstructionExample constructionExample = (ConstructionExample) super.getSession().get(ConstructionExample.class, new Integer(exampleId));
super.getSession().evict(constructionExample);
return constructionExample.getExampleRelativeNumber()+"."+(constructionExample.getExampleDefects().size()+1);
} catch (Exception e) {
return "0.0.0.1";
}
}
public List getConstructionDefectsByExampleId(String exampleId){
ConstructionExample constructionExample=(ConstructionExample)super.getSession().get(ConstructionExample.class,new Integer(exampleId));
return constructionExample.getExampleDefects();
}
public List getConstructionDefectsForOtherReason(String otherReason,String typeId,String buildingObjectId){
return super.getSession().createQuery("from ConstructionDefect constructionDefect where (constructionDefect.exampleId in (select exampleId from ConstructionExample constructionExample where constructionExample.buildObjectId=? AND constructionExample.constructionType.constructionTypeId=?)) AND constructionDefect.otherReason=? order by constructionDefect.defectType,constructionDefect.defectVarity").setString(0,buildingObjectId).setString(1,typeId).setString(2,otherReason).list();
}
public List getConstructionDefectsForReason(String reasonId,String typeId,String buildingObjectId){
return super.getSession().createQuery("from ConstructionDefect constructionDefect where constructionDefect.exampleId in (select exampleId from ConstructionExample constructionExample where constructionExample.buildObjectId=? AND constructionExample.constructionType.constructionTypeId=?) AND constructionDefect.reason.reasonId=? order by constructionDefect.defectType,constructionDefect.defectVarity").setString(0,buildingObjectId).setString(1,typeId).setString(2,reasonId).list();
}
/**
* @see com.vst.dao.ConstructionDefectDao#getConstructionDefects(com.vst.model.ConstructionDefect)
*/
public List getConstructionDefects(final ConstructionDefect constructionDefect) {
return getHibernateTemplate().find("from ConstructionDefect");
/* Remove the line above and uncomment this code block if you want
to use Hibernate's Query by Example API.
if (constructionDefect == null) {
return getHibernateTemplate().find("from ConstructionDefect");
} else {
// filter on properties set in the constructionDefect
HibernateCallback callback = new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException {
Example ex = Example.create(constructionDefect).ignoreCase().enableLike(MatchMode.ANYWHERE);
return session.createCriteria(ConstructionDefect.class).add(ex).list();
}
};
return (List) getHibernateTemplate().execute(callback);
}*/
}
/**
* @see com.vst.dao.ConstructionDefectDao#getConstructionDefect(Integer constructionDefectId)
*/
public ConstructionDefect getConstructionDefect(final Integer constructionDefectId) {
ConstructionDefect constructionDefect = (ConstructionDefect) getObject(constructionDefectId);
return constructionDefect;
}
/**
* @see com.vst.dao.ConstructionDefectDao#saveConstructionDefect(ConstructionDefect constructionDefect)
*/
public void saveConstructionDefect(final ConstructionDefect constructionDefect) {
saveObject(constructionDefect);
}
/**
* @see com.vst.dao.ConstructionDefectDao#removeConstructionDefect(Integer constructionDefectId)
*/
public void removeConstructionDefect(final Integer constructionDefectId) {
//TODO find out the real solution for updating index positions
ConstructionDefect constructionDefect=(ConstructionDefect)getConstructionDefect(constructionDefectId);
removeEntity(constructionDefect);
super.getSession().createQuery("update ConstructionDefect set constructionDefectPosition=constructionDefectPosition-1 where exampleId=? AND constructionDefectPosition>?").setString(0,String.valueOf(constructionDefect.getExampleId())).setString(1,String.valueOf(constructionDefect.getConstructionDefectPosition()));
}
public String getDefectConstructionNumberZone(String exampleId){
try {
ConstructionExample constructionExample = (ConstructionExample) super.getSession().get(ConstructionExample.class, new Integer(exampleId));
super.getSession().evict(constructionExample);
return constructionExample.getExampleRelativeNumber()+"."+(constructionExample.getExampleDefects().size()+1);
} catch (Exception e) {
return "0.0.0.1";
}
}
public Integer getDefectNumberByObjectConstructionId(Integer objectConstructionId) {
try {
Integer number=(Integer)(((Integer)super.getSession().createQuery("select max(pointRelativeNumber) from ConstructionDefect where exampleId in (select exampleId from ConstructionExample where objectConstructionId=?)").setInteger(0,objectConstructionId.intValue()).uniqueResult()).intValue()+1);
return number;
} catch (Exception e) {
return new Integer(0);
}
}
public List getConstructionDefectsZoneByExampleId(String exampleId){
ConstructionExample constructionExample=(ConstructionExample)super.getSession().get(ConstructionExample.class,new Integer(exampleId));
return constructionExample.getExampleDefectsZone();
}
public List getConstructionDefectsZoneForOtherReason(String otherReason,String typeId,String buildingObjectId){
return super.getSession().createQuery("from ConstructionDefectZone constructionDefectZone where (constructionDefectZone.exampleId in (select exampleId from ConstructionExample constructionExample where constructionExample.buildObjectId=? AND constructionExample.constructionType.constructionTypeId=?)) AND constructionDefectZone.otherReason=? order by constructionDefectZone.defectType,constructionDefectZone.defectVarity").setString(0,buildingObjectId).setString(1,typeId).setString(2,otherReason).list();
}
public List getConstructionDefectsZoneForReason(String reasonId,String typeId,String buildingObjectId){
return super.getSession().createQuery("from ConstructionDefectZone constructionDefectZone where constructionDefectZone.exampleId in (select exampleId from ConstructionExample constructionExample where constructionExample.buildObjectId=? AND constructionExample.constructionType.constructionTypeId=?) AND constructionDefectZone.reason.reasonId=? order by constructionDefectZone.defectType,constructionDefectZone.defectVarity").setString(0,buildingObjectId).setString(1,typeId).setString(2,reasonId).list();
}
/**
* @see com.vst.dao.ConstructionDefectDao#getConstructionDefects(com.vst.model.ConstructionDefect)
*/
public List getConstructionDefectsZone(final ConstructionDefectZone constructionDefect) {
return getHibernateTemplate().find("from ConstructionDefectZone");
}
/**
* @see com.vst.dao.ConstructionDefectDao#getConstructionDefect(Integer constructionDefectId)
*/
public ConstructionDefectZone getConstructionDefectZone(final Integer constructionDefectId) {
ConstructionDefectZone constructionDefect = (ConstructionDefectZone) getHibernateTemplate().get(ConstructionDefectZone.class, constructionDefectId);
if (constructionDefect == null) {
log.warn("uh oh, constructionDefect with constructionDefectId '" + constructionDefectId + "' not found...");
throw new ObjectRetrievalFailureException(ConstructionDefectZone.class, constructionDefectId);
}
return constructionDefect;
}
/**
* @see com.vst.dao.ConstructionDefectDao#saveConstructionDefect(ConstructionDefect constructionDefect)
*/
public void saveConstructionDefectZone(final ConstructionDefectZone constructionDefectZone) {
System.out.println("CDZ:"+constructionDefectZone.getConstructionDefectId());
List consList = constructionDefectZone.getDefectParameters();
for(Object o:consList){
DefectParameter df = (DefectParameter) o;
System.out.println(df);
// getHibernateTemplate().save(df);
}
getHibernateTemplate().save(constructionDefectZone);
}
public void saveConstructionDefectZone1(final ConstructionDefectZone constructionDefectZone) {
System.out.println("CDZ1:"+constructionDefectZone.getConstructionDefectId());
getHibernateTemplate().save(constructionDefectZone);
}
/**
* @see com.vst.dao.ConstructionDefectDao#removeConstructionDefect(Integer constructionDefectId)
*/
public void removeConstructionDefectZone(final Integer constructionDefectId) {
//TODO find out the real solution for updating index positions
ConstructionDefectZone constructionDefect=getConstructionDefectZone(constructionDefectId);
getHibernateTemplate().delete(constructionDefect);
System.out.println("getExampleId = "+constructionDefect.getExampleId()+"; getConstructionDefectPosition = "+constructionDefect.getConstructionDefectPosition());
super.getSession().createQuery("update ConstructionDefectsZone set constructionDefectPosition=constructionDefectPosition-1 where exampleId=? AND constructionDefectPosition>?").setString(0,String.valueOf(constructionDefect.getExampleId())).setString(1,String.valueOf(constructionDefect.getConstructionDefectPosition()));
}
public void deleteConstructionDefectZone(ConstructionDefectZone constructionDefectZone){
getHibernateTemplate().delete(constructionDefectZone);
}
private List setValues(ResultSet rs) throws SQLException {
List list = new ArrayList();
while (rs.next()) {
ConstructionDefectZone constructionDefectZone = new ConstructionDefectZone();
constructionDefectZone.setConstructionDefectId(new Integer(rs.getInt(1)));
constructionDefectZone=getConstructionDefectZone(constructionDefectZone.getConstructionDefectId());
list.add(constructionDefectZone);
}
return list;
}
public List getConstructionDefectZonesByConstrType(String typeId, String buildingObjectId,Properties props) throws SQLException {
Connection dbConn = getConnection(props);
Statement st = (Statement) dbConn.createStatement();
ResultSet rs = (ResultSet) st.executeQuery("select constructionDefectsZone.constructionDefectId from constructionExamples, constructionDefectsZone where constructionExamples.buildObjectId="+buildingObjectId+" and constructionExamples.exampleId=constructionDefectsZone.exampleId and constructionDefectsZone.constructionTypeId="+typeId+" group by uniqueForExamples");
List plList = setValues(rs);
rs.close();
return plList;
}
public List getExamplesByDefect(String unique,Properties properties) throws SQLException {
Connection dbConn = getConnection(properties);
Statement st = (Statement) dbConn.createStatement();
ResultSet rs = (ResultSet) st.executeQuery("select exampleId from constructionDefectsZone where uniqueForExamples="+unique);
List list=new ArrayList();
while (rs.next()) {
list.add(rs.getString(1));
}
rs.close();
return list;
}
private Connection getConnection(Properties props) {
String driverURL = "jdbc:mysql://localhost/vstbase?useUnicode=true&characterEncoding=UTF-8";
Connection dbConn = null;
try {
Class.forName("com.mysql.jdbc.Driver");
dbConn = (Connection) DriverManager.getConnection(driverURL, props);
return dbConn;
}
catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
Class<ConstructionDefect> getServiceClass() {
return ConstructionDefect.class;
}
}