Subversion Repositories SmartDukaan

Rev

Rev 23672 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 23672 Rev 23673
Line 44... Line 44...
44
 
44
 
45
import com.google.gson.Gson;
45
import com.google.gson.Gson;
46
import com.google.gson.reflect.TypeToken;
46
import com.google.gson.reflect.TypeToken;
47
 
47
 
48
public class ContentGenerationUtility {
48
public class ContentGenerationUtility {
49
    private static final String   UPDATE_TYPE_CATALOG         = "CATALOG";
49
	private static final String UPDATE_TYPE_CATALOG = "CATALOG";
50
 
50
 
51
    private static final String   UPDATE_TYPE_CONTENT         = "CONTENT";
51
	private static final String UPDATE_TYPE_CONTENT = "CONTENT";
52
 
52
 
53
    private static final String   COMMAND_LINE                = "java ContentGenerationUtility.class -t { ALL | INCREMENTAL | ONE } -s { yyyy-MM-dd-HH-mm-ss } -u { CONTENT | CATALOG } -e {EntityId} ";
53
	private static final String COMMAND_LINE = "java ContentGenerationUtility.class -t { ALL | INCREMENTAL | ONE } -s { yyyy-MM-dd-HH-mm-ss } -u { CONTENT | CATALOG } -e {EntityId} ";
54
 
54
 
55
    private static Log            log                         = LogFactory
-
 
56
                                                                      .getLog(ContentGenerationUtility.class);
55
	private static Log log = LogFactory.getLog(ContentGenerationUtility.class);
57
    private static long 			ONE_DAY					  = 24*60*60*1000;  	//milliseconds in a day						 	
56
	private static long ONE_DAY = 24 * 60 * 60 * 1000; // milliseconds in a day
58
    // Commandline options
57
	// Commandline options
59
    private static Options        options                     = null;
58
	private static Options options = null;
60
    private static final String   GENERATION_TYPE_INCREMENTAL = "INCREMENTAL";
59
	private static final String GENERATION_TYPE_INCREMENTAL = "INCREMENTAL";
61
    private static final String   GENERATION_TYPE_ALL         = "ALL";
60
	private static final String GENERATION_TYPE_ALL = "ALL";
62
    private static final String   GENERATION_TYPE_ONE         = "ONE";
61
	private static final String GENERATION_TYPE_ONE = "ONE";
63
    private static final String   UPDATE_TYPE_OPTION          = "u";
62
	private static final String UPDATE_TYPE_OPTION = "u";
64
    private static final String   GENERATION_TYPE_OPTION      = "t";
63
	private static final String GENERATION_TYPE_OPTION = "t";
65
    private static final String   ENTITY_ID_OPTION            = "e";
64
	private static final String ENTITY_ID_OPTION = "e";
66
    private static final String   TIMESTAMP_OPTION            = "s";
65
	private static final String TIMESTAMP_OPTION = "s";
67
 
66
 
68
    // Default values of cmdline options
67
	// Default values of cmdline options
69
    private static String         UPDATE_TYPE                 = UPDATE_TYPE_CONTENT;
68
	private static String UPDATE_TYPE = UPDATE_TYPE_CONTENT;
70
    private static String         GENERATION_TYPE             = GENERATION_TYPE_INCREMENTAL;
69
	private static String GENERATION_TYPE = GENERATION_TYPE_INCREMENTAL;
71
    private static String         ENTITY_ID                   = "ALL";
70
	private static String ENTITY_ID = "ALL";
72
    private static String [] 	  DOMAINPATHS 			      = Utils.DOMAIN_NAMES_FOR_CONTENT_GENERATION.split(";");
71
	private static String[] DOMAINPATHS = Utils.DOMAIN_NAMES_FOR_CONTENT_GENERATION.split(";");
73
 
72
 
74
    private Date 				  timeStamp			  		  = null;
73
	private Date timeStamp = null;
75
    private CommandLine           cmd                         = null;
74
	private CommandLine cmd = null;
76
    private Map<Long, List<Item>> entityIdItemMap             = new LinkedHashMap<Long, List<Item>>();
75
	private Map<Long, List<Item>> entityIdItemMap = new LinkedHashMap<Long, List<Item>>();
77
    private Long                  lastGenerationTime          = 0l;
76
	private Long lastGenerationTime = 0l;
78
    private Long                  lastGenerationTimeNonZero          = CreationUtils.getLastContentGenerationTime();
77
	private Long lastGenerationTimeNonZero = CreationUtils.getLastContentGenerationTime();
79
    private Map<Long, Entity>     entities;
78
	private Map<Long, Entity> entities;
80
    private List<Long> removeEntities = new ArrayList<Long>();
79
	private List<Long> removeEntities = new ArrayList<Long>();
81
    private List<Item>            items;
80
	private List<Item> items;
82
    private List<Source>          sources;
81
	private List<Source> sources;
83
    private CatalogClient         csc;
82
	private CatalogClient csc;
84
    List<Entity> validPartialEntities = new ArrayList<Entity>();
83
	List<Entity> validPartialEntities = new ArrayList<Entity>();
85
    private Client                client;
84
	private Client client;
86
    private List<Item>			  alertItems;	
85
	private List<Item> alertItems;
87
    private long                  newLastGenerationTime;
86
	private long newLastGenerationTime;
88
 
87
 
89
    static {
88
	static {
90
        options = new Options();
89
		options = new Options();
91
        options.addOption(GENERATION_TYPE_OPTION, true, "Generation type");
90
		options.addOption(GENERATION_TYPE_OPTION, true, "Generation type");
92
        options.addOption(UPDATE_TYPE_OPTION, true, "Default is : "
91
		options.addOption(UPDATE_TYPE_OPTION, true, "Default is : " + UPDATE_TYPE);
93
                + UPDATE_TYPE);
-
 
94
        options.addOption(ENTITY_ID_OPTION, true, "all entities " + ENTITY_ID
92
		options.addOption(ENTITY_ID_OPTION, true, "all entities " + ENTITY_ID + " by default");
95
                + " by default");
-
 
96
        options.addOption(TIMESTAMP_OPTION, true, "Manual timestamp");
93
		options.addOption(TIMESTAMP_OPTION, true, "Manual timestamp");
97
 
94
 
98
    }
95
	}
99
 
96
 
100
    public ContentGenerationUtility() throws Exception {
97
	public ContentGenerationUtility() throws Exception {
101
        csc = new CatalogClient();
98
		csc = new CatalogClient();
102
        client = csc.getClient();
99
		client = csc.getClient();
103
        sources = client.getAllSources();
100
		sources = client.getAllSources();
104
        alertItems = new ArrayList<Item>();	
101
		alertItems = new ArrayList<Item>();
105
    }
102
	}
106
 
103
 
107
    /**
104
	/**
108
     * @param args
105
	 * @param args
109
     * @throws Exception
106
	 * @throws Exception
110
     */
107
	 */
111
    public static void main(String[] args) throws Exception {
108
	public static void main(String[] args) throws Exception {
112
        ContentGenerationUtility cgu = new ContentGenerationUtility();
109
		ContentGenerationUtility cgu = new ContentGenerationUtility();
113
 
110
 
114
        // Load arguments
111
		// Load arguments
115
        cgu.loadArgs(args);
112
		cgu.loadArgs(args);
116
 
113
 
117
        // Call method based on arguments
114
		// Call method based on arguments
118
        cgu.callMethod();
115
		cgu.callMethod();
119
    }
116
	}
120
 
117
 
121
    /**
118
	/**
122
     * Validate and set command line arguments. Exit after printing usage if
119
	 * Validate and set command line arguments. Exit after printing usage if
123
     * anything is astray
120
	 * anything is astray
124
     * 
121
	 * 
125
     * @param args
122
	 * @param args
126
     *            String[] args as featured in public static void main()
123
	 *            String[] args as featured in public static void main()
127
     */
124
	 */
128
    private void loadArgs(String[] args) {
125
	private void loadArgs(String[] args) {
129
        CommandLineParser parser = new PosixParser();
126
		CommandLineParser parser = new PosixParser();
130
 
127
 
131
        try {
128
		try {
132
            cmd = parser.parse(options, args);
129
			cmd = parser.parse(options, args);
133
        } catch (ParseException e) {
130
		} catch (ParseException e) {
134
            log.error("Error parsing arguments", e);
131
			log.error("Error parsing arguments", e);
135
            System.exit(1);
132
			System.exit(1);
136
        }
133
		}
137
 
134
 
138
        // Check for mandatory args
135
		// Check for mandatory args
139
        if (!(cmd.hasOption(GENERATION_TYPE_OPTION) && cmd
136
		if (!(cmd.hasOption(GENERATION_TYPE_OPTION) && cmd.hasOption(UPDATE_TYPE_OPTION))) {
140
                .hasOption(UPDATE_TYPE_OPTION))) {
-
 
141
            HelpFormatter formatter = new HelpFormatter();
137
			HelpFormatter formatter = new HelpFormatter();
142
            formatter.printHelp(COMMAND_LINE, options);
138
			formatter.printHelp(COMMAND_LINE, options);
143
            System.exit(1);
139
			System.exit(1);
144
        }
140
		}
145
        
-
 
146
        
-
 
147
        GENERATION_TYPE = cmd.getOptionValue(GENERATION_TYPE_OPTION);
-
 
148
        
-
 
149
        UPDATE_TYPE = cmd.getOptionValue(UPDATE_TYPE_OPTION);
-
 
150
 
141
 
151
        // Look for optional args.
-
 
152
        if (GENERATION_TYPE.equals(GENERATION_TYPE_ONE)) {
-
 
153
            if (cmd.hasOption(ENTITY_ID_OPTION)) {
-
 
154
                ENTITY_ID = cmd.getOptionValue(ENTITY_ID_OPTION);
142
		GENERATION_TYPE = cmd.getOptionValue(GENERATION_TYPE_OPTION);
155
            } else {
-
 
156
                HelpFormatter formatter = new HelpFormatter();
-
 
157
                formatter.printHelp(COMMAND_LINE, options);
-
 
158
                System.exit(1);
-
 
159
            }
-
 
160
        }
-
 
161
        
-
 
162
        if (GENERATION_TYPE_INCREMENTAL.equals(GENERATION_TYPE))
-
 
163
        	if (cmd.hasOption(TIMESTAMP_OPTION)) {
-
 
164
        		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
-
 
165
        		try {
-
 
166
        		    timeStamp = df.parse(cmd.getOptionValue(TIMESTAMP_OPTION));
-
 
167
        		} catch(Exception e) {
-
 
168
        			HelpFormatter formatter = new HelpFormatter();
-
 
169
                    formatter.printHelp(COMMAND_LINE, options);
-
 
170
                    System.exit(1);
-
 
171
        		}
-
 
172
        	}
-
 
173
    }
-
 
174
 
143
 
175
    /**
-
 
176
     * Call method based on arguments
-
 
177
     * 
-
 
178
     * @throws Exception
-
 
179
     */
-
 
180
    private void callMethod() {
-
 
181
        boolean isSuccess = false;
-
 
182
        String logfile = "/tmp/content-from-cms.log";
-
 
183
        if (UPDATE_TYPE.equals(UPDATE_TYPE_CONTENT)) {
144
		UPDATE_TYPE = cmd.getOptionValue(UPDATE_TYPE_OPTION);
184
            logfile = "/tmp/content-from-cms.log";
-
 
185
            try {
-
 
186
                this.generateContent();
-
 
187
                isSuccess = true;
-
 
188
            } catch (Exception e) {
-
 
189
                log.error("Error generating content", e);
-
 
190
            }
-
 
191
        }
-
 
192
 
145
 
-
 
146
		// Look for optional args.
193
        if (UPDATE_TYPE.equals(UPDATE_TYPE_CATALOG)) {
147
		if (GENERATION_TYPE.equals(GENERATION_TYPE_ONE)) {
-
 
148
			if (cmd.hasOption(ENTITY_ID_OPTION)) {
194
            logfile = "/tmp/content-from-catalog.log";
149
				ENTITY_ID = cmd.getOptionValue(ENTITY_ID_OPTION);
195
            try {
150
			} else {
196
                this.updatePrices();
151
				HelpFormatter formatter = new HelpFormatter();
197
                isSuccess = true;
152
				formatter.printHelp(COMMAND_LINE, options);
198
            } catch (Exception e) {
153
				System.exit(1);
199
                log.error("Error updating prices", e);
-
 
200
            }
154
			}
201
        }
155
		}
202
 
156
 
-
 
157
		if (GENERATION_TYPE_INCREMENTAL.equals(GENERATION_TYPE))
-
 
158
			if (cmd.hasOption(TIMESTAMP_OPTION)) {
-
 
159
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
-
 
160
				try {
-
 
161
					timeStamp = df.parse(cmd.getOptionValue(TIMESTAMP_OPTION));
-
 
162
				} catch (Exception e) {
203
        GmailUtils gm = new GmailUtils();
163
					HelpFormatter formatter = new HelpFormatter();
204
        String[] sendTo = { "amit.gupta@shop2020.in" };
164
					formatter.printHelp(COMMAND_LINE, options);
-
 
165
					System.exit(1);
-
 
166
				}
-
 
167
			}
-
 
168
	}
205
 
169
 
-
 
170
	/**
-
 
171
	 * Call method based on arguments
-
 
172
	 * 
206
        try {
173
	 * @throws Exception
-
 
174
	 */
-
 
175
	private void callMethod() {
207
            gm.sendSSLMessage(sendTo, "Content Generation Successful ? : "
176
		boolean isSuccess = false;
208
                    + isSuccess, "Content generation completed at time : "
177
		String logfile = "/tmp/content-from-cms.log";
209
                    + Calendar.getInstance().getTime().toString(),
178
		if (UPDATE_TYPE.equals(UPDATE_TYPE_CONTENT)) {
210
                    "build@shop2020.in", "cafe@nes", logfile);
179
			logfile = "/tmp/content-from-cms.log";
-
 
180
			try {
-
 
181
				this.generateContent();
-
 
182
				isSuccess = true;
211
        } catch (MessagingException e) {
183
			} catch (Exception e) {
212
            log.error("Could not send status mail", e);
184
				log.error("Error generating content", e);
213
        }
185
			}
214
    }
186
		}
215
 
187
 
216
    public boolean cleanDir(File dir, boolean deleteSelf) {
188
		if (UPDATE_TYPE.equals(UPDATE_TYPE_CATALOG)) {
217
        if (dir.isDirectory()) {
189
			logfile = "/tmp/content-from-catalog.log";
-
 
190
			try {
218
            String[] children = dir.list();
191
				this.updatePrices();
219
            for (int i = 0; i < children.length; i++) {
192
				isSuccess = true;
220
                boolean success = cleanDir(new File(dir, children[i]), true);
-
 
221
                if (!success) {
193
			} catch (Exception e) {
222
                    return false;
194
				log.error("Error updating prices", e);
223
                }
-
 
224
            }
195
			}
225
        }
196
		}
226
 
197
 
227
        // The directory is now empty so delete it
-
 
228
        if (deleteSelf) {
198
		GmailUtils gm = new GmailUtils();
229
            return dir.delete();
199
		String[] sendTo = { "amit.gupta@shop2020.in" };
230
        }
-
 
231
 
200
 
-
 
201
		try {
-
 
202
			gm.sendSSLMessage(sendTo, "Content Generation Successful ? : " + isSuccess,
-
 
203
					"Content generation completed at time : " + Calendar.getInstance().getTime().toString(),
-
 
204
					"build@shop2020.in", "cafe@nes", logfile);
232
        return true;
205
		} catch (MessagingException e) {
-
 
206
			log.error("Could not send status mail", e);
233
    }
207
		}
-
 
208
	}
234
 
209
 
235
    private void removeOldResources() throws IOException {
-
 
236
        File f = new File(Utils.EXPORT_SOLR_PATH);
210
	public boolean cleanDir(File dir, boolean deleteSelf) {
237
        if (f.exists()) {
211
		if (dir.isDirectory()) {
238
            cleanDir(f, false);
212
			String[] children = dir.list();
239
        }
-
 
240
        
-
 
241
        for(String domainPath : DOMAINPATHS){
213
			for (int i = 0; i < children.length; i++) {
242
        	String pathName = domainPath.split("\\.")[0].split(":")[0];
-
 
243
        	File f1 = new File(Utils.EXPORT_PATH + "html/entities-" +  pathName);
214
				boolean success = cleanDir(new File(dir, children[i]), true);
244
        	if (f1.exists()) {
215
				if (!success) {
245
        		cleanDir(f1, false);
216
					return false;
246
        	}else {
-
 
247
        		f1.mkdir();
-
 
248
        	}
217
				}
249
        }
218
			}
250
    }
219
		}
251
 
220
 
252
    /**
-
 
253
     * Update the prices in the generated content
221
		// The directory is now empty so delete it
254
     * 
-
 
255
     * @throws Exception
-
 
256
     */
-
 
257
    private void updatePrices() throws Exception {
-
 
258
        lastGenerationTime = new Long(0);
-
 
259
        List<Long> activeItems = new ArrayList<Long>();
-
 
260
        if (GENERATION_TYPE.equals(GENERATION_TYPE_ONE)) {
-
 
261
            items = client.getItemsByCatalogId(Long.parseLong(ENTITY_ID));
-
 
262
            Iterator<Item> it = items.iterator();
-
 
263
            while(it.hasNext()){
-
 
264
            	Item ite = it.next();
-
 
265
            	status st = ite.getItemStatus();
-
 
266
            	if(!(st.equals(status.ACTIVE) || st.equals(status.PAUSED) || 
-
 
267
            				st.equals(status.COMING_SOON))){
-
 
268
            		it.remove();
-
 
269
            	}
-
 
270
            	if(st.equals(status.ACTIVE)){
-
 
271
            		activeItems.add(ite.getId());
-
 
272
            	}
-
 
273
            }
-
 
274
            try {
-
 
275
            	//Generate prices and availability data for amazon
-
 
276
            	AmazonSCDataGenerator.generatePricesAndAvailability(items);
-
 
277
            } catch (Exception e) {
-
 
278
            	log.info("Could not generate Amazon prices and availability", e);
-
 
279
            }
-
 
280
            //ProductListGenerator.updatePriceForEntity(Long.parseLong(ENTITY_ID), items.get(0).getSellingPrice(), items.get(0).getMrp());
-
 
281
        } else {
222
		if (deleteSelf) {
282
        	log.info("Before getting active items.");
-
 
283
            items = client.getAllItemsByStatus(status.ACTIVE);
-
 
284
            for (Item item : items) {
-
 
285
            	activeItems.add(item.getId());
-
 
286
            }
-
 
287
            log.info("Before getting coming items.");
-
 
288
            items.addAll(client.getAllItemsByStatus(status.COMING_SOON));
-
 
289
            log.info("Before getting paused items.");
-
 
290
            items.addAll(client.getAllItemsByStatus(status.PAUSED));
-
 
291
            // Clean up the data from the solr directories.
-
 
292
            log.info("Before removing old resources.");
-
 
293
            removeOldResources();
223
			return dir.delete();
294
        }
224
		}
295
 
225
 
296
/*        if (GENERATION_TYPE.equals(GENERATION_TYPE_INCREMENTAL)) {
-
 
297
        }
226
		return true;
298
*/
227
	}
299
        populateEntityIdItemMap();
-
 
300
        // Generate partners and json objects for phones only
-
 
301
        if (!GENERATION_TYPE.equals(GENERATION_TYPE_ONE)) {
-
 
302
        	ProductListGenerator generator = new ProductListGenerator(entityIdItemMap);
-
 
303
        	
-
 
304
        	log.info("Before auto suggest json");
-
 
305
        	synonymTitlesExporter();
-
 
306
 
228
 
307
        	log.info("Before product list js.");
229
	private void removeOldResources() throws IOException {
308
        	generator.generateProductListJavascript(); 
230
		File f = new File(Utils.EXPORT_SOLR_PATH);
309
        }
231
		if (f.exists()) {
310
        PriceInsertor priceInserter = new PriceInsertor();
232
			cleanDir(f, false);
-
 
233
		}
311
 
234
 
312
        Map<Long,List<String>> entityTags = client.getAllEntityTags();
-
 
313
        Map<Long, Integer> popularityMap = getPolularityMap();
-
 
314
        List<Long> pausedByRiskItems = getRiskyPausedItems();
-
 
315
        for (Map.Entry<Long, List<Item>> entry : entityIdItemMap.entrySet()) {
-
 
316
            long entityId = entry.getKey();
-
 
317
            List<Item> items = entry.getValue();
-
 
318
            double minPrice = 0d;
-
 
319
            
-
 
320
            //Evaluating availability
-
 
321
            String availability = "Out of Stock";
-
 
322
            for(Item i: items){
-
 
323
            	if(i.getItemStatus().equals(status.ACTIVE)) {
-
 
324
            		if(!(i.isRisky() && pausedByRiskItems.contains(i.getId()) )){
-
 
325
            			availability = "In Stock";
-
 
326
            		}
-
 
327
            	}
-
 
328
            }
-
 
329
            StringBuilder priceString = new StringBuilder();
-
 
330
            StringBuilder availabilityString = new StringBuilder();
-
 
331
            boolean domainOnce = true;
-
 
332
            boolean sourceOnce = true;
-
 
333
            for(String domainPath : DOMAINPATHS){
235
		for (String domainPath : DOMAINPATHS) {
334
            	String domainName = domainPath;
-
 
335
            	String pathName = domainPath.split("\\.")[0].split(":")[0];
236
			String pathName = domainPath.split("\\.")[0].split(":")[0];
336
            	if(!removeEntities.contains(entityId)){
-
 
337
            		priceInserter.insertPriceInHtml(items, entityId, domainName, Utils.EXPORT_PATH + "html/entities-" +  pathName + "/", null);
237
			File f1 = new File(Utils.EXPORT_PATH + "html/entities-" + pathName);
338
            	}
-
 
339
            	if(domainOnce){
238
			if (f1.exists()) {
340
            		minPrice = getMinPrice(items, entityId, null);
-
 
341
            		priceString.append("<field name=\"F_50002\">" + minPrice + "</field>");
-
 
342
            		availabilityString.append("<field name=\"F_50028\">" + availability + "</field>");
-
 
343
            		if(entityTags.containsKey(entityId)) {
-
 
344
            			List<String> tags = entityTags.get(entityId);
-
 
345
            			for(String tag: tags){
239
				cleanDir(f1, false);
346
            				availabilityString.append("\n<field name=\"F_50029\">" + tag + "</field>");
-
 
347
            			}
-
 
348
            		}
-
 
349
            		if(popularityMap.containsKey(entityId)) {
-
 
350
            			availabilityString.append("\n<field name=\"F_50030\">" + popularityMap.get(entityId) + "</field>");
-
 
351
            		}else {
240
			} else {
352
            			availabilityString.append("\n<field name=\"F_50030\">" + "0" + "</field>");
-
 
353
            		}
241
				f1.mkdir();
354
            		domainOnce = false;
-
 
355
            	}
242
			}
356
            	if(sources != null){
-
 
357
            		for (Source source : sources) {
-
 
358
						priceInserter.insertPriceInHtml(items, entityId,domainName, Utils.EXPORT_PATH + "html/entities-" + pathName + "/",source);
-
 
359
                        if(sourceOnce){
-
 
360
                        	minPrice = getMinPrice(items, entityId, source);
-
 
361
                        	priceString.append("<field name=\"F_50002_"
-
 
362
                                + source.getId() + "\">" + minPrice + "</field>");
-
 
363
                        }
-
 
364
            		}
243
		}
365
            		sourceOnce = false;
-
 
366
            	}
244
	}
367
            }
-
 
368
 
245
 
-
 
246
	/**
-
 
247
	 * Update the prices in the generated content
-
 
248
	 * 
-
 
249
	 * @throws Exception
-
 
250
	 */
-
 
251
	private void updatePrices() throws Exception {
-
 
252
		lastGenerationTime = new Long(0);
-
 
253
		List<Long> activeItems = new ArrayList<Long>();
-
 
254
		if (GENERATION_TYPE.equals(GENERATION_TYPE_ONE)) {
-
 
255
			items = client.getItemsByCatalogId(Long.parseLong(ENTITY_ID));
-
 
256
			Iterator<Item> it = items.iterator();
-
 
257
			while (it.hasNext()) {
-
 
258
				Item ite = it.next();
-
 
259
				status st = ite.getItemStatus();
-
 
260
				if (!(st.equals(status.ACTIVE) || st.equals(status.PAUSED) || st.equals(status.COMING_SOON))) {
-
 
261
					it.remove();
-
 
262
				}
-
 
263
				if (st.equals(status.ACTIVE)) {
-
 
264
					activeItems.add(ite.getId());
-
 
265
				}
-
 
266
			}
-
 
267
			try {
-
 
268
				// Generate prices and availability data for amazon
369
            priceInserter.insertPriceInSolrData(entityId,
269
				AmazonSCDataGenerator.generatePricesAndAvailability(items);
-
 
270
			} catch (Exception e) {
-
 
271
				log.info("Could not generate Amazon prices and availability", e);
-
 
272
			}
-
 
273
			// ProductListGenerator.updatePriceForEntity(Long.parseLong(ENTITY_ID),
370
                    priceString.toString(), availabilityString.toString());
274
			// items.get(0).getSellingPrice(), items.get(0).getMrp());
371
        }
275
		} else {
-
 
276
			log.info("Before getting active items.");
-
 
277
			items = client.getAllItemsByStatus(status.ACTIVE);
-
 
278
			for (Item item : items) {
-
 
279
				activeItems.add(item.getId());
-
 
280
			}
-
 
281
			log.info("Before getting coming items.");
-
 
282
			items.addAll(client.getAllItemsByStatus(status.COMING_SOON));
-
 
283
			log.info("Before getting paused items.");
-
 
284
			items.addAll(client.getAllItemsByStatus(status.PAUSED));
-
 
285
			// Clean up the data from the solr directories.
-
 
286
			log.info("Before removing old resources.");
-
 
287
			removeOldResources();
-
 
288
		}
372
 
289
 
-
 
290
		/*
-
 
291
		 * if (GENERATION_TYPE.equals(GENERATION_TYPE_INCREMENTAL)) { }
-
 
292
		 */
-
 
293
		populateEntityIdItemMap();
-
 
294
		// Generate partners and json objects for phones only
-
 
295
		if (!GENERATION_TYPE.equals(GENERATION_TYPE_ONE)) {
-
 
296
			ProductListGenerator generator = new ProductListGenerator(entityIdItemMap);
-
 
297
 
-
 
298
			log.info("Before auto suggest json");
-
 
299
			synonymTitlesExporter();
-
 
300
 
-
 
301
			log.info("Before product list js.");
-
 
302
			generator.generateProductListJavascript();
-
 
303
		}
-
 
304
		PriceInsertor priceInserter = new PriceInsertor();
-
 
305
 
-
 
306
		Map<Long, List<String>> entityTags = client.getAllEntityTags();
-
 
307
		Map<Long, Integer> popularityMap = getPolularityMap();
-
 
308
		List<Long> pausedByRiskItems = getRiskyPausedItems();
-
 
309
		for (Map.Entry<Long, List<Item>> entry : entityIdItemMap.entrySet()) {
-
 
310
			long entityId = entry.getKey();
-
 
311
			List<Item> items = entry.getValue();
-
 
312
			double minPrice = 0d;
-
 
313
 
-
 
314
			// Evaluating availability
-
 
315
			String availability = "Out of Stock";
-
 
316
			for (Item i : items) {
-
 
317
				if (i.getItemStatus().equals(status.ACTIVE)) {
-
 
318
					if (!(i.isRisky() && pausedByRiskItems.contains(i.getId()))) {
-
 
319
						availability = "In Stock";
-
 
320
					}
-
 
321
				}
-
 
322
			}
-
 
323
			StringBuilder priceString = new StringBuilder();
-
 
324
			StringBuilder availabilityString = new StringBuilder();
-
 
325
			boolean domainOnce = true;
-
 
326
			boolean sourceOnce = true;
-
 
327
			for (String domainPath : DOMAINPATHS) {
-
 
328
				String domainName = domainPath;
-
 
329
				String pathName = domainPath.split("\\.")[0].split(":")[0];
-
 
330
				if (!removeEntities.contains(entityId)) {
-
 
331
					priceInserter.insertPriceInHtml(items, entityId, domainName,
-
 
332
							Utils.EXPORT_PATH + "html/entities-" + pathName + "/", null);
-
 
333
				}
-
 
334
				if (domainOnce) {
-
 
335
					minPrice = getMinPrice(items, entityId, null);
-
 
336
					priceString.append("<field name=\"F_50002\">" + minPrice + "</field>");
-
 
337
					availabilityString.append("<field name=\"F_50028\">" + availability + "</field>");
-
 
338
					if (entityTags.containsKey(entityId)) {
-
 
339
						List<String> tags = entityTags.get(entityId);
-
 
340
						for (String tag : tags) {
-
 
341
							availabilityString.append("\n<field name=\"F_50029\">" + tag + "</field>");
-
 
342
						}
-
 
343
					}
-
 
344
					if (popularityMap.containsKey(entityId)) {
-
 
345
						availabilityString
-
 
346
								.append("\n<field name=\"F_50030\">" + popularityMap.get(entityId) + "</field>");
-
 
347
					} else {
-
 
348
						availabilityString.append("\n<field name=\"F_50030\">" + "0" + "</field>");
-
 
349
					}
-
 
350
					domainOnce = false;
-
 
351
				}
-
 
352
				if (sources != null) {
-
 
353
					for (Source source : sources) {
-
 
354
						priceInserter.insertPriceInHtml(items, entityId, domainName,
-
 
355
								Utils.EXPORT_PATH + "html/entities-" + pathName + "/", source);
-
 
356
						if (sourceOnce) {
-
 
357
							minPrice = getMinPrice(items, entityId, source);
-
 
358
							priceString
-
 
359
									.append("<field name=\"F_50002_" + source.getId() + "\">" + minPrice + "</field>");
-
 
360
						}
-
 
361
					}
-
 
362
					sourceOnce = false;
-
 
363
				}
-
 
364
			}
-
 
365
 
-
 
366
			priceInserter.insertPriceInSolrData(entityId, priceString.toString(), availabilityString.toString());
-
 
367
		}
-
 
368
 
373
        priceInserter.copySolrSchemaFiles();
369
		priceInserter.copySolrSchemaFiles();
374
    }
370
	}
375
 
371
 
376
	private Map<Long, Integer> getPolularityMap() {
372
	private Map<Long, Integer> getPolularityMap() {
377
		try {
373
		try {
378
			Reader reader = new FileReader(Utils.EXPORT_PATH + Utils.POPULARITY_JSON);
374
			Reader reader = new FileReader(Utils.EXPORT_PATH + Utils.POPULARITY_JSON);
379
			Map<Long, Integer> result =  new Gson().fromJson(reader, new TypeToken<Map<Long, Integer>>() {}.getType());
375
			Map<Long, Integer> result = new Gson().fromJson(reader, new TypeToken<Map<Long, Integer>>() {
-
 
376
			}.getType());
380
			if(result == null) {
377
			if (result == null) {
381
				result = new HashMap<Long, Integer>();
378
				result = new HashMap<Long, Integer>();
382
			}
379
			}
383
			return result; 
380
			return result;
384
		} catch (Exception e) {
381
		} catch (Exception e) {
385
			log.error("Could not read popularity file");
382
			log.error("Could not read popularity file");
386
			e.printStackTrace();
383
			e.printStackTrace();
387
			return new HashMap<Long, Integer>();
384
			return new HashMap<Long, Integer>();
388
		}
385
		}
389
    	
386
 
390
	}
387
	}
-
 
388
 
391
	private List<Long> getRiskyPausedItems() {
389
	private List<Long> getRiskyPausedItems() {
392
		try {
390
		try {
393
			Reader reader = new FileReader(Utils.EXPORT_PATH + Utils.RISKY_PAUSED_JSON);
391
			Reader reader = new FileReader(Utils.EXPORT_PATH + Utils.RISKY_PAUSED_JSON);
394
			List<Long> result = new Gson().fromJson(reader, new TypeToken<List<Long>>() {}.getType());
392
			List<Long> result = new Gson().fromJson(reader, new TypeToken<List<Long>>() {
-
 
393
			}.getType());
395
			if(result==null){
394
			if (result == null) {
396
				result = new ArrayList<Long>(); 
395
				result = new ArrayList<Long>();
397
			}
396
			}
398
			return result;
397
			return result;
399
		} catch (FileNotFoundException e) {
398
		} catch (FileNotFoundException e) {
400
			log.error("Could not read paused file");
399
			log.error("Could not read paused file");
401
			e.printStackTrace();
400
			e.printStackTrace();
Line 518... Line 517...
518
 
517
 
519
 
518
 
520
        log.info("Generating Solr files");
519
        log.info("Generating Solr files");
521
        NewIR ir = new NewIR(validEntities);
520
        NewIR ir = new NewIR(validEntities);
522
        ir.exportIRData();
521
        ir.exportIRData();
523
        // ir.transformIrDataXMLtoSolrXML();
522
        // ir.transformIrDataXMLtoSolrXML();em.
524
        
523
        
525
        if(!GENERATION_TYPE.equals(GENERATION_TYPE_ONE)) {
524
        if(!GENERATION_TYPE.equals(GENERATION_TYPE_ONE)) {
526
	        ir.exportIRMetaData();
525
	        ir.exportIRMetaData();
527
	        ir.transformIrMetaDataXMLtoSolrSchemaXML();
526
	        ir.transformIrMetaDataXMLtoSolrSchemaXML();
528
	        ir.transformIrMetaDataXMLtoSolrCatchAllXML();
527
	        ir.transformIrMetaDataXMLtoSolrCatchAllXML();
529
        }
528
        }
-
 
529
        csc = new CatalogClient();
530
        client = csc.getClient();
530
        client = csc.getClient();
531
        for (Map.Entry<Long, List<Item>> entry : entityIdItemMap.entrySet()) {
531
        for (Map.Entry<Long, List<Item>> entry : entityIdItemMap.entrySet()) {
532
            List<Item> items = entry.getValue();
532
            List<Item> items = entry.getValue();
533
            for (Item item : items) {
533
            for (Item item : items) {
534
                if (item.getItemStatus() == status.CONTENT_COMPLETE 
534
                if (item.getItemStatus() == status.CONTENT_COMPLETE 
Line 550... Line 550...
550
                				item.getExpectedArrivalDate() > new Date().getTime() + ONE_DAY) {
550
                				item.getExpectedArrivalDate() > new Date().getTime() + ONE_DAY) {
551
                				alertItems.add(item);
551
                				alertItems.add(item);
552
                		}
552
                		}
553
                		item.setStatus_description(statusDescription);
553
                		item.setStatus_description(statusDescription);
554
                	}
554
                	}
-
 
555
                	try {
-
 
556
                		client.updateItem(item);
-
 
557
                	}catch( Exception e){
-
 
558
                		csc = new CatalogClient();
-
 
559
                        client = csc.getClient();
-
 
560
                        log.info("Client generated again for item" + item.getId());
555
                    client.updateItem(item);
561
                        client.updateItem(item);
-
 
562
                	}
556
            	}
563
            	}
557
            }
564
            }
558
        }
565
        }
559
        sendAlertToCategoryTeam(alertItems);
566
        sendAlertToCategoryTeam(alertItems);
560
    }
