Subversion Repositories SmartDukaan

Rev

Rev 3574 | Details | Compare with Previous | Last modification | View Log | RSS feed

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