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