}
}
String itemTierPriceId = form.getItemTierPriceId();
Item item = ItemDAO.load(site.getSiteId(), Format.getLong(form.getItemId()));
ItemTierPrice itemTierPrice = null;
ItemTierPriceCurrency itemTierPriceCurrency = null;
boolean exist = true;
if (!Format.isNullOrEmpty(itemTierPriceId)) {
itemTierPrice = ItemTierPriceDAO.load(site.getSiteId(), Format.getLong(itemTierPriceId));
itemTierPriceCurrency = itemTierPrice.getItemTierPriceCurrency();
}
else {
itemTierPrice = new ItemTierPrice();
itemTierPrice.setRecCreateBy(adminBean.getUser().getUserId());
itemTierPrice.setRecCreateDatetime(new Date(System.currentTimeMillis()));
itemTierPrice.setItem(item);
itemTierPriceCurrency = new ItemTierPriceCurrency();
exist = false;
itemTierPriceCurrency.setRecCreateBy(adminBean.getUser().getUserId());
itemTierPriceCurrency.setRecCreateDatetime(new Date(System.currentTimeMillis()));
itemTierPrice.setItemTierPriceCurrency(itemTierPriceCurrency);
itemTierPrice.getItemTierPriceCurrencies().add(itemTierPriceCurrency);
SiteCurrencyClass siteCurrencyClass = SiteCurrencyClassDAO.load(form.getSiteCurrencyClassDefaultId());
itemTierPriceCurrency.setSiteCurrencyClass(siteCurrencyClass);
}
if (form.isSiteCurrencyClassDefault()) {
itemTierPriceCurrency.setItemPrice(Format.getFloat(form.getItemTierPrice()));
itemTierPriceCurrency.setRecUpdateBy(adminBean.getUser().getUserId());
itemTierPriceCurrency.setRecUpdateDatetime(new Date(System.currentTimeMillis()));
if (!exist) {
em.persist(itemTierPriceCurrency);
}
itemTierPrice.setItemTierQty(Format.getIntObj(form.getItemTierQty()));
if (!Format.isNullOrEmpty(form.getItemTierPricePublishOn())) {
itemTierPrice.setItemTierPricePublishOn(Format.getDate(form.getItemTierPricePublishOn()));
}
else {
itemTierPrice.setItemTierPricePublishOn(null);
}
if (!Format.isNullOrEmpty(form.getItemTierPriceExpireOn())) {
itemTierPrice.setItemTierPriceExpireOn(Format.getDate(form.getItemTierPriceExpireOn()));
}
else {
itemTierPrice.setItemTierPriceExpireOn(null);
}
itemTierPrice.setRecUpdateBy(adminBean.getUser().getUserId());
itemTierPrice.setRecUpdateDatetime(new Date(System.currentTimeMillis()));
if (Format.isNullOrEmpty(form.getCustClassId())) {
itemTierPrice.setCustomerClass(null);
}
else {
CustomerClass customerClass = CustomerClassDAO.load(site.getSiteId(), Format.getLong(form.getCustClassId()));
itemTierPrice.setCustomerClass(customerClass);
}
em.persist(itemTierPrice);
}
else {
itemTierPriceCurrency = null;
SiteCurrencyClass siteCurrencyClass = SiteCurrencyClassDAO.load(form.getSiteCurrencyClassId());
for (ItemTierPriceCurrency priceCurrency : itemTierPrice.getItemTierPriceCurrencies()) {
if (priceCurrency.getSiteCurrencyClass().getSiteCurrencyClassId().equals(siteCurrencyClass.getSiteCurrencyClassId())) {
itemTierPriceCurrency = priceCurrency;
break;
}
}
exist = true;
if (itemTierPriceCurrency == null) {
itemTierPriceCurrency = new ItemTierPriceCurrency();
exist = false;
itemTierPriceCurrency.setRecCreateBy(adminBean.getUser().getUserId());
itemTierPriceCurrency.setRecCreateDatetime(new Date(System.currentTimeMillis()));
itemTierPriceCurrency.setItemTierPrice(itemTierPrice);
itemTierPriceCurrency.setSiteCurrencyClass(siteCurrencyClass);
}
if (form.isItemTierPriceOverride()) {
itemTierPriceCurrency.setItemPrice(Format.getFloat(form.getItemTierPrice()));
}
else {
itemTierPriceCurrency.setItemPrice(null);
}
itemTierPriceCurrency.setRecUpdateBy(adminBean.getUser().getUserId());
itemTierPriceCurrency.setRecUpdateDatetime(new Date(System.currentTimeMillis()));
if (!exist) {
em.persist(itemTierPriceCurrency);
}
}
item.setRecUpdateBy(adminBean.getUser().getUserId());
item.setRecUpdateDatetime(new Date(System.currentTimeMillis()));
em.persist(item);
jsonResult = new JSONEscapeObject();
jsonResult.put("status", Constants.WEBSERVICE_STATUS_SUCCESS);
jsonResult.put("itemTierPriceId", itemTierPrice.getItemTierPriceId());
jsonResult.put("recUpdateBy", item.getRecUpdateBy());
jsonResult.put("recUpdateDatetime", Format.getFullDatetime(item.getRecUpdateDatetime()));
streamWebService(response, jsonResult.toHtmlString());
return null;
}