Subversion Repositories SmartDukaan

Rev

Rev 10564 | Rev 11230 | 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
 
9619 amit.gupta 3
import in.shop2020.metamodel.core.Bullet;
9218 amit.gupta 4
import in.shop2020.metamodel.core.Entity;
9280 amit.gupta 5
import in.shop2020.metamodel.core.ExpertReview;
9218 amit.gupta 6
import in.shop2020.metamodel.core.Feature;
9410 amit.gupta 7
import in.shop2020.metamodel.core.FreeformContent;
9218 amit.gupta 8
import in.shop2020.metamodel.core.Media;
9
import in.shop2020.metamodel.core.Slide;
10
import in.shop2020.metamodel.definitions.Catalog;
11
import in.shop2020.metamodel.definitions.DefinitionsContainer;
12
import in.shop2020.metamodel.definitions.SlideDefinition;
13
import in.shop2020.metamodel.util.ContentPojo;
11142 amit.gupta 14
import in.shop2020.metamodel.util.CreationUtils;
9218 amit.gupta 15
import in.shop2020.metamodel.util.ExpandedBullet;
9280 amit.gupta 16
import in.shop2020.metamodel.util.ExpandedEntity;
9218 amit.gupta 17
import in.shop2020.metamodel.util.ExpandedFeature;
9280 amit.gupta 18
import in.shop2020.metamodel.util.ExpertReviewPojo;
9218 amit.gupta 19
import in.shop2020.metamodel.util.ItemPojo;
9280 amit.gupta 20
import in.shop2020.metamodel.util.MediaPojo;
9218 amit.gupta 21
import in.shop2020.metamodel.util.Specification;
22
import in.shop2020.metamodel.util.SpecificationGroup;
23
import in.shop2020.storage.mongo.StorageManager;
24
import in.shop2020.ui.util.NewVUI;
25
 
26
import java.text.MessageFormat;
27
import java.util.ArrayList;
28
import java.util.Arrays;
29
import java.util.Collection;
30
import java.util.List;
31
 
32
import org.apache.commons.lang.StringUtils;
33
import org.apache.commons.logging.Log;
34
import org.apache.commons.logging.LogFactory;
35
 
