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;
242 naveen 7
import in.shop2020.creation.util.CreationUtils;
8
import in.shop2020.metamodel.core.Entity;
249 naveen 9
import in.shop2020.metamodel.core.Slide;
242 naveen 10
import in.shop2020.metamodel.definitions.Category;
11
import in.shop2020.metamodel.definitions.CategorySlideDefinition;
12
import in.shop2020.metamodel.definitions.DefinitionsContainer;
13
import in.shop2020.metamodel.definitions.EntityContainer;
14
 
15
import java.util.ArrayList;
16
import java.util.Arrays;
17
import java.util.HashMap;
250 naveen 18
import java.util.LinkedHashMap;
242 naveen 19
import java.util.List;
20
import java.util.Map;
21
 
22
import org.apache.juli.logging.Log;
23
import org.apache.juli.logging.LogFactory;
24
import org.apache.struts2.convention.annotation.Result;
25
import org.apache.struts2.convention.annotation.Results;
26
import org.apache.struts2.interceptor.ParameterAware;
27
 
28
@Results({
29
    @Result(name="success", type="redirectAction", 
30
    		params = {"actionName" , "slides"}),
31
    @Result(name="redirect", location="${url}", type="redirect")
32
})
33
 
34
public class SlidesController implements ParameterAware {
35
 
36
	/**
37
	 * 
38
	 */
39
	private static final long serialVersionUID = 1L;
40
 
41
	/**
42
	 * 
43
	 */
44
	private static Log log = LogFactory.getLog(SlidesController.class);
45
 
46
	/**
47
	 * 
48
	 */
49
	private String id;
50
	private String redirectURL;
51
	private Map<String, String[]> reqparams;
52
	private EntityContainer ents;
53
	private DefinitionsContainer defs;
54
	private List<List<String[]>> allSlidesData = 
55
		new ArrayList<List<String[]>>();
56
 
250 naveen 57
	private Map<Long, CategorySlideDefinition> mapAllCatSlideDefs = 
242 naveen 58
		new HashMap<Long, CategorySlideDefinition>();
59
 
250 naveen 60
	private Map<Long, List<Long>> catSlides;
61
 
62
	private Entity entity;
242 naveen 63
	/**
64
	 * 
65
	 * @return
66
	 */
67
    public String edit() {
68
    	log.info("SlidesController.edit");
69
 
70
    	long entityID = Long.parseLong(this.getId());
71
    	log.info("entityID:" + entityID);
72
 
73
    	DefinitionsContainer defs = this.getDefinitionsContainer();
74
 
75
    	try {
250 naveen 76
    		Entity entity = this.getEntity(entityID);
242 naveen 77
 
310 rajveer 78
    		List<Long> selection = CreationUtils.getSlideSequence(entityID, entity.getCategoryID());
242 naveen 79
    		log.info("selection:" + selection);
80
 
81
    		// Entity's category first
82
			long catID = entity.getCategoryID();
83
			List<String[]> slidesData = this.getSlidesData(catID, selection, 
84
					true);
85
 
86
			this.allSlidesData.add(slidesData);
87
 
88
			// Get slides for rest of the categories
89
			List<Category> cats = defs.getChildrenCategories(10001);
90
			for(Category cat : cats) {
91
				if(cat.getID() != catID) {
92
					List<String[]> thisSlidesData = 
93
						this.getSlidesData(cat.getID(), selection, false);
94
 
95
					this.allSlidesData.add(thisSlidesData);
96
				}
97
			}
98
 
99
		} catch (Exception e) {
100
			log.error(CreationUtils.getStackTrace(e));
101
			this.setErrorString(CreationUtils.getStackTrace(e));
102
			return "fatal";
103
		}
104
    	return "edit";
105
    }
106
 
107
    /**
108
     * 
109
     * @return
110
     */
111
    public String update() {
112
    	log.info("SlidesController.update");
113
 
114
    	try {
250 naveen 115
    		long entityID = Long.parseLong(this.getId());    		
116
	    	EntityContainer ents = this.getEntityContainer();
117
	    	Entity entity = this.getEntity(entityID);
118
 
119
	    	// Slides of entity's category
120
	    	String[] strSlideDefIDs = this.reqparams.get(
121
	    			new Long(entity.getCategoryID()).toString());
122
 
123
	    	log.info("strSlideDefIDs:" + Arrays.toString(strSlideDefIDs));
124
 
125
	    	// To restore order of keys
126
	    	this.catSlides = new LinkedHashMap<Long, List<Long>>();
127
 
128
	    	List<Long> allSlideDefIDs = new ArrayList<Long>();
129
 
130
	    	// Add entity's category slides first
242 naveen 131
    		List<Long> slideDefIDs = new ArrayList<Long>();
132
    		for(String strSlideDefID : strSlideDefIDs) {
133
    			slideDefIDs.add(new Long(Long.parseLong(strSlideDefID)));
134
    		}
250 naveen 135
    		this.catSlides.put(new Long(entity.getCategoryID()), slideDefIDs);
136
    		allSlideDefIDs.addAll(slideDefIDs);
242 naveen 137
 
250 naveen 138
        	DefinitionsContainer defs = this.getDefinitionsContainer();
139
        	List<Category> cats = defs.getChildrenCategories(10001L);
140
 
141
        	// Add slides borrowed from other categories
142
        	for(Category cat : cats) {
143
        		Long lCat = new Long(cat.getID());
144
        		String[] strCatSlideDefIDs = this.reqparams.get(
145
        				lCat.toString());
146
 
147
        		List<Long> catSlideDefIDs = new ArrayList<Long>();
148
        		if(strCatSlideDefIDs != null && strCatSlideDefIDs.length > 0) {
149
            		for(String strCatSlideDefID : strCatSlideDefIDs) {
150
            			catSlideDefIDs.add(new Long(Long.parseLong(
151
            					strCatSlideDefID)));
152
            		}
153
        		}
154
 
155
        		this.catSlides.put(lCat, catSlideDefIDs);
156
        		allSlideDefIDs.addAll(catSlideDefIDs);
157
        	}
158
        	log.info("this.catSlides:" + this.catSlides);
159
 
249 naveen 160
    		// Store for sequence reference
250 naveen 161
	    	CreationUtils.storeSlideSequence(entityID, this.catSlides);
242 naveen 162
 
249 naveen 163
	    	// Delete slides no longer present in the list
164
	    	List<Slide> slides = entity.getSlides();
250 naveen 165
	    	if(slides != null) {
166
		    	List<Slide> deletedSlides = new ArrayList<Slide>();
167
		    	for(Slide slide : slides) {
168
		    		Long lSlideDefID = new Long(slide.getSlideDefinitionID());
169
 
170
		    		if(!allSlideDefIDs.contains(lSlideDefID)) {
171
		    			log.info("deleted lSlideDefID:" + lSlideDefID);
172
		    			deletedSlides.add(slide);
173
		    		}
174
		    	}
175
 
176
		    	for(Slide deletedSlide : deletedSlides) {
177
		    		slides.remove(deletedSlide);
178
		    	}
249 naveen 179
	    	}
250 naveen 180
 
181
			ContentValidator validator = new ContentValidator();
182
			if(!validator.validate(entity)) {
183
				CreationUtils.addToIncomplete(entity);
184
 
185
				// Delete from repository if incomplete
186
				ents.deleteEntity(entity);
187
			}
188
			else {
189
				ents.updateEntity(entity);
190
			}
191
 
192
	    	// Store
193
	    	CreationUtils.rewriteRepository(ents.getEntities(), 
194
	    			ents.getEntitiesbyCategory());
249 naveen 195
 
250 naveen 196
	    	if(this.reqparams.containsKey("save")) {
197
		    	this.redirectURL = "/entity";
249 naveen 198
	    	}
250 naveen 199
	    	else {
200
		    	this.redirectURL = "/entity/" + this.getId() + "/edit" +
201
		    		"?slideDefID=" + slideDefIDs.get(0);
202
	    	}
249 naveen 203
 
242 naveen 204
	        return "redirect";
205
		} catch (Exception e) {
206
			log.error(CreationUtils.getStackTrace(e));
207
			this.setErrorString(CreationUtils.getStackTrace(e));
208
			return "fatal";
209
		}
210
    }
211
 
212
    /**
213
     * 
214
     */
215
	@Override
216
	public void setParameters(Map<String, String[]> reqmap) {
217
		log.info("setParameters:" + reqmap);
218
 
219
		this.reqparams = reqmap;
220
	}
221
 
222
	/**
223
	 * @param id the id to set
224
	 */
225
	public void setId(String id) {
226
		this.id = id;
227
	}
228
 
229
	/**
230
	 * @return the id
231
	 */
232
	public String getId() {
233
		return id;
234
	}
235
 
236
	/**
237
	 * 
238
	 * @return
239
	 */
240
	public List<List<String[]>> getAllSlidesData() {
241
		return this.allSlidesData;
242
	}
243
 
244
	/**
245
	 * 
246
	 * @return
247
	 */
248
    public String getUrl() {
249
    	return this.redirectURL;
250
    }
251
 
252
	/**
253
	 * @param errorString the exceptionString to set
254
	 */
255
	public void setErrorString(String errorString) {
256
	}
257
 
258
	/**
259
	 * 
260
	 * @return
261
	 */
262
	public List<String> getMandatorySlideLabels() {
263
		List<String[]> slidesdata = this.allSlidesData.get(0);
264
 
265
		List<String> mandatorySlideLabels = new ArrayList<String>();
266
		for(String[] data : slidesdata) {
267
			if("Mandatory".equals(data[4])) {
268
				mandatorySlideLabels.add(data[3]);
269
			}
270
		}
271
 
272
		return mandatorySlideLabels;
273
	}
274
 
275
	/**
276
	 * 
277
	 * @param catID
278
	 * @param selection
279
	 * @return
280
	 * @throws Exception
281
	 */
282
    private List<String[]> getSlidesData(long catID, List<Long> selection, 
283
    		boolean isEntityCategory) throws Exception {
250 naveen 284
    	log.info("catID:" + catID);
242 naveen 285
    	DefinitionsContainer defs = this.getDefinitionsContainer();
286
		List<Long> slideDefIDs = defs.getCategorySlideSequence(catID);
250 naveen 287
		log.info("slideDefIDs:" + slideDefIDs);
242 naveen 288
 
289
		List<CategorySlideDefinition> catSlideDefs = 
290
			defs.getCategorySlideDefinitions(catID);
250 naveen 291
		log.info("catSlideDefs:" + catSlideDefs);
242 naveen 292
 
293
		// Convert it into Map, this will help put CategorySlideDefinition
294
		// objects in sequence
295
		// FIXME - Quick and dirty
296
		List<Long> excludeList = new ArrayList<Long>();
297
		for(CategorySlideDefinition catSlideDef : catSlideDefs) {
298
			Long slideDefID = new Long(catSlideDef.getSlideDefintionID());
299
 
300
			if(!this.mapAllCatSlideDefs.containsKey(slideDefID)) {
301
				this.mapAllCatSlideDefs.put(slideDefID, catSlideDef);
302
			}
303
			else {
304
				excludeList.add(slideDefID);
305
			}
306
		}
250 naveen 307
		log.info("excludeList:" + excludeList);
242 naveen 308
 
309
		List<String[]> slidesData = new ArrayList<String[]>();
310
 
311
		String categoryLabel = defs.getCategory(catID).getLabel();
312
 
313
		for(Long slideDefID : slideDefIDs) {
314
			// Do not repeat if already covered
315
			if(excludeList.contains(slideDefID)) {
316
				continue;
317
			}
318
 
319
			CategorySlideDefinition catSlideDef = 
320
				this.mapAllCatSlideDefs.get(slideDefID);
321
 
322
			String slideLabel = defs.getSlideDefinition(
323
					catSlideDef.getSlideDefintionID()).getLabel();
324
 
325
			Long lSlideDefID = new Long(catSlideDef.getSlideDefintionID());
326
 
327
			String selected = "0";
328
			if(selection != null) {
329
				selected = selection.contains(lSlideDefID) ? "1" : "0";
330
			}
331
			else if(isEntityCategory) {
332
				String edImp = catSlideDef.getEditorialImportance().toString();
333
				selected = (edImp.equals("Mandatory") || 
334
					edImp.endsWith("Recommended")) ? "1" : "0";
335
			}
336
 
337
			String[] data = new String[] {
338
				new Long(catID).toString(),
339
				categoryLabel,
340
				lSlideDefID.toString(),
341
				slideLabel,
342
				catSlideDef.getEditorialImportance().toString(),
343
				selected
344
			};
345
 
346
			slidesData.add(data);
347
		}
348
 
349
		return slidesData;
350
	}
351
 
352
	/**
353
	 * 
354
	 * @return
355
	 */
356
    protected EntityContainer getEntityContainer() {
357
    	if(this.ents == null) {
358
    		this.ents = new EntityContainer();
359
    	}
360
 
361
	    return this.ents;
362
    }
363
 
364
    /**
365
     * 
366
     * @return
367
     */
368
    protected DefinitionsContainer getDefinitionsContainer() {
369
    	if(this.defs == null) {
370
    		this.defs = new DefinitionsContainer();
371
    	}
372
 
373
	    return this.defs;
374
    }
250 naveen 375
 
376
    /**
377
     * 
378
     * @param entityID
379
     * @return
380
     * @throws Exception 
381
     */
382
    private Entity getEntity(long entityID) throws Exception {
383
    	log.info("#### SlidesController.getEntity ####");
384
		if(this.entity == null) {
385
			this.entity = CreationUtils.getEntity(entityID);
386
		}
387
 
388
		return this.entity;
389
	}
242 naveen 390
}