Subversion Repositories SmartDukaan

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
242 naveen 1
/**
2
 * 
3
 */
4
package in.shop2020.creation.controllers;
5
 
250 naveen 6
import in.shop2020.creation.util.ContentValidator;
480 rajveer 7
import in.shop2020.metamodel.util.CreationUtils;
242 naveen 8
import in.shop2020.metamodel.core.Entity;
249 naveen 9
import in.shop2020.metamodel.core.Slide;
480 rajveer 10
import in.shop2020.metamodel.definitions.Catalog;
242 naveen 11
import in.shop2020.metamodel.definitions.Category;
12
import in.shop2020.metamodel.definitions.CategorySlideDefinition;
13
import in.shop2020.metamodel.definitions.DefinitionsContainer;
14
import in.shop2020.metamodel.definitions.EntityContainer;
15
 
16
import java.util.ArrayList;
17
import java.util.Arrays;
18
import java.util.HashMap;
250 naveen 19
import java.util.LinkedHashMap;
242 naveen 20
import java.util.List;
21
import java.util.Map;
22
 
23
import org.apache.juli.logging.Log;
24
import org.apache.juli.logging.LogFactory;
25
import org.apache.struts2.convention.annotation.Result;
26
import org.apache.struts2.convention.annotation.Results;
27
import org.apache.struts2.interceptor.ParameterAware;
28
 
29
@Results({
30
    @Result(name="success", type="redirectAction", 
31
    		params = {"actionName" , "slides"}),
32
    @Result(name="redirect", location="${url}", type="redirect")
33
})
34
 