36
public class PojoCreator {
37
	private static final String IMG_URL_TEMPLATE ="http://static{0}.saholic.com/images/media/{1}/{2}";
38
    private static Log log = LogFactory.getLog(PojoCreator.class);
39
	private static DefinitionsContainer defs = Catalog.getInstance().getDefinitionsContainer();
40
 
9280 amit.gupta 41
	public void createAndStoreContentPojo(ExpandedEntity expEntity, List<ExpertReview> expertReviews, String warranty){
9218 amit.gupta 42
		try {
9280 amit.gupta 43
			ContentPojo cp = new ContentPojo(EntityUtils.getProductName(expEntity), getKeySpecs(expEntity), getDetailedSpecs(expEntity), getWarranty());
44
			if(expertReviews != null && expertReviews.size()!=0){
45
				List<ExpertReviewPojo> erPojos = new ArrayList<ExpertReviewPojo>();
46
				for(ExpertReview er : expertReviews){
47
					ExpertReviewPojo erPojo = new ExpertReviewPojo();
48
					erPojo.setSource(er.getSource());
49
					erPojo.setReview(er.getReviewContent());
50
					erPojos.add(erPojo);
51
				}
52
				cp.setExpertReviews(erPojos);
53
			}
54
			cp.setPackageContents(getPackageContents(expEntity));
55
			cp.setWarranty(warranty);
56
			cp.setUrl(EntityUtils.getEntityNoSlashURL(expEntity));
57
			setImagesAndVideos(expEntity, cp);
9218 amit.gupta 58
			//StorageManager sm  = StorageManager.getStorageManager();
9280 amit.gupta 59
			StorageManager.insertOrUpdateById(StorageManager.views.siteContent, expEntity.getID(), cp);
9218 amit.gupta 60
		} catch (Exception e){
9280 amit.gupta 61
			log.info("Could not generate Pojo for entity:" + expEntity.getID());
9218 amit.gupta 62
			e.printStackTrace();
63
		}
64
	}
65
 
66
	private List<SpecificationGroup> getDetailedSpecs(Entity entity) throws Exception {
67
		List<SpecificationGroup> spGroups = new ArrayList<SpecificationGroup>();
68
		for (Slide slide : entity.getSlides()){
69
			if(slide != null) {
70
				long slideDefinitionId = slide.getSlideDefinitionID();
71
				if(slideDefinitionId == 130054l || slideDefinitionId == 130001l || slideDefinitionId == 130025l) continue;
72
 
73
				SlideDefinition slideDef = defs.getSlideDefinition(slideDefinitionId);
74
				List<Specification> specs = new ArrayList<Specification>();
75
				SpecificationGroup spGroup = new SpecificationGroup(slideDef.getLabel(), specs);
76
 
77
				if(slideDefinitionId == 130043l){
78
					List<String> values = new ArrayList<String>();
79
					List<Slide> slides= slide.getChildrenSlides();
80
					if(slides != null){
81
						for(Slide sl : slides){
82
							//For capacity slide
83
							if ( sl.getSlideDefinitionID() == 130046l){
84
								String one = "";
85
								String two = "";
86
								String three = "";
87
								List<Slide> capacityChildren = sl.getChildrenSlides();
88
								if(capacityChildren != null && capacityChildren.size()>0){
89
									for(Slide capSlide : capacityChildren){
90
										if (capSlide != null){
91
											if(capSlide.getSlideDefinitionID() == 130048l){
92
												one = "Talk time";
93
											}else if (capSlide.getSlideDefinitionID() == 130049l){
94
												one = "Standby time";
95
											}
96
											List<Feature> fs = capSlide.getFeatures();
97
											List<String> values1 = new ArrayList<String>();
98
											if(fs!=null){
99
												for(Feature f: fs){
100
													if (f != null){
101
														ExpandedFeature expFeature = new ExpandedFeature(f);
102
														two = expFeature.getFeatureDefinition().getLabel();
103
														List<ExpandedBullet> expBullets = expFeature.getExpandedBullets();
104
														if(expBullets != null){
105
															for(ExpandedBullet bullet : expBullets){
106
																if(bullet != null) {
107
																	three = bullet.displayText();
9619 amit.gupta 108
																	if(!three.equals("Not available")){
109
																		values1.add(MessageFormat.format(Utils.CAPACITY_TEMPLATE, one,two, three));
110
																	}
9218 amit.gupta 111
																}
112
															}
113
														}
114
													}
115
												}
116
											}
117
											if(values1.size()>0){
118
												values.add(StringUtils.join(values1,","));
119
											}
120
										}
121
									}
122
								}
123
								List<Feature> capacityFeatures = sl.getFeatures(); 
124
								if(capacityFeatures != null){
125
									for(Feature f: capacityFeatures){
126
										if (f != null){
127
											ExpandedFeature expFeature = new ExpandedFeature(f);
128
											String fLabel = expFeature.getFeatureDefinition().getLabel();
129
											List<ExpandedBullet> expBullets = expFeature.getExpandedBullets();
130
											List<String> bulletValuesList = new ArrayList<String>();
131
											if(expBullets != null){
132
												for(ExpandedBullet bullet : expBullets){
133
													if(bullet != null) {
134
														String value = bullet.displayText();
9619 amit.gupta 135
														if(!value.equals("Not available") && value != null && !value.trim().equals("")){
136
															bulletValuesList.add(value);
9218 amit.gupta 137
														}
138
													}
139
												}
140
											}
141
											if(bulletValuesList.size() > 0){
9619 amit.gupta 142
												values.add(fLabel  +  " - " + StringUtils.join(bulletValuesList, ", "));
9218 amit.gupta 143
											}
144
										}
145
									}
146
								}
147
								if(values.size()>0){
148
									Specification spec = new Specification("Capacity", values);
149
									specs.add(spec);
150
								}
151
							}
152
						}
153
					}
154
				}
11142 amit.gupta 155
				if(slide.getFeatures()== null || slide.getFeatures().size()==0){
156
					//Lets collect all the freeform if no features are available
157
					System.out.println("Lets collect all the freeform if no features are available");
158
					FreeformContent ffc = slide.getFreeformContent();
159
					List<String> f = new ArrayList<String>();
160
					if(ffc != null){
161
						List<String> freeformTexts = slide.getFreeformContent().getFreeformTexts();
162
						if(freeformTexts.size()>0){
163
							f = Arrays.asList(StringUtils.join(freeformTexts, "\r\n").split("\r\n"));
164
						}
165
						specs.add(new Specification("", f));
166
					}
10564 amit.gupta 167
					log.info("Got no features for slide: " + slide.getSlideDefinitionID() + " and entity: " + entity.getID());
168
				}else {
169
					for(Feature f : slide.getFeatures()) {
170
						List<String> sb = new ArrayList<String>();
171
						if (f != null){
172
							ExpandedFeature expFeature = new ExpandedFeature(f);
173
							expFeature.getFeatureDefinition().getLabel();
174
							List<ExpandedBullet> expBullets = expFeature.getExpandedBullets();
175
							if(expBullets != null){
176
								for(ExpandedBullet bullet : expBullets){
177
									if(bullet != null) {
178
										String displayText = bullet.displayText();
179
										if(!displayText.equals("") && !displayText.equals("Not available")){
180
											sb.add(displayText);
181
										}
11142 amit.gupta 182
									}else {
183
										if(expFeature.getFeatureDefinition().allowsBlank()){
184
											sb.add("Yes");
185
										}
9218 amit.gupta 186
									}
187
								}
188
							}
10564 amit.gupta 189
							if(sb.size()>0){
190
								Specification spec = new Specification(expFeature.getFeatureDefinition().getLabel(), Arrays.asList(StringUtils.join(sb,", ")));
191
								specs.add(spec);
192
							}
9218 amit.gupta 193
						}
194
					}
195
				}
11142 amit.gupta 196
 
9619 amit.gupta 197
				if(slideDefinitionId == 130133l){
198
					List<Slide> processorChildren = slide.getChildrenSlides();
199
					if(processorChildren != null) {
200
						Slide processorSlide = processorChildren.get(0);
201
						if(processorSlide != null) {
202
							List<Feature> processorFeatures = processorSlide.getFeatures();
203
							if(processorFeatures != null && processorFeatures.size()>0){
204
								List<String> sb = new ArrayList<String>();
205
								String processorString = "";
206
								String processorFfc = "";
207
								for(Feature f : processorFeatures) {
208
									if(f!=null){
209
										ExpandedFeature ef = new ExpandedFeature(f);
210
										if(f.getFeatureDefinitionID()==120326l)
211
										{
212
											List<ExpandedBullet> bullets = ef.getExpandedBullets();
213
											if(bullets !=null && bullets.size() >0){
214
												sb.add("Family -" + bullets.get(0).displayText());
215
											}
216
										}
217
										if(f.getFeatureDefinitionID()==120327l)
218
										{
219
											List<ExpandedBullet> bullets = ef.getExpandedBullets();
220
											if(bullets !=null && bullets.size() >0){
221
												processorString = bullets.get(0).displayText();
222
											}
223
											FreeformContent ffc = f.getFreeformContent();
224
											if(ffc != null){
225
												List<String> freeformTexts = ffc.getFreeformTexts();
226
												if(freeformTexts.size()>0){
227
													processorFfc = StringUtils.join(StringUtils.join(freeformTexts, "\r\n").split("\r\n"),", ");
228
												}
229
											} 
230
										}
231
										if(f.getFeatureDefinitionID()==120163l)
232
										{
233
											List<ExpandedBullet> bullets = ef.getExpandedBullets();
234
											if(bullets !=null && bullets.size() >0){
235
												processorString += " " + bullets.get(0).displayText();
236
												sb.add(processorString);
237
											}
238
											FreeformContent ffc = f.getFreeformContent();
239
											if(ffc != null){
240
												List<String> freeformTexts = ffc.getFreeformTexts();
241
												if(freeformTexts.size()>0){
242
													processorFfc += ", " + StringUtils.join(StringUtils.join(freeformTexts, "\r\n").split("\r\n"),", ");
243
												}
244
											} 
245
										}
246
									}
247
								}
248
								if(!processorFfc.equals("")) sb.add(processorFfc);
249
								if(sb.size()>0) {
250
									Specification spec = new Specification("Processor", Arrays.asList(StringUtils.join(sb,", ")));
251
									specs.add(spec);
252
								}
253
							}
254
						}
255
					}
256
				}
11142 amit.gupta 257
				if(spGroup!=null && spGroup.getSpecs() != null && spGroup.getSpecs().size()>0) {
258
					spGroups.add(spGroup);
259
				}
9218 amit.gupta 260
			}
261
		}
262
		return spGroups;
263
	}
264
 
9280 amit.gupta 265
	private void setImagesAndVideos(Entity entity, ContentPojo cp) {
9218 amit.gupta 266
 
267
		String fileNamePrefix = EntityUtils.getMediaPrefix(entity);
9280 amit.gupta 268
		List<MediaPojo> images = new ArrayList<MediaPojo>();
269
		List<MediaPojo> videos = new ArrayList<MediaPojo>();
9218 amit.gupta 270
		List<Slide> slides = entity.getSlides();
271
		String entityNumber = entity.getID() + "";
11142 amit.gupta 272
		Media defaultMedia = null; 
9218 amit.gupta 273
		if(slides!=null){
274
			for(Slide slide : slides){
275
				if (slide != null){
276
					if(slide.getSlideDefinitionID() == 130054l){
11142 amit.gupta 277
						defaultMedia = slide.getFreeformContent().getMedias().get("default");
278
						if(defaultMedia!=null){
279
							cp.setDefaultImageUrl(MessageFormat.format(IMG_URL_TEMPLATE, entity.getID()%3, entityNumber, NewVUI.computeNewFileName(fileNamePrefix, NewVUI.DEFAULT_JPG, String.valueOf(defaultMedia.getCreationTime().getTime()))));
280
							cp.setThumbnailImageUrl(MessageFormat.format(IMG_URL_TEMPLATE, entity.getID()%3, entityNumber, NewVUI.computeNewFileName(fileNamePrefix, NewVUI.THUMBNAIL_JPG, String.valueOf(defaultMedia.getCreationTime().getTime()))));
281
							cp.setIconImageUrl(MessageFormat.format(IMG_URL_TEMPLATE, entity.getID()%3, entityNumber, NewVUI.computeNewFileName(fileNamePrefix, NewVUI.ICON_JPG, String.valueOf(defaultMedia.getCreationTime().getTime()))));
9427 amit.gupta 282
						}else {
283
							log.info("Could not find default image for entity:" + entity.getID());
284
						}
9218 amit.gupta 285
					}
286
					else if(slide.getFreeformContent() !=null && slide.getFreeformContent().getMedias() !=null){
287
						Collection<Media> medias = slide.getFreeformContent().getMedias().values();
288
						for (Media media : medias){
289
							if(media.getType().equals(Media.Type.IMAGE)){
9280 amit.gupta 290
								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 291
								images.add(ip);
9280 amit.gupta 292
							}else if(media.getType().equals(Media.Type.VIDEO_WITH_SKIN) || media.getType().equals(Media.Type.VIDEO_WITH_SKIN)){
293
								MediaPojo vp = new MediaPojo(media.getLabel(), media.getYoutubeId());
294
								videos.add(vp);
9218 amit.gupta 295
							}
296
						}
297
					}
298
				}
299
			}
11142 amit.gupta 300
			if(images.size()==0){
301
				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())))));
