Subversion Repositories SmartDukaan

Rev

Rev 20423 | 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
				}
11235 amit.gupta 155
				if((slide.getFeatures()== null || slide.getFeatures().size()==0) && (slide.getChildrenSlides()==null || slide.getChildrenSlides().size()==0 )){
11142 amit.gupta 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){
11230 amit.gupta 163
							String appended = StringUtils.join(StringUtils.join(freeformTexts, "\r\n").split("\r\n"),", ");
164
							if(appended.length() > 0) {
165
								f = new ArrayList<String>(Arrays.asList(StringUtils.join(freeformTexts, "\r\n").split("\r\n")));
166
								specs.add(new Specification("", f));
167
							}
11142 amit.gupta 168
						}
169
					}
10564 amit.gupta 170
					log.info("Got no features for slide: " + slide.getSlideDefinitionID() + " and entity: " + entity.getID());
171
				}else {
172
					for(Feature f : slide.getFeatures()) {
173
						List<String> sb = new ArrayList<String>();
174
						if (f != null){
175
							ExpandedFeature expFeature = new ExpandedFeature(f);
176
							expFeature.getFeatureDefinition().getLabel();
177
							List<ExpandedBullet> expBullets = expFeature.getExpandedBullets();
11230 amit.gupta 178
							if(expBullets != null && expBullets.size() > 0){
10564 amit.gupta 179
								for(ExpandedBullet bullet : expBullets){
180
									if(bullet != null) {
181
										String displayText = bullet.displayText();
182
										if(!displayText.equals("") && !displayText.equals("Not available")){
183
											sb.add(displayText);
11230 amit.gupta 184
										} else {
185
											if(expBullets.size()==1 && expFeature.getFeatureDefinition().allowsBlank()){
186
												sb.add("Yes");
187
											}
10564 amit.gupta 188
										}
9218 amit.gupta 189
									}
190
								}
11230 amit.gupta 191
							} else {
192
								if(expFeature.getFeatureDefinition().allowsBlank()){
193
										sb.add("Yes");
194
								}
9218 amit.gupta 195
							}
10564 amit.gupta 196
							if(sb.size()>0){
197
								Specification spec = new Specification(expFeature.getFeatureDefinition().getLabel(), Arrays.asList(StringUtils.join(sb,", ")));
198
								specs.add(spec);
199
							}
9218 amit.gupta 200
						}
201
					}
202
				}
11142 amit.gupta 203
 
9619 amit.gupta 204
				if(slideDefinitionId == 130133l){
205
					List<Slide> processorChildren = slide.getChildrenSlides();
206
					if(processorChildren != null) {
207
						Slide processorSlide = processorChildren.get(0);
208
						if(processorSlide != null) {
209
							List<Feature> processorFeatures = processorSlide.getFeatures();
210
							if(processorFeatures != null && processorFeatures.size()>0){
211
								List<String> sb = new ArrayList<String>();
212
								String processorString = "";
213
								String processorFfc = "";
214
								for(Feature f : processorFeatures) {
215
									if(f!=null){
216
										ExpandedFeature ef = new ExpandedFeature(f);
217
										if(f.getFeatureDefinitionID()==120326l)
218
										{
219
											List<ExpandedBullet> bullets = ef.getExpandedBullets();
220
											if(bullets !=null && bullets.size() >0){
221
												sb.add("Family -" + bullets.get(0).displayText());
222
											}
223
										}
224
										if(f.getFeatureDefinitionID()==120327l)
225
										{
226
											List<ExpandedBullet> bullets = ef.getExpandedBullets();
227
											if(bullets !=null && bullets.size() >0){
228
												processorString = bullets.get(0).displayText();
229
											}
230
											FreeformContent ffc = f.getFreeformContent();
231
											if(ffc != null){
232
												List<String> freeformTexts = ffc.getFreeformTexts();
233
												if(freeformTexts.size()>0){
11230 amit.gupta 234
													String appended = StringUtils.join(StringUtils.join(freeformTexts, "\r\n").split("\r\n"),", ");
235
													if(appended.length() > 0)
236
														processorFfc = appended;
9619 amit.gupta 237
												}
238
											} 
239
										}
240
										if(f.getFeatureDefinitionID()==120163l)
241
										{
242
											List<ExpandedBullet> bullets = ef.getExpandedBullets();
243
											if(bullets !=null && bullets.size() >0){
244
												processorString += " " + bullets.get(0).displayText();
245
												sb.add(processorString);
246
											}
247
											FreeformContent ffc = f.getFreeformContent();
248
											if(ffc != null){
249
												List<String> freeformTexts = ffc.getFreeformTexts();
250
												if(freeformTexts.size()>0){
11230 amit.gupta 251
													String appended = StringUtils.join(StringUtils.join(freeformTexts, "\r\n").split("\r\n"),", ");
252
													if(appended.length() > 0) processorFfc += ", " + appended;
9619 amit.gupta 253
												}
254
											} 
255
										}
256
									}
257
								}
258
								if(!processorFfc.equals("")) sb.add(processorFfc);
259
								if(sb.size()>0) {
260
									Specification spec = new Specification("Processor", Arrays.asList(StringUtils.join(sb,", ")));
261
									specs.add(spec);
262
								}
263
							}
264
						}
265
					}
266
				}
