AdminBean adminBean = getAdminBean(request);
Site site = adminBean.getSite();
initSiteProfiles(form, site);
MessageResources resources = this.getResources(request);
JSONEscapeObject jsonResult = new JSONEscapeObject();
Item item = new Item();
item = ItemDAO.load(site.getSiteId(), Format.getLong(form.getKey()));
FormFile file = form.getFile();
byte fileData[] = file.getFileData();
ActionMessages errors = validateUploadImage(form, file);
if (errors.size() != 0) {
jsonResult.put("status", Constants.WEBSERVICE_STATUS_FAILED);
jsonResult.put("message", resources.getMessage("error.string.required"));
streamWebService(response, jsonResult.toHtmlString());
}
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;
}
ItemLanguage itemLanguage = null;
for (ItemLanguage language : item.getItemLanguages()) {
if (language.getSiteProfileClass().getSiteProfileClassId().equals(form.getSiteProfileClassId())) {
itemLanguage = language;
break;
}
}
if (form.isSiteProfileClassDefault()) {
ItemImage itemImage = new ItemImage();
itemImage.setImageName(file.getFileName());
itemImage.setContentType("image/jpeg");
itemImage.setImageValue(scaler.getBytes());
itemImage.setImageHeight(scaler.getHeight());
itemImage.setImageWidth(scaler.getWidth());
itemImage.setRecUpdateBy(adminBean.getUser().getUserId());
itemImage.setRecUpdateDatetime(new Date(System.currentTimeMillis()));
itemImage.setRecCreateBy(adminBean.getUser().getUserId());
itemImage.setRecCreateDatetime(new Date(System.currentTimeMillis()));
em.persist(itemImage);
if (itemLanguage.getImage() == null) {
itemLanguage.setImage(itemImage);
}
else {
itemImage.setItemLanguage(itemLanguage);
itemLanguage.getImages().add(itemImage);
}
itemLanguage.setRecUpdateBy(adminBean.getUser().getUserId());
itemLanguage.setRecUpdateDatetime(new Date(System.currentTimeMillis()));
em.persist(itemLanguage);
}
else {
if (itemLanguage == null) {
itemLanguage = new ItemLanguage();
itemLanguage.setItem(item);
itemLanguage.setRecCreateBy(adminBean.getUser().getUserId());
itemLanguage.setRecCreateDatetime(new Date(System.currentTimeMillis()));
SiteProfileClass siteProfileClass = SiteProfileClassDAO.load(form.getSiteProfileClassId());
itemLanguage.setSiteProfileClass(siteProfileClass);
}
ItemImage itemImage = new ItemImage();
itemImage.setImageName(file.getFileName());
itemImage.setContentType("image/jpeg");
itemImage.setImageValue(scaler.getBytes());
itemImage.setImageHeight(scaler.getHeight());
itemImage.setImageWidth(scaler.getWidth());
itemImage.setRecUpdateBy(adminBean.getUser().getUserId());
itemImage.setRecUpdateDatetime(new Date(System.currentTimeMillis()));
itemImage.setRecCreateBy(adminBean.getUser().getUserId());
itemImage.setRecCreateDatetime(new Date(System.currentTimeMillis()));
em.persist(itemImage);
if (itemLanguage.getImage() == null) {
itemLanguage.setImage(itemImage);
}
else {
itemImage.setItemLanguage(itemLanguage);
itemLanguage.getImages().add(itemImage);
}
itemLanguage.setRecUpdateBy(adminBean.getUser().getUserId());
itemLanguage.setRecUpdateDatetime(new Date(System.currentTimeMillis()));
em.persist(itemImage);
}
em.flush();
em.refresh(item);
jsonResult = createJsonImages(site.getSiteId(), item, form);
jsonResult.put("recUpdateBy", item.getRecUpdateBy());
jsonResult.put("recUpdateDatetime", Format.getFullDatetime(item.getRecUpdateDatetime()));
String jsonString = jsonResult.toHtmlString();
streamWebService(response, jsonString);
return null;
}