302
			}
9218 amit.gupta 303
		}
304
		cp.setImages(images);
9280 amit.gupta 305
		cp.setVideos(videos);
9218 amit.gupta 306
	}
307
 
308
	private String getWarranty() {
309
		// TODO Auto-generated method stub
310
		return null;
311
	}
312
 
313
	private List<String> getKeySpecs(Entity entity) throws Exception {
314
		List<String> keySpecs = new ArrayList<String>();
315
		Slide summarySlide = entity.getSlide(130054l);
316
		List<Feature> summaryFeatures = summarySlide.getFeatures();
317
		if(summaryFeatures != null){
318
			for(Feature f : summaryFeatures) {
319
				if(f.getFeatureDefinitionID()==120081l){
320
					ExpandedFeature expFeature = new ExpandedFeature(f);
321
					expFeature.getFeatureDefinition().getLabel();
322
					List<ExpandedBullet> expBullets = expFeature.getExpandedBullets();
323
					if(expBullets != null){
324
						for(ExpandedBullet bullet : expBullets){
325
							if(bullet != null) {
9619 amit.gupta 326
								String displayText =  bullet.displayText();
327
								if(!displayText.equals("Not available"))
328
								keySpecs.add(displayText);
9218 amit.gupta 329
							}
330
						}
331
					}
332
					break;
333
				}
334
			}
335
		}
336
		return keySpecs;
337
	}
