Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.user.util;

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

import in.shop2020.model.v1.user.Address;
import in.shop2020.model.v1.user.AddressType;
import in.shop2020.model.v1.user.Affiliate;
import in.shop2020.model.v1.user.CartStatus;
import in.shop2020.model.v1.user.Discount;
import in.shop2020.model.v1.user.Line;
import in.shop2020.model.v1.user.LineStatus;
import in.shop2020.model.v1.user.MasterAffiliate;
import in.shop2020.model.v1.user.Sex;
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.UserCommunication;
import in.shop2020.model.v1.user.UserCommunicationType;
import in.shop2020.model.v1.user.UserNote;
import in.shop2020.model.v1.user.Cart;

public class Converter {
        public static Address toThriftAddress(in.shop2020.user.domain.Address address) {
                Address tAddress = new Address();
                if(address!=null) {
                        tAddress.setId(address.getId());
                    tAddress.setLine1(address.getLine_1());
                    tAddress.setLine2(address.getLine_2());
                    tAddress.setLandmark(address.getLandmark());
                    tAddress.setCity(address.getCity());
                    tAddress.setState(address.getState());
                    tAddress.setPin(address.getPin());
                    tAddress.setCountry(address.getCountry());
                    tAddress.setEnabled(address.isEnabled());
                    tAddress.setType(AddressType.findByValue(address.getType()));
                    if(address.getAdded_on()!=null) {
                        tAddress.setAddedOn(address.getAdded_on().getTime());
                    }
                    tAddress.setName(address.getName());
                    tAddress.setPhone(address.getPhone());
                }
                return tAddress;
        }
        
        public static User toThriftUser(in.shop2020.user.domain.User user) {
                User tUser = new User();
                if(user == null) {
                        tUser.setUserId(-1);
                        return tUser;
                }
                tUser.setUserId(user.getId());
                tUser.setName(user.getName());
                tUser.setEmail(user.getEmail());
                tUser.setPassword(user.getPassword());
                tUser.setSex(Sex.findByValue(user.getSex()));
                tUser.setCommunicationEmail(user.getCommunication_email());
                tUser.setDefaultAddressId(user.getDefault_address_id());
                tUser.setIsAnonymous(user.isIs_anonymous());
                tUser.setActiveCartId(user.getActive_cart_id());
                tUser.setDateOfBirth(user.getDate_of_birth());
                tUser.setMobileNumber(user.getMobile_number());
                if(user.getLastLogin()!=null) {
                        tUser.setLastLogin(user.getLastLogin().getTime());
                }
                if(user.getLastLogout()!=null) {
                        tUser.setLastLogout(user.getLastLogout().getTime());
                }
                if(user.getActiveSince()!=null) {
                        tUser.setActiveSince(user.getActiveSince().getTime());
                }
                tUser.setSource(user.getSource());
                if(user.getSource_start_time()!=null) {
                        tUser.setSourceStartTime(user.getSource_start_time().getTime());
                }
                if(user.getTrust_level()!=null) {
                        tUser.setTrustLevel(user.getTrust_level());
                }
                if(user.getAddresses()!=null) {
                        tUser.setAddresses(new ArrayList<Address>());
                        for(in.shop2020.user.domain.Address address :user.getAddresses()){
                                tUser.getAddresses().add(toThriftAddress(address));
                        }
                }
                return tUser;
        }
        
        
        public static Cart toThriftCart(in.shop2020.user.domain.Cart cart){
                Cart tCart = new Cart();
                
                if(cart!=null){
                        tCart.setId(cart.getId());
                        tCart.setStatus(CartStatus.findByValue(cart.getCart_status()));
                        if(cart.getLines()!=null) {
                                List<Line> lines = new ArrayList<Line>(); 
                                for (in.shop2020.user.domain.Line line : cart.getLines()) {
                                        lines.add(toThriftLine(line));
                                }
                                tCart.setLines(lines);
                        }
                        tCart.setTotalPrice(cart.getTotal_price());
                        tCart.setDiscountedPrice(cart.getDiscounted_price());
                        tCart.setCouponCode(cart.getCoupon_code());
                        if(cart.getCreated_on()!=null) {
                                tCart.setCreatedOn(cart.getCreated_on().getTime());
                        }
                        if(cart.getUpdated_on()!=null) {
                                tCart.setUpdatedOn(cart.getUpdated_on().getTime());
                        }
                        tCart.setAddressId(cart.getAddress_id());
                        if(cart.getChecked_out_on()!=null) {
                                tCart.setCheckedOutOn(cart.getChecked_out_on().getTime());
                        }
                }
                return tCart;
        }
        
        
        public static Line toThriftLine(in.shop2020.user.domain.Line line){
                Line tLine = new Line();
                
                if(line!=null) {
                        tLine.setCartId(line.getCart_id());
                        tLine.setItemId(line.getItem_id());
                        tLine.setQuantity(line.getQuantity());
                        tLine.setEstimate(line.getEstimate());
                        tLine.setActualPrice(line.getActual_price());
                        tLine.setDiscountedPrice(line.getDiscounted_price());
                        if(line.getCreated_on()!=null) {
                                tLine.setCreatedOn(line.getCreated_on().getTime());
                        }
                        if(line.getUpdated_on()!=null) {
                                tLine.setUpdatedOn(line.getUpdated_on().getTime());
                        }
                        tLine.setLineStatus(LineStatus.findByValue(line.getLine_status()));
                        
                        if(line.getDiscounts()!=null) {
                                List<Discount> discounts = new ArrayList<Discount>();
                                for(in.shop2020.user.domain.Discount discount : line.getDiscounts()){
                                        discounts.add(toThriftDiscount(discount));
                                }
                                tLine.setDiscounts(discounts);
                        }
                }
                return tLine;
        }

