JSONEscapeObject jsonResult = new JSONEscapeObject();
Content content = new Content();
content = ContentDAO.load(site.getSiteId(), Format.getLong(form.getKey()));
FormFile file = form.getFile();
byte fileData[] = file.getFileData();
if (Format.isNullOrEmpty(file.getFileName())) {
jsonResult.put("status", Constants.WEBSERVICE_STATUS_FAILED);
jsonResult.put("message", resources.getMessage("error.string.required"));
streamWebService(response, jsonResult.toHtmlString());
return null;
}
ImageScaler scaler = null;
try {
scaler = new ImageScaler(fileData, file.getContentType());
scaler.resize(600);
}
catch (OutOfMemoryError outOfMemoryError) {
jsonResult.put("status", Constants.WEBSERVICE_STATUS_FAILED);
jsonResult.put("message", resources.getMessage("content.error.image.size"));
streamWebService(response, jsonResult.toHtmlString());
return null;
}
catch (Throwable e) {
jsonResult.put("status", Constants.WEBSERVICE_STATUS_FAILED);
jsonResult.put("message", resources.getMessage("error.image.invalid"));
streamWebService(response, jsonResult.toHtmlString());
return null;
}
ContentLanguage contentLanguage = null;
for (ContentLanguage language : content.getContentLanguages()) {
if (language.getSiteProfileClass().getSiteProfileClassId().equals(form.getSiteProfileClassId())) {
contentLanguage = language;
break;
}
}
if (form.isSiteProfileClassDefault()) {
ContentImage contentImage = new ContentImage();
contentImage.setImageName(file.getFileName());
contentImage.setContentType("image/jpeg");
contentImage.setImageValue(scaler.getBytes());
contentImage.setImageHeight(scaler.getHeight());
contentImage.setImageWidth(scaler.getWidth());
contentImage.setRecUpdateBy(adminBean.getUser().getUserId());
contentImage.setRecUpdateDatetime(new Date(System.currentTimeMillis()));
contentImage.setRecCreateBy(adminBean.getUser().getUserId());
contentImage.setRecCreateDatetime(new Date(System.currentTimeMillis()));
em.persist(contentImage);
if (contentLanguage.getImage() == null) {
contentLanguage.setImage(contentImage);
}
else {
contentImage.setContentLanguage(contentLanguage);
contentLanguage.getImages().add(contentImage);
}
contentLanguage.setRecUpdateBy(adminBean.getUser().getUserId());
contentLanguage.setRecUpdateDatetime(new Date(System.currentTimeMillis()));
em.persist(contentLanguage);
}
else {
if (contentLanguage == null) {
contentLanguage = new ContentLanguage();
contentLanguage.setContent(content);
contentLanguage.setRecCreateBy(adminBean.getUser().getUserId());
contentLanguage.setRecCreateDatetime(new Date(System.currentTimeMillis()));
SiteProfileClass siteProfileClass = SiteProfileClassDAO.load(form.getSiteProfileClassId());
contentLanguage.setSiteProfileClass(siteProfileClass);
em.persist(contentLanguage);
}
ContentImage contentImage = new ContentImage();
contentImage.setImageName(file.getFileName());
contentImage.setContentType("image/jpeg");
contentImage.setImageValue(scaler.getBytes());
contentImage.setImageHeight(scaler.getHeight());
contentImage.setImageWidth(scaler.getWidth());
contentImage.setRecUpdateBy(adminBean.getUser().getUserId());