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