Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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