Rev 22033 | Blame | Last modification | View Log | RSS feed
package com.spice.profitmandi.dao.repository.dtr;import java.util.List;import java.util.Set;import org.hibernate.Criteria;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.criterion.Restrictions;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;import com.spice.profitmandi.common.model.ProfitMandiConstants;import com.spice.profitmandi.dao.entity.dtr.UserAccounts;import com.spice.profitmandi.dao.enumuration.dtr.AccountType;import com.spice.profitmandi.dao.model.UserCart;@Componentpublic class UserAccountImpl implements UserAccountRepository{private final Logger logger=LoggerFactory.getLogger(UserAccountImpl.class);@Autowiredprivate SessionFactory sessionFactory;@Overridepublic void persist(UserAccounts userAccounts) throws ProfitMandiBusinessException {Session session = sessionFactory.getCurrentSession();session.save(userAccounts);}@SuppressWarnings({ "deprecation", "unchecked" })@Overridepublic List<UserAccounts> getUserAccountsForUser(int userId) throws ProfitMandiBusinessException {Session session = sessionFactory.getCurrentSession();Criteria cr = session.createCriteria(UserAccounts.class);cr.add(Restrictions.eq("user_id", userId));return (List<UserAccounts>)cr.list();}@SuppressWarnings("deprecation")@Overridepublic UserAccounts getUserAccountByType(int userId, AccountType accountType) throws ProfitMandiBusinessException {Session session = sessionFactory.getCurrentSession();Criteria cr = session.createCriteria(UserAccounts.class);cr.add(Restrictions.eq("user_id", userId));cr.add(Restrictions.eq("account_type", accountType));UserAccounts userAccount = (UserAccounts)cr.uniqueResult();if (userAccount == null){throw new ProfitMandiBusinessException(ProfitMandiConstants.ID, userId, "USRACNT_1000");}return userAccount;}@Overridepublic UserCart getUserCart(int userId) throws ProfitMandiBusinessException {UserCart uc = new UserCart();List<UserAccounts> userAccounts = this.getUserAccountsForUser(userId);if(userAccounts.isEmpty()){return null;}for (UserAccounts ua : userAccounts) {if(ua.getAccount_type().equals(AccountType.cartId)){uc.setCartId(Integer.parseInt(ua.getAccount_key()));} else if(ua.getAccount_type().equals(AccountType.saholic)){uc.setUserId(Integer.parseInt(ua.getAccount_key()));}}return uc;}@Overridepublic int selectUserIdByRetailerId(int retailerId) throws ProfitMandiBusinessException {Session session = sessionFactory.getCurrentSession();@SuppressWarnings("deprecation")Criteria criteria = session.createCriteria(UserAccounts.class);criteria.add(Restrictions.and(Restrictions.eq(ProfitMandiConstants.ACCOUNT_KEY, String.valueOf(retailerId)),Restrictions.eq(ProfitMandiConstants.ACCOUNT_TYPE, AccountType.saholic)));UserAccounts userAccounts = (UserAccounts)criteria.uniqueResult();if(userAccounts == null){throw new ProfitMandiBusinessException(ProfitMandiConstants.RETAILER_ID, retailerId, "");}return userAccounts.getUser_id();}@SuppressWarnings("unchecked")@Overridepublic List<UserAccounts> selectAllSaholicByUserIds(Set<Integer> userIds) {Session session = sessionFactory.getCurrentSession();@SuppressWarnings("deprecation")Criteria criteria = session.createCriteria(UserAccounts.class);criteria.add(Restrictions.and(Restrictions.in(ProfitMandiConstants.ID, userIds),Restrictions.eq(ProfitMandiConstants.ACCOUNT_TYPE, AccountType.saholic)));return (List<UserAccounts>)criteria.list();}@SuppressWarnings("unchecked")@Overridepublic List<UserAccounts> getAll() throws ProfitMandiBusinessException {Session session = sessionFactory.getCurrentSession();@SuppressWarnings("deprecation")Criteria criteria = session.createCriteria(UserAccounts.class);return criteria.list();}@Overridepublic int selectRetailerIdByUserId(int userId) throws ProfitMandiBusinessException {Session session = sessionFactory.getCurrentSession();@SuppressWarnings("deprecation")Criteria criteria = session.createCriteria(UserAccounts.class);criteria.add(Restrictions.and(Restrictions.eq("user_id", userId),Restrictions.eq(ProfitMandiConstants.ACCOUNT_TYPE, AccountType.saholic)));UserAccounts userAccounts = (UserAccounts)criteria.uniqueResult();if(userAccounts == null){throw new ProfitMandiBusinessException(ProfitMandiConstants.USER_ID, userId, "");}return Integer.parseInt(userAccounts.getAccount_key());}}