        public static Discount toThriftDiscount(in.shop2020.user.domain.Discount discount) {
                Discount tDiscount = new Discount();
                if(discount!=null){
                        tDiscount.setCart_id(discount.getLine_cart_id());;
                        tDiscount.setItem_id(discount.getLine_item_id());
                        tDiscount.setDiscount(discount.getDiscount());
                        tDiscount.setQuantity(discount.getQuantity());
                }
                
                return tDiscount;
        }
        
        public static UserCommunication toThriftUserCommunication(in.shop2020.user.domain.UserCommunication userCommunication){
                UserCommunication tUserCommunication = new UserCommunication();
                if(userCommunication!=null){
                        tUserCommunication.setId(userCommunication.getId());
                        tUserCommunication.setUserId(userCommunication.getUser_id());
                        tUserCommunication.setCommunicationType(UserCommunicationType.findByValue((int)(userCommunication.getCommunication_type())));
                        tUserCommunication.setOrderId(userCommunication.getOrder_id());
                        tUserCommunication.setAirwaybillNo(userCommunication.getAirwaybill_no());
                        tUserCommunication.setReplyTo(userCommunication.getReply_to());
                        tUserCommunication.setProductName(userCommunication.getProduct_name());
                        tUserCommunication.setSubject(userCommunication.getSubject());
                        tUserCommunication.setMessage(userCommunication.getMessage());
                        tUserCommunication.setCommunication_timestamp(userCommunication.getCommunication_timestamp().getTime());
                }
                return tUserCommunication;
        }
        
        public static MasterAffiliate toThriftMasterAffiliate(in.shop2020.user.domain.MasterAffiliate masterAffiliate) {
                MasterAffiliate tMasterAffiliate = new MasterAffiliate();
                if(masterAffiliate!=null){
                        tMasterAffiliate.setId(masterAffiliate.getId());
                        tMasterAffiliate.setName(masterAffiliate.getName());
                        if(masterAffiliate.getAdded_on()!=null) {
                                tMasterAffiliate.setAddedOn(masterAffiliate.getAdded_on().getTime());
                        }
                }
                return tMasterAffiliate;
        }
        
        public static Affiliate toThriftAffiliate(in.shop2020.user.domain.Affiliate affiliate) {
                Affiliate tAffiliate = new Affiliate();
                if(affiliate!=null) {
                        tAffiliate.setId(affiliate.getId());
                        tAffiliate.setName(affiliate.getName());
                        tAffiliate.setUrl(affiliate.getUrl());
                        tAffiliate.setMasterAffiliateId(affiliate.getMaster_affiliate_id());
                        if(affiliate.getAdded_on()!=null) {
                                tAffiliate.setAddedOn(affiliate.getAdded_on().getTime());
                        }
                }
                return tAffiliate;
        }
        
