package com.vst.webapp.action;
import com.vst.Constants;
import com.vst.model.BuildingObject;
import com.vst.model.ObjectPhoto;
import com.vst.service.BuildingObjectManager;
import com.vst.service.ObjectPhotoManager;
import com.vst.webapp.util.FileHelper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.sql.Blob;
import java.util.List;
import java.util.ResourceBundle;
public class ObjectPhotoController implements Controller {
private final Log log = LogFactory.getLog(ObjectPhotoController.class);
private ObjectPhotoManager objectPhotoManager = null;
private BuildingObjectManager buildingObjectManager = null;
public void setBuildingObjectManager(BuildingObjectManager buildingObjectManager) {
this.buildingObjectManager = buildingObjectManager;
}
public void setObjectPhotoManager(ObjectPhotoManager objectPhotoManager) {
this.objectPhotoManager = objectPhotoManager;
}
public ModelAndView handleRequest(HttpServletRequest request,
HttpServletResponse response)
throws Exception {
if (log.isDebugEnabled()) {
log.debug("entering 'handleRequest' method...");
}
if (request.getParameter("edited") != null) {
request.setAttribute("addition", "?edited=1");
}
String pageNumber = request.getParameter("d-2586907-p");
if (pageNumber != null) {
request.setAttribute("pageNumber", "d-2586907-p=" + pageNumber);
}
String objectId = request.getParameter("objectId");
if (request.getParameter("delete") != null) {
String objectPhotoId = request.getParameter("id");
ResourceBundle bundle = ResourceBundle.getBundle("ApplicationResources");
boolean hasFatalError = false;
try {
BuildingObject buildingObject = buildingObjectManager.getBuildingObject(objectId);
ObjectPhoto objectPhoto = objectPhotoManager.getObjectPhoto(objectPhotoId);
buildingObject.getPhotoes().remove(objectPhoto);
objectPhotoManager.removeObjectPhoto(objectPhotoId);
buildingObjectManager.saveBuildingObject(buildingObject);
request.setAttribute("message", bundle.getString("objectPhoto.deleted"));
} catch (Exception e) {
hasFatalError = true;
}
if (hasFatalError) {
request.setAttribute("errormessage", bundle.getString("commonMistake"));
}
}
List objectPhotos = objectPhotoManager.getObjectPhotos(objectId);
//retrieving all photoes to the hard drive
for (int i = 0; i < objectPhotos.size(); i++) {
ObjectPhoto objectPhoto = (ObjectPhoto) objectPhotos.get(i);
File file = new File(com.vst.util.FileHelper.getCurrentPath(request) + objectPhoto.getWayToPhoto());
if (file.exists()) {
file.delete();
}
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(FileHelper.getCurrentPath(request) + objectPhoto.getWayToPhoto());
Blob blob = objectPhoto.getPhotoBlob();
byte[] bufer = new byte[62000];
InputStream inputStream = blob.getBinaryStream();
while (inputStream.read(bufer) != -1) {
fileOutputStream.write(bufer);
}
inputStream.close();
fileOutputStream.close();
}
return new ModelAndView("objectPhotoList", Constants.OBJECTPHOTO_LIST, objectPhotos);
}
}