567
    }
561
 
568
 
562
    private void sendAlertToCategoryTeam(List<Item> items) {
569
	private void sendAlertToCategoryTeam(List<Item> items) {
563
    	if(items!=null && items.size()!=0){
570
		if (items != null && items.size() != 0) {
564
			GmailUtils util = new GmailUtils();
571
			GmailUtils util = new GmailUtils();
565
			String[] recipients = {"amit.gupta@shop2020.in", "chaitnaya.vats@shop2020.in", "khushal.bhatia@shop2020.in", "vrinda.k@shop2020.in"};
572
			String[] recipients = { "amit.gupta@shop2020.in", "chaitnaya.vats@shop2020.in",
-
 
573
					"khushal.bhatia@shop2020.in", "vrinda.k@shop2020.in" };
566
			String from = "build@shop2020.in";
574
			String from = "build@shop2020.in";
567
			String password = "cafe@nes";
575
			String password = "cafe@nes";
568
			String subject = Utils.EXPECTED_ARRIVAL_ACHIEVED_TEMPLATE;
576
			String subject = Utils.EXPECTED_ARRIVAL_ACHIEVED_TEMPLATE;
569
			StringBuffer message = new StringBuffer("Please check the following items:\n");
577
			StringBuffer message = new StringBuffer("Please check the following items:\n");
570
			List<File> emptyList = new ArrayList<File>();
578
			List<File> emptyList = new ArrayList<File>();
571
			for( Item item : items){
579
			for (Item item : items) {
572
				message.append("\t" + getProductName(item));
580
				message.append("\t" + getProductName(item));
573
			}
581
			}
574
			try {
582
			try {
575
				util.sendSSLMessage(recipients, subject, message.toString(), from, password, emptyList);
583
				util.sendSSLMessage(recipients, subject, message.toString(), from, password, emptyList);
576
			} catch (Exception e){
584
			} catch (Exception e) {
577
				log.info("Could not send alert" + e);
585
				log.info("Could not send alert" + e);
578
			}
586
			}
579
    	}
587
		}
580
	}
588
	}
