Subversion Repositories SmartDukaan

Rev

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

package in.shop2020.util;

import in.shop2020.metamodel.core.Bullet;
import in.shop2020.metamodel.core.Entity;
import in.shop2020.metamodel.core.ExpertReview;
import in.shop2020.metamodel.core.Feature;
import in.shop2020.metamodel.core.FreeformContent;
import in.shop2020.metamodel.core.Media;
import in.shop2020.metamodel.core.Slide;
import in.shop2020.metamodel.definitions.Catalog;
import in.shop2020.metamodel.definitions.DefinitionsContainer;
import in.shop2020.metamodel.definitions.SlideDefinition;
import in.shop2020.metamodel.util.ContentPojo;
import in.shop2020.metamodel.util.CreationUtils;
import in.shop2020.metamodel.util.ExpandedBullet;
import in.shop2020.metamodel.util.ExpandedEntity;
import in.shop2020.metamodel.util.ExpandedFeature;
import in.shop2020.metamodel.util.ExpertReviewPojo;
import in.shop2020.metamodel.util.ItemPojo;
import in.shop2020.metamodel.util.MediaPojo;
import in.shop2020.metamodel.util.Specification;
import in.shop2020.metamodel.util.SpecificationGroup;
import in.shop2020.storage.mongo.StorageManager;
import in.shop2020.ui.util.NewVUI;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class PojoCreator {
        private static final String IMG_URL_TEMPLATE ="http://static{0}.saholic.com/images/media/{1}/{2}";
    private static Log log = LogFactory.getLog(PojoCreator.class);
        private static DefinitionsContainer defs = Catalog.getInstance().getDefinitionsContainer();

        public void createAndStoreContentPojo(ExpandedEntity expEntity, List<ExpertReview> expertReviews, String warranty){
                try {
                        ContentPojo cp = new ContentPojo(EntityUtils.getProductName(expEntity), getKeySpecs(expEntity), getDetailedSpecs(expEntity), getWarranty());
                        if(expertReviews != null && expertReviews.size()!=0){
                                List<ExpertReviewPojo> erPojos = new ArrayList<ExpertReviewPojo>();
                                for(ExpertReview er : expertReviews){
                                        ExpertReviewPojo erPojo = new ExpertReviewPojo();
                                        erPojo.setSource(er.getSource());
                                        erPojo.setReview(er.getReviewContent());
                                        erPojos.add(erPojo);
                                }
                                cp.setExpertReviews(erPojos);
                        }
                        cp.setPackageContents(getPackageContents(expEntity));
                        cp.setWarranty(warranty);
                        cp.setUrl(EntityUtils.getEntityNoSlashURL(expEntity));
                        setImagesAndVideos(expEntity, cp);
                        //StorageManager sm  = StorageManager.getStorageManager();
                        StorageManager.insertOrUpdateById(StorageManager.views.siteContent, expEntity.getID(), cp);
                } catch (Exception e){
                        log.info("Could not generate Pojo for entity:" + expEntity.getID());
                        e.printStackTrace();
                }
        }

        private List<SpecificationGroup> getDetailedSpecs(Entity entity) throws Exception {
                List<SpecificationGroup> spGroups = new ArrayList<SpecificationGroup>();
                for (Slide slide : entity.getSlides()){
                        if(slide != null) {
                                long slideDefinitionId = slide.getSlideDefinitionID();
                                if(slideDefinitionId == 130054l || slideDefinitionId == 130001l || slideDefinitionId == 130025l) continue;

                                SlideDefinition slideDef = defs.getSlideDefinition(slideDefinitionId);
                                List<Specification> specs = new ArrayList<Specification>();
                                SpecificationGroup spGroup = new SpecificationGroup(slideDef.getLabel(), specs);
                                
                                if(slideDefinitionId == 130043l){
                                        List<String> values = new ArrayList<String>();
                                        List<Slide> slides= slide.getChildrenSlides();
                                        if(slides != null){
                                                for(Slide sl : slides){
                                                        //For capacity slide
                                                        if ( sl.getSlideDefinitionID() == 130046l){
                                                                String one = "";
                                                                String two = "";
                                                                String three = "";
                                                                List<Slide> capacityChildren = sl.getChildrenSlides();
                                                                if(capacityChildren != null && capacityChildren.size()>0){
                                                                        for(Slide capSlide : capacityChildren){
                                                                                if (capSlide != null){
                                                                                        if(capSlide.getSlideDefinitionID() == 130048l){
                                                                                                one = "Talk time";
                                                                                        }else if (capSlide.getSlideDefinitionID() == 130049l){
                                                                                                one = "Standby time";
                                                                                        }
                                                                                        List<Feature> fs = capSlide.getFeatures();
                                                                                        List<String> values1 = new ArrayList<String>();
                                                                                        if(fs!=null){
                                                                                                for(Feature f: fs){
                                                                                                        if (f != null){
                                                                                                                ExpandedFeature expFeature = new ExpandedFeature(f);
                                                                                                                two = expFeature.getFeatureDefinition().getLabel();
                                                                                                                List<ExpandedBullet> expBullets = expFeature.getExpandedBullets();
                                                                                                                if(expBullets != null){
                                                                                                                        for(ExpandedBullet bullet : expBullets){
                                                                                                                                if(bullet != null) {
                                                                                                                                        three = bullet.displayText();
                                                                                                                                        if(!three.equals("Not available")){
                                                                                                                                                values1.add(MessageFormat.format(Utils.CAPACITY_TEMPLATE, one,two, three));
                                                                                                                                        }
                                                                                                                                }
                                                                                                                        }
                                                                                                                }
                                                                                                        }
                                                                                                }
                                                                                        }
                                                                                        if(values1.size()>0){
                                                                                                values.add(StringUtils.join(values1,","));
                                                                                        }
                                                                                }
                                                                        }
                                                                }
                                                                List<Feature> capacityFeatures = sl.getFeatures(); 
                                                                if(capacityFeatures != null){
                                                                        for(Feature f: capacityFeatures){
                                                                                if (f != null){
                                                                                        ExpandedFeature expFeature = new ExpandedFeature(f);
                                                                                        String fLabel = expFeature.getFeatureDefinition().getLabel();
                                                                                        List<ExpandedBullet> expBullets = expFeature.getExpandedBullets();
                                                                                        List<String> bulletValuesList = new ArrayList<String>();
                                                                                        if(expBullets != null){
                                                                                                for(ExpandedBullet bullet : expBullets){
                                                                                                        if(bullet != null) {
                                                                                                                String value = bullet.displayText();
                                                                                                                if(!value.equals("Not available") && value != null && !value.trim().equals("")){
                                                                                                                        bulletValuesList.add(value);
                                                                                                                }
                                                                                                        }
                                                                                                }
                                                                                        }
                                                                                        if(bulletValuesList.size() > 0){
                                                                                                values.add(fLabel  +  " - " + StringUtils.join(bulletValuesList, ", "));
                                                                                        }
                                                                                }
                                                                        }
                                                                }
                                                                if(values.size()>0){
                                                                        Specification spec = new Specification("Capacity", values);
                                                                        specs.add(spec);
                                                                }
                                                        }
                                                }
                                        }
                                }
                                if((slide.getFeatures()== null || slide.getFeatures().size()==0) && (slide.getChildrenSlides()==null || slide.getChildrenSlides().size()==0 )){
                                        //Lets collect all the freeform if no features are available
                                        System.out.println("Lets collect all the freeform if no features are available");
                                        FreeformContent ffc = slide.getFreeformContent();
                                        List<String> f = new ArrayList<String>();
                                        if(ffc != null){
                                                List<String> freeformTexts = slide.getFreeformContent().getFreeformTexts();
                                                if(freeformTexts.size()>0){
                                                        String appended = StringUtils.join(StringUtils.join(freeformTexts, "\r\n").split("\r\n"),", ");
                                                        if(appended.length() > 0) {
                                                                f = new ArrayList<String>(Arrays.asList(StringUtils.join(freeformTexts, "\r\n").split("\r\n")));
                                                                specs.add(new Specification("", f));
                                                        }
                                                }
                                        }
                                        log.info("Got no features for slide: " + slide.getSlideDefinitionID() + " and entity: " + entity.getID());
                                }else {
                                        for(Feature f : slide.getFeatures()) {
                                                List<String> sb = new ArrayList<String>();
                                                if (f != null){
                                                        ExpandedFeature expFeature = new ExpandedFeature(f);
                                                        expFeature.getFeatureDefinition().getLabel();
                                                        List<ExpandedBullet> expBullets = expFeature.getExpandedBullets();
                                                        if(expBullets != null && expBullets.size() > 0){
                                                                for(ExpandedBullet bullet : expBullets){
                                                                        if(bullet != null) {
                                                                                String displayText = bullet.displayText();
                                                                                if(!displayText.equals("") && !displayText.equals("Not available")){
                                                                                        sb.add(displayText);
                                                                                } else {
                                                                                        if(expBullets.size()==1 && expFeature.getFeatureDefinition().allowsBlank()){
                                                                                                sb.add("Yes");
                                                                                        }
                                                                                }
                                                                        }
                                                                }
                                                        } else {
                                                                if(expFeature.getFeatureDefinition().allowsBlank()){
                                                                                sb.add("Yes");
                                                                }
                                                        }
                                                        if(sb.size()>0){
                                                                Specification spec = new Specification(expFeature.getFeatureDefinition().getLabel(), Arrays.asList(StringUtils.join(sb,", ")));
                                                                specs.add(spec);
                                                        }
                                                }
                                        }
                                }
                                
                                if(slideDefinitionId == 130133l){
                                        List<Slide> processorChildren = slide.getChildrenSlides();
                                        if(processorChildren != null) {
                                                Slide processorSlide = processorChildren.get(0);
                                                if(processorSlide != null) {
                                                        List<Feature> processorFeatures = processorSlide.getFeatures();
                                                        if(processorFeatures != null && processorFeatures.size()>0){
                                                                List<String> sb = new ArrayList<String>();
                                                                String processorString = "";
                                                                String processorFfc = "";
                                                                for(Feature f : processorFeatures) {
                                                                        if(f!=null){
                                                                                ExpandedFeature ef = new ExpandedFeature(f);
                                                                                if(f.getFeatureDefinitionID()==120326l)
                                                                                {
                                                                                        List<ExpandedBullet> bullets = ef.getExpandedBullets();
                                                                                        if(bullets !=null && bullets.size() >0){
                                                                                                sb.add("Family -" + bullets.get(0).displayText());
                                                                                        }
                                                                                }
                                                                                if(f.getFeatureDefinitionID()==120327l)
                                                                                {
                                                                                        List<ExpandedBullet> bullets = ef.getExpandedBullets();
                                                                                        if(bullets !=null && bullets.size() >0){
                                                                                                processorString = bullets.get(0).displayText();
                                                                                        }
                                                                                        FreeformContent ffc = f.getFreeformContent();
                                                                                        if(ffc != null){
                                                                                                List<String> freeformTexts = ffc.getFreeformTexts();
                                                                                                if(freeformTexts.size()>0){
                                                                                                        String appended = StringUtils.join(StringUtils.join(freeformTexts, "\r\n").split("\r\n"),", ");
                                                                                                        if(appended.length() > 0)
                                                                                                                processorFfc = appended;
                                                                                                }
                                                                                        } 
                                                                                }
                                                                                if(f.getFeatureDefinitionID()==120163l)
                                                                                {
                                                                                        List<ExpandedBullet> bullets = ef.getExpandedBullets();
                                                                                        if(bullets !=null && bullets.size() >0){
                                                                                                processorString += " " + bullets.get(0).displayText();
                                                                                                sb.add(processorString);
                                                                                        }
                                                                                        FreeformContent ffc = f.getFreeformContent();
                                                                                        if(ffc != null){
                                                                                                List<String> freeformTexts = ffc.getFreeformTexts();
                                                                                                if(freeformTexts.size()>0){
                                                                                                        String appended = StringUtils.join(StringUtils.join(freeformTexts, "\r\n").split("\r\n"),", ");
                                                                                                        if(appended.length() > 0) processorFfc += ", " + appended;
                                                                                                }
                                                                                        } 
                                                                                }
                                                                        }
                                                                }
                                                                if(!processorFfc.equals("")) sb.add(processorFfc);
                                                                if(sb.size()>0) {
                                                                        Specification spec = new Specification("Processor", Arrays.asList(StringUtils.join(sb,", ")));
                                                                        specs.add(spec);
                                                                }
                                                        }
                                                }
                                        }
                                }
                                if(spGroup!=null && spGroup.getSpecs() != null && spGroup.getSpecs().size()>0) {
                                        spGroups.add(spGroup);
                                }
                        }
                }
                return spGroups;
        }

        private void setImagesAndVideos(Entity entity, ContentPojo cp) {
                
                String fileNamePrefix = EntityUtils.getMediaPrefix(entity);
                List<MediaPojo> images = new ArrayList<MediaPojo>();
                List<MediaPojo> videos = new ArrayList<MediaPojo>();
                List<Slide> slides = entity.getSlides();
                String entityNumber = entity.getID() + "";
                Media defaultMedia = null; 
                if(slides!=null){
                        for(Slide slide : slides){
                                if (slide != null){
                                        if(slide.getSlideDefinitionID() == 130054l){
                                                defaultMedia = slide.getFreeformContent().getMedias().get("default");
                                                if(defaultMedia!=null){
                                                        cp.setDefaultImageUrl(MessageFormat.format(IMG_URL_TEMPLATE, entity.getID()%3, entityNumber, NewVUI.computeNewFileName(fileNamePrefix, NewVUI.DEFAULT_JPG, String.valueOf(defaultMedia.getCreationTime().getTime()))));
                                                        cp.setThumbnailImageUrl(MessageFormat.format(IMG_URL_TEMPLATE, entity.getID()%3, entityNumber, NewVUI.computeNewFileName(fileNamePrefix, NewVUI.THUMBNAIL_JPG, String.valueOf(defaultMedia.getCreationTime().getTime()))));
                                                        cp.setIconImageUrl(MessageFormat.format(IMG_URL_TEMPLATE, entity.getID()%3, entityNumber, NewVUI.computeNewFileName(fileNamePrefix, NewVUI.ICON_JPG, String.valueOf(defaultMedia.getCreationTime().getTime()))));
                                                }else {
                                                        log.info("Could not find default image for entity:" + entity.getID());
                                                }
                                        }
                                        else if(slide.getFreeformContent() !=null && slide.getFreeformContent().getMedias() !=null){
                                                Collection<Media> medias = slide.getFreeformContent().getMedias().values();
                                                for (Media media : medias){
                                                        if(media.getType().equals(Media.Type.IMAGE)){
                                                                MediaPojo ip = new MediaPojo(media.getLabel(), MessageFormat.format(IMG_URL_TEMPLATE, entity.getID()%3, entityNumber, NewVUI.computeNewFileName(fileNamePrefix, media.getFileName(), String.valueOf(media.getCreationTime().getTime()))));
                                                                images.add(ip);
                                                        }else if(media.getType().equals(Media.Type.VIDEO_WITH_SKIN) || media.getType().equals(Media.Type.VIDEO_WITH_SKIN)){
                                                                MediaPojo vp = new MediaPojo(media.getLabel(), media.getYoutubeId());
                                                                videos.add(vp);
                                                        }
                                                }
                                        }
                                        if(slide.getSlideDefinitionID()==130001l){
                                                try{
                                                        cp.setIntroduction(slide.getFreeformContent().getFreeformTexts().get(0));
                                                }catch(Exception e){
                                                        log.info("Could not add introduction.");
                                                }
                                        }
                                }
                        }
                        if(images.size()==0){
                                images.add(new MediaPojo(defaultMedia.getLabel(), MessageFormat.format(IMG_URL_TEMPLATE, entity.getID()%3, entityNumber, NewVUI.computeNewFileName(fileNamePrefix, defaultMedia.getFileName(), String.valueOf(defaultMedia.getCreationTime().getTime())))));
                        }
                }
                cp.setImages(images);
                cp.setVideos(videos);
        }

        private String getWarranty() {
                // TODO Auto-generated method stub
                return null;
        }

        private List<String> getKeySpecs(Entity entity) throws Exception {
                List<String> keySpecs = new ArrayList<String>();
                Slide summarySlide = entity.getSlide(130054l);
                List<Feature> summaryFeatures = summarySlide.getFeatures();
                if(summaryFeatures != null){
                        for(Feature f : summaryFeatures) {
                                if(f.getFeatureDefinitionID()==120081l){
                                        ExpandedFeature expFeature = new ExpandedFeature(f);
                                        expFeature.getFeatureDefinition().getLabel();
                                        List<ExpandedBullet> expBullets = expFeature.getExpandedBullets();
                                        if(expBullets != null){
                                                for(ExpandedBullet bullet : expBullets){
                                                        if(bullet != null) {
                                                                String displayText =  bullet.displayText();
                                                                if(!displayText.equals("Not available"))
                                                                keySpecs.add(displayText);
                                                        }
                                                }
                                        }
                                        break;
                                }
                        }
                }
                return keySpecs;
        }
        
        private List<String> getPackageContents(Entity entity){
                List<String> packageContents = null;
                Slide summarySlide = entity.getSlide(130025l);
                if (summarySlide != null){
                        FreeformContent ffc = summarySlide.getFreeformContent();
                        if(ffc != null){
                                List<String> freeformTexts = summarySlide.getFreeformContent().getFreeformTexts();
                                if(freeformTexts.size()>0){
                                        String appended = StringUtils.join(StringUtils.join(freeformTexts, "\r\n").split("\r\n"),", ");
                                        if(appended.length()>0) {
                                                packageContents = Arrays.asList(StringUtils.join(freeformTexts, "\r\n").split("\r\n"));
                                        }
                                }
                        }
                }
                return packageContents; 
        }

        public static void main (String [] args) throws Exception{
                
                PojoCreator creator = new PojoCreator();
                ExpandedEntity exp = new ExpandedEntity(CreationUtils.getEntity(1003970l));
                creator.createAndStoreContentPojo(exp, null, "4 months Warranty");
                creator.updateCatalogInfo(1003970, "This is offer", null);
        }
        
        public void updateCatalogInfo(long entityId, String offerText, List<ItemPojo> items) {
                ContentPojo cp = new ContentPojo(entityId);
                cp.setOfferText(offerText);
                cp.setItems(items);
                StorageManager.addById(StorageManager.views.siteContent, entityId, cp);
        }
}