Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
208 naveen 1
/**
2
 * 
3
 */
4
package in.shop2020.creation.util;
5
 
249 naveen 6
import in.shop2020.metamodel.core.Entity;
310 rajveer 7
import in.shop2020.metamodel.core.Slide;
8
import in.shop2020.metamodel.definitions.Catalog;
9
import in.shop2020.metamodel.definitions.CategorySlideDefinition;
10
import in.shop2020.metamodel.definitions.DefinitionsContainer;
11
import in.shop2020.metamodel.definitions.SlideDefinition;
210 naveen 12
import in.shop2020.util.DBUtils;
13
import in.shop2020.util.Utils;
14
 
242 naveen 15
import java.io.File;
208 naveen 16
import java.io.PrintWriter;
17
import java.io.StringWriter;
18
import java.io.Writer;
210 naveen 19
import java.util.ArrayList;
310 rajveer 20
import java.util.HashMap;
210 naveen 21
import java.util.List;
22
import java.util.Map;
208 naveen 23
 
250 naveen 24
import org.apache.juli.logging.Log;
25
import org.apache.juli.logging.LogFactory;
26
 
208 naveen 27
/**
28
 * @author naveen
29
 *
30
 */
31
public class CreationUtils {
250 naveen 32
 
33
	/**
34
	 * 
35
	 */
36
	private static Log log = LogFactory.getLog(CreationUtils.class);
242 naveen 37
 
208 naveen 38
	/**
39
	 * 
210 naveen 40
	 * @param entityID
41
	 * @return
42
	 * @throws Exception 
43
	 */
44
	public static List<String> getMediaLabels(long entityID) throws Exception {
45
		Map<String, Media> rawMedia = getRawMedia(entityID);
46
 
47
		List<String> labels = new ArrayList<String>();
48
		for(Media media : rawMedia.values()) {
49
			labels.add(media.getLabel());
50
		}
51
 
52
		return labels;
53
	}
54
 
55
	/**
56
	 * 
57
	 * @param entityID
58
	 * @param type
59
	 * @return
60
	 * @throws Exception
61
	 */
62
	public static List<String> getMediaLabels(long entityID, String type) 
63
		throws Exception {
64
		Map<String, Media> rawMedia = getRawMedia(entityID);
65
 
66
		List<String> labels = new ArrayList<String>();
67
		for(Media media : rawMedia.values()) {
68
			if(media.getType().equals(type)) {
69
				labels.add(media.getLabel());
70
			}
71
		}
72
 
73
		return labels;
74
	}
75
 
76
	/**
77
	 * 
78
	 * @param entityID
79
	 * @return
80
	 * @throws Exception 
81
	 */
82
	@SuppressWarnings("unchecked")
83
	public static Map<String, Media> getRawMedia(long entityID) 
84
		throws Exception {
85
		String dbFile = Utils.MEDIA_DB_PATH + entityID + ".ser";
86
		return (Map<String, Media>)DBUtils.read(dbFile);
87
	}
88
 
89
	/**
90
	 * 
242 naveen 91
	 * @param entityID
92
	 * @param label
93
	 * @throws Exception
94
	 */
95
	public static void removeMedia(long entityID, String label) 
96
		throws Exception {
97
		String dbFile = Utils.MEDIA_DB_PATH + entityID + ".ser";
98
 
99
		Map<String, Media> media = getRawMedia(entityID);
100
		media.remove(label);
101
 
102
		DBUtils.store(media, dbFile);
103
	}
104
	/**
105
	 * 
208 naveen 106
	 * @param aThrowable
107
	 * @return
108
	 */
109
	public static String getStackTrace(Throwable aThrowable) {
110
	    final Writer result = new StringWriter();
111
	    final PrintWriter printWriter = new PrintWriter(result);
112
	    aThrowable.printStackTrace(printWriter);
113
	    return result.toString();
114
	}
242 naveen 115
 
116
	/**
117
	 * 
118
	 * @param entityID
119
	 * @param slideDefIDs
120
	 */
121
	public static void storeSlideSequence(long entityID, 
250 naveen 122
			Map<Long, List<Long>> catSlideDefIDs) throws Exception {
242 naveen 123
		String entityDirPath = Utils.ENTITIES_DB_PATH + entityID;
250 naveen 124
		log.info("entityDirPath:" + entityDirPath);
125
 
242 naveen 126
		File entityDir = new File(entityDirPath);
127
		if(!entityDir.exists()) {
128
			entityDir.mkdir();
129
		}
130
 
131
		String slideSeqFileName = entityDirPath + "/slidesequence.ser";
250 naveen 132
		log.info("slideSeqFileName:" + slideSeqFileName);
133
 
242 naveen 134
		File slideSeqFile = new File(slideSeqFileName);
250 naveen 135
		if(slideSeqFile.exists()) {
136
			slideSeqFile.delete();
242 naveen 137
		}
250 naveen 138
 
139
		slideSeqFile.createNewFile();
242 naveen 140
 
250 naveen 141
		DBUtils.store(catSlideDefIDs, slideSeqFileName);
242 naveen 142
	}
143
 
144
	/**
145
	 * 
146
	 * @param entityID
147
	 * @return
250 naveen 148
	 * @throws Exception
242 naveen 149
	 */
150
	@SuppressWarnings("unchecked")
250 naveen 151
	public static Map<Long, List<Long>> getRawSlideSequence(long entityID) 
152
		throws Exception {
242 naveen 153
		String entityDirPath = Utils.ENTITIES_DB_PATH + entityID;
154
		String slideSeqFileName = entityDirPath + "/slidesequence.ser";
250 naveen 155
		return (Map<Long, List<Long>>)DBUtils.read(slideSeqFileName);
156
	}
157
 
158
	/**
159
	 * 
160
	 * @param entityID
161
	 * @return
162
	 */
163
	public static List<Long> getSlideSequence(long entityID) throws Exception {
164
		Map<Long, List<Long>> catSlides = getRawSlideSequence(entityID);
242 naveen 165
 
250 naveen 166
		return getSlideSequence(catSlides);
242 naveen 167
	}
249 naveen 168
 
169
	/**
170
	 * 
310 rajveer 171
	 * @param entityID
172
	 * @return
173
	 */
174
	public static List<Long> getSlideSequence(long entityID, long categoryID) throws Exception {
175
		Map<Long, List<Long>> catSlides = getRawSlideSequence(entityID);
176
 
177
		return getOrderedSlideSequence(catSlides, categoryID);
178
	}
179
 
180
	/**
181
	 * 
250 naveen 182
	 * @param catSlides
183
	 * @return
184
	 * @throws Exception
185
	 */
186
	public static List<Long> getSlideSequence(Map<Long, List<Long>> catSlides) 
187
		throws Exception {
188
		if(catSlides == null) {
189
			return null;
190
		}
310 rajveer 191
		log.info(" IN GetSlideSequence" );
250 naveen 192
		List<Long> slideDefIDs = new ArrayList<Long>();
193
 
194
		for(Long catID : catSlides.keySet()) {
195
			slideDefIDs.addAll(catSlides.get(catID));
196
		}
197
 
198
		return slideDefIDs;
199
	}
200
 
201
	/**
202
	 * 
310 rajveer 203
	 * @param catSlides
204
	 * @param categoryID
205
	 * @return
206
	 * @throws Exception
207
	 */
208
	public static List<Long> getOrderedSlideSequence(Map<Long, List<Long>> catSlides, Long categoryID) 
209
		throws Exception {
210
		if(catSlides == null) {
211
			return null;
212
		}
213
 
214
		List<Long> slideDefIDs = new ArrayList<Long>();
215
		List<Long> orderedSlideDefIDs = new ArrayList<Long>();
216
 
217
		DefinitionsContainer defs = Catalog.getInstance().getDefinitionsContainer();
218
		List<Long> categorySlideSequence = defs.getCategorySlideSequence(categoryID);
219
 
220
 
221
		for(Long catID : catSlides.keySet()) {
222
			slideDefIDs.addAll(catSlides.get(catID));
223
		}
224
		log.info("slideDefIDs:" + slideDefIDs);
225
 
226
		Map<String,Long> labelIDMap = new HashMap<String, Long>();
227
		for(long slideDefID : slideDefIDs){
228
			labelIDMap.put(defs.getSlideDefinition(slideDefID).getLabel(), slideDefID);
229
		}
230
		log.info("labelIDMap:" + labelIDMap);
231
 
232
		for(Long slideID : categorySlideSequence){
233
			String currentSlideLabel = defs.getSlideDefinition(slideID).getLabel();
234
			if(labelIDMap.containsKey(currentSlideLabel)){
235
				orderedSlideDefIDs.add(labelIDMap.get(currentSlideLabel));
236
				labelIDMap.remove(currentSlideLabel);
237
			}
238
		}
239
		log.info("orderedSlideDefIDs:" + orderedSlideDefIDs);
240
 
241
		for(String label : labelIDMap.keySet()){
242
			orderedSlideDefIDs.add(labelIDMap.get(label));
243
			labelIDMap.remove(label);
244
		}
245
		log.info(" orderedSlideDefIDs:" + orderedSlideDefIDs);
246
		return orderedSlideDefIDs;
247
	}
248
 
249
	/**
250
	 * 
249 naveen 251
	 * @param entities
252
	 * @param entitiesByCategory
253
	 * @throws Exception
254
	 */
250 naveen 255
	public static void rewriteRepository(Map<Long, Entity> entities, 
249 naveen 256
			Map<Long, List<Entity>> entitiesByCategory) throws Exception {
257
 
258
		String entityDBFile = Utils.ENTITIES_DB_PATH + "entities.ser";
259
 
260
		// Remove existing
261
		DBUtils.delete(entityDBFile);
262
 
263
		DBUtils.store(entities, entityDBFile);
264
 
265
		String entitiesbycategoryDBFile = Utils.ENTITIES_DB_PATH + 
266
			"entitiesbycategory.ser";
267
 
268
		// Remove existing
269
		DBUtils.delete(entitiesbycategoryDBFile);
270
 
271
		DBUtils.store(entitiesByCategory, entitiesbycategoryDBFile);		
272
	}
250 naveen 273
 
274
	/**
275
	 * 
276
	 * @param entityID
277
	 * @return
278
	 * @throws Exception
279
	 */
280
	public static Entity getEntity(long entityID) throws Exception {
281
		String entityDBFile = Utils.ENTITIES_DB_PATH + entityID + 
282
			"/entity.ser";
283
 
284
		return (Entity)DBUtils.read(entityDBFile);
285
	}
286
 
287
	/**
288
	 * 
289
	 * @param entity
290
	 * @throws Exception
291
	 */
292
	public static void storeEntity(Entity entity) throws Exception {
293
		String entityDBDir = Utils.ENTITIES_DB_PATH + entity.getID();
294
 
295
		File entityDBDirFile = new File(entityDBDir);
296
		if(!entityDBDirFile.exists()) {
297
			entityDBDirFile.mkdir();
298
		}
299
 
300
		String entityDBFile =  entityDBDir + "/entity.ser";
301
 
302
		File entityFile = new File(entityDBFile);
303
		if(entityFile.exists()) {
304
			entityFile.delete();
305
		}
306
		else {
307
			entityFile.createNewFile();
308
		}
309
 
310
		DBUtils.store(entity, entityDBFile);		
311
	}
312
 
313
	/**
314
	 * 
315
	 * @param entityID
316
	 */
317
	public static void deleteEntity(long entityID) {
318
		String entityDBDir = Utils.ENTITIES_DB_PATH + entityID;
319
		String entityDBFile = entityDBDir + "/entity.ser";
320
 
321
		File entityFile = new File(entityDBFile);
322
		if(entityFile.exists()) {
323
			entityFile.delete();
324
		}
325
 
326
		String slideSeqFileName = entityDBDir + "/slidesequence.ser";
327
		File slideSeqFileNameFile = new File(slideSeqFileName);
328
		if(slideSeqFileNameFile.exists()) {
329
			slideSeqFileNameFile.delete();
330
		}
331
 
332
		File entityDBDirFile = new File(entityDBDir);
333
		if(entityDBDirFile.exists()) {
334
			entityDBDirFile.delete();
335
		}
336
	}
337
 
338
	/**
339
	 * 
340
	 * @param entity
341
	 * @throws Exception 
342
	 */
343
	public static void addToIncomplete(Entity entity) throws Exception {
344
		List<Entity> incompleteList = getIncomplete();
345
		if(incompleteList == null) {
346
			incompleteList = new ArrayList<Entity>();
347
		}
348
 
349
		if(!incompleteList.contains(entity)) {
350
			String incompleteFileName = Utils.ENTITIES_DB_PATH + "/incomplete.ser";
351
			File incompleteFile = new File(incompleteFileName);
352
			if(!incompleteFile.exists()) {
353
				incompleteFile.createNewFile();
354
			}
355
 
356
			incompleteList.add(entity);
357
 
358
			DBUtils.store(incompleteList, incompleteFileName);
359
		}
360
	}
361
 
362
	/**
363
	 * 
364
	 * @param entity
365
	 * @throws Exception 
366
	 */
367
	public static void deleteFromIncomplete(Entity entity) throws Exception {		
368
		List<Entity> incompleteList = getIncomplete();
369
 
370
		if(incompleteList != null) {
371
			if(incompleteList.contains(entity)) {
372
				incompleteList.remove(entity);
373
 
374
				String incompleteFileName = Utils.ENTITIES_DB_PATH + 
375
					"/incomplete.ser";
376
				DBUtils.store(incompleteList, incompleteFileName);
377
			}
378
		}
379
	}
380
 
381
	/**
382
	 * 
383
	 * @return
384
	 * @throws Exception 
385
	 */
386
	@SuppressWarnings("unchecked")
387
	public static List<Entity> getIncomplete() throws Exception {
388
		String incompleteFileName = Utils.ENTITIES_DB_PATH + "/incomplete.ser";
389
 
390
		File incompleteFile = new File(incompleteFileName);
391
		if(!incompleteFile.exists()) {
392
			return null;
393
		}
394
 
395
		return (List<Entity>)DBUtils.read(incompleteFileName);
396
	}
397
 
208 naveen 398
}