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