338
 
339
	private List<String> getPackageContents(Entity entity){
340
		List<String> packageContents = null;
341
		Slide summarySlide = entity.getSlide(130025l);
9412 amit.gupta 342
		if (summarySlide != null){
343
			FreeformContent ffc = summarySlide.getFreeformContent();
344
			if(ffc != null){
345
				List<String> freeformTexts = summarySlide.getFreeformContent().getFreeformTexts();
346
				if(freeformTexts.size()>0){
347
					packageContents = Arrays.asList(StringUtils.join(freeformTexts, "\r\n").split("\r\n"));
348
				}
9410 amit.gupta 349
			}
9218 amit.gupta 350
		}
351
		return packageContents; 
352
	}
353
 
354
	public static void main (String [] args) throws Exception{
11142 amit.gupta 355
 
9218 amit.gupta 356
		PojoCreator creator = new PojoCreator();
11142 amit.gupta 357
		ExpandedEntity exp = new ExpandedEntity(CreationUtils.getEntity(1003970l));
358
		creator.createAndStoreContentPojo(exp, null, "4 months Warranty");
359
		creator.updateCatalogInfo(1003970, "This is offer", null);
9218 amit.gupta 360
	}
361
 
9313 amit.gupta 362
	public void updateCatalogInfo(long entityId, String offerText, List<ItemPojo> items) {
9218 amit.gupta 363
		ContentPojo cp = new ContentPojo(entityId);
364
		cp.setOfferText(offerText);
365
		cp.setItems(items);
366
		StorageManager.addById(StorageManager.views.siteContent, entityId, cp);
367
	}
368
}