581
 
589
 
582
	private String getProductName(Item item) {
590
	private String getProductName(Item item) {
583
    	String brand = item.getBrand();
591
		String brand = item.getBrand();
584
		String modelName = item.getModelName();
592
		String modelName = item.getModelName();
585
		String modelNumber = item.getModelNumber();
593
		String modelNumber = item.getModelNumber();
586
		String product = "";
594
		String product = "";
587
		if(StringUtils.isEmpty(modelName)){
595
		if (StringUtils.isEmpty(modelName)) {
588
			product = brand + " " + modelNumber;
596
			product = brand + " " + modelNumber;
589
		}else {
597
		} else {
590
			product = brand + " " + modelName + " " + modelNumber;
598
			product = brand + " " + modelName + " " + modelNumber;
591
		}
599
		}
592
		return product;
600
		return product;
593
	}
601
	}
594
 
602
 
595
    /**
603
	/**
596
     * Checks weather entity is valid or not. Entity will be invalid in one of
604
	 * Checks weather entity is valid or not. Entity will be invalid in one of these
597
     * these cases:
605
	 * cases:
598
     * <ol>
606
	 * <ol>
599
     * <li>The entity is not ready.
607
	 * <li>The entity is not ready.
600
     * <li>The category has not been updated yet. (Set to -1).
608
	 * <li>The category has not been updated yet. (Set to -1).
601
     * <li>Content has not been updated after last content generation timestamp.
609
	 * <li>Content has not been updated after last content generation timestamp.
602
     * </ol>
610
	 * </ol>
603
     * 
611
	 * 
604
     * @param entity
612
	 * @param entity
605
     * @return
613
	 * @return
606
     * @throws Exception
614
	 * @throws Exception
607
     */
