Subversion Repositories SmartDukaan

Rev

Rev 9427 | Rev 10564 | 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
				}
154
				for(Feature f : slide.getFeatures()) {
155
					List<String> sb = new ArrayList<String>();
156
					if (f != null){
157
						ExpandedFeature expFeature = new ExpandedFeature(f);
158
						expFeature.getFeatureDefinition().getLabel();
159
						List<ExpandedBullet> expBullets = expFeature.getExpandedBullets();
160
						if(expBullets != null){
161
							for(ExpandedBullet bullet : expBullets){
162
								if(bullet != null) {
163
									String displayText = bullet.displayText();
9619 amit.gupta 164
									if(!displayText.equals("") && !displayText.equals("Not available")){
165
										sb.add(displayText);
9218 amit.gupta 166
									}
167
								}
168
							}
169
						}
170
						if(sb.size()>0){
9619 amit.gupta 171
							Specification spec = new Specification(expFeature.getFeatureDefinition().getLabel(), Arrays.asList(StringUtils.join(sb,", ")));
9218 amit.gupta 172
							specs.add(spec);
173
						}
174
					}
175
				}
176
				spGroups.add(spGroup);
9619 amit.gupta 177
				if(slideDefinitionId == 130133l){
178
					List<Slide> processorChildren = slide.getChildrenSlides();
179
					if(processorChildren != null) {
180
						Slide processorSlide = processorChildren.get(0);
181
						if(processorSlide != null) {
182
							List<Feature> processorFeatures = processorSlide.getFeatures();
183
							if(processorFeatures != null && processorFeatures.size()>0){
184
								List<String> sb = new ArrayList<String>();
185
								String processorString = "";
186
								String processorFfc = "";
187
								for(Feature f : processorFeatures) {
188
									if(f!=null){
189
										ExpandedFeature ef = new ExpandedFeature(f);
190
										if(f.getFeatureDefinitionID()==120326l)
191
										{
192
											List<ExpandedBullet> bullets = ef.getExpandedBullets();
193
											if(bullets !=null && bullets.size() >0){
194
												sb.add("Family -" + bullets.get(0).displayText());
195
											}
196
										}
197
										if(f.getFeatureDefinitionID()==120327l)
198
										{
199
											List<ExpandedBullet> bullets = ef.getExpandedBullets();
200
											if(bullets !=null && bullets.size() >0){
201
												processorString = bullets.get(0).displayText();
202
											}
203
											FreeformContent ffc = f.getFreeformContent();
204
											if(ffc != null){
205
												List<String> freeformTexts = ffc.getFreeformTexts();
206
												if(freeformTexts.size()>0){
207
													processorFfc = StringUtils.join(StringUtils.join(freeformTexts, "\r\n").split("\r\n"),", ");
208
												}
209
											} 
210
										}
211
										if(f.getFeatureDefinitionID()==120163l)
212
										{
213
											List<ExpandedBullet> bullets = ef.getExpandedBullets();
214
											if(bullets !=null && bullets.size() >0){
215
												processorString += " " + bullets.get(0).displayText();
216
												sb.add(processorString);
217
											}
218
											FreeformContent ffc = f.getFreeformContent();
219
											if(ffc != null){
220
												List<String> freeformTexts = ffc.getFreeformTexts();
221
												if(freeformTexts.size()>0){
222
													processorFfc += ", " + StringUtils.join(StringUtils.join(freeformTexts, "\r\n").split("\r\n"),", ");
223
												}
224
											} 
225
										}
226
									}
227
								}
228
								if(!processorFfc.equals("")) sb.add(processorFfc);
229
								if(sb.size()>0) {
230
									Specification spec = new Specification("Processor", Arrays.asList(StringUtils.join(sb,", ")));
231
									specs.add(spec);
232
								}
233
							}
234
						}
235
					}
236
				}
9218 amit.gupta 237
			}
238
		}
239
		return spGroups;
240
	}
241
 
