public ItemInfo formatItem(Item item) throws Exception {
EntityManager em = JpaConnection.getInstance().getCurrentEntityManager();
SiteProfile siteProfile = contentBean.getContentSessionBean().getSiteProfile();
SiteCurrency siteCurrency = contentBean.getContentSessionBean().getSiteCurrency();
Item master = item;
if (item.getItemTypeCd().equals(Constants.ITEM_TYPE_SKU)) {
master = item.getItemSkuParent();
}
Long defaultSiteCurrencyClassId = contentBean.getContentSessionBean().getSiteDomain().getSite().getSiteCurrencyClassDefault().getSiteCurrencyClassId();
ItemInfo itemInfo = new ItemInfo();
ItemLanguage masterLanguageDefault = master.getItemLanguage();
itemInfo.setItemShortDesc(masterLanguageDefault.getItemShortDesc());
itemInfo.setItemDesc(masterLanguageDefault.getItemDesc());
if (Format.isNullOrEmpty(masterLanguageDefault.getPageTitle())) {
itemInfo.setPageTitle(masterLanguageDefault.getItemShortDesc());
}
else {
itemInfo.setPageTitle(masterLanguageDefault.getPageTitle());
}
if (Format.isNullOrEmpty(masterLanguageDefault.getMetaKeywords())) {
itemInfo.setMetaKeywords(masterLanguageDefault.getItemShortDesc());
}
else {
itemInfo.setMetaKeywords(masterLanguageDefault.getMetaKeywords());
}
if (Format.isNullOrEmpty(masterLanguageDefault.getMetaDescription())) {
itemInfo.setMetaDescription(masterLanguageDefault.getItemShortDesc());
}
else {
itemInfo.setMetaDescription(masterLanguageDefault.getMetaDescription());
}
itemInfo.setItemDefaultImageUrl(null);
if (masterLanguageDefault.getImage() != null) {
String imageUrl = imageUrlPrefix + "?type=I&imageId=" + masterLanguageDefault.getImage().getImageId();
itemInfo.setItemDefaultImageUrl(imageUrl);
}
Vector<String> itemImages = new Vector<String>();
Iterator<?> iterator = masterLanguageDefault.getImages().iterator();
while (iterator.hasNext()) {
ItemImage image = (ItemImage) iterator.next();
String imageUrl = imageUrlPrefix + "?type=I&imageId=" + image.getImageId();
itemImages.add(imageUrl);
}
itemInfo.setItemImageUrls(itemImages);
if (!contentBean.getContentSessionKey().isSiteProfileClassDefault()) {
ItemLanguage masterLanguage = master.getItemLanguage();
for (ItemLanguage language : master.getItemLanguages()) {
if (language.getSiteProfileClass().getSiteProfileClassId().equals(siteProfile.getSiteProfileClass().getSiteProfileClassId())) {
masterLanguage = language;
break;
}
}
if (masterLanguage != null) {
if (masterLanguage.getItemShortDesc() != null) {
itemInfo.setItemShortDesc(masterLanguage.getItemShortDesc());
}
if (masterLanguage.getItemDesc() != null) {
itemInfo.setItemDesc(masterLanguage.getItemDesc());
}
if (masterLanguage.getPageTitle() != null) {
itemInfo.setPageTitle(masterLanguage.getPageTitle());
}
if (masterLanguage.getMetaKeywords() != null) {
itemInfo.setMetaKeywords(masterLanguage.getMetaKeywords());
}
if (masterLanguage.getMetaDescription() != null) {
itemInfo.setMetaDescription(masterLanguage.getMetaDescription());
}
if (masterLanguage.getItemImageOverride().equals(String.valueOf(Constants.VALUE_YES))) {
String url = null;
itemInfo.setItemDefaultImageUrl(null);
if (masterLanguage.getImage() != null) {
url = "?type=I&imageId=" + masterLanguage.getImage().getImageId();
itemInfo.setItemDefaultImageUrl(imageUrlPrefix + url);
}
itemImages = new Vector<String>();
Iterator<?> images = masterLanguage.getImages().iterator();
while (images.hasNext()) {
ItemImage image = (ItemImage) images.next();
url = "?type=I&imageId=" + image.getImageId();
itemImages.add(imageUrlPrefix + url);
}
itemInfo.setItemImageUrls(itemImages);
}
}
}
itemInfo.setItemNaturalKey(item.getItemNaturalKey());
itemInfo.setItemId(item.getItemId().toString());
itemInfo.setItemNum(item.getItemNum());
itemInfo.setItemUpcCd(item.getItemUpcCd());
itemInfo.setItemSkuCd(item.getItemSkuCd());
itemInfo.setItemTypeCd(item.getItemTypeCd());
itemInfo.setItemPrice(formatter.formatCurrency(getItemPrice(item)));
itemInfo.setItemSpecPrice("");
itemInfo.setSpecial(false);
if (ItemDAO.isSpecialOn(item, defaultSiteCurrencyClassId)) {
itemInfo.setSpecial(true);
itemInfo.setItemSpecPrice(formatter.formatCurrency(getItemSpecPrice(item)));
itemInfo.setItemSpecPublishOn(formatter.formatDatetime(item.getItemSpecPrice().getItemPricePublishOn()));
itemInfo.setItemSpecExpireOn(formatter.formatDatetime(item.getItemSpecPrice().getItemPricePublishOn()));
}
itemInfo.setItemPriceRange(false);
if (item.getItemTypeCd().equals(Constants.ITEM_TYPE_TEMPLATE)) {
float itemPriceFrom = Integer.MAX_VALUE;
float itemPriceTo = 0;
for (Item child : item.getItemSkus()) {
float price = getItemPrice(child);
if (ItemDAO.isSpecialOn(child, defaultSiteCurrencyClassId)) {
price = getItemSpecPrice(child);
}
if (price < itemPriceFrom) {
itemPriceFrom = price;
}
if (price > itemPriceTo) {
itemPriceTo = price;
}
}
if (itemPriceFrom != Integer.MAX_VALUE) {
if (itemPriceTo != itemPriceFrom) {
itemInfo.setItemPriceRange(true);
itemInfo.setItemPriceFrom(formatter.formatCurrency(itemPriceFrom));
itemInfo.setItemPriceTo(formatter.formatCurrency(itemPriceTo));
}
}
}
if (item.getItemTypeCd().equals(Constants.ITEM_TYPE_RECOMMAND_BUNDLE)) {
float itemPrice = 0;
float itemSpecPrice = 0;
boolean special = false;
for (Item child : item.getChildren()) {
float price = getItemPrice(child);
float specPrice = price;
if (ItemDAO.isSpecialOn(child, defaultSiteCurrencyClassId)) {
special = true;
specPrice = getItemSpecPrice(child);
}
itemPrice += price;
itemSpecPrice += specPrice;
}
itemInfo.setItemPrice(formatter.formatCurrency(itemPrice));
itemInfo.setItemSpecPrice("");
itemInfo.setItemSpecPublishOn("");
itemInfo.setItemSpecExpireOn("");
if (special) {
itemInfo.setItemSpecPrice(formatter.formatCurrency(itemSpecPrice));
}
}
Vector<ItemTierPriceInfo> itemTierPrices = new Vector<ItemTierPriceInfo>();
if (item.getItemTypeCd().equals(Constants.ITEM_TYPE_TEMPLATE)) {
for (Item child : item.getItemSkus()) {
for (ItemTierPrice itemTierPrice : child.getItemTierPrices()) {
if (!ItemTierPriceDAO.isApplicable(itemTierPrice, getCustomer())) {
continue;
}
ItemTierPriceInfo itemTierPriceInfo = new ItemTierPriceInfo();
itemTierPriceInfo.setItemTierQty(formatter.formatNumber(itemTierPrice.getItemTierQty().intValue()));
itemTierPriceInfo.setItemTierPrice(formatter.formatCurrency(itemTierPrice.getItemTierPriceCurrency().getItemPrice()));
if (!siteCurrency.getSiteCurrencyClass().getSiteCurrencyClassId().equals(defaultSiteCurrencyClassId)) {
boolean found = false;
for (ItemTierPriceCurrency currency : itemTierPrice.getItemTierPriceCurrencies()) {
if (currency.getSiteCurrencyClass().getSiteCurrencyClassId().equals(siteCurrency.getSiteCurrencyClass().getSiteCurrencyClassId())) {
if (currency.getItemPrice() != null) {
itemTierPriceInfo.setItemTierPrice(formatter.formatCurrency(currency.getItemPrice()));
found = true;
}
break;
}
}
if (!found) {
itemTierPriceInfo.setItemTierPrice(formatter.formatCurrency(currencyConverter.convert(itemTierPrice.getItemTierPriceCurrency().getItemPrice())));
}
}
boolean exist = false;
for (int i = 0; i < itemTierPrices.size(); i++) {
ItemTierPriceInfo price = itemTierPrices.elementAt(i);
if (price.getItemTierQty().equals(itemTierPriceInfo.getItemTierQty()) &&
price.getItemTierPrice().equals(itemTierPriceInfo.getItemTierPrice())) {
exist = true;
break;
}
}
if (!exist) {
int index = -1;
for (int i = 0; i < itemTierPrices.size(); i++) {
ItemTierPriceInfo price = itemTierPrices.elementAt(i);
if (price.getItemTierQty().equals(itemTierPriceInfo.getItemTierQty())) {
index = i;
break;
}
if (Integer.valueOf(price.getItemTierQty()) > Integer.valueOf(itemTierPriceInfo.getItemTierQty())) {
index = i;
break;
}
}
if (index == -1) {
itemTierPrices.add(itemTierPriceInfo);
}
else {
itemTierPrices.add(index, itemTierPriceInfo);
}
}
}
}
}
else {
for (ItemTierPrice itemTierPrice : item.getItemTierPrices()) {
if (!ItemTierPriceDAO.isApplicable(itemTierPrice, getCustomer())) {
continue;
}
ItemTierPriceInfo itemTierPriceInfo = new ItemTierPriceInfo();
itemTierPriceInfo.setItemTierQty(formatter.formatNumber(itemTierPrice.getItemTierQty().intValue()));
itemTierPriceInfo.setItemTierPrice(formatter.formatCurrency(itemTierPrice.getItemTierPriceCurrency().getItemPrice()));
if (!siteCurrency.getSiteCurrencyClass().getSiteCurrencyClassId().equals(defaultSiteCurrencyClassId)) {
boolean found = false;
for (ItemTierPriceCurrency currency : itemTierPrice.getItemTierPriceCurrencies()) {
if (currency.getSiteCurrencyClass().getSiteCurrencyClassId().equals(siteCurrency.getSiteCurrencyClass().getSiteCurrencyClassId())) {
if (currency.getItemPrice() != null) {
itemTierPriceInfo.setItemTierPrice(formatter.formatCurrency(currency.getItemPrice()));
found = true;
}
break;