package com.vst.service.impl;
import com.vst.dao.ConstructionExampleDao;
import com.vst.dao.ObjectAnswerDao;
import com.vst.dao.ObjectConstructionDao;
import com.vst.dao.QuestionDao;
import com.vst.model.ConstructionExample;
import com.vst.model.ConstructionType;
import com.vst.model.ObjectAnswer;
import com.vst.model.ObjectConstruction;
import com.vst.service.ObjectConstructionManager;
import com.vst.util.ImageUtil;
import org.hibernate.Hibernate;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class ObjectConstructionManagerImpl extends BaseManager implements ObjectConstructionManager {
private ObjectConstructionDao dao;
private ConstructionExampleDao constructionExampleDao;
private QuestionDao questionDao;
private ObjectAnswerDao objectAnswerDao;
public void setConstructionExampleDao(ConstructionExampleDao constructionExampleDao) {
this.constructionExampleDao = constructionExampleDao;
}
public void setQuestionDao(QuestionDao questionDao) {
this.questionDao = questionDao;
}
public void setObjectAnswerDao(ObjectAnswerDao objectAnswerDao) {
this.objectAnswerDao = objectAnswerDao;
}
public List getListForObjectConstructionPage(Integer objectId){
return dao.getListForObjectConstructionPage(objectId);
}
public void updateConstructionIndexesByObject(Integer objectId,Integer constructionPosition){
dao.updateConstructionIndexesByObject(objectId,constructionPosition);
}
public void setNewEskizToAllExamples(ObjectConstruction objectConstruction, HttpServletRequest request) throws IOException, SQLException {
List constructionExamples = constructionExampleDao.getConstructionExamples(objectConstruction.getTypeId().toString());
boolean hasEskiz = false;
for (int i = 0; i < constructionExamples.size(); i++) {
ConstructionExample constructionExample = (ConstructionExample) constructionExamples.get(i);
if (constructionExample.getDefectEsckizBlob() != null) {
hasEskiz = true;
break;
}
}
if (!hasEskiz) {
//setting new esckiz blob to all examples
for (int i = 0; i < constructionExamples.size(); i++) {
Blob blob=Hibernate.createBlob(new FileInputStream(objectConstruction.getWayToEsckiz()));
String wayToDefectEskiz = ImageUtil.getUniqueJPEGFile(request);
File file = new File(wayToDefectEskiz);
try {
file.createNewFile();
} catch (IOException e) {
}
FileOutputStream fileOutputStream = new FileOutputStream(wayToDefectEskiz);
byte[] bufer = new byte[62000];
InputStream inputStream = blob.getBinaryStream();
while (inputStream.read(bufer) != -1) {
fileOutputStream.write(bufer);
}
inputStream.close();
fileOutputStream.close();
String wayToPowerEskiz = ImageUtil.getUniqueJPEGFile(request);
file = new File(wayToPowerEskiz);
if (file.exists()) {
file.delete();
}
file.createNewFile();
fileOutputStream = new FileOutputStream(wayToPowerEskiz);
byte[] powerBufer = new byte[62000];
blob=Hibernate.createBlob(new FileInputStream(objectConstruction.getWayToEsckiz()));
inputStream = blob.getBinaryStream();
while (inputStream.read(powerBufer) != -1) {
fileOutputStream.write(powerBufer);
}
inputStream.close();
fileOutputStream.close();
ConstructionExample constructionExample = (ConstructionExample) constructionExamples.get(i);
constructionExample.setDefectEsckizBlob(Hibernate.createBlob(new FileInputStream(objectConstruction.getWayToEsckiz())));
constructionExample.setWayToDefectEsckiz(wayToDefectEskiz);
constructionExample.setWayToPowerEsckiz(wayToPowerEskiz);
constructionExample.setPowerEsckizBlob(Hibernate.createBlob(new FileInputStream(objectConstruction.getWayToEsckiz())));
constructionExampleDao.saveConstructionExample(constructionExample);
// constructionExampleDao.evict(constructionExample);
}
}
}
public ObjectAnswer getNextQuestion(Integer typeId, Integer questionId) {
return objectAnswerDao.getNextConstructionQuestion(typeId, questionId);
}
public ObjectAnswer getPrevQuestion(Integer typeId, Integer questionId) {
return objectAnswerDao.getPrevConstructionQuestion(typeId, questionId);
}
public List getConstructionQuestionsByTypeId(String typeId) {
return questionDao.getConstructionQuestionsByTypeId(typeId);
}
public List fillExampleNumbers(List objectConstructions) {
for (int i = 0; i < objectConstructions.size(); i++) {
ObjectConstruction objectConstruction = (ObjectConstruction) objectConstructions.get(i);
objectConstruction.setNumberInObject(getExampleNumberByConstructionType(objectConstruction.getTypeId().toString(), objectConstruction.getObjectId().toString()).intValue());
//System.out.println("objectConstruction SIZE == "+objectConstruction.getTypeId()+"; "+objectConstruction.getObjectId()+"; === "+objectConstruction.getNumberInObject());
}
return objectConstructions;
}
public Integer getExampleNumberByConstructionType(String constructionTypeId, String buildObjectId) {
return dao.getExampleNumberByConstructionType(constructionTypeId, buildObjectId);
}
public List getConstructionQuestions(String constructionTypeId) {
return dao.getConstructionQuestions(constructionTypeId);
}
/**
* Set the Dao for communication with the data layer.
*
* @param dao
*/
public void setObjectConstructionDao(ObjectConstructionDao dao) {
this.dao = dao;
}
public List getObjectConstructions(Integer objectId) {
return dao.getObjectConstructions(objectId);
}
/**
* @see com.vst.service.ObjectConstructionManager#getObjectConstruction(String typeId)
*/
public ObjectConstruction getObjectConstruction(final String typeId) {
return dao.getObjectConstruction(new Integer(typeId));
}
public ObjectConstruction getObjectConstructionForBreadCrump(final String typeId){
return dao.getObjectConstructionForBreadCrump(typeId);
}
/**
* @see com.vst.service.ObjectConstructionManager#saveObjectConstruction(ObjectConstruction objectConstruction)
*/
public void saveObjectConstruction(ObjectConstruction objectConstruction) {
dao.saveObjectConstruction(objectConstruction);
}
/**
* @see com.vst.service.ObjectConstructionManager#removeObjectConstruction(String typeId)
*/
public void removeObjectConstruction(final String typeId) {
dao.removeObjectConstruction(new Integer(typeId));
}
public List getObjectConstructionByObjectIdAndConstructionType(Integer objectId, Integer constructionType) {
List list=dao.getObjectConstructions(objectId);
List resultList=new ArrayList();
for (int i=0; i<list.size(); i++){
ObjectConstruction objectConstruction=(ObjectConstruction)list.get(i);
if (objectConstruction.getConstructionType().getConstructionTypeId().equals(constructionType)){
resultList.add(objectConstruction);
}
}
return resultList; //To change body of implemented methods use File | Settings | File Templates.
}
public Blob getObjectConstructionImage(Integer typeId, String imageFldName) {
return dao.getObjectConstructionImage(typeId, imageFldName);
}
}