public CategoryInfo getCategory(String catNaturalKey, String topCatNaturalKey, int pageSize, int pageNavCount, int pageNum, String sortBy, ContentFilterBean contentFilterBeans[]) throws Exception {
CategoryInfo categoryInfo = new CategoryInfo();
EntityManager em = JpaConnection.getInstance().getCurrentEntityManager();
String key = Utility.reEncode(catNaturalKey);
String topKey = Utility.reEncode(topCatNaturalKey);
Category category = dataApi.getCategory(siteDomain.getSite().getSiteId(), key);
if (category == null) {
return null;
}
categoryInfo.setCatNaturalKey(catNaturalKey);
categoryInfo.setCatId(category.getCatId().toString());
categoryInfo.setTopCatId(null);
categoryInfo.setTopCatNaturalKey(topCatNaturalKey);
categoryInfo.setCatShortTitle(category.getCategoryLanguage().getCatShortTitle());
categoryInfo.setCatTitle(category.getCategoryLanguage().getCatTitle());
categoryInfo.setCatDesc(category.getCategoryLanguage().getCatDesc());
categoryInfo.setDataCount(getDataInCategoryCounts(category.getCatId()));
if (!contentBean.getContentSessionKey().isSiteProfileClassDefault()) {
for (CategoryLanguage language : category.getCategoryLanguages()) {
if (language.getSiteProfileClass().getSiteProfileClassId().equals(contentBean.getContentSessionKey().getSiteProfileClassId())) {
if (language.getCatShortTitle() != null) {
categoryInfo.setCatShortTitle(language.getCatShortTitle());
}
if (language.getCatTitle() != null) {
categoryInfo.setCatTitle(language.getCatTitle());
}
if (language.getCatDesc() != null) {
categoryInfo.setCatDesc(language.getCatDesc());
}
}
}
}
String categoryUrl = "/" + ApplicationGlobal.getContextPath() +
Constants.FRONTEND_URL_PREFIX +
"/" + contentBean.getContentSessionBean().getSiteDomain().getSiteDomainPrefix() +
"/" + contentBean.getContentSessionBean().getSiteProfile().getSiteProfileClass().getSiteProfileClassName() +
"/" + Constants.FRONTEND_URL_SECTION +
"/" +
topKey + // topCategory
"/" +
category.getCatNaturalKey(); // category
String filterUrl = "";
for (ContentFilterBean contentFilterBean : contentFilterBeans) {
filterUrl += "&filter=" + contentFilterBean.getCustomAttribId() + "," + contentFilterBean.getCustomAttribOptionId();
}
categoryInfo.setCategoryBasicUrl(categoryUrl);
categoryInfo.setFilterUrl(filterUrl);
categoryInfo.setSortBy(sortBy == null ? "" : sortBy);
categoryInfo.setCategoryUrl(categoryUrl + "?sortBy" + sortBy + filterUrl);
int itemStart = (pageNum - 1) * pageSize;
int itemEnd = itemStart + pageSize - 1;
int index = 0;
String value = sortBy;
if (value == null || value.length() == 0) {
value = String.valueOf(Constants.SECTION_SORT_DESC_ASC);
}
char sortByValue = value.charAt(0);
String filterCiteria = "";
Double minPrice = Double.MIN_VALUE * -1;
Double maxPrice = Double.MAX_VALUE;
for (ContentFilterBean contentFilterBean : contentFilterBeans) {
if (contentFilterBean.getSystemRecord() == Constants.VALUE_YES) {
CustomAttribute customAttribute = CustomAttributeDAO.load(siteDomain.getSite().getSiteId(), contentFilterBean.getCustomAttribId());
PriceRange priceRange = getAttributePriceRange(customAttribute, contentFilterBean.getCustomAttribOptionId());
minPrice = Double.valueOf(priceRange.getMinPrice());
maxPrice = Double.valueOf(priceRange.getMaxprice());
}
else {
String categoryAttributeSubquery = NamedQuery.getInstance().getQuery("category.attribute.subquery");
categoryAttributeSubquery = categoryAttributeSubquery.replaceFirst(":customAttribOptionId", contentFilterBean.getCustomAttribOptionId().toString());
filterCiteria += " " + categoryAttributeSubquery;
}
}
String sql = "";
if (siteDomain.getSite().getShareInventory() == Constants.VALUE_YES) {
sql = NamedQuery.getInstance().getQuery("category.item.language");
}
else {
sql = NamedQuery.getInstance().getQuery("category.item.language.noshare");
}
sql += " and category.catId = :catId ";
sql += " " + filterCiteria;
sql += " " + NamedQuery.getInstance().getQuery("category.item.language.suffix");
switch (sortByValue) {
case Constants.SECTION_SORT_DESC_ASC:
sql += " order by itemDescSearch.itemShortDesc";
break;
case Constants.SECTION_SORT_DESC_DSC:
sql += " order by itemDescSearch.itemShortDesc desc";
break;
case Constants.SECTION_SORT_PRICE_ASC:
sql += " order by sum(itemPriceSearch.itemPrice)";
break;
case Constants.SECTION_SORT_PRICE_DSC:
sql += " order by sum(itemPriceSearch.itemPrice) desc";
break;
}
Vector<DataInfo> dataVector = new Vector<DataInfo>();
Query query = em.createQuery(sql);
query.setParameter("siteId", siteDomain.getSite().getSiteId());
if (siteDomain.getSite().getShareInventory() != Constants.VALUE_YES) {
query.setParameter("siteDomainId", siteDomain.getSiteDomainId());
}
query.setParameter("catId", category.getCatId());
query.setParameter("siteProfileClassId", contentBean.getContentSessionKey().getSiteProfileClassId());
query.setParameter("siteCurrencyClassId", contentBean.getContentSessionKey().getSiteCurrencyClassId());
query.setParameter("minPrice", minPrice);
query.setParameter("maxPrice", maxPrice);
query.setParameter("exchangeRate", contentBean.getContentSessionBean().getSiteCurrency().getExchangeRate() - 1);
List<?> list = query.getResultList();
int totalCount = 0;
int itemSize = list.size();
totalCount += itemSize;
Iterator<?> sqlIterator = list.iterator();
while (sqlIterator.hasNext()) {
Long itemId = (Long) sqlIterator.next();
if (index >= itemStart && index <= itemEnd) {
Item item = ItemDAO.load(siteDomain.getSite().getSiteId(), itemId);
ItemInfo itemInfo = formatItem(item);
dataVector.add(itemInfo);
}
index++;
if (index > itemEnd) {
break;
}
}
sql = NamedQuery.getInstance().getQuery("category.content.language");
switch (sortByValue) {
case Constants.SECTION_SORT_DESC_ASC:
sql += " order by contentDescSearch.contentTitle";
break;
case Constants.SECTION_SORT_DESC_DSC:
sql += " order by contentDescSearch.contentTitle desc";
break;
case Constants.SECTION_SORT_PRICE_ASC:
sql += " order by contentDescSearch.contentTitle";
break;
case Constants.SECTION_SORT_PRICE_DSC:
sql += " order by contentDescSearch.contentTitle";
break;
}
query = em.createQuery(sql);
query.setParameter("siteId", siteDomain.getSite().getSiteId());
query.setParameter("catId", category.getCatId());
query.setParameter("siteProfileClassId", contentBean.getContentSessionKey().getSiteProfileClassId());
list = query.getResultList();
totalCount += list.size();
sqlIterator = list.iterator();
while (sqlIterator.hasNext()) {
Long contentId = (Long) sqlIterator.next();
if (index >= itemStart && index <= itemEnd) {
Content content = ContentDAO.load(siteDomain.getSite().getSiteId(), contentId);
ContentInfo contentInfo = formatContent(content);
dataVector.add(contentInfo);
}
index++;
if (index > itemEnd) {
break;
}
}
int midpoint = pageNavCount / 2;
int recordCount = totalCount;
int pageTotal = recordCount / pageSize;
if (recordCount % pageSize > 0) {
pageTotal++;
}
categoryInfo.setPageTotal(pageTotal);
categoryInfo.setPageNum(pageNum);
int pageStart = pageNum - midpoint;
if (pageStart < 1) {
pageStart = 1;
}
int pageEnd = pageStart + pageNavCount - 1;
if (pageEnd > pageTotal) {
pageEnd = pageTotal;
if (pageEnd - pageStart + 1 < pageNavCount) {
pageStart = pageEnd - pageNavCount + 1;
if (pageStart < 1) {
pageStart = 1;
}
}
}
categoryInfo.setPageStart(pageStart);
categoryInfo.setPageEnd(pageEnd);
Object categoryDatas[] = new Object[dataVector.size()];
dataVector.copyInto(categoryDatas);
categoryInfo.setCategoryDatas(categoryDatas);
/*
* Extract filtering information
*/
Vector<CategoryCustomAttributeInfo> categoryCustomAttributeVector = new Vector<CategoryCustomAttributeInfo>();
for (CustomAttribute customAttribute : category.getCustomAttributes()) {
boolean isFilterAttribute = false;
ContentFilterBean contentFilterBean = null;
for (ContentFilterBean bean : contentFilterBeans) {
if (bean.getCustomAttribId().equals(customAttribute.getCustomAttribId())) {
contentFilterBean = bean;