Subversion Repositories SmartDukaan

Rev

Rev 5432 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.user.handler;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.thrift.TException;
import org.omg.CORBA.UserException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.annotation.Transactional;

import in.shop2020.model.v1.catalog.InventoryService;
import in.shop2020.model.v1.user.Address;
import in.shop2020.model.v1.user.Affiliate;
import in.shop2020.model.v1.user.AuthenticationException;
import in.shop2020.model.v1.user.Cart;
import in.shop2020.model.v1.user.CartStatus;
import in.shop2020.model.v1.user.Discount;
import in.shop2020.model.v1.user.LineStatus;
import in.shop2020.model.v1.user.MasterAffiliate;
import in.shop2020.model.v1.user.ShoppingCartException;
import in.shop2020.model.v1.user.TrackLog;
import in.shop2020.model.v1.user.TrackLogType;
import in.shop2020.model.v1.user.Tracker;
import in.shop2020.model.v1.user.User;
import in.shop2020.model.v1.user.UserAffiliateException;
import in.shop2020.model.v1.user.UserCommunication;
import in.shop2020.model.v1.user.UserCommunicationException;
import in.shop2020.model.v1.user.UserContextException;
import in.shop2020.model.v1.user.UserContextService.Iface;
import in.shop2020.model.v1.user.UserNote;
import in.shop2020.model.v1.user.UserType;
import in.shop2020.model.v1.user.WidgetException;
import in.shop2020.user.handler.AddressHandler;
import in.shop2020.user.handler.AffiliateHandler;
import in.shop2020.user.handler.CartHandler;
import in.shop2020.user.handler.MiscellaneousHandler;
import in.shop2020.user.handler.TrackHandler;
import in.shop2020.user.handler.UserHandler;
import in.shop2020.user.handler.UserWidgetHandler;
import in.shop2020.user.util.Converter;

public class UserServiceHandler implements Iface {

        private static Logger logger = LoggerFactory.getLogger(UserServiceHandler.class);
        
        private AddressHandler addressHandler; 
        private AffiliateHandler affiliateHandler;
        private CartHandler cartHandler; 
        private MiscellaneousHandler miscellaneousHandler;
        private TrackHandler trackHandler;
        private UserHandler userHandler; 
        private UserWidgetHandler userWidgetHandler;
        private ApplicationContext context;
        
        
        public UserServiceHandler() {
                logger.info("Creating context");
                context = new ClassPathXmlApplicationContext("context.xml");
                addressHandler              = context.getBean(AddressHandler.class);
                affiliateHandler            = context.getBean(AffiliateHandler.class);
                cartHandler                 = context.getBean(CartHandler.class);
                miscellaneousHandler        = context.getBean(MiscellaneousHandler.class);
                trackHandler                = context.getBean(TrackHandler.class);
                userHandler                 = context.getBean(UserHandler.class);
                userWidgetHandler                   = context.getBean(UserWidgetHandler.class);
                
                
        }
        
        @Override
        public boolean isAlive() throws TException {
                 try {
                                userHandler.getUser(1);
                    return true;
                } catch (Exception e) {
                    logger.error("Could not fetch users", e);
                    return false;
                }
         }

        @Override
        public void closeSession() throws TException {
        }

        @Transactional
        @Override
        public User createAnonymousUser(String jsession_id)
                        throws UserContextException, TException {
                return userHandler.createAnonymousUser(jsession_id);
        }

        @Override
        public User getUserById(long userId) throws UserContextException,
                        TException {
                return userHandler.getUserById(userId);
        }

        @Override
        public User getUserByEmail(String email) throws UserContextException,
                        TException {
                return userHandler.getUserByEmail(email);
        }

        @Override
        public User getUserByMobileNumber(long mobileNumber)
                        throws UserContextException, TException {
                return userHandler.getUserByMobileNumber(new Long(mobileNumber).toString());
        }

        @Transactional
        @Override
        public User createUser(User user) throws UserContextException, TException {
                return userHandler.createUser(user);
        }

        @Override
        public User updateUser(User user) throws UserContextException, TException {
                return userHandler.updateUser(user);
        }

        @Override
        public User authenticateUser(String email, String password)
                        throws AuthenticationException, TException {
                return userHandler.authenticateUser(email,password);
        }

        @Override
        public boolean userExists(String email) throws UserContextException,
                        TException {
                return userHandler.userExists(email);
        }