615
	 */
608
    private boolean isValidEntity(Entity entity) throws Exception {
616
	private boolean isValidEntity(Entity entity) throws Exception {
609
        EntityState state = CreationUtils.getEntityState(entity.getID());
617
		EntityState state = CreationUtils.getEntityState(entity.getID());
610
        long categoryID = state.getCategoryID();
618
		long categoryID = state.getCategoryID();
611
        if (state.getStatus() != EntityStatus.READY || categoryID == -1) {
619
		if (state.getStatus() != EntityStatus.READY || categoryID == -1) {
612
            return false;
620
			return false;
613
        }
621
		}
614
        if(state.getMerkedReadyOn().getTime() < this.lastGenerationTime) {
622
		if (state.getMerkedReadyOn().getTime() < this.lastGenerationTime) {
615
            return false;
623
			return false;
616
        }
624
		}
617
    	if(state.getMerkedReadyOn().getTime() > this.lastGenerationTimeNonZero){
625
		if (state.getMerkedReadyOn().getTime() > this.lastGenerationTimeNonZero) {
618
        		Utils.info("Added to Partail:" + entity.getID());
626
			Utils.info("Added to Partail:" + entity.getID());
619
        		validPartialEntities.add(entity);
627
			validPartialEntities.add(entity);
620
        }
628
		}
621
        return true;
629
		return true;
-
 
630
	}
-
 
631
 
-
 
632
	private void populateEntityIdItemMap() {
-
 
633
		Date todate = new Date();
-
 
634
		Utils.info("Processing " + items.size() + " items");
-
 
635
		for (Item item : items) {
-
 
636
			Utils.info(item.getId() + ":" + item.getItemStatus() + ":" + item.getCatalogItemId());
-
 
637
			// TODO Can be removed as we are checking in calling function
-
 
638
			/*
-
 
639
			 * if (!(item.getItemStatus() == status.ACTIVE || item.getItemStatus() ==
-
 
640
			 * status.CONTENT_COMPLETE || item .getItemStatus() == status.PAUSED ||
-
 
641
			 * item.getItemStatus() == status.COMING_SOON)) { continue; }
622
    }
642
			 */
-
 
643
			SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
-
 
644
			Utils.info(df1.format(item.getStartDate()) + ":" + item.getSellingPrice());
623
 
645
 
624
    private void populateEntityIdItemMap() {
-
 
625
        Date todate = new Date();
-
 
626
        Utils.info("Processing " + items.size() + " items");
-
 
627
        for (Item item : items) {
-
 
628
            Utils.info(item.getId() + ":" + item.getItemStatus() + ":" + item.getCatalogItemId());
-
 
629
            // TODO Can be removed as we are checking in calling function
-
 
630
            /*if (!(item.getItemStatus() == status.ACTIVE
-
 
631
                    || item.getItemStatus() == status.CONTENT_COMPLETE || item
-
 
632
                    .getItemStatus() == status.PAUSED || item.getItemStatus() == status.COMING_SOON)) {
-
 
633
                continue;
-
 
634
            }*/
-
 
635
            SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
-
 
636
            Utils.info(df1.format(item.getStartDate()) + ":" + item.getSellingPrice());
-
 
637
            
-
 
638
			if (todate.getTime() < item.getStartDate()
646
			if (todate.getTime() < item.getStartDate()
639
					&& (!item.isSetExpectedArrivalDate() || todate.getTime() < item.getComingSoonStartDate()) 
647
					&& (!item.isSetExpectedArrivalDate() || todate.getTime() < item.getComingSoonStartDate())
640
					|| item.getSellingPrice() == 0) {
648
					|| item.getSellingPrice() == 0) {
641
				continue;
649
				continue;
642
			}
650
			}
643
            Utils.info(item.getId() + " Item is adding");
651
			Utils.info(item.getId() + " Item is adding");
644
            List<Item> itemList = entityIdItemMap.get(item.getCatalogItemId());
652
			List<Item> itemList = entityIdItemMap.get(item.getCatalogItemId());
645
            if (itemList == null) {
653
			if (itemList == null) {
646
                itemList = new ArrayList<Item>();
654
				itemList = new ArrayList<Item>();
647
            } 
655
			}
648
            itemList.add(item);
656
			itemList.add(item);
649
            entityIdItemMap.put(item.getCatalogItemId(), itemList);
657
			entityIdItemMap.put(item.getCatalogItemId(), itemList);
650
        }
658
		}
651
 
659
 
652
        Utils.info("Processing " + entityIdItemMap.size() + " entities");
660
		Utils.info("Processing " + entityIdItemMap.size() + " entities");
653
        // Remove all items which have not been updated since last content
661
		// Remove all items which have not been updated since last content
654
        // generation.
662
		// generation.
655
        if (!(UPDATE_TYPE_CONTENT.equals(UPDATE_TYPE) || lastGenerationTime == 0)) {
663
		if (!(UPDATE_TYPE_CONTENT.equals(UPDATE_TYPE) || lastGenerationTime == 0)) {
656
	        for (Long entityId : entityIdItemMap.keySet()) {
664
			for (Long entityId : entityIdItemMap.keySet()) {
657
	            boolean isValidEntity = false;
665
				boolean isValidEntity = false;
658
	            // If any one of the items has been updated before current
666
				// If any one of the items has been updated before current
659
	            // timestamp, than we generate content for pricing
667
				// timestamp, than we generate content for pricing
660
	            for (Item item : entityIdItemMap.get(entityId)) {
668
				for (Item item : entityIdItemMap.get(entityId)) {
661
	                if (item.getUpdatedOn() > lastGenerationTime) {
669
					if (item.getUpdatedOn() > lastGenerationTime) {
662
	                    isValidEntity = true;
670
						isValidEntity = true;
663
	                }
671
					}
664
	            }
672
				}
665
	            if (!isValidEntity) {
673
				if (!isValidEntity) {
666
	                removeEntities.add(entityId);
674
					removeEntities.add(entityId);
667
	            }
675
				}
668
	        }
676
			}
669
        }
677
		}
670
 
678
 
671
        Utils.info("Final valid entities to be processed: " + entityIdItemMap.size());
679
		Utils.info("Final valid entities to be processed: " + entityIdItemMap.size());
672
    }
680
	}
673
 
681
 
674
    private void synonymTitlesExporter() {
682
	private void synonymTitlesExporter() {
675
    	SynonymExporter sx = new SynonymExporter();
683
		SynonymExporter sx = new SynonymExporter();
676
        Map<Long, Map<String,List<String>>> synonyms = sx.getSynonyms();
684
		Map<Long, Map<String, List<String>>> synonyms = sx.getSynonyms();
677
        Map<String, List<String>> finalsynonyms = new HashMap<String, List<String>>();
685
		Map<String, List<String>> finalsynonyms = new HashMap<String, List<String>>();
678
    	for (Map.Entry<Long, List<Item>> entry : entityIdItemMap.entrySet()) {
686
		for (Map.Entry<Long, List<Item>> entry : entityIdItemMap.entrySet()) {
679
            long entityId = entry.getKey();
687
			long entityId = entry.getKey();
680
	    	try{
688
			try {
681
	            String brand = "";
689
				String brand = "";
682
	            String originalModelName = "";
690
				String originalModelName = "";
683
	            String originalModelNumber = "";
691
				String originalModelNumber = "";
684
	            List<String> modelNameSynonyms =  new ArrayList<String>();
692
				List<String> modelNameSynonyms = new ArrayList<String>();
685
	            List<String> modelNumberSynonyms =  new ArrayList<String>();
693
				List<String> modelNumberSynonyms = new ArrayList<String>();
686
	            List<String> titles = new ArrayList<String>();
694
				List<String> titles = new ArrayList<String>();
687
	            Map<String,List<String>> synonymMap = synonyms.get(entityId);
695
				Map<String, List<String>> synonymMap = synonyms.get(entityId);
688
	            if(synonymMap != null && !synonymMap.isEmpty()){
696
				if (synonymMap != null && !synonymMap.isEmpty()) {
-
 
697
					if (synonymMap.get("ORIGINAL_MODEL_NAME") != null
689
	            	if(synonymMap.get("ORIGINAL_MODEL_NAME") != null && !synonymMap.get("ORIGINAL_MODEL_NAME").isEmpty()){
698
							&& !synonymMap.get("ORIGINAL_MODEL_NAME").isEmpty()) {
690
	            		modelNameSynonyms.addAll(synonymMap.get("ORIGINAL_MODEL_NAME"));
699
						modelNameSynonyms.addAll(synonymMap.get("ORIGINAL_MODEL_NAME"));
691
	            		originalModelName = synonymMap.get("ORIGINAL_MODEL_NAME").get(0);
700
						originalModelName = synonymMap.get("ORIGINAL_MODEL_NAME").get(0);
692
	            	}
701
					}
693
	            	if(synonymMap.get("MODEL_NAME") != null && !synonymMap.get("MODEL_NAME").isEmpty()){
702
					if (synonymMap.get("MODEL_NAME") != null && !synonymMap.get("MODEL_NAME").isEmpty()) {
694
	            		modelNameSynonyms.addAll(synonymMap.get("MODEL_NAME"));
703
						modelNameSynonyms.addAll(synonymMap.get("MODEL_NAME"));
695
	            	}
704
					}
-
 
705
					if (synonymMap.get("ORIGINAL_MODEL_NUMBER") != null
696
	            	if(synonymMap.get("ORIGINAL_MODEL_NUMBER") != null && !synonymMap.get("ORIGINAL_MODEL_NUMBER").isEmpty()){
706
							&& !synonymMap.get("ORIGINAL_MODEL_NUMBER").isEmpty()) {
697
	            		modelNumberSynonyms.addAll(synonymMap.get("ORIGINAL_MODEL_NUMBER"));
707
						modelNumberSynonyms.addAll(synonymMap.get("ORIGINAL_MODEL_NUMBER"));
698
	            		originalModelNumber = synonymMap.get("ORIGINAL_MODEL_NUMBER").get(0);
708
						originalModelNumber = synonymMap.get("ORIGINAL_MODEL_NUMBER").get(0);
699
	            	}
709
					}
700
	            	if(synonymMap.get("MODEL_NUMBER") != null && !synonymMap.get("MODEL_NUMBER").isEmpty()){
710
					if (synonymMap.get("MODEL_NUMBER") != null && !synonymMap.get("MODEL_NUMBER").isEmpty()) {
701
	            		modelNumberSynonyms.addAll(synonymMap.get("MODEL_NUMBER"));
711
						modelNumberSynonyms.addAll(synonymMap.get("MODEL_NUMBER"));
702
	            	}
712
					}
703
	            	brand = ((synonymMap.get("ORIGINAL_BRAND") != null && !synonymMap.get("ORIGINAL_BRAND").isEmpty()) ? synonymMap.get("ORIGINAL_BRAND").get(0) : "");
713
					brand = ((synonymMap.get("ORIGINAL_BRAND") != null && !synonymMap.get("ORIGINAL_BRAND").isEmpty())
-
 
714
							? synonymMap.get("ORIGINAL_BRAND").get(0)
704
	            }
715
							: "");
-
 
716
				}
705
	            for(String model_name: modelNameSynonyms){
717
				for (String model_name : modelNameSynonyms) {
706
	            	for(String model_number: modelNumberSynonyms){
718
					for (String model_number : modelNumberSynonyms) {
707
	            		String title = brand + " " + model_name + " " + model_number;
719
						String title = brand + " " + model_name + " " + model_number;
708
	            		title = title.replaceAll("  ", " ");
720
						title = title.replaceAll("  ", " ");
709
	            		titles.add(title);
721
						titles.add(title);
710
	            	}
722
					}
711
	            }
723
				}
712
	            String originaltitle = brand + " " + originalModelName + " " + originalModelNumber;
724
				String originaltitle = brand + " " + originalModelName + " " + originalModelNumber;
713
	            originaltitle = originaltitle.replaceAll("  ", " ");
725
				originaltitle = originaltitle.replaceAll("  ", " ");
714
	            originaltitle = originaltitle.trim();
726
				originaltitle = originaltitle.trim();
715
	            if(!originaltitle.isEmpty()) {
727
				if (!originaltitle.isEmpty()) {
716
	            	finalsynonyms.put(originaltitle, titles);
728
					finalsynonyms.put(originaltitle, titles);
717
	            }
729
				}
718
	        } catch (Exception e) {
730
			} catch (Exception e) {
719
				e.printStackTrace();
731
				e.printStackTrace();
720
			}
732
			}
721
    	}
733
		}
722
    	
734
 
723
    	String autosuggestFilename = Utils.EXPORT_JAVASCRIPT_CONTENT_PATH + "autosuggest.json";
735
		String autosuggestFilename = Utils.EXPORT_JAVASCRIPT_CONTENT_PATH + "autosuggest.json";
724
        Gson gson = new Gson();
736
		Gson gson = new Gson();
725
		try {
737
		try {
726
			DBUtils.store(gson.toJson(finalsynonyms), autosuggestFilename);
738
			DBUtils.store(gson.toJson(finalsynonyms), autosuggestFilename);
727
		} catch (Exception e) {
739
		} catch (Exception e) {
728
			e.printStackTrace();
740
			e.printStackTrace();
729
		}
741
		}
730
    }
742
	}
731
 
743
 
732
	public double getMinPrice(List<Item> items, long catalogId, Source source) {
744
	public double getMinPrice(List<Item> items, long catalogId, Source source) {
733
		Item minPriceItem = null;
745
		Item minPriceItem = null;
734
		for (Item item : items) {
746
		for (Item item : items) {
735
			if (minPriceItem == null
-
 
736
					|| minPriceItem.getSellingPrice() > item.getSellingPrice()) {
747
			if (minPriceItem == null || minPriceItem.getSellingPrice() > item.getSellingPrice()) {
737
				minPriceItem = item;
748
				minPriceItem = item;
738
			}
749
			}
739
		}
750
		}
740
		return minPriceItem.getSellingPrice();
751
		return minPriceItem.getSellingPrice();
741
	}
752
	}