11142 amit.gupta 267
				if(spGroup!=null && spGroup.getSpecs() != null && spGroup.getSpecs().size()>0) {
268
					spGroups.add(spGroup);
269
				}
9218 amit.gupta 270
			}
271
		}
272
		return spGroups;
273
	}
274
 
9280 amit.gupta 275
	private void setImagesAndVideos(Entity entity, ContentPojo cp) {
9218 amit.gupta 276
 
277
		String fileNamePrefix = EntityUtils.getMediaPrefix(entity);
9280 amit.gupta 278
		List<MediaPojo> images = new ArrayList<MediaPojo>();
279
		List<MediaPojo> videos = new ArrayList<MediaPojo>();
9218 amit.gupta 280
		List<Slide> slides = entity.getSlides();
281
		String entityNumber = entity.getID() + "";
11142 amit.gupta 282
		Media defaultMedia = null; 
9218 amit.gupta 283
		if(slides!=null){
284
			for(Slide slide : slides){
285
				if (slide != null){
286
					if(slide.getSlideDefinitionID() == 130054l){
11142 amit.gupta 287
						defaultMedia = slide.getFreeformContent().getMedias().get("default");
288
						if(defaultMedia!=null){
289
							cp.setDefaultImageUrl(MessageFormat.format(IMG_URL_TEMPLATE, entity.getID()%3, entityNumber, NewVUI.computeNewFileName(fileNamePrefix, NewVUI.DEFAULT_JPG, String.valueOf(defaultMedia.getCreationTime().getTime()))));
290
							cp.setThumbnailImageUrl(MessageFormat.format(IMG_URL_TEMPLATE, entity.getID()%3, entityNumber, NewVUI.computeNewFileName(fileNamePrefix, NewVUI.THUMBNAIL_JPG, String.valueOf(defaultMedia.getCreationTime().getTime()))));
291
							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 292
						}else {
293
							log.info("Could not find default image for entity:" + entity.getID());
294
						}
9218 amit.gupta 295
					}
296
					else if(slide.getFreeformContent() !=null && slide.getFreeformContent().getMedias() !=null){
22631 amit.gupta 297
						log.info("Slide ID - for media:" + slide.getSlideDefinitionID());
9218 amit.gupta 298
						Collection<Media> medias = slide.getFreeformContent().getMedias().values();
299
						for (Media media : medias){
300
							if(media.getType().equals(Media.Type.IMAGE)){
9280 amit.gupta 301
								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 302
								images.add(ip);
9280 amit.gupta 303
							}else if(media.getType().equals(Media.Type.VIDEO_WITH_SKIN) || media.getType().equals(Media.Type.VIDEO_WITH_SKIN)){
304
								MediaPojo vp = new MediaPojo(media.getLabel(), media.getYoutubeId());
305
								videos.add(vp);
9218 amit.gupta 306
							}
307
						}
308
					}
20423 amit.gupta 309
					if(slide.getSlideDefinitionID()==130001l){
310
						try{
311
							cp.setIntroduction(slide.getFreeformContent().getFreeformTexts().get(0));
312
						}catch(Exception e){
313
							log.info("Could not add introduction.");
314
						}
315
					}
9218 amit.gupta 316
				}
317
			}
11142 amit.gupta 318
			if(images.size()==0){
22631 amit.gupta 319
				images.add(new MediaPojo(defaultMedia.getLabel(), MessageFormat.format(IMG_URL_TEMPLATE, entity.getID()%3, 
320
																	entityNumber, 
321
																	NewVUI.computeNewFileName(fileNamePrefix, defaultMedia.getFileName(), 
322
																	String.valueOf(defaultMedia.getCreationTime().getTime())))));
11142 amit.gupta 323
			}
9218 amit.gupta 324
		}
325
		cp.setImages(images);
9280 amit.gupta 326
		cp.setVideos(videos);
9218 amit.gupta 327
	}