        @Override
        public long addAddressForUser(long userId, Address address,
                        boolean setDefault) throws UserContextException, TException {
                long addressId = -1;
                User user = getUserById(userId);
                if(user==null) {
                        throw new UserContextException(103, "No such user");
                }
                addressId = addressHandler.addAddressForUser(Converter.toDBAddress(address, userId));
                if(setDefault||user.getDefaultAddressId()==0) {
                        setDefaultAddress(userId, addressId);
                }
                return addressId;
        }

        @Override
        public boolean removeAddressForUser(long userid, long addressId)
                        throws UserContextException, TException {
                in.shop2020.user.domain.Address address = addressHandler.getAddress(addressId);
                
                if(address == null) {
                        throw new UserContextException(103,"No address found with this addressId");
                }
                if(address.getUser_id()!=userid) {
                        throw new UserContextException(104, "This address belongs to some other user");
                }
                
                addressHandler.removeAddressForUser(addressId);
                return true;
        }

        @Override
        public boolean setUserAsLoggedIn(long userId, long timestamp)
                        throws UserContextException, TException {
                return userHandler.setUserAsLoggedIn(userId, timestamp);
        }

        @Override
        public boolean setUserAsLoggedOut(long userId, long timestamp)
                        throws UserContextException, TException {
                return userHandler.setUserAsLoggedOut(userId, timestamp);
        }

        @Override
        public boolean setDefaultAddress(long userId, long addressId)
                        throws UserContextException, TException {
                if (userHandler.getUserById(userId) == null) {
                throw new UserContextException(103, "No such user in system");
                }
                in.shop2020.user.domain.Address address = addressHandler.getAddress(addressId);
                if(address == null) {
                        throw new UserContextException(103, "Address not found");
                }
                if(address.getUser_id()!=userId) {
                        throw new UserContextException(104, "This address belongs to some other user");
                }
                userHandler.setDefaultAddress(userId, addressId);
                return true;
        }

        @Override
        public boolean updatePassword(long userid, String oldPassword,
                        String newPassword) throws UserContextException, TException {
                return userHandler.updatePassword(userid, oldPassword, newPassword);
        }

        @Override
        public boolean forgotPassword(String email, String newPassword)
                        throws UserContextException, TException {
                return userHandler.forgotPassword(email, newPassword);
        }

        @Override
        public List<Address> getAllAddressesForUser(long userId)
                        throws UserContextException, TException {
                return addressHandler.getAllAddressesForUser(userId);
        }

        @Override
        public Address getAddressById(long addressId) throws UserContextException,
                        TException {
                return Converter.toThriftAddress(addressHandler.getAddress(addressId));
        }

        @Override
        public long getDefaultAddressId(long userId) throws UserContextException,
                        TException {
                User user = userHandler.getUserById(userId);
                if(user == null) {
                        throw new UserContextException(101, "no such user in system UserId : "+userId);
                }
                return user.getDefaultAddressId();
        }

        @Override
        public String getDefaultPincode(long userId) throws UserContextException,
                        TException {
                return userHandler.getDefaultPincode(userId);
        }

        @Override
        public boolean saveUserCommunication(long userId, String replyTo,
                        long communicationType, long orderId, String airwaybillNo,
                        String productName, String subject, String message)
                        throws UserCommunicationException, TException {
                return userHandler.saveUserCommunication(userId, replyTo,
                                communicationType, orderId, airwaybillNo,productName, 
                                subject, message);
        }

        @Override
        public UserCommunication getUserCommunicationById(long id)
                        throws UserCommunicationException, TException {
                return userHandler.getUserCommunicationById(id);
        }

        @Override
        public List<UserCommunication> getUserCommunicationByUser(long userId)
                        throws UserCommunicationException, TException {
                return userHandler.getUserCommunicationByUser(userId);
        }

        @Override
        public List<UserCommunication> getAllUserCommunications()
                        throws UserCommunicationException, TException {
                return userHandler.getAllUserCommunications();
        }

        public void removeUserCommunication(long id) 
                        throws UserCommunicationException, TException {
                userHandler.removeUserCommunication(id);
        }
        
        @Override
        public MasterAffiliate createMasterAffiliate(String name, long addedOn)
                        throws UserAffiliateException, TException {
                return affiliateHandler.createMasterAffiliate(name, addedOn);
        }