9280 amit.gupta 242
	private void setImagesAndVideos(Entity entity, ContentPojo cp) {
9218 amit.gupta 243
 
244
		String fileNamePrefix = EntityUtils.getMediaPrefix(entity);
9280 amit.gupta 245
		List<MediaPojo> images = new ArrayList<MediaPojo>();
246
		List<MediaPojo> videos = new ArrayList<MediaPojo>();
9218 amit.gupta 247
		List<Slide> slides = entity.getSlides();
248
		String entityNumber = entity.getID() + "";
249
		if(slides!=null){
250
 
251
			for(Slide slide : slides){
252
				if (slide != null){
253
					if(slide.getSlideDefinitionID() == 130054l){
254
						Media m = slide.getFreeformContent().getMedias().get("default");
9427 amit.gupta 255
						if(m!=null){
256
							cp.setDefaultImageUrl(MessageFormat.format(IMG_URL_TEMPLATE, entity.getID()%3, entityNumber, NewVUI.computeNewFileName(fileNamePrefix, NewVUI.DEFAULT_JPG, String.valueOf(m.getCreationTime().getTime()))));
257
							cp.setThumbnailImageUrl(MessageFormat.format(IMG_URL_TEMPLATE, entity.getID()%3, entityNumber, NewVUI.computeNewFileName(fileNamePrefix, NewVUI.THUMBNAIL_JPG, String.valueOf(m.getCreationTime().getTime()))));
258
							cp.setIconImageUrl(MessageFormat.format(IMG_URL_TEMPLATE, entity.getID()%3, entityNumber, NewVUI.computeNewFileName(fileNamePrefix, NewVUI.ICON_JPG, String.valueOf(m.getCreationTime().getTime()))));
259
						}else {
260
							log.info("Could not find default image for entity:" + entity.getID());
261
						}
9218 amit.gupta 262
					}
263
					else if(slide.getFreeformContent() !=null && slide.getFreeformContent().getMedias() !=null){
264
						Collection<Media> medias = slide.getFreeformContent().getMedias().values();
265
						for (Media media : medias){
266
							if(media.getType().equals(Media.Type.IMAGE)){
9280 amit.gupta 267
								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 268
								images.add(ip);
9280 amit.gupta 269
							}else if(media.getType().equals(Media.Type.VIDEO_WITH_SKIN) || media.getType().equals(Media.Type.VIDEO_WITH_SKIN)){
270
								MediaPojo vp = new MediaPojo(media.getLabel(), media.getYoutubeId());
271
								videos.add(vp);
9218 amit.gupta 272
							}
273
						}
274
					}
275
				}
276
			}
277
		}
278
		cp.setImages(images);
9280 amit.gupta 279
		cp.setVideos(videos);
9218 amit.gupta 280
	}
281
 
282
	private String getWarranty() {
283
		// TODO Auto-generated method stub
284
		return null;
285
	}
286
 
287
	private List<String> getKeySpecs(Entity entity) throws Exception {
288
		List<String> keySpecs = new ArrayList<String>();
289
		Slide summarySlide = entity.getSlide(130054l);
290
		List<Feature> summaryFeatures = summarySlide.getFeatures();
291
		if(summaryFeatures != null){
292
			for(Feature f : summaryFeatures) {
293
				if(f.getFeatureDefinitionID()==120081l){
294
					ExpandedFeature expFeature = new ExpandedFeature(f);
295
					expFeature.getFeatureDefinition().getLabel();
296
					List<ExpandedBullet> expBullets = expFeature.getExpandedBullets();
297
					if(expBullets != null){
298
						for(ExpandedBullet bullet : expBullets){
299
							if(bullet != null) {
9619 amit.gupta 300
								String displayText =  bullet.displayText();
301
								if(!displayText.equals("Not available"))
302
								keySpecs.add(displayText);
9218 amit.gupta 303
							}
304
						}
305
					}
306
					break;
307
				}
308
			}
309
		}
310
		return keySpecs;
311
	}
312
 
313
	private List<String> getPackageContents(Entity entity){
314
		List<String> packageContents = null;
315
		Slide summarySlide = entity.getSlide(130025l);
9412 amit.gupta 316
		if (summarySlide != null){
317
			FreeformContent ffc = summarySlide.getFreeformContent();
318
			if(ffc != null){
319
				List<String> freeformTexts = summarySlide.getFreeformContent().getFreeformTexts();
320
				if(freeformTexts.size()>0){
321
					packageContents = Arrays.asList(StringUtils.join(freeformTexts, "\r\n").split("\r\n"));
322
				}
9410 amit.gupta 323
			}
9218 amit.gupta 324
		}
325
		return packageContents; 
326
	}
327
 
328
	public static void main (String [] args) throws Exception{
329
		PojoCreator creator = new PojoCreator();
330
		//System.out.println(creator.getShortContent(1007425l));
331
	}
332
 
9313 amit.gupta 333
	public void updateCatalogInfo(long entityId, String offerText, List<ItemPojo> items) {
9218 amit.gupta 334
		ContentPojo cp = new ContentPojo(entityId);
335
		cp.setOfferText(offerText);
336
		cp.setItems(items);
337
		StorageManager.addById(StorageManager.views.siteContent, entityId, cp);
338
	}
339
}