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