Subversion Repositories SmartDukaan

Rev

Rev 953 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 953 Rev 1051
Line 1... Line 1...
1
/**
1
/**
2
 * 
2
 * 
3
 */
3
 */
4
package in.shop2020.creation.controllers;
4
package in.shop2020.creation.controllers;
5
 
5
 
6
import in.shop2020.creation.util.ContentValidator;
-
 
7
import in.shop2020.metamodel.util.CreationUtils;
6
import in.shop2020.metamodel.util.CreationUtils;
8
import in.shop2020.metamodel.core.Entity;
7
import in.shop2020.metamodel.core.Entity;
9
import in.shop2020.metamodel.core.Slide;
8
import in.shop2020.metamodel.core.Slide;
10
import in.shop2020.metamodel.definitions.Catalog;
9
import in.shop2020.metamodel.definitions.Catalog;
11
import in.shop2020.metamodel.definitions.Category;
10
import in.shop2020.metamodel.definitions.Category;
12
import in.shop2020.metamodel.definitions.CategorySlideDefinition;
11
import in.shop2020.metamodel.definitions.CategorySlideDefinition;
13
import in.shop2020.metamodel.definitions.DefinitionsContainer;
12
import in.shop2020.metamodel.definitions.DefinitionsContainer;
14
import in.shop2020.metamodel.definitions.EntityContainer;
-
 
15
 
13
 
16
import java.util.ArrayList;
14
import java.util.ArrayList;
17
import java.util.Arrays;
15
import java.util.Arrays;
18
import java.util.HashMap;
16
import java.util.HashMap;
19
import java.util.LinkedHashMap;
17
import java.util.LinkedHashMap;
20
import java.util.List;
18
import java.util.List;
21
import java.util.Map;
19
import java.util.Map;
22
 
20
 
23
import org.apache.juli.logging.Log;
21
import org.apache.juli.logging.Log;
24
import org.apache.juli.logging.LogFactory;
22
import org.apache.juli.logging.LogFactory;
-
 
23
import org.apache.struts2.convention.annotation.InterceptorRef;
-
 
24
import org.apache.struts2.convention.annotation.InterceptorRefs;
25
import org.apache.struts2.convention.annotation.Result;
25
import org.apache.struts2.convention.annotation.Result;
26
import org.apache.struts2.convention.annotation.Results;
26
import org.apache.struts2.convention.annotation.Results;
-
 
27
 
-
 
28
@InterceptorRefs({
27
import org.apache.struts2.interceptor.ParameterAware;
29
    @InterceptorRef("myDefault"),
-
 
30
    @InterceptorRef("login")
-
 
31
})
28
 
32
 
29
@Results({
33
@Results({
30
    @Result(name="success", type="redirectAction", 
34
    @Result(name="success", type="redirectAction", 
31
    		params = {"actionName" , "slides"}),
35
    		params = {"actionName" , "slides"}),
32
    @Result(name="redirect", location="${url}", type="redirect")
36
    @Result(name="redirect", location="${url}", type="redirect")
33
})
37
})
34
 
38
 