        public static Tracker toThriftTracker(in.shop2020.user.domain.Tracker tracker) {
                Tracker tTracker = new Tracker();
                if(tracker!=null) {
                        tTracker.setAffiliateId(tracker.getAffiliate_id());
                }
                return tTracker;
        }

        public static TrackLog toThriftTrackLog(in.shop2020.user.domain.TrackLog trackLog) {
                TrackLog tTrackLog = new TrackLog();
                if(trackLog!=null) {
                        tTrackLog.setId(trackLog.getId());
                        tTrackLog.setAffiliateId(trackLog.getAffiliate_id());
                        tTrackLog.setUserId(trackLog.getUser_id());
                        tTrackLog.setEventType(TrackLogType.findByValue((int)trackLog.getEvent_id()));
                        tTrackLog.setUrl(trackLog.getUrl());
                        tTrackLog.setData(trackLog.getData());
                        if(trackLog.getAdded_on()!=null) {
                                tTrackLog.setAddedOn(trackLog.getAdded_on().getTime());
                        }
                }
                return tTrackLog;
        }

        public static UserNote toThriftUserNote(in.shop2020.user.domain.UserNote userNote) {
                UserNote tUserNote = new UserNote();
                if(userNote!=null) {
                        tUserNote.setUser_id(userNote.getUser_id());
                        tUserNote.setEntity_id(userNote.getEntity_id());
                        tUserNote.setSlide(userNote.getSlide());
                        tUserNote.setNote(userNote.getNote());
                }
                return tUserNote;
        }

        public static in.shop2020.user.domain.Address toDBAddress(Address address, long userId) {
                in.shop2020.user.domain.Address dAddress = new in.shop2020.user.domain.Address(); 
                dAddress.setId(address.getId());
                dAddress.setLine_1(address.getLine1());
                dAddress.setLine_2(address.getLine2());
                dAddress.setLandmark(address.getLandmark());
                dAddress.setCity(address.getCity());
                dAddress.setState(address.getState());
                dAddress.setPin(address.getPin());
                dAddress.setCountry(address.getCountry());
                dAddress.setEnabled(address.isEnabled());
                dAddress.setType(address.getType().getValue());
                dAddress.setAdded_on(new Date(address.getAddedOn()));
                dAddress.setName(address.getName());
                dAddress.setPhone(address.getPhone());
                dAddress.setUser_id(userId);
                
                return dAddress;
        }
        
        public static in.shop2020.user.domain.User toDBUser(User tUser) {
                in.shop2020.user.domain.User user = new in.shop2020.user.domain.User();
                user.setId(tUser.getUserId());
                user.setEmail(tUser.getEmail());
                user.setPassword(tUser.getPassword());
                user.setName(tUser.getName());
                user.setDefault_address_id(tUser.getDefaultAddressId());
                user.setCommunication_email(tUser.getCommunicationEmail());
                user.setActive_cart_id(tUser.getActiveCartId());
                user.setJsession_id(tUser.getJsessionId());
                user.setIs_anonymous(tUser.isIsAnonymous());
                user.setDate_of_birth(tUser.getDateOfBirth());
                if(tUser.getSex()!=null) {
                        user.setSex(tUser.getSex().getValue());
                }
                user.setMobile_number(tUser.getMobileNumber());
                user.setSource(tUser.getSource());
                user.setSource_start_time(new Date(tUser.getSourceStartTime()));
                user.setTrust_level(tUser.getTrustLevel());
                if(tUser.getAddresses()!=null) {
                        List<in.shop2020.user.domain.Address> addresses = new ArrayList<in.shop2020.user.domain.Address>();
                        for(Address tAddress : tUser.getAddresses()) {
                                addresses.add(Converter.toDBAddress(tAddress, user.getId()));
                        }
                        user.setAddresses(addresses);
                }
                
                return user;
        }
        
}