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