Subversion Repositories SmartDukaan

Rev

Rev 9410 | Rev 9427 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
9218 amit.gupta 1
package in.shop2020.util;
2
 
3
import in.shop2020.metamodel.core.Entity;
9280 amit.gupta 4
import in.shop2020.metamodel.core.ExpertReview;
9218 amit.gupta 5
import in.shop2020.metamodel.core.Feature;
9410 amit.gupta 6
import in.shop2020.metamodel.core.FreeformContent;
9218 amit.gupta 7
import in.shop2020.metamodel.core.Media;
8
import in.shop2020.metamodel.core.Slide;
9
import in.shop2020.metamodel.definitions.Catalog;
10
import in.shop2020.metamodel.definitions.DefinitionsContainer;
11
import in.shop2020.metamodel.definitions.SlideDefinition;
12
import in.shop2020.metamodel.util.ContentPojo;
13
import in.shop2020.metamodel.util.ExpandedBullet;
9280 amit.gupta 14
import in.shop2020.metamodel.util.ExpandedEntity;
9218 amit.gupta 15
import in.shop2020.metamodel.util.ExpandedFeature;
9280 amit.gupta 16
import in.shop2020.metamodel.util.ExpertReviewPojo;
9218 amit.gupta 17
import in.shop2020.metamodel.util.ItemPojo;
9280 amit.gupta 18
import in.shop2020.metamodel.util.MediaPojo;
9218 amit.gupta 19
import in.shop2020.metamodel.util.Specification;
20
import in.shop2020.metamodel.util.SpecificationGroup;
21
import in.shop2020.storage.mongo.StorageManager;
22
import in.shop2020.ui.util.NewVUI;
23
 
24
import java.text.MessageFormat;
25
import java.util.ArrayList;
26
import java.util.Arrays;
27
import java.util.Collection;
28
import java.util.List;
29
 
30
import org.apache.commons.lang.StringUtils;
31
import org.apache.commons.logging.Log;
32
import org.apache.commons.logging.LogFactory;
33
 
