Subversion Repositories SmartDukaan

Rev

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