Site site = SiteDAO.load(siteId);
if (site == null) {
throw new ItemSaveException(resources.getMessage("IE.ERROR.E109", "site", "siteId", siteId));
}
SiteProfileClass siteProfileClassDefault = site.getSiteProfileClassDefault();
SiteCurrencyClass siteCurrencyClassDefault = site.getSiteCurrencyClassDefault();
com.jada.jpa.entity.Item item = null;
if (itemId != null) {
item = ItemDAO.load(siteId, itemId);
if (item == null) {
throw new ItemSaveException(resources.getMessage("IE.ERROR.E109", "item", "itemId", itemId));
}
}
else if (itemSkuCd != null) {
item = ItemDAO.loadBySku(siteId, itemSkuCd);
}
if (item == null) {
isNew = true;
item = new com.jada.jpa.entity.Item();
item.setItemUpcCd(itemSkuCd);
item.setItemQty(0);
item.setItemTypeCd(Constants.ITEM_TYPE_REGULAR);
item.setItemBookedQty(0);
item.setItemExpireOn(Format.HIGHDATE);
item.setItemPublishOn(new Date());
item.setItemHitCounter(0);
item.setItemRating(Float.valueOf(0));
item.setItemRatingCount(0);
item.setSeqNum(0);
item.setRecCreateBy(Constants.USERNAME_IMPORT);
item.setRecCreateDatetime(new Date());
item.setSite(site);
}
if (isNew) {
if (isSimple) {
boolean found = false;
for (ItemLanguage itemLanguage : this.getItemLanguages()) {
if (itemLanguage.getSiteProfileClassId() != null) {
if (itemLanguage.getSiteProfileClassId().equals(siteProfileClassDefault.getSiteProfileClassId())) {
found = true;
break;
}
}
else {
if (itemLanguage.getSiteProfileClassName().equals(siteProfileClassDefault.getSiteProfileClassName())) {
found = true;
break;
}
}
}
if (!found) {
throw new ItemSaveException(resources.getMessage("IE.ERROR.E111", siteProfileClassDefault.getSiteProfileClassName()));
}
found = false;
for (ItemPriceCurrency itemPriceCurrency : this.getItemPriceCurrencies()) {
if (itemPriceCurrency.getSiteCurrencyClassId() != null) {
if (itemPriceCurrency.getSiteCurrencyClassId().equals(siteCurrencyClassDefault.getSiteCurrencyClassId())) {
found = true;
break;
}
}
else {
if (itemPriceCurrency.getSiteCurrencyClassName().equals(siteCurrencyClassDefault.getSiteCurrencyClassName())) {
found = true;
break;
}
}
}
if (!found) {
throw new ItemSaveException(resources.getMessage("IE.ERROR.E112", siteCurrencyClassDefault.getSiteCurrencyClassName()));
}
}
}
item.setItemNum(itemNum);
item.setItemSkuCd(itemSkuCd);
item.setItemNaturalKey(Utility.encode(itemSkuCd));
item.setItemSellable(itemSellable);
item.setItemCost(itemCost);
if (itemUpcCd != null) {
item.setItemUpcCd(itemUpcCd);
}
if (itemTypeCd != null) {
item.setItemTypeCd(itemTypeCd);
}
if (itemPublishOn != null) {
item.setItemPublishOn(itemPublishOn);
}
if (itemExpireOn != null) {
item.setItemExpireOn(itemExpireOn);
}
if (itemHitCounter != null) {
item.setItemHitCounter(itemHitCounter);
}
if (itemRating != null) {
item.setItemRating(itemRating);
}
if (itemRatingCount != null) {
item.setItemRatingCount(itemRatingCount);
}
if (itemQty != null) {
item.setItemQty(itemQty);
}
if (itemBookedQty != null) {
item.setItemBookedQty(itemBookedQty);
}
item.setPublished(published);
item.setRecUpdateBy(Constants.USERNAME_IMPORT);
item.setRecUpdateDatetime(new Date());
for (ItemLanguage itemLanguageImport : this.getItemLanguages()) {
com.jada.jpa.entity.ItemLanguage itemLanguage = null;
boolean found = false;
for (com.jada.jpa.entity.ItemLanguage language : item.getItemLanguages()) {
if (itemLanguageImport.getSiteProfileClassId() != null) {
if (language.getSiteProfileClass().getSiteProfileClassId().equals(itemLanguageImport.getSiteProfileClassId())) {
itemLanguage = language;
found = true;
break;
}
}
else {
if (language.getSiteProfileClass().getSiteProfileClassName().equals(itemLanguageImport.getSiteProfileClassName())) {
itemLanguage = language;
found = true;
break;
}
}
}
if (!found) {
itemLanguage = new com.jada.jpa.entity.ItemLanguage();
itemLanguage.setItemImageOverride(String.valueOf(Constants.VALUE_NO));
itemLanguage.setRecCreateBy(Constants.USERNAME_IMPORT);
itemLanguage.setRecCreateDatetime(new Date());
SiteProfileClass siteProfileClass = getSiteProfileClass(siteId, itemLanguageImport.getSiteProfileClassId(), itemLanguageImport.getSiteProfileClassName());
if (siteProfileClass == null) {
throw new ItemSaveException(resources.getMessage("IE.ERROR.E109",
"siteProfileClass",
"siteProfileClassId and siteProfileClassName",
itemLanguageImport.getSiteProfileClassId().toString() + " and " + itemLanguageImport.getSiteProfileClassName()));
}
itemLanguage.setSiteProfileClass(siteProfileClass);
itemLanguage.setItem(item);
if (itemLanguage.getSiteProfileClass().getSiteProfileClassId().equals(siteProfileClassDefault.getSiteProfileClassId())) {
item.setItemLanguage(itemLanguage);
}
}
itemLanguage.setItemShortDesc(itemLanguageImport.getItemShortDesc());
itemLanguage.setItemDesc(itemLanguageImport.getItemDesc());
itemLanguage.setPageTitle(itemLanguageImport.getPageTitle());
itemLanguage.setMetaKeywords(itemLanguageImport.getMetaKeywords());
itemLanguage.setMetaDescription(itemLanguageImport.getMetaDescription());
itemLanguage.setItemImageOverride(itemLanguageImport.getItemImageOverride());
for (com.jada.jpa.entity.ItemImage image : itemLanguage.getImages()) {
em.remove(image);
}
itemLanguage.getImages().clear();
for (ItemImage imageImport : itemLanguageImport.getImages()) {
com.jada.jpa.entity.ItemImage itemImage = new com.jada.jpa.entity.ItemImage();
itemImage = new com.jada.jpa.entity.ItemImage();
itemImage.setContentType("image/jpeg");
itemImage.setImageName("");
itemImage.setImageHeight(-1);
itemImage.setImageWidth(-1);
itemImage.setRecCreateBy(Constants.USERNAME_IMPORT);
itemImage.setRecCreateDatetime(new Date());
itemLanguage.setImage(itemImage);
itemImage.setRecUpdateBy(Constants.USERNAME_IMPORT);
itemImage.setRecUpdateDatetime(new Date());
itemImage.setImageValue(imageImport.getImageValue());
em.persist(itemImage);
if (imageImport.getDefaultImage().equals(String.valueOf(Constants.VALUE_YES))) {
itemLanguage.setImage(itemImage);
}
}
itemLanguage.setRecUpdateBy(Constants.USERNAME_IMPORT);
itemLanguage.setRecUpdateDatetime(new Date());
em.persist(itemLanguage);
}
for (ItemPriceCurrency itemPriceCurrencyImport : this.getItemPriceCurrencies()) {
com.jada.jpa.entity.ItemPriceCurrency itemPriceCurrency = null;
boolean found = false;
for (com.jada.jpa.entity.ItemPriceCurrency currency : item.getItemPriceCurrencies()) {
if (currency.getItemPriceTypeCode() != itemPriceCurrencyImport.getItemPriceTypeCode()) {
continue;
}
if (itemPriceCurrencyImport.getSiteCurrencyClassId() != null) {
if (currency.getSiteCurrencyClass().getSiteCurrencyClassId().equals(itemPriceCurrencyImport.getSiteCurrencyClassId())) {
itemPriceCurrency = currency;
found = true;
break;
}
}
else {
if (currency.getSiteCurrencyClass().getSiteCurrencyClassName().equals(itemPriceCurrencyImport.getSiteCurrencyClassName())) {
itemPriceCurrency = currency;
found = true;
break;
}
}
}
if (!found) {
itemPriceCurrency = new com.jada.jpa.entity.ItemPriceCurrency();
itemPriceCurrency.setItemPriceTypeCode(itemPriceCurrencyImport.getItemPriceTypeCode());
itemPriceCurrency.setRecCreateBy(Constants.USERNAME_IMPORT);
itemPriceCurrency.setRecCreateDatetime(new Date());
SiteCurrencyClass siteCurrencyClass = getSiteCurrencyClass(siteId, itemPriceCurrencyImport.getSiteCurrencyClassId(), itemPriceCurrencyImport.getSiteCurrencyClassName());
if (siteCurrencyClass == null) {
throw new ItemSaveException(resources.getMessage("IE.ERROR.E109",
"siteCurrencyClass",
"siteCurrencyClassId and siteCurrencyClassName",
itemPriceCurrencyImport.getSiteCurrencyClassId().toString() + " and " + itemPriceCurrencyImport.getSiteCurrencyClassName()));
}
itemPriceCurrency.setSiteCurrencyClass(siteCurrencyClass);
itemPriceCurrency.setItem(item);
if (itemPriceCurrency.getSiteCurrencyClass().getSiteCurrencyClassId().equals(siteCurrencyClassDefault.getSiteCurrencyClassId())) {
if (itemPriceCurrency.getItemPriceTypeCode() == Constants.ITEM_PRICE_TYPE_CODE_REGULAR) {
item.setItemPrice(itemPriceCurrency);
}
else {
item.setItemSpecPrice(itemPriceCurrency);
}
}
}
itemPriceCurrency.setItemPrice(itemPriceCurrencyImport.getItemPrice());
itemPriceCurrency.setItemPricePublishOn(itemPriceCurrencyImport.getItemPricePublishOn());
itemPriceCurrency.setItemPriceExpireOn(itemPriceCurrencyImport.getItemPriceExpireOn());
itemPriceCurrency.setRecUpdateBy(Constants.USERNAME_IMPORT);
itemPriceCurrency.setRecUpdateDatetime(new Date());
em.persist(itemPriceCurrency);
itemPriceCurrencyImport.setSiteCurrencyClassId(itemPriceCurrency.getSiteCurrencyClass().getSiteCurrencyClassId());
if (isNew) {
item.setItemPrice(itemPriceCurrency);
}
else {
if (itemPriceCurrency.getSiteCurrencyClass().getSiteCurrencyClassId().equals(siteCurrencyClassDefault.getSiteCurrencyClassId())) {
if (itemPriceCurrency.getItemPriceTypeCode() == Constants.ITEM_PRICE_TYPE_CODE_REGULAR) {
item.setItemPrice(itemPriceCurrency);
}
else {
item.setItemSpecPrice(itemPriceCurrency);
}
}
}
}
for (ItemPriceCurrency itemPriceCurrencyImport : this.getItemPriceCurrencies()) {
if (itemPriceCurrencyImport.getItemPriceTypeCode() != Constants.ITEM_PRICE_TYPE_CODE_REGULAR) {
continue;
}
// look for corresponding special price
boolean found = false;
for (ItemPriceCurrency itemSpecPriceCurrencyImport : this.getItemPriceCurrencies()) {
if (itemSpecPriceCurrencyImport.getItemPriceTypeCode() != Constants.ITEM_PRICE_TYPE_CODE_SPECIAL) {
continue;
}
if (itemPriceCurrencyImport.getSiteCurrencyClassId().equals(itemSpecPriceCurrencyImport.getSiteCurrencyClassId())) {
found = true;
break;
}
}
// if not found, ensure special price is removed.
if (!found) {
if (itemPriceCurrencyImport.getSiteCurrencyClassId().equals(siteCurrencyClassDefault.getSiteCurrencyClassId())) {
item.setItemSpecPrice(null);
}
for (com.jada.jpa.entity.ItemPriceCurrency itemPriceCurrency : item.getItemPriceCurrencies()) {
if (itemPriceCurrency.getItemPriceTypeCode() != Constants.ITEM_PRICE_TYPE_CODE_SPECIAL) {
continue;
}
if (!itemPriceCurrencyImport.getSiteCurrencyClassId().equals(siteCurrencyClassDefault.getSiteCurrencyClassId())) {
if (!itemPriceCurrency.getSiteCurrencyClass().getSiteCurrencyClassId().equals(itemPriceCurrencyImport.getSiteCurrencyClassId())) {
continue;
}
}
em.remove(itemPriceCurrency);
}
}
}
em.persist(item);
CategorySearchUtil.itemPriceSearchUpdate(item, site, adminBean);
CategorySearchUtil.itemDescSearchUpdate(item, site, adminBean);
ShippingType shippingType = null;
if (shippingTypeId != null) {
shippingType = ShippingTypeDAO.load(siteId, shippingTypeId);
}
else {
shippingType = ShippingTypeDAO.loadByName(siteId, shippingTypeName);
}
if (shippingType == null) {
throw new ItemSaveException(resources.getMessage("IE.ERROR.E109",
"shippingType",
"shippingTypeId and shippingTypeName",
shippingTypeId.toString() + " and " + shippingTypeName));
}
item.setShippingType(shippingType);
ProductClass productClass = null;
if (productClassId != null) {
productClass = ProductClassDAO.load(siteId, productClassId);
}
else {
productClass = ProductClassDAO.loadByName(siteId, productClassName);
}
if (productClass == null) {
throw new ItemSaveException(resources.getMessage("IE.ERROR.E109",
"productClass",
"productClassId and productClassName",
productClassId.toString() + " and " + productClassName));
}
item.setProductClass(productClass);
if (itemParentId != null || itemParentSkuCd != null) {
com.jada.jpa.entity.Item parent = null;
if (itemParentId != null) {
parent = ItemDAO.load(siteId, itemParentId);
}
else {
parent = ItemDAO.loadBySku(siteId, itemParentSkuCd);
}
if (parent == null) {
throw new ItemSaveException(resources.getMessage("IE.ERROR.E109",
"item",
"itemId and itemSkuCd",
itemParentId.toString() + " and " + itemParentSkuCd));
}
item.setItemSkuParent(parent);
}
item.getCategories().clear();
if (this.getCategories() != null) {
for (Category categoryImport : this.getCategories()) {
com.jada.jpa.entity.Category category = null;
if (categoryImport.getCatId() != null) {
category = CategoryDAO.load(siteId, categoryImport.getCatId());
}
else {
category = CategoryDAO.loadByShortTitle(siteId, categoryImport.getCatShortTitle());
}
if (category == null) {
throw new ItemSaveException(resources.getMessage("IE.ERROR.E109",
"category",
"catId and catShortTitle",
categoryImport.getCatId().toString() + " and " + categoryImport.getCatShortTitle()));
}
item.getCategories().add(category);
}
}
item.getItemsRelated().clear();
if (this.getItemsRelated() != null) {
for (Item i : this.getItemsRelated()) {
com.jada.jpa.entity.Item child = null;
if (i.getItemId() != null) {
child = ItemDAO.load(siteId, i.getItemId());
}
else {
child = ItemDAO.loadBySku(siteId, i.getItemSkuCd());
}
if (child == null) {
throw new ItemSaveException(resources.getMessage("IE.ERROR.E109",
"item",
"itemId and itemSkuCd",
i.getItemId().toString() + " and " + i.getItemSkuCd()));
}
item.getItemsRelated().add(child);
}
}
item.getItemsUpSell().clear();
if (this.getItemsUpSell() != null) {
for (Item i : this.getItemsUpSell()) {
com.jada.jpa.entity.Item child = null;
if (i.getItemId() != null) {
child = ItemDAO.load(siteId, i.getItemId());
}
else {
child = ItemDAO.loadBySku(siteId, i.getItemSkuCd());
}
if (child == null) {
throw new ItemSaveException(resources.getMessage("IE.ERROR.E109",
"item",
"itemId and itemSkuCd",
i.getItemId().toString() + " and " + i.getItemSkuCd()));
}
item.getItemsUpSell().add(child);
}
}
item.getItemsCrossSell().clear();
if (this.getItemsCrossSell() != null) {
for (Item i : this.getItemsCrossSell()) {
com.jada.jpa.entity.Item child = null;
if (i.getItemId() != null) {
child = ItemDAO.load(siteId, i.getItemId());
}
else {
child = ItemDAO.loadBySku(siteId, i.getItemSkuCd());
}
if (child == null) {
throw new ItemSaveException(resources.getMessage("IE.ERROR.E109",
"item",
"itemId and itemSkuCd",
i.getItemId().toString() + " and " + i.getItemSkuCd()));
}
item.getItemsCrossSell().add(child);
}
}
if (this.getItemTierPrices() != null) {
for (com.jada.xml.ie.ItemTierPrice itemTierPriceImport : this.getItemTierPrices()) {
com.jada.jpa.entity.ItemTierPrice itemTierPrice = null;
boolean foundTier = false;
for (com.jada.jpa.entity.ItemTierPrice price : item.getItemTierPrices()) {
if (!itemTierPriceImport.getItemTierQty().equals(price.getItemTierQty())) {
continue;
}
if (price.getCustomerClass() == null) {
if (itemTierPriceImport.getCustClassId() == null && itemTierPriceImport.getCustClassName() == null) {
itemTierPrice = price;
foundTier = true;
break;
}
}
else {
if (itemTierPriceImport.getCustClassId() == null) {
if (itemTierPriceImport.getCustClassName().equals(price.getCustomerClass().getCustClassName())) {
itemTierPrice = price;
foundTier = true;
break;
}
}
else {
if (itemTierPriceImport.getCustClassId().equals(price.getCustomerClass().getCustClassId())) {
itemTierPrice = price;
foundTier = true;
break;
}
}
}
}
if (!foundTier) {
if (!defaultCurrency && !defaultProfile) {
throw new ItemSaveException("Item tier price does not exist in base record");
}
itemTierPrice = new com.jada.jpa.entity.ItemTierPrice();
itemTierPrice.setRecCreateBy(Constants.USERNAME_IMPORT);
itemTierPrice.setRecCreateDatetime(new Date());
itemTierPrice.setItem(item);
}
itemTierPrice.setItemTierQty(itemTierPriceImport.getItemTierQty());
itemTierPrice.setItemTierPricePublishOn(itemTierPriceImport.getItemTierPricePublishOn());
itemTierPrice.setItemTierPriceExpireOn(itemTierPriceImport.getItemTierPriceExpireOn());
itemTierPrice.setRecUpdateBy(Constants.USERNAME_IMPORT);
itemTierPrice.setRecUpdateDatetime(new Date());
for (ItemTierPriceCurrency itemTierPriceCurrencyImport : itemTierPriceImport.getItemTierPriceCurrencies()) {
com.jada.jpa.entity.ItemTierPriceCurrency itemTierPriceCurrency = null;
boolean found = false;
for (com.jada.jpa.entity.ItemTierPriceCurrency currency : itemTierPrice.getItemTierPriceCurrencies()) {
if (itemTierPriceCurrencyImport.getSiteCurrencyClassId() != null) {
if (currency.getSiteCurrencyClass().getSiteCurrencyClassId().equals(itemTierPriceCurrencyImport.getSiteCurrencyClassId())) {
itemTierPriceCurrency = currency;
found = true;
break;
}
}
else {
if (currency.getSiteCurrencyClass().getSiteCurrencyClassId().equals(itemTierPriceCurrencyImport.getSiteCurrencyClassName())) {
itemTierPriceCurrency = currency;
found = true;
break;
}
}
}
if (itemTierPriceCurrencyImport.getItemPrice() == null) {
if (found) {
em.remove(itemTierPriceCurrency);
itemTierPrice.getItemTierPriceCurrencies().remove(itemTierPriceCurrency);
}
}
else {
if (!found) {
itemTierPriceCurrency = new com.jada.jpa.entity.ItemTierPriceCurrency();
itemTierPriceCurrency.setRecCreateBy(Constants.USERNAME_IMPORT);
itemTierPriceCurrency.setRecCreateDatetime(new Date());
SiteCurrencyClass siteCurrencyClass = getSiteCurrencyClass(siteId, itemTierPriceCurrencyImport.getSiteCurrencyClassId(),
itemTierPriceCurrencyImport.getSiteCurrencyClassName());
itemTierPriceCurrency.setSiteCurrencyClass(siteCurrencyClass);
itemTierPriceCurrency.setItemTierPrice(itemTierPrice);
if (siteCurrencyClass.getSiteCurrencyClassId().equals(siteCurrencyClassDefault.getSiteCurrencyClassId())) {
itemTierPrice.setItemTierPriceCurrency(itemTierPriceCurrency);
}
}
itemTierPriceCurrency.setItemPrice(itemTierPriceCurrencyImport.getItemPrice());
itemTierPriceCurrency.setRecUpdateBy(Constants.USERNAME_IMPORT);