328
 
329
	private String getWarranty() {
330
		// TODO Auto-generated method stub
331
		return null;
332
	}
333
 
334
	private List<String> getKeySpecs(Entity entity) throws Exception {
335
		List<String> keySpecs = new ArrayList<String>();
336
		Slide summarySlide = entity.getSlide(130054l);
337
		List<Feature> summaryFeatures = summarySlide.getFeatures();
338
		if(summaryFeatures != null){
339
			for(Feature f : summaryFeatures) {
340
				if(f.getFeatureDefinitionID()==120081l){
341
					ExpandedFeature expFeature = new ExpandedFeature(f);
342
					expFeature.getFeatureDefinition().getLabel();
343
					List<ExpandedBullet> expBullets = expFeature.getExpandedBullets();
344
					if(expBullets != null){
345
						for(ExpandedBullet bullet : expBullets){
346
							if(bullet != null) {
9619 amit.gupta 347
								String displayText =  bullet.displayText();
348
								if(!displayText.equals("Not available"))
349
								keySpecs.add(displayText);
9218 amit.gupta 350
							}
351
						}
352
					}
353
					break;
354
				}
355
			}
356
		}
357
		return keySpecs;
358
	}
359
 
360
	private List<String> getPackageContents(Entity entity){
361
		List<String> packageContents = null;
362
		Slide summarySlide = entity.getSlide(130025l);
9412 amit.gupta 363
		if (summarySlide != null){
364
			FreeformContent ffc = summarySlide.getFreeformContent();
365
			if(ffc != null){
366
				List<String> freeformTexts = summarySlide.getFreeformContent().getFreeformTexts();
367
				if(freeformTexts.size()>0){
11230 amit.gupta 368
					String appended = StringUtils.join(StringUtils.join(freeformTexts, "\r\n").split("\r\n"),", ");
369
					if(appended.length()>0) {
370
						packageContents = Arrays.asList(StringUtils.join(freeformTexts, "\r\n").split("\r\n"));
371
					}
9412 amit.gupta 372
				}
9410 amit.gupta 373
			}
9218 amit.gupta 374
		}
375
		return packageContents; 
376
	}
377
 
378
	public static void main (String [] args) throws Exception{
11142 amit.gupta 379
 
9218 amit.gupta 380
		PojoCreator creator = new PojoCreator();
11142 amit.gupta 381
		ExpandedEntity exp = new ExpandedEntity(CreationUtils.getEntity(1003970l));
22631 amit.gupta 382
		creator.setImagesAndVideos(CreationUtils.getEntity(1005480l), new ContentPojo(1005480l));
9218 amit.gupta 383
	}
384
 
9313 amit.gupta 385
	public void updateCatalogInfo(long entityId, String offerText, List<ItemPojo> items) {
9218 amit.gupta 386
		ContentPojo cp = new ContentPojo(entityId);
387
		cp.setOfferText(offerText);
388
		cp.setItems(items);
389
		StorageManager.addById(StorageManager.views.siteContent, entityId, cp);
390
	}
391
}