use of com.salesmanager.core.model.common.GenericEntityList in project shopizer by shopizer-ecommerce.
the class UserRepositoryImpl method listByCriteria.
@SuppressWarnings("unchecked")
@Override
public GenericEntityList<User> listByCriteria(Criteria criteria) throws ServiceException {
try {
StringBuilder req = new StringBuilder();
req.append("select distinct u from User as u left join fetch u.groups ug left join fetch u.defaultLanguage ud join fetch u.merchantStore um");
StringBuilder countBuilder = new StringBuilder();
countBuilder.append("select count(distinct u) from User as u join u.merchantStore um");
if (!StringUtils.isBlank(criteria.getStoreCode())) {
req.append(" where um.code=:storeCode");
countBuilder.append(" where um.code=:storeCode");
}
if (!StringUtils.isBlank(criteria.getCriteriaOrderByField())) {
req.append(" order by u." + criteria.getCriteriaOrderByField() + " " + criteria.getOrderBy().name().toLowerCase());
}
Query countQ = this.em.createQuery(countBuilder.toString());
String hql = req.toString();
Query q = this.em.createQuery(hql);
if (!StringUtils.isBlank(criteria.getSearch())) {
// TODO
} else {
if (criteria.getStoreCode() != null) {
countQ.setParameter("storeCode", criteria.getStoreCode());
q.setParameter("storeCode", criteria.getStoreCode());
}
}
Number count = (Number) countQ.getSingleResult();
@SuppressWarnings("rawtypes") GenericEntityList entityList = new GenericEntityList();
entityList.setTotalCount(count.intValue());
/**
* Configure pagination using setMaxResults and setFirstResult method
*/
q = RepositoryHelper.paginateQuery(q, count, entityList, criteria);
/* if(criteria.isLegacyPagination()) {
if (criteria.getMaxCount() > 0) {
q.setFirstResult(criteria.getStartIndex());
if (criteria.getMaxCount() < count.intValue()) {
q.setMaxResults(criteria.getMaxCount());
} else {
q.setMaxResults(count.intValue());
}
}
} else {
}*/
List<User> users = q.getResultList();
entityList.setList(users);
return entityList;
} catch (javax.persistence.NoResultException ers) {
} catch (Exception e) {
LOGGER.error(e.getMessage());
throw new ServiceException(e);
}
return null;
}
use of com.salesmanager.core.model.common.GenericEntityList in project shopizer by shopizer-ecommerce.
the class StoreFacadeImpl method getMerchantStoresByCriteria.
private ReadableMerchantStoreList getMerchantStoresByCriteria(MerchantStoreCriteria criteria, Language language) {
try {
GenericEntityList<MerchantStore> stores = Optional.ofNullable(merchantStoreService.getByCriteria(criteria)).orElseThrow(() -> new ResourceNotFoundException("Criteria did not match any store"));
ReadableMerchantStoreList storeList = new ReadableMerchantStoreList();
storeList.setData((List<ReadableMerchantStore>) stores.getList().stream().map(s -> convertMerchantStoreToReadableMerchantStore(language, s)).collect(Collectors.toList()));
storeList.setTotalPages(stores.getTotalPages());
storeList.setRecordsTotal(stores.getTotalCount());
storeList.setNumber(stores.getList().size());
return storeList;
} catch (ServiceException e) {
throw new ServiceRuntimeException(e);
}
}
use of com.salesmanager.core.model.common.GenericEntityList in project shopizer by shopizer-ecommerce.
the class RepositoryHelper method paginateQuery.
@SuppressWarnings("rawtypes")
public static Query paginateQuery(Query q, Number count, GenericEntityList entityList, Criteria criteria) {
if (entityList == null) {
entityList = new GenericEntityList();
}
if (criteria.isLegacyPagination()) {
if (criteria.getMaxCount() > 0) {
q.setFirstResult(criteria.getStartIndex());
q.setMaxResults(Math.min(criteria.getMaxCount(), count.intValue()));
}
} else {
// int firstResult = ((criteria.getStartPage()==0?criteria.getStartPage()+1:criteria.getStartPage()) - 1) * criteria.getPageSize();
int firstResult = ((criteria.getStartPage() == 0 ? 0 : criteria.getStartPage())) * criteria.getPageSize();
q.setFirstResult(firstResult);
q.setMaxResults(criteria.getPageSize());
int lastPageNumber = (count.intValue() / criteria.getPageSize()) + 1;
entityList.setTotalPages(lastPageNumber);
entityList.setTotalCount(count.intValue());
}
return q;
}
use of com.salesmanager.core.model.common.GenericEntityList in project shopizer by shopizer-ecommerce.
the class ProductRepositoryImpl method listByStore.
/**
* This query is used for filtering products based on criterias
*
* @param store
* @param first
* @param max
* @return
*/
@Override
public ProductList listByStore(MerchantStore store, Language language, ProductCriteria criteria) {
ProductList productList = new ProductList();
StringBuilder countBuilderSelect = new StringBuilder();
countBuilderSelect.append("select count(distinct p) from Product as p");
StringBuilder countBuilderWhere = new StringBuilder();
countBuilderWhere.append(" where p.merchantStore.id=:mId");
if (!CollectionUtils.isEmpty(criteria.getProductIds())) {
countBuilderWhere.append(" and p.id in (:pId)");
}
countBuilderSelect.append(" inner join p.descriptions pd");
if (criteria.getLanguage() != null && !criteria.getLanguage().equals("_all")) {
countBuilderWhere.append(" and pd.language.code=:lang");
}
if (!StringUtils.isBlank(criteria.getProductName())) {
countBuilderWhere.append(" and lower(pd.name) like:nm");
}
if (!CollectionUtils.isEmpty(criteria.getCategoryIds())) {
countBuilderSelect.append(" INNER JOIN p.categories categs");
countBuilderWhere.append(" and categs.id in (:cid)");
}
if (criteria.getManufacturerId() != null) {
countBuilderSelect.append(" INNER JOIN p.manufacturer manuf");
countBuilderWhere.append(" and manuf.id = :manufid");
}
if (!StringUtils.isBlank(criteria.getCode())) {
countBuilderWhere.append(" and lower(p.sku) like :sku");
}
// RENTAL
if (!StringUtils.isBlank(criteria.getStatus())) {
countBuilderWhere.append(" and p.rentalStatus = :status");
}
if (criteria.getOwnerId() != null) {
countBuilderSelect.append(" INNER JOIN p.owner owner");
countBuilderWhere.append(" and owner.id = :ownerid");
}
// attribute or option values
if (CollectionUtils.isNotEmpty(criteria.getAttributeCriteria()) || CollectionUtils.isNotEmpty(criteria.getOptionValueIds())) {
countBuilderSelect.append(" INNER JOIN p.attributes pattr");
countBuilderSelect.append(" INNER JOIN pattr.productOption po");
countBuilderSelect.append(" INNER JOIN pattr.productOptionValue pov ");
countBuilderSelect.append(" INNER JOIN pov.descriptions povd ");
if (CollectionUtils.isNotEmpty(criteria.getAttributeCriteria())) {
int count = 0;
for (AttributeCriteria attributeCriteria : criteria.getAttributeCriteria()) {
if (count == 0) {
countBuilderWhere.append(" and po.code =:").append(attributeCriteria.getAttributeCode());
countBuilderWhere.append(" and povd.description like :").append("val").append(count).append(attributeCriteria.getAttributeCode());
}
count++;
}
if (criteria.getLanguage() != null && !criteria.getLanguage().equals("_all")) {
countBuilderWhere.append(" and povd.language.code=:lang");
}
}
if (CollectionUtils.isNotEmpty(criteria.getOptionValueIds())) {
countBuilderWhere.append(" and pov.id in (:povid)");
}
}
if (criteria.getAvailable() != null) {
if (criteria.getAvailable()) {
countBuilderWhere.append(" and p.available=true and p.dateAvailable<=:dt");
} else {
countBuilderWhere.append(" and p.available=false or p.dateAvailable>:dt");
}
}
Query countQ = this.em.createQuery(countBuilderSelect.toString() + countBuilderWhere.toString());
countQ.setParameter("mId", store.getId());
if (!CollectionUtils.isEmpty(criteria.getCategoryIds())) {
countQ.setParameter("cid", criteria.getCategoryIds());
}
if (CollectionUtils.isNotEmpty(criteria.getOptionValueIds())) {
countQ.setParameter("povid", criteria.getOptionValueIds());
}
if (criteria.getAvailable() != null) {
countQ.setParameter("dt", new Date());
}
if (!StringUtils.isBlank(criteria.getCode())) {
countQ.setParameter("sku", new StringBuilder().append("%").append(criteria.getCode().toLowerCase()).append("%").toString());
}
if (criteria.getManufacturerId() != null) {
countQ.setParameter("manufid", criteria.getManufacturerId());
}
if (!CollectionUtils.isEmpty(criteria.getAttributeCriteria())) {
int count = 0;
for (AttributeCriteria attributeCriteria : criteria.getAttributeCriteria()) {
countQ.setParameter(attributeCriteria.getAttributeCode(), attributeCriteria.getAttributeCode());
countQ.setParameter("val" + count + attributeCriteria.getAttributeCode(), "%" + attributeCriteria.getAttributeValue() + "%");
count++;
}
}
if (criteria.getLanguage() != null && !criteria.getLanguage().equals("_all")) {
countQ.setParameter("lang", language.getCode());
}
if (!StringUtils.isBlank(criteria.getProductName())) {
countQ.setParameter("nm", new StringBuilder().append("%").append(criteria.getProductName().toLowerCase()).append("%").toString());
}
if (!CollectionUtils.isEmpty(criteria.getProductIds())) {
countQ.setParameter("pId", criteria.getProductIds());
}
// RENTAL
if (!StringUtils.isBlank(criteria.getStatus())) {
countQ.setParameter("status", criteria.getStatus());
}
if (criteria.getOwnerId() != null) {
countQ.setParameter("ownerid", criteria.getOwnerId());
}
Number count = (Number) countQ.getSingleResult();
productList.setTotalCount(count.intValue());
if (count.intValue() == 0)
return productList;
StringBuilder qs = new StringBuilder();
qs.append("select distinct p from Product as p ");
qs.append("join fetch p.merchantStore merch ");
qs.append("join fetch p.availabilities pa ");
qs.append("left join fetch pa.prices pap ");
qs.append("join fetch p.descriptions pd ");
qs.append("left join fetch p.categories categs ");
qs.append("left join fetch categs.descriptions cd ");
// images
qs.append("left join fetch p.images images ");
// other lefts
qs.append("left join fetch p.manufacturer manuf ");
qs.append("left join fetch manuf.descriptions manufd ");
qs.append("left join fetch p.type type ");
qs.append("left join fetch p.taxClass tx ");
// RENTAL
qs.append("left join fetch p.owner owner ");
// attributes
if (!CollectionUtils.isEmpty(criteria.getAttributeCriteria())) {
qs.append(" inner join p.attributes pattr");
qs.append(" inner join pattr.productOption po");
qs.append(" inner join po.descriptions pod");
qs.append(" inner join pattr.productOptionValue pov ");
qs.append(" inner join pov.descriptions povd");
} else {
qs.append(" left join fetch p.attributes pattr");
qs.append(" left join fetch pattr.productOption po");
qs.append(" left join fetch po.descriptions pod");
qs.append(" left join fetch pattr.productOptionValue pov");
qs.append(" left join fetch pov.descriptions povd");
}
qs.append(" left join fetch p.relationships pr");
qs.append(" where merch.id=:mId");
if (criteria.getLanguage() != null && !criteria.getLanguage().equals("_all")) {
qs.append(" and pd.language.code=:lang");
}
if (!CollectionUtils.isEmpty(criteria.getProductIds())) {
qs.append(" and p.id in (:pId)");
}
if (!CollectionUtils.isEmpty(criteria.getCategoryIds())) {
qs.append(" and categs.id in (:cid)");
}
if (criteria.getManufacturerId() != null) {
qs.append(" and manuf.id = :manufid");
}
if (criteria.getAvailable() != null) {
if (criteria.getAvailable()) {
qs.append(" and p.available=true and p.dateAvailable<=:dt");
} else {
qs.append(" and p.available=false and p.dateAvailable>:dt");
}
}
if (!StringUtils.isBlank(criteria.getProductName())) {
qs.append(" and lower(pd.name) like :nm");
}
if (!StringUtils.isBlank(criteria.getCode())) {
qs.append(" and lower(p.sku) like :sku");
}
// RENTAL
if (!StringUtils.isBlank(criteria.getStatus())) {
qs.append(" and p.rentalStatus = :status");
}
if (criteria.getOwnerId() != null) {
qs.append(" and owner.id = :ownerid");
}
if (!CollectionUtils.isEmpty(criteria.getAttributeCriteria())) {
int cnt = 0;
for (AttributeCriteria attributeCriteria : criteria.getAttributeCriteria()) {
qs.append(" and po.code =:").append(attributeCriteria.getAttributeCode());
qs.append(" and povd.description like :").append("val").append(cnt).append(attributeCriteria.getAttributeCode());
cnt++;
}
if (criteria.getLanguage() != null && !criteria.getLanguage().equals("_all")) {
qs.append(" and povd.language.code=:lang");
}
}
if (CollectionUtils.isNotEmpty(criteria.getOptionValueIds())) {
qs.append(" and pov.id in (:povid)");
}
qs.append(" order by p.sortOrder asc");
String hql = qs.toString();
Query q = this.em.createQuery(hql);
if (criteria.getLanguage() != null && !criteria.getLanguage().equals("_all")) {
q.setParameter("lang", language.getCode());
}
q.setParameter("mId", store.getId());
if (!CollectionUtils.isEmpty(criteria.getCategoryIds())) {
q.setParameter("cid", criteria.getCategoryIds());
}
if (CollectionUtils.isNotEmpty(criteria.getOptionValueIds())) {
q.setParameter("povid", criteria.getOptionValueIds());
}
if (!CollectionUtils.isEmpty(criteria.getProductIds())) {
q.setParameter("pId", criteria.getProductIds());
}
if (criteria.getAvailable() != null) {
q.setParameter("dt", new Date());
}
if (criteria.getManufacturerId() != null) {
q.setParameter("manufid", criteria.getManufacturerId());
}
if (!StringUtils.isBlank(criteria.getCode())) {
q.setParameter("sku", new StringBuilder().append("%").append(criteria.getCode().toLowerCase()).append("%").toString());
}
if (!CollectionUtils.isEmpty(criteria.getAttributeCriteria())) {
int cnt = 0;
for (AttributeCriteria attributeCriteria : criteria.getAttributeCriteria()) {
q.setParameter(attributeCriteria.getAttributeCode(), attributeCriteria.getAttributeCode());
q.setParameter("val" + cnt + attributeCriteria.getAttributeCode(), "%" + attributeCriteria.getAttributeValue() + "%");
cnt++;
}
}
// RENTAL
if (!StringUtils.isBlank(criteria.getStatus())) {
q.setParameter("status", criteria.getStatus());
}
if (criteria.getOwnerId() != null) {
q.setParameter("ownerid", criteria.getOwnerId());
}
if (!StringUtils.isBlank(criteria.getProductName())) {
q.setParameter("nm", new StringBuilder().append("%").append(criteria.getProductName().toLowerCase()).append("%").toString());
}
@SuppressWarnings("rawtypes") GenericEntityList entityList = new GenericEntityList();
entityList.setTotalCount(count.intValue());
q = RepositoryHelper.paginateQuery(q, count, entityList, criteria);
@SuppressWarnings("unchecked") List<Product> products = q.getResultList();
productList.setProducts(products);
return productList;
}
use of com.salesmanager.core.model.common.GenericEntityList in project shopizer by shopizer-ecommerce.
the class MerchantRepositoryImpl method listByCriteria.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public GenericEntityList listByCriteria(MerchantStoreCriteria criteria) throws ServiceException {
try {
StringBuilder req = new StringBuilder();
req.append("select distinct m from MerchantStore m left join fetch m.country mc left join fetch m.parent cp left join fetch m.currency mc left join fetch m.zone mz left join fetch m.defaultLanguage md left join fetch m.languages mls");
StringBuilder countBuilder = new StringBuilder();
countBuilder.append("select count(distinct m) from MerchantStore m");
if (criteria.getCode() != null) {
req.append(" where lower(m.code) like:code");
countBuilder.append(" where lower(m.code) like:code");
}
if (criteria.getName() != null) {
if (criteria.getCode() == null) {
req.append(" where");
countBuilder.append(" where ");
} else {
req.append(" or");
countBuilder.append(" or ");
}
req.append(" lower(m.storename) like:name");
countBuilder.append(" lower(m.storename) like:name");
}
if (!StringUtils.isBlank(criteria.getCriteriaOrderByField())) {
req.append(" order by m.").append(criteria.getCriteriaOrderByField()).append(" ").append(criteria.getOrderBy().name().toLowerCase());
}
Query countQ = this.em.createQuery(countBuilder.toString());
String hql = req.toString();
Query q = this.em.createQuery(hql);
if (criteria.getCode() != null) {
countQ.setParameter("code", "%" + criteria.getCode().toLowerCase() + "%");
q.setParameter("code", "%" + criteria.getCode().toLowerCase() + "%");
}
if (criteria.getName() != null) {
countQ.setParameter("name", "%" + criteria.getCode().toLowerCase() + "%");
q.setParameter("name", "%" + criteria.getCode().toLowerCase() + "%");
}
Number count = (Number) countQ.getSingleResult();
GenericEntityList entityList = new GenericEntityList();
entityList.setTotalCount(count.intValue());
q = RepositoryHelper.paginateQuery(q, count, entityList, criteria);
List<MerchantStore> stores = q.getResultList();
entityList.setList(stores);
return entityList;
} catch (javax.persistence.NoResultException ers) {
} catch (Exception e) {
LOGGER.error(e.getMessage());
throw new ServiceException(e);
}
return null;
}
Aggregations