Subversion Repositories SmartDukaan

Rev

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