        @Override
        public List<MasterAffiliate> getAllMasterAffiliates()
                        throws UserAffiliateException, TException {
                return affiliateHandler.getAllMasterAffiliates();
        }

        @Override
        public MasterAffiliate getMasterAffiliateById(long id)
                        throws UserAffiliateException, TException {
                return affiliateHandler.getMasterAffiliateById(id);
        }

        @Override
        public MasterAffiliate getMasterAffiliateByName(String name)
                        throws UserAffiliateException, TException {
                return affiliateHandler.getMasterAffiliateByName(name);
        }

        @Override
        public Affiliate createAffiliate(String name, String url,
                        long masterAffiliateId, long addedOn)
                        throws UserAffiliateException, TException {
                return affiliateHandler.createAffiliate(name, url, masterAffiliateId, addedOn);
        }

        @Override
        public Affiliate getAffiliateById(long id) throws UserAffiliateException,
                        TException {
                return affiliateHandler.getAffiliateById(id);
        }

        @Override
        public Affiliate getAffiliateByName(String name)
                        throws UserAffiliateException, TException {
                return affiliateHandler.getAffiliateByName(name);
        }

        @Override
        public Tracker getTrackerById(long id) throws UserAffiliateException,
                        TException {
                return trackHandler.getTrackerById(id);
        }

        @Override
        public List<Affiliate> getAffiliatesByMasterAffiliate(long id)
                        throws UserAffiliateException, TException {
                return affiliateHandler.getAffiliatesByMasterAffiliate(id);
        }

        @Override
        public long addTrackLog(long affiliateId, long userId, TrackLogType event,
                        String url, String data, long addedOn)
                        throws UserAffiliateException, TException {
                return trackHandler.addTrackLog(affiliateId, userId, event, url, 
                                data, addedOn);
        }

        @Override
        public TrackLog getTrackLogById(long id) throws UserAffiliateException,
                        TException {
                return Converter.toThriftTrackLog(trackHandler.getTrackLogById(id));
        }

        @Override
        public List<TrackLog> getTrackLogsByAffiliate(long affiliateId,
                        long startDate, long endDate) throws UserAffiliateException,
                        TException {
                return trackHandler.getTrackLogsByAffiliate(affiliateId,
                                startDate, endDate);
        }

        @Override
        public List<TrackLog> getTrackLogsByUser(long userId)
                        throws UserAffiliateException, TException {
                return trackHandler.getTrackLogsByUser(userId);
        }

        @Override
        public List<TrackLog> getTrackLogs(long userId, String event, String url)
                        throws UserAffiliateException, TException {
                return trackHandler.getTrackLogs(userId, event, url);
        }

        @Override
        public Cart getCurrentCart(long userId) throws ShoppingCartException,
                        TException {
                return cartHandler.getCartsForUser(userId, CartStatus.ACTIVE);
        }

        @Override
        public Cart getCart(long cartId) throws ShoppingCartException, TException {
                return cartHandler.getCart(cartId);
        }

        @Override
        public List<Cart> getCartsByTime(long from_time, long to_time,
                        CartStatus status) throws ShoppingCartException, TException {
                return cartHandler.getCartsByTime(from_time, to_time, status);
        }

        @Override
        public String addItemToCart(long cartId, long itemId, long quantity,
                        long sourceId) throws ShoppingCartException, TException {
                return cartHandler.addItemToCart(cartId, itemId, quantity, sourceId);
        }

        @Override
        public void deleteItemFromCart(long cartId, long itemId)
                        throws ShoppingCartException, TException {
                cartHandler.deleteItemFromCart(cartId, itemId);
        }

        @Override
        public void addAddressToCart(long cartId, long addressId)
                        throws ShoppingCartException, TException {
                cartHandler.addAddressToCart(cartId, addressId);
                
        }

        @Override
        public void applyCouponToCart(long cartId, String couponCode,
                        double totalPrice, double discountedPrice)
                        throws ShoppingCartException, TException {
                cartHandler.applyCouponToCart(cartId, couponCode, totalPrice, discountedPrice);
        }

        @Override
        public void removeCoupon(long cartId) throws ShoppingCartException,
                        TException {
                cartHandler.removeCoupon(cartId);
        }

        @Override
        public void deleteDiscountsFromCart(long cartId)
                        throws ShoppingCartException, TException {
                cartHandler.deleteDiscountsFromCart(cartId);
        }