34
public class PojoCreator {
35
	private static final String IMG_URL_TEMPLATE ="http://static{0}.saholic.com/images/media/{1}/{2}";
36
    private static Log log = LogFactory.getLog(PojoCreator.class);
37
	private static DefinitionsContainer defs = Catalog.getInstance().getDefinitionsContainer();
38
 
9280 amit.gupta 39
	public void createAndStoreContentPojo(ExpandedEntity expEntity, List<ExpertReview> expertReviews, String warranty){
9218 amit.gupta 40
		try {
9280 amit.gupta 41
			ContentPojo cp = new ContentPojo(EntityUtils.getProductName(expEntity), getKeySpecs(expEntity), getDetailedSpecs(expEntity), getWarranty());
42
			if(expertReviews != null && expertReviews.size()!=0){
43
				List<ExpertReviewPojo> erPojos = new ArrayList<ExpertReviewPojo>();
44
				for(ExpertReview er : expertReviews){
45
					ExpertReviewPojo erPojo = new ExpertReviewPojo();
46
					erPojo.setSource(er.getSource());
47
					erPojo.setReview(er.getReviewContent());
48
					erPojos.add(erPojo);
49
				}
50
				cp.setExpertReviews(erPojos);
51
			}
52
			cp.setPackageContents(getPackageContents(expEntity));
53
			cp.setWarranty(warranty);
54
			cp.setUrl(EntityUtils.getEntityNoSlashURL(expEntity));
55
			setImagesAndVideos(expEntity, cp);
9218 amit.gupta 56
			//StorageManager sm  = StorageManager.getStorageManager();
9280 amit.gupta 57
			StorageManager.insertOrUpdateById(StorageManager.views.siteContent, expEntity.getID(), cp);
9218 amit.gupta 58
		} catch (Exception e){
9280 amit.gupta 59
			log.info("Could not generate Pojo for entity:" + expEntity.getID());
9218 amit.gupta 60
			e.printStackTrace();
61
		}
62
	}
63
 
64
	private List<SpecificationGroup> getDetailedSpecs(Entity entity) throws Exception {
65
		List<SpecificationGroup> spGroups = new ArrayList<SpecificationGroup>();
66
		for (Slide slide : entity.getSlides()){
67
			if(slide != null) {
68
				long slideDefinitionId = slide.getSlideDefinitionID();
69
				if(slideDefinitionId == 130054l || slideDefinitionId == 130001l || slideDefinitionId == 130025l) continue;
70
 
71
				SlideDefinition slideDef = defs.getSlideDefinition(slideDefinitionId);
72
				List<Specification> specs = new ArrayList<Specification>();
73
				SpecificationGroup spGroup = new SpecificationGroup(slideDef.getLabel(), specs);
74
 
75
				if(slideDefinitionId == 130043l){
76
					List<String> values = new ArrayList<String>();
77
					List<Slide> slides= slide.getChildrenSlides();
78
					if(slides != null){
79
						for(Slide sl : slides){
80
							//For capacity slide
81
							if ( sl.getSlideDefinitionID() == 130046l){
82
								String one = "";
83
								String two = "";
84
								String three = "";
85
								List<Slide> capacityChildren = sl.getChildrenSlides();
86
								if(capacityChildren != null && capacityChildren.size()>0){
87
									for(Slide capSlide : capacityChildren){
88
										if (capSlide != null){
89
											if(capSlide.getSlideDefinitionID() == 130048l){
90
												one = "Talk time";
91
											}else if (capSlide.getSlideDefinitionID() == 130049l){
92
												one = "Standby time";
93
											}
94
											List<Feature> fs = capSlide.getFeatures();
95
											List<String> values1 = new ArrayList<String>();
96
											if(fs!=null){
97
												for(Feature f: fs){
98
													if (f != null){
99
														ExpandedFeature expFeature = new ExpandedFeature(f);
100
														two = expFeature.getFeatureDefinition().getLabel();
101
														List<ExpandedBullet> expBullets = expFeature.getExpandedBullets();
102
														if(expBullets != null){
103
															for(ExpandedBullet bullet : expBullets){
104
																if(bullet != null) {
105
																	three = bullet.displayText();
106
																	values1.add(MessageFormat.format(Utils.CAPACITY_TEMPLATE, one,two, three));
107
																}
108
															}
109
														}
110
													}
111
												}
112
											}
113
											if(values1.size()>0){
114
												values.add(StringUtils.join(values1,","));
115
											}
116
										}
117
									}
118
								}
119
								List<Feature> capacityFeatures = sl.getFeatures(); 
120
								if(capacityFeatures != null){
121
									for(Feature f: capacityFeatures){
122
										if (f != null){
123
											ExpandedFeature expFeature = new ExpandedFeature(f);
124
											String fLabel = expFeature.getFeatureDefinition().getLabel();
125
											List<ExpandedBullet> expBullets = expFeature.getExpandedBullets();
126
											List<String> bulletValuesList = new ArrayList<String>();
127
											if(expBullets != null){
128
												for(ExpandedBullet bullet : expBullets){
129
													if(bullet != null) {
130
														String value = bullet.displayText();
131
														if(value != null && !value.trim().equals("")){
132
															bulletValuesList.add(bullet.displayText());
133
														}
134
													}
135
												}
136
											}
137
											if(bulletValuesList.size() > 0){
138
												values.add(fLabel  +  " - " + StringUtils.join(bulletValuesList, ","));
139
											}
140
										}
141
									}
142
								}
143
								if(values.size()>0){
144
									Specification spec = new Specification("Capacity", values);
145
									specs.add(spec);
146
								}
147
							}
148
						}
149
					}
150
				}
151
				for(Feature f : slide.getFeatures()) {
152
					List<String> sb = new ArrayList<String>();
153
					if (f != null){
154
						ExpandedFeature expFeature = new ExpandedFeature(f);
155
						expFeature.getFeatureDefinition().getLabel();
156
						List<ExpandedBullet> expBullets = expFeature.getExpandedBullets();
157
						if(expBullets != null){
158
							for(ExpandedBullet bullet : expBullets){
159
								if(bullet != null) {
160
									String displayText = bullet.displayText();
161
									if(!displayText.trim().equals("")){
162
										sb.add(bullet.displayText());
163
									}
164
								}
165
							}
166
						}
167
						if(sb.size()>0){
168
							Specification spec = new Specification(expFeature.getFeatureDefinition().getLabel(), Arrays.asList(StringUtils.join(sb,",")));
169
							specs.add(spec);
170
						}
171
					}
172
				}
173
				spGroups.add(spGroup);
174
			}
175
		}
176
		return spGroups;
177
	}
178
 
9280 amit.gupta 179
	private void setImagesAndVideos(Entity entity, ContentPojo cp) {
9218 amit.gupta 180
 
181
		String fileNamePrefix = EntityUtils.getMediaPrefix(entity);
9280 amit.gupta 182
		List<MediaPojo> images = new ArrayList<MediaPojo>();
183
		List<MediaPojo> videos = new ArrayList<MediaPojo>();
9218 amit.gupta 184
		List<Slide> slides = entity.getSlides();
185
		String entityNumber = entity.getID() + "";
186
		if(slides!=null){
187
 
188
			for(Slide slide : slides){
189
				if (slide != null){
190
					if(slide.getSlideDefinitionID() == 130054l){
191
						Media m = slide.getFreeformContent().getMedias().get("default");
192
						cp.setDefaultImageUrl(MessageFormat.format(IMG_URL_TEMPLATE, entity.getID()%3, entityNumber, NewVUI.computeNewFileName(fileNamePrefix, NewVUI.DEFAULT_JPG, String.valueOf(m.getCreationTime().getTime()))));
193
						cp.setThumbnailImageUrl(MessageFormat.format(IMG_URL_TEMPLATE, entity.getID()%3, entityNumber, NewVUI.computeNewFileName(fileNamePrefix, NewVUI.THUMBNAIL_JPG, String.valueOf(m.getCreationTime().getTime()))));
194
						cp.setIconImageUrl(MessageFormat.format(IMG_URL_TEMPLATE, entity.getID()%3, entityNumber, NewVUI.computeNewFileName(fileNamePrefix, NewVUI.ICON_JPG, String.valueOf(m.getCreationTime().getTime()))));
195
					}
196
					else if(slide.getFreeformContent() !=null && slide.getFreeformContent().getMedias() !=null){
197
						Collection<Media> medias = slide.getFreeformContent().getMedias().values();
198
						for (Media media : medias){
199
							if(media.getType().equals(Media.Type.IMAGE)){
9280 amit.gupta 200
								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()))));
9218 amit.gupta 201
								images.add(ip);
9280 amit.gupta 202
							}else if(media.getType().equals(Media.Type.VIDEO_WITH_SKIN) || media.getType().equals(Media.Type.VIDEO_WITH_SKIN)){
203
								MediaPojo vp = new MediaPojo(media.getLabel(), media.getYoutubeId());
204
								videos.add(vp);
9218 amit.gupta 205
							}
206
						}
207
					}
208
				}