35
public class SlidesController implements ParameterAware {
36
 
37
	/**
38
	 * 
39
	 */
40
	private static final long serialVersionUID = 1L;
41
 
42
	/**
43
	 * 
44
	 */
45
	private static Log log = LogFactory.getLog(SlidesController.class);
46
 
47
	/**
48
	 * 
49
	 */
50
	private String id;
51
	private String redirectURL;
52
	private Map<String, String[]> reqparams;
53
	private EntityContainer ents;
54
	private DefinitionsContainer defs;
55
	private List<List<String[]>> allSlidesData = 
56
		new ArrayList<List<String[]>>();
57
 
953 chandransh 58
	/**
59
	 * Key: Slide Definition Id
60
	 * Value: Category Slide Definition
61
	 */
250 naveen 62
	private Map<Long, CategorySlideDefinition> mapAllCatSlideDefs = 
242 naveen 63
		new HashMap<Long, CategorySlideDefinition>();
64
 
250 naveen 65
	private Map<Long, List<Long>> catSlides;
66
 
67
	private Entity entity;
242 naveen 68
	/**
69
	 * 
70
	 * @return
71
	 */
72
    public String edit() {
73
    	log.info("SlidesController.edit");
74
 
75
    	long entityID = Long.parseLong(this.getId());
76
    	log.info("entityID:" + entityID);
77
 
78
    	DefinitionsContainer defs = this.getDefinitionsContainer();
79
 
80
    	try {
250 naveen 81
    		Entity entity = this.getEntity(entityID);
242 naveen 82
 
310 rajveer 83
    		List<Long> selection = CreationUtils.getSlideSequence(entityID, entity.getCategoryID());
242 naveen 84
    		log.info("selection:" + selection);
85
 
86
    		// Entity's category first
87
			long catID = entity.getCategoryID();
88
			List<String[]> slidesData = this.getSlidesData(catID, selection, 
89
					true);
90
 
91
			this.allSlidesData.add(slidesData);
92
 
93
			// Get slides for rest of the categories
499 rajveer 94
			long parentCatID  = (defs.getCategory(catID)).getParentCategory().getID();
95
 
96
			List<Category> cats = defs.getChildrenCategories(parentCatID);
242 naveen 97
			for(Category cat : cats) {
98
				if(cat.getID() != catID) {
99
					List<String[]> thisSlidesData = 
100
						this.getSlidesData(cat.getID(), selection, false);
953 chandransh 101
					if(!thisSlidesData.isEmpty()){
102
						this.allSlidesData.add(thisSlidesData);
103
					}
242 naveen 104
				}
105
			}
106
 
107
		} catch (Exception e) {
108
			log.error(CreationUtils.getStackTrace(e));
109
			this.setErrorString(CreationUtils.getStackTrace(e));
110
			return "fatal";
111
		}
112
    	return "edit";
113
    }
114
 
115
    /**
116
     * 
117
     * @return
118
     */
119
    public String update() {
120
    	log.info("SlidesController.update");
480 rajveer 121
    	for(String reqparam: this.reqparams.keySet()){
122
    		String[] values = this.reqparams.get(reqparam);
123
    		for(String value: values){
124
    			System.out.println("Param:   " + reqparam + "Value:   " + value);
125
    		}
126
    	}
242 naveen 127
 
480 rajveer 128
    	if(this.reqparams.get("slideid") != null){
129
    		long slideId = Long.parseLong(this.reqparams.get("slideid")[0]);
130
    		long afterSlideId = Long.parseLong(this.reqparams.get("afterslide")[0]);
131
    		long entityID = Long.parseLong(this.getId());
132
 
133
//	    	List<Long> slideSequence = CreationUtils.getSlideSequence(entityID);
134
//	    	List<Long> newSlideSequence = new ArrayList<Long>();
135
//	    	
136
//	    	for(Long slide: slideSequence){
137
//	    		newSlideSequence.add(slide);
138
//	    		if(slide.equals(new Long(afterSlideId))){
139
//	    			newSlideSequence.add(new Long(slideId));
140
//	    		}
141
//	    	}
142
 
143
    	try{
144
    		EntityContainer ents = this.getEntityContainer();
145
	    	Entity entity = this.getEntity(entityID);
146
	    	Long catId = new Long(entity.getCategoryID());
147
 
148
 
149
	    	Map<Long, List<Long>> catSlideSequence = CreationUtils.getRawSlideSequence(entityID);
150
	    	List<Long> allSlideSequence = catSlideSequence.get(new Long(-1));
151
	    	List<Long> newAllSlideSequence = new ArrayList<Long>();
152
 
153
	    	catSlideSequence.remove(new Long(-1));
154
	    	Map<Long, List<Long>> newCatSlideSequence = new HashMap<Long, List<Long>>();
155
 
156
	    	for(Long slide: allSlideSequence){
157
	    		newAllSlideSequence.add(slide);
158
    			if(slide.equals(new Long(afterSlideId))){
159
	    			newAllSlideSequence.add(new Long(slideId));
160
	    		}	
161
	    	}
162
 
163
	    	DefinitionsContainer defs = Catalog.getInstance().getDefinitionsContainer();
164
	    	for(Long cat: catSlideSequence.keySet()){
165
	    		List<Long> newSlideSequence = new ArrayList<Long>();
166
	    		List<Long> slides =  catSlideSequence.get(cat);
167
 
168
	    		boolean contains = false;
169
	    		List<CategorySlideDefinition> catdefs = defs.getCategorySlideDefinitions(cat);
170
	    		for(CategorySlideDefinition  catdef: catdefs){
171
	    			if(catdef.getSlideDefintionID()==slideId){
172
	    				contains = true;
173
	    			}
174
	    		}
175
 
176
 
177
 
178
	    		for(Long slide: slides){
179
	    			newSlideSequence.add(slide);
180
	    		}
181
	    		if(contains){
182
	    			newSlideSequence.add(new Long(slideId));	
183
	    		}
184
 
185
	    		newCatSlideSequence.put(cat, newSlideSequence);
186
	    	}
187
 
188
	    	newCatSlideSequence.put(new Long(-1), newAllSlideSequence);
189
 
190
	    	CreationUtils.storeSlideSequence(entityID, newCatSlideSequence);
191
    	}catch(Exception e){
192
    		e.printStackTrace();
193
    	}
194
	    	this.redirectURL = "/slides/" + this.getId() + "/edit";
195
	    	return "redirect";
196
    	}
197
 
198
 
242 naveen 199
    	try {
250 naveen 200
    		long entityID = Long.parseLong(this.getId());    		
201
	    	EntityContainer ents = this.getEntityContainer();
202
	    	Entity entity = this.getEntity(entityID);
480 rajveer 203
	    	Long catId = new Long(entity.getCategoryID());
250 naveen 204
	    	// Slides of entity's category
205
	    	String[] strSlideDefIDs = this.reqparams.get(
206
	    			new Long(entity.getCategoryID()).toString());
207
 
208
	    	log.info("strSlideDefIDs:" + Arrays.toString(strSlideDefIDs));
209
 
480 rajveer 210
 
211
	    	List<Long> sequence= CreationUtils.getSlideSequence(entityID);
212
        	if(sequence == null){
213
        		sequence = new ArrayList<Long>();
214
        	}
215
 
250 naveen 216
	    	// To restore order of keys
217
	    	this.catSlides = new LinkedHashMap<Long, List<Long>>();
218
 
219
	    	List<Long> allSlideDefIDs = new ArrayList<Long>();
220
 
221
	    	// Add entity's category slides first
242 naveen 222
    		List<Long> slideDefIDs = new ArrayList<Long>();
223
    		for(String strSlideDefID : strSlideDefIDs) {
224
    			slideDefIDs.add(new Long(Long.parseLong(strSlideDefID)));
480 rajveer 225
    			if(!sequence.contains(new Long(Long.parseLong(strSlideDefID)))){
226
    				sequence.add(new Long(Long.parseLong(strSlideDefID)));
227
    			}
242 naveen 228
    		}
250 naveen 229
    		this.catSlides.put(new Long(entity.getCategoryID()), slideDefIDs);
230
    		allSlideDefIDs.addAll(slideDefIDs);
242 naveen 231
 
250 naveen 232
        	DefinitionsContainer defs = this.getDefinitionsContainer();
233
 
499 rajveer 234
			long parentCatID  = (defs.getCategory(entity.getCategoryID())).getParentCategory().getID();
235
 
236
        	List<Category> cats = defs.getChildrenCategories(parentCatID);
237
 
250 naveen 238
        	// Add slides borrowed from other categories
239
        	for(Category cat : cats) {
240
        		Long lCat = new Long(cat.getID());
480 rajveer 241
 
250 naveen 242
        		String[] strCatSlideDefIDs = this.reqparams.get(
243
        				lCat.toString());
480 rajveer 244
 
250 naveen 245
        		List<Long> catSlideDefIDs = new ArrayList<Long>();
246
        		if(strCatSlideDefIDs != null && strCatSlideDefIDs.length > 0) {
247
            		for(String strCatSlideDefID : strCatSlideDefIDs) {
248
            			catSlideDefIDs.add(new Long(Long.parseLong(
249
            					strCatSlideDefID)));
480 rajveer 250
            			if(!lCat.equals(catId) && !sequence.contains(new Long(Long.parseLong(strCatSlideDefID)))){
251
            				sequence.add(new Long(Long.parseLong(strCatSlideDefID)));
252
            			}
250 naveen 253
            		}
254
        		}
255
 
256
        		this.catSlides.put(lCat, catSlideDefIDs);
480 rajveer 257
 
250 naveen 258
        	}
480 rajveer 259
        	this.catSlides.remove(new Long(-1));
260
 
499 rajveer 261
        	// to remove obsolete elements
262
        	List<Long> removeList = new ArrayList<Long>();
480 rajveer 263
 
499 rajveer 264
        	for(Long slideId: sequence){
265
        		boolean isContains = false;
266
        		for(List<Long> catwiseList: this.catSlides.values()){
267
        			if(catwiseList.contains(slideId)){
268
        				isContains = true;
269
        			}
270
        		}
271
        		if(!isContains){
272
        			removeList.add(slideId);
273
        		}
274
        	}
275
        	for(Long slideId: removeList){
276
        		sequence.remove(slideId);
277
        	}
278
        	// end 
279
 
480 rajveer 280
        	this.catSlides.put(new Long(-1), sequence);
250 naveen 281
        	log.info("this.catSlides:" + this.catSlides);
282
 
249 naveen 283
    		// Store for sequence reference
250 naveen 284
	    	CreationUtils.storeSlideSequence(entityID, this.catSlides);
480 rajveer 285
	    	this.catSlides.remove(new Long(-1));
249 naveen 286
	    	// Delete slides no longer present in the list
287
	    	List<Slide> slides = entity.getSlides();
250 naveen 288
	    	if(slides != null) {
289
		    	List<Slide> deletedSlides = new ArrayList<Slide>();
290
		    	for(Slide slide : slides) {
291
		    		Long lSlideDefID = new Long(slide.getSlideDefinitionID());
292
 
293
		    		if(!allSlideDefIDs.contains(lSlideDefID)) {
294
		    			log.info("deleted lSlideDefID:" + lSlideDefID);
295
		    			deletedSlides.add(slide);
296
		    		}
297
		    	}
298
 
299
		    	for(Slide deletedSlide : deletedSlides) {
300
		    		slides.remove(deletedSlide);
301
		    	}
249 naveen 302
	    	}
250 naveen 303
 
304
			ContentValidator validator = new ContentValidator();
305
			if(!validator.validate(entity)) {
306
				CreationUtils.addToIncomplete(entity);
307
 
308
				// Delete from repository if incomplete
309
				ents.deleteEntity(entity);
310
			}
311
			else {
312
				ents.updateEntity(entity);
313
			}
314
 
315
	    	// Store
316
	    	CreationUtils.rewriteRepository(ents.getEntities(), 
317
	    			ents.getEntitiesbyCategory());
249 naveen 318
 
250 naveen 319
	    	if(this.reqparams.containsKey("save")) {
320
		    	this.redirectURL = "/entity";
249 naveen 321
	    	}
250 naveen 322
	    	else {
323
		    	this.redirectURL = "/entity/" + this.getId() + "/edit" +
324
		    		"?slideDefID=" + slideDefIDs.get(0);
325
	    	}
249 naveen 326
 
242 naveen 327
	        return "redirect";
328
		} catch (Exception e) {
329
			log.error(CreationUtils.getStackTrace(e));
330
			this.setErrorString(CreationUtils.getStackTrace(e));
331
			return "fatal";
332
		}
333
    }
334
 
335
    /**
336
     * 
337
     */
338
	@Override
339
	public void setParameters(Map<String, String[]> reqmap) {
340
		log.info("setParameters:" + reqmap);
341
 
342
		this.reqparams = reqmap;
343
	}
344
 
345
	/**
346
	 * @param id the id to set
347
	 */
348
	public void setId(String id) {
349
		this.id = id;
350
	}
351
 
352
	/**
353
	 * @return the id
354
	 */
355
	public String getId() {
356
		return id;
357
	}
358
 
359
	/**
360
	 * 
361
	 * @return
362
	 */
363
	public List<List<String[]>> getAllSlidesData() {
364
		return this.allSlidesData;
365
	}
366
 
367
	/**
368
	 * 
369
	 * @return
370
	 */
371
    public String getUrl() {
372
    	return this.redirectURL;
373
    }
374
 
375
	/**
376
	 * @param errorString the exceptionString to set
377
	 */
378
	public void setErrorString(String errorString) {
379
	}
380
 
381
	/**
382
	 * 
383
	 * @return
384
	 */
385
	public List<String> getMandatorySlideLabels() {
386
		List<String[]> slidesdata = this.allSlidesData.get(0);
387
 
388
		List<String> mandatorySlideLabels = new ArrayList<String>();
389
		for(String[] data : slidesdata) {
390
			if("Mandatory".equals(data[4])) {
391
				mandatorySlideLabels.add(data[3]);
392
			}
393
		}
394
 
395
		return mandatorySlideLabels;
396
	}
397
 
398
	/**
399
	 * 
400
	 * @param catID
401
	 * @param selection
402
	 * @return
403
	 * @throws Exception
404
	 */
405
    private List<String[]> getSlidesData(long catID, List<Long> selection, 
406
    		boolean isEntityCategory) throws Exception {
250 naveen 407
    	log.info("catID:" + catID);
242 naveen 408
    	DefinitionsContainer defs = this.getDefinitionsContainer();
409
		List<Long> slideDefIDs = defs.getCategorySlideSequence(catID);
250 naveen 410
		log.info("slideDefIDs:" + slideDefIDs);
242 naveen 411
 
412
		List<CategorySlideDefinition> catSlideDefs = 
413
			defs.getCategorySlideDefinitions(catID);
250 naveen 414
		log.info("catSlideDefs:" + catSlideDefs);
242 naveen 415
 
416
		// Convert it into Map, this will help put CategorySlideDefinition
417
		// objects in sequence
418
		// FIXME - Quick and dirty
419
		List<Long> excludeList = new ArrayList<Long>();
420
		for(CategorySlideDefinition catSlideDef : catSlideDefs) {
421
			Long slideDefID = new Long(catSlideDef.getSlideDefintionID());
422
 
423
			if(!this.mapAllCatSlideDefs.containsKey(slideDefID)) {
424
				this.mapAllCatSlideDefs.put(slideDefID, catSlideDef);
425
			}
426
			else {
427
				excludeList.add(slideDefID);
428
			}
429
		}
250 naveen 430
		log.info("excludeList:" + excludeList);
242 naveen 431
 
432
		List<String[]> slidesData = new ArrayList<String[]>();
433
 
434
		String categoryLabel = defs.getCategory(catID).getLabel();
435
 
436
		for(Long slideDefID : slideDefIDs) {
437
			// Do not repeat if already covered
438
			if(excludeList.contains(slideDefID)) {
439
				continue;
440
			}
441
 
953 chandransh 442
			// Category slide definition for the slide def id. 
443
			CategorySlideDefinition catSlideDef = this.mapAllCatSlideDefs.get(slideDefID);
444
			log.info("catSlideDef: " + catSlideDef);
242 naveen 445
 
953 chandransh 446
			String slideLabel = defs.getSlideDefinition(catSlideDef.getSlideDefintionID()).getLabel();
242 naveen 447
 
448
			Long lSlideDefID = new Long(catSlideDef.getSlideDefintionID());
449
 
450
			String selected = "0";
451
			if(selection != null) {
452
				selected = selection.contains(lSlideDefID) ? "1" : "0";
453
			}
454
			else if(isEntityCategory) {
455
				String edImp = catSlideDef.getEditorialImportance().toString();
456
				selected = (edImp.equals("Mandatory") || 
457
					edImp.endsWith("Recommended")) ? "1" : "0";
458
			}
459
 
460
			String[] data = new String[] {
461
				new Long(catID).toString(),
462
				categoryLabel,
463
				lSlideDefID.toString(),
464
				slideLabel,
465
				catSlideDef.getEditorialImportance().toString(),
466
				selected
467
			};
468
 
469
			slidesData.add(data);
470
		}
471
 
472
		return slidesData;
473
	}
474
 
475
	/**
476
	 * 
477
	 * @return
478
	 */
479
    protected EntityContainer getEntityContainer() {
480
    	if(this.ents == null) {
481
    		this.ents = new EntityContainer();
482
    	}
483
 
484
	    return this.ents;
485
    }
486
 
487
    /**
488
     * 
489
     * @return
490
     */
491
    protected DefinitionsContainer getDefinitionsContainer() {
492
    	if(this.defs == null) {
493
    		this.defs = new DefinitionsContainer();
494
    	}
495
 
496
	    return this.defs;
497
    }
250 naveen 498
 
499
    /**
500
     * 
501
     * @param entityID
502
     * @return
503
     * @throws Exception 
504
     */
505
    private Entity getEntity(long entityID) throws Exception {
506
    	log.info("#### SlidesController.getEntity ####");
507
		if(this.entity == null) {
508
			this.entity = CreationUtils.getEntity(entityID);
509
		}
510
 
511
		return this.entity;
512
	}
242 naveen 513
}