        @Override
        public void saveDiscounts(List<Discount> discounts)
                        throws ShoppingCartException, TException {
                cartHandler.saveDiscounts(discounts);
        }

        @Override
        public String validateCart(long cartId, long sourceId)
                        throws ShoppingCartException, TException {
                return cartHandler.validateCart(cartId, sourceId);
        }

        @Override
        public void mergeCart(long fromCartId, long toCartId) throws TException {
                cartHandler.mergeCart(fromCartId, toCartId);
                
        }

        @Override
        public boolean checkOut(long cartId) throws ShoppingCartException,
                        TException {
                return cartHandler.checkOut(cartId);
        }

        @Override
        public boolean resetCart(long cartId, Map<Long, Double> items)
                        throws ShoppingCartException, TException {
                return cartHandler.resetCart(cartId, items);
        }

        @Override
        public long getUserCount(UserType userType) throws TException {
                return userHandler.getUserCount(userType);
        }

        @Override
        public List<User> getAllUsers(UserType userType, long startDate,
                        long endDate) throws TException {
                
                List<User> tUsers = new ArrayList<User>();
                for(in.shop2020.user.domain.User user : userHandler.getAllUsers(userType, startDate, endDate)) {
                        tUsers.add(Converter.toThriftUser(user));
                }
                return tUsers;
        }

        @Override
        public void putUserNote(long user_id, long entity_id, String slide,
                        String note) throws TException {
                miscellaneousHandler.putUserNote(user_id, entity_id, slide, note);
                
        }

        @Override
        public List<UserNote> getUserNotes(long user_id, long entity_id)
                        throws TException {
                return miscellaneousHandler.getUserNotes(user_id, entity_id);
        }

        @Override
        public List<Long> getMyResearchItems(long userId) throws WidgetException,
                        TException {
                return userWidgetHandler.getMyResearchItems(userId);
        }

        @Override
        public boolean updateMyResearch(long userId, long itemId)
                        throws WidgetException, TException {
                return userWidgetHandler.updateMyResearch(userId, itemId);
        }

        @Override
        public void deleteItemFromMyResearch(long userId, long itemId)
                        throws WidgetException, TException {
                userWidgetHandler.deleteItemFromMyResearch(userId, itemId);
        }

        @Override
        public List<Long> getBrowseHistoryItems(long userId)
                        throws WidgetException, TException {
                return userWidgetHandler.getBrowseHistoryItems(userId);
        }

        @Override
        public void updateBrowseHistory(long userId, long itemId) throws TException {
                if(!userWidgetHandler.isItemInBrowseHistory(userId, itemId)) {
                        userWidgetHandler.updateBrowseHistory(userId, itemId);
                }
        }

        @Override
        public long getCartsWithCouponCount(String couponCode) throws TException {
                return cartHandler.getCartsWithCouponCount(couponCode);
        }

        @Override
        public void increaseTrustLevel(long userId, double trustLevelDelta)
                        throws TException {
                userHandler.increaseTrustLevel(userId, trustLevelDelta);
        }
        
        @Override
        public double getTrustLevel(long userId) throws TException {
                return userHandler.getTrustLevel(userId);
        }

        @Override
        public boolean showCODOption(long cartId, long sourceId, String pincode)
                        throws TException {
                return cartHandler.showCODOption(cartId, sourceId, pincode);
        }

        @Override
        public User getUserByCartId(long cartId) throws UserContextException,
                        TException {
                return userHandler.getUserByCartId(cartId);
        }

        @Override
        public long createOrders(long cartId, String sessionSource,
                        long sessionStartTime, String firstSource, long firstSourceTime,
                        long userId) throws ShoppingCartException, TException {
                return cartHandler.commitCart(cartId, sessionSource, sessionStartTime, 
                                firstSource, firstSourceTime, userId);
        }

        public void setDataSourceUrl(String dbHost) {
                org.apache.commons.dbcp.BasicDataSource ds = (org.apache.commons.dbcp.BasicDataSource)context.getBean("dataSource");
                ds.setUrl(dbHost);
        }

        public String getDataSourceUrl() {
                org.apache.commons.dbcp.BasicDataSource ds = (org.apache.commons.dbcp.BasicDataSource)context.getBean("dataSource");
                return ds.getUrl();
        }

        @Override
        public void addStoreToCart(long cartId, long storeId)
                        throws ShoppingCartException, TException {
                        cartHandler.addStoreToCart(cartId, storeId);
                
        }
        
}