209
			}
210
		}
211
		cp.setImages(images);
9280 amit.gupta 212
		cp.setVideos(videos);
9218 amit.gupta 213
	}
214
 
215
	private String getWarranty() {
216
		// TODO Auto-generated method stub
217
		return null;
218
	}
219
 
220
	private List<String> getKeySpecs(Entity entity) throws Exception {
221
		List<String> keySpecs = new ArrayList<String>();
222
		Slide summarySlide = entity.getSlide(130054l);
223
		List<Feature> summaryFeatures = summarySlide.getFeatures();
224
		if(summaryFeatures != null){
225
			for(Feature f : summaryFeatures) {
226
				if(f.getFeatureDefinitionID()==120081l){
227
					ExpandedFeature expFeature = new ExpandedFeature(f);
228
					expFeature.getFeatureDefinition().getLabel();
229
					List<ExpandedBullet> expBullets = expFeature.getExpandedBullets();
230
					if(expBullets != null){
231
						for(ExpandedBullet bullet : expBullets){
232
							if(bullet != null) {
233
								keySpecs.add(bullet.displayText());
234
							}
235
						}
236
					}
237
					break;
238
				}
239
			}
240
		}
241
		return keySpecs;
242
	}
243
 
244
	private List<String> getPackageContents(Entity entity){
245
		List<String> packageContents = null;
246
		Slide summarySlide = entity.getSlide(130025l);
9412 amit.gupta 247
		if (summarySlide != null){
248
			FreeformContent ffc = summarySlide.getFreeformContent();
249
			if(ffc != null){
250
				List<String> freeformTexts = summarySlide.getFreeformContent().getFreeformTexts();
251
				if(freeformTexts.size()>0){
252
					packageContents = Arrays.asList(StringUtils.join(freeformTexts, "\r\n").split("\r\n"));
253
				}
9410 amit.gupta 254
			}
9218 amit.gupta 255
		}
256
		return packageContents; 
257
	}
258
 
259
	public static void main (String [] args) throws Exception{
260
		PojoCreator creator = new PojoCreator();
261
		//System.out.println(creator.getShortContent(1007425l));
262
	}
263
 
9313 amit.gupta 264
	public void updateCatalogInfo(long entityId, String offerText, List<ItemPojo> items) {
9218 amit.gupta 265
		ContentPojo cp = new ContentPojo(entityId);
266
		cp.setOfferText(offerText);
267
		cp.setItems(items);
268
		StorageManager.addById(StorageManager.views.siteContent, entityId, cp);
269
	}
270
}