35
public class SlidesController implements ParameterAware {
39
public class SlidesController extends BaseController {
36
	
40
	
37
	/**
41
	/**
38
	 * 
42
	 * 
39
	 */
43
	 */
40
	private static final long serialVersionUID = 1L;
44
	private static final long serialVersionUID = 1L;
Line 48... Line 52...
48
	 * 
52
	 * 
49
	 */
53
	 */
50
	private String id;
54
	private String id;
51
	private String redirectURL;
55
	private String redirectURL;
52
	private Map<String, String[]> reqparams;
56
	private Map<String, String[]> reqparams;
53
	private EntityContainer ents;
-
 
54
	private DefinitionsContainer defs;
57
	private DefinitionsContainer defs = Catalog.getInstance().getDefinitionsContainer();
55
	private List<List<String[]>> allSlidesData = 
58
	private List<List<String[]>> allSlidesData = new ArrayList<List<String[]>>();
56
		new ArrayList<List<String[]>>();
-
 
57
	
59
	
58
	/**
60
	/**
59
	 * Key: Slide Definition Id
61
	 * Key: Slide Definition Id
60
	 * Value: Category Slide Definition
62
	 * Value: Category Slide Definition
61
	 */
63
	 */
62
	private Map<Long, CategorySlideDefinition> mapAllCatSlideDefs = 
64
	private Map<Long, CategorySlideDefinition> mapAllCatSlideDefs = 
63
		new HashMap<Long, CategorySlideDefinition>();
65
		new HashMap<Long, CategorySlideDefinition>();
64
 
66
 
65
	private Map<Long, List<Long>> catSlides;
-
 
66
 
-
 
67
	private Entity entity;
67
	private Entity entity;
68
	/**
68
	/**
69
	 * 
69
	 * 
70
	 * @return
70
	 * @return
71
	 */
71
	 */
Line 73... Line 73...
73
    	log.info("SlidesController.edit");
73
    	log.info("SlidesController.edit");
74
 
74
 
75
    	long entityID = Long.parseLong(this.getId());
75
    	long entityID = Long.parseLong(this.getId());
76
    	log.info("entityID:" + entityID);
76
    	log.info("entityID:" + entityID);
77
    	
77
    	
78
    	DefinitionsContainer defs = this.getDefinitionsContainer();
-
 
79
    	
-
 
80
    	try {
78
    	try {
81
    		Entity entity = this.getEntity(entityID);
79
    		entity = CreationUtils.getEntity(entityID);
82
			
80
			
83
    		List<Long> selection = CreationUtils.getSlideSequence(entityID, entity.getCategoryID());
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
    		}
84
    		log.info("selection:" + selection);
88
    		log.info("selection:" + selection);
85
    		
89
    		
86
    		// Entity's category first
90
    		// Entity's category first
87
			long catID = entity.getCategoryID();
91
			long catID = entity.getCategoryID();
88
			List<String[]> slidesData = this.getSlidesData(catID, selection, 
92
			List<String[]> slidesData = this.getSlidesData(catID, selection, 
Line 122... Line 126...
122
    		String[] values = this.reqparams.get(reqparam);
126
    		String[] values = this.reqparams.get(reqparam);
123
    		for(String value: values){
127
    		for(String value: values){
124
    			System.out.println("Param:   " + reqparam + "Value:   " + value);
128
    			System.out.println("Param:   " + reqparam + "Value:   " + value);
125
    		}
129
    		}
126
    	}
130
    	}
-
 
131
    	if(this.reqparams.get("reorder") != null){
-
 
132
    		long entityID = Long.parseLong(this.getId());
-
 
133
    		try {
-
 
134
				List<Long> slideSequence = new ArrayList<Long>();
-
 
135
				
-
 
136
			} catch (Exception e) {
-
 
137
				// TODO Auto-generated catch block
-
 
138
				e.printStackTrace();
-
 
139
			}
-
 
140
    		this.redirectURL = "/slides/" + this.getId() + "/edit";
-
 
141
	    	return "redirect";
127
    	
142
    	}
-
 
143
    	//FIXME
-
 
144
    	/*  Need to fix me while reordering
128
    	if(this.reqparams.get("slideid") != null){
145
    	if(this.reqparams.get("slideid") != null){
129
    		long slideId = Long.parseLong(this.reqparams.get("slideid")[0]);
146
    		long slideId = Long.parseLong(this.reqparams.get("slideid")[0]);
130
    		long afterSlideId = Long.parseLong(this.reqparams.get("afterslide")[0]);
147
    		long afterSlideId = Long.parseLong(this.reqparams.get("afterslide")[0]);
131
    		long entityID = Long.parseLong(this.getId());
148
    		long entityID = Long.parseLong(this.getId());
132
    		
149
    		
Line 139... Line 156...
139
//	    			newSlideSequence.add(new Long(slideId));
156
//	    			newSlideSequence.add(new Long(slideId));
140
//	    		}
157
//	    		}
141
//	    	}
158
//	    	}
142
    		
159
    		
143
    	try{
160
    	try{
144
    		EntityContainer ents = this.getEntityContainer();
-
 
145
	    	Entity entity = this.getEntity(entityID);
161
    		entity = CreationUtils.getEntity(entityID);
146
	    	Long catId = new Long(entity.getCategoryID());
-
 
147
	    	
162
	    	
-
 
163
	    	//Map<Long, List<Long>> catSlideSequence = CreationUtils.getRawSlideSequence(entityID);
-
 
164
    		
-
 
165
	    	Map<Long, List<Long>> catSlideSequence = defs.getCategorySlideSequence();
148
	    	
166
	    	
149
	    	Map<Long, List<Long>> catSlideSequence = CreationUtils.getRawSlideSequence(entityID);
-
 
150
	    	List<Long> allSlideSequence = catSlideSequence.get(new Long(-1));
167
	    	List<Long> allSlideSequence = catSlideSequence.get(new Long(-1));
151
	    	List<Long> newAllSlideSequence = new ArrayList<Long>();
168
	    	List<Long> newAllSlideSequence = new ArrayList<Long>();
152
	    	
169
	    	
153
	    	catSlideSequence.remove(new Long(-1));
170
	    	catSlideSequence.remove(new Long(-1));
154
	    	Map<Long, List<Long>> newCatSlideSequence = new HashMap<Long, List<Long>>();
171
	    	Map<Long, List<Long>> newCatSlideSequence = new HashMap<Long, List<Long>>();
Line 157... Line 174...
157
	    		newAllSlideSequence.add(slide);
174
	    		newAllSlideSequence.add(slide);
158
    			if(slide.equals(new Long(afterSlideId))){
175
    			if(slide.equals(new Long(afterSlideId))){
159
	    			newAllSlideSequence.add(new Long(slideId));
176
	    			newAllSlideSequence.add(new Long(slideId));
160
	    		}	
177
	    		}	
161
	    	}
178
	    	}
162
	    	
179
 
163
	    	DefinitionsContainer defs = Catalog.getInstance().getDefinitionsContainer();
-
 
164
	    	for(Long cat: catSlideSequence.keySet()){
180
	    	for(Long cat: catSlideSequence.keySet()){
165
	    		List<Long> newSlideSequence = new ArrayList<Long>();
181
	    		List<Long> newSlideSequence = new ArrayList<Long>();
166
	    		List<Long> slides =  catSlideSequence.get(cat);
182
	    		List<Long> slides =  catSlideSequence.get(cat);
167
	    		
183
	    		
168
	    		boolean contains = false;
184
	    		boolean contains = false;
Line 185... Line 201...
185
	    		newCatSlideSequence.put(cat, newSlideSequence);
201
	    		newCatSlideSequence.put(cat, newSlideSequence);
186
	    	}
202
	    	}
187
	    	
203
	    	
188
	    	newCatSlideSequence.put(new Long(-1), newAllSlideSequence);
204
	    	newCatSlideSequence.put(new Long(-1), newAllSlideSequence);
189
 
205
 
190
	    	CreationUtils.storeSlideSequence(entityID, newCatSlideSequence);
206
	    	//CreationUtils.storeSlideSequence(entityID, newCatSlideSequence);
-
 
207
	    	
-
 
208
	    	entity.setSlideSequence(newAllSlideSequence);
-
 
209
	    	
191
    	}catch(Exception e){
210
    	}catch(Exception e){
192
    		e.printStackTrace();
211
    		e.printStackTrace();
193
    	}
212
    	}
194
	    	this.redirectURL = "/slides/" + this.getId() + "/edit";
213
	    	this.redirectURL = "/slides/" + this.getId() + "/edit";
195
	    	return "redirect";
214
	    	return "redirect";
196
    	}
215
    	}
197
    	
216
    	
-
 
217
    	*/
198
    	
218
    	
199
    	try {
219
    	try {
200
    		long entityID = Long.parseLong(this.getId());    		
220
    		long entityID = Long.parseLong(this.getId());    		
201
	    	EntityContainer ents = this.getEntityContainer();
-
 
202
	    	Entity entity = this.getEntity(entityID);
221
    		entity = CreationUtils.getEntity(entityID);
203
	    	Long catId = new Long(entity.getCategoryID());
222
	    	Long catId = new Long(entity.getCategoryID());
204
	    	// Slides of entity's category
223
	    	// Slides of entity's category
205
	    	String[] strSlideDefIDs = this.reqparams.get(
224
	    	String[] strSlideDefIDs = this.reqparams.get(
206
	    			new Long(entity.getCategoryID()).toString());
225
	    			new Long(entity.getCategoryID()).toString());
207
	    	
226
	    	
208
	    	log.info("strSlideDefIDs:" + Arrays.toString(strSlideDefIDs));
227
	    	log.info("strSlideDefIDs:" + Arrays.toString(strSlideDefIDs));
209
	    	
228
	    	
210
	    	
-
 
211
	    	List<Long> sequence= CreationUtils.getSlideSequence(entityID);
229
	    	List<Long> sequence= entity.getSlideSequence();
-
 
230
	    	log.info("S1"+sequence);
212
        	if(sequence == null){
231
        	if(sequence == null){
213
        		sequence = new ArrayList<Long>();
232
        		sequence = new ArrayList<Long>();
-
 
233
        		entity.setSlideSequence(sequence);
214
        	}
234
        	}
215
        	
235
        	
216
	    	// To restore order of keys
-
 
217
	    	this.catSlides = new LinkedHashMap<Long, List<Long>>();
-
 
218
        	
236
        	
219
	    	List<Long> allSlideDefIDs = new ArrayList<Long>();
-
 
220
	    	
237
        	
221
	    	// Add entity's category slides first
238
	    	// Add entity's category slides first
222
    		List<Long> slideDefIDs = new ArrayList<Long>();
239
    		List<Long> slideDefIDs = new ArrayList<Long>();
223
    		for(String strSlideDefID : strSlideDefIDs) {
240
    		for(String strSlideDefID : strSlideDefIDs) {
224
    			slideDefIDs.add(new Long(Long.parseLong(strSlideDefID)));
241
    			slideDefIDs.add(new Long(Long.parseLong(strSlideDefID)));
225
    			if(!sequence.contains(new Long(Long.parseLong(strSlideDefID)))){
242
    			if(!sequence.contains(new Long(Long.parseLong(strSlideDefID)))){
226
    				sequence.add(new Long(Long.parseLong(strSlideDefID)));
243
    				sequence.add(new Long(Long.parseLong(strSlideDefID)));
227
    			}
244
    			}
228
    		}
245
    		}
229
    		this.catSlides.put(new Long(entity.getCategoryID()), slideDefIDs);
-
 
230
    		allSlideDefIDs.addAll(slideDefIDs);
-
 
231
    		
246
    		
232
        	DefinitionsContainer defs = this.getDefinitionsContainer();
247
        	log.info("S2"+sequence);
233
        	
-
 
234
			long parentCatID  = (defs.getCategory(entity.getCategoryID())).getParentCategory().getID();
248
			long parentCatID  = (defs.getCategory(entity.getCategoryID())).getParentCategory().getID();
235
 
249
 
236
        	List<Category> cats = defs.getChildrenCategories(parentCatID);
250
        	List<Category> cats = defs.getChildrenCategories(parentCatID);
237
        	
251
        	
238
        	// Add slides borrowed from other categories
252
        	// Add slides borrowed from other categories
239
        	for(Category cat : cats) {
253
        	for(Category cat : cats) {
240
        		Long lCat = new Long(cat.getID());
254
        		Long lCat = new Long(cat.getID());
241
        		
-
 
242
        		String[] strCatSlideDefIDs = this.reqparams.get(
255
        		String[] strCatSlideDefIDs = this.reqparams.get(lCat.toString());
243
        				lCat.toString());
-
 
244
        		
-
 
245
        		List<Long> catSlideDefIDs = new ArrayList<Long>();
-
 
246
        		if(strCatSlideDefIDs != null && strCatSlideDefIDs.length > 0) {
256
        		if(strCatSlideDefIDs != null && strCatSlideDefIDs.length > 0) {
247
            		for(String strCatSlideDefID : strCatSlideDefIDs) {
257
            		for(String strCatSlideDefID : strCatSlideDefIDs) {
248
            			catSlideDefIDs.add(new Long(Long.parseLong(
-
 
249
            					strCatSlideDefID)));
-
 
250
            			if(!lCat.equals(catId) && !sequence.contains(new Long(Long.parseLong(strCatSlideDefID)))){
258
            			if(!lCat.equals(catId) && !sequence.contains(new Long(Long.parseLong(strCatSlideDefID)))){
251
            				sequence.add(new Long(Long.parseLong(strCatSlideDefID)));
259
            				sequence.add(new Long(Long.parseLong(strCatSlideDefID)));
-
 
260
            				log.info("S3"+sequence);
252
            			}
261
            			}
253
            		}
262
            		}
254
        		}
263
        		}
255
        		
-
 
256
        		this.catSlides.put(lCat, catSlideDefIDs);
-
 
257
        		
-
 
258
        	}
264
        	}
259
        	this.catSlides.remove(new Long(-1));
-
 
260
        	
265
        	
-
 
266
 
-
 
267
        	//FIXME To remove not required slide ids and slides
261
        	// to remove obsolete elements
268
        	// to remove obsolete elements
-
 
269
        	/*
262
        	List<Long> removeList = new ArrayList<Long>();
270
        	List<Long> removeList = new ArrayList<Long>();
263
        	
271
        	
264
        	for(Long slideId: sequence){
272
        	for(Long slideId: sequence){
265
        		boolean isContains = false;
273
        		boolean isContains = false;
266
        		for(List<Long> catwiseList: this.catSlides.values()){
274
        		for(List<Long> catwiseList: this.catSlides.values()){
Line 270... Line 278...
270
        		}
278
        		}
271
        		if(!isContains){
279
        		if(!isContains){
272
        			removeList.add(slideId);
280
        			removeList.add(slideId);
273
        		}
281
        		}
274
        	}
282
        	}
-
 
283
        	log.info("To remove"+removeList);
275
        	for(Long slideId: removeList){
284
        	for(Long slideId: removeList){
276
        		sequence.remove(slideId);
285
        		sequence.remove(slideId);
277
        	}
286
        	}
-
 
287
        	*/
278
        	// end 
288
        	// end 
279
        	
289
        	
280
        	this.catSlides.put(new Long(-1), sequence);
-
 
281
        	log.info("this.catSlides:" + this.catSlides);
-
 
282
        	
290
        	
283
    		// Store for sequence reference
-
 
284
	    	CreationUtils.storeSlideSequence(entityID, this.catSlides);
-
 
285
	    	this.catSlides.remove(new Long(-1));
-
 
286
	    	// Delete slides no longer present in the list
291
	    	// Delete slides no longer present in the list
287
	    	List<Slide> slides = entity.getSlides();
292
	    	List<Slide> slides = entity.getSlides();
288
	    	if(slides != null) {
293
	    	if(slides != null) {
289
		    	List<Slide> deletedSlides = new ArrayList<Slide>();
294
		    	List<Slide> deletedSlides = new ArrayList<Slide>();
290
		    	for(Slide slide : slides) {
295
		    	for(Slide slide : slides) {
291
		    		Long lSlideDefID = new Long(slide.getSlideDefinitionID());
296
		    		Long lSlideDefID = new Long(slide.getSlideDefinitionID());
292
		    		
297
		    			
-
 
298
		    		log.info(sequence);
293
		    		if(!allSlideDefIDs.contains(lSlideDefID)) {
299
		    		if(!sequence.contains(lSlideDefID)) {
294
		    			log.info("deleted lSlideDefID:" + lSlideDefID);
300
		    			log.info("deleted lSlideDefID:" + lSlideDefID);
295
		    			deletedSlides.add(slide);
301
		    			deletedSlides.add(slide);
296
		    		}
302
		    		}
297
		    	}
303
		    	}
298
		    	
304
		    	
299
		    	for(Slide deletedSlide : deletedSlides) {
305
		    	for(Slide deletedSlide : deletedSlides) {
300
		    		slides.remove(deletedSlide);
306
		    		slides.remove(deletedSlide);
301
		    	}
307
		    	}
302
	    	}
308
	    	}
303
 
-
 
304
			ContentValidator validator = new ContentValidator();
-
 
305
			if(!validator.validate(entity)) {
-
 
306
				CreationUtils.addToIncomplete(entity);
-
 
307
				
309
	    	
308
				// Delete from repository if incomplete
-
 
309
				ents.deleteEntity(entity);
-
 
310
			}
-
 
311
			else {
-
 
312
				ents.updateEntity(entity);
310
	    	CreationUtils.updateEntity(entity);
313
			}
-
 
314
			
-
 
315
	    	// Store
-
 
316
	    	CreationUtils.rewriteRepository(ents.getEntities(), 
-
 
317
	    			ents.getEntitiesbyCategory());
-
 
318
	    	
311
	    	
319
	    	if(this.reqparams.containsKey("save")) {
312
	    	if(this.reqparams.containsKey("save")) {
320
		    	this.redirectURL = "/entity";
313
		    	this.redirectURL = "/entity";
321
	    	}
314
	    	}
322
	    	else {
315
	    	else {
Line 403... Line 396...
403
	 * @throws Exception
396
	 * @throws Exception
404
	 */
397
	 */
405
    private List<String[]> getSlidesData(long catID, List<Long> selection, 
398
    private List<String[]> getSlidesData(long catID, List<Long> selection, 
406
    		boolean isEntityCategory) throws Exception {
399
    		boolean isEntityCategory) throws Exception {
407
    	log.info("catID:" + catID);
400
    	log.info("catID:" + catID);
408
    	DefinitionsContainer defs = this.getDefinitionsContainer();
-
 
409
		List<Long> slideDefIDs = defs.getCategorySlideSequence(catID);
401
		List<Long> slideDefIDs = defs.getCategorySlideSequence(catID);
410
		log.info("slideDefIDs:" + slideDefIDs);
402
		log.info("slideDefIDs:" + slideDefIDs);
411
		
403
		
412
		List<CategorySlideDefinition> catSlideDefs = 
404
		List<CategorySlideDefinition> catSlideDefs = 
413
			defs.getCategorySlideDefinitions(catID);
405
			defs.getCategorySlideDefinitions(catID);
Line 469... Line 461...
469
			slidesData.add(data);
461
			slidesData.add(data);
470
		}
462
		}
471
		
463
		
472
		return slidesData;
464
		return slidesData;
473
	}
465
	}
474
	
-
 
475
	/**
-
 
476
	 * 
-
 
477
	 * @return
-
 
478
	 */
-
 
479
    protected EntityContainer getEntityContainer() {
-
 
480
    	if(this.ents == null) {
-
 
481
    		this.ents = new EntityContainer();
-
 
482
    	}
-
 
483
    	
466
    
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();
467
    public List<Long> getSlideSequence(){
494
    	}
-
 
495
    	
-
 
496
	    return this.defs;
468
    	return this.entity.getSlideSequence();
497
    }
469
    }
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
		
470
	
511
		return this.entity;
-
 
512
	}
-
 
513
}
471
}