Subversion Repositories SmartDukaan

Rev

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

Rev 3720 Rev 3929
Line 1... Line 1...
1
package in.shop2020.util;
1
package in.shop2020.util;
-
 
2
 
2
import in.shop2020.metamodel.core.Entity;
3
import in.shop2020.metamodel.core.Entity;
3
import in.shop2020.metamodel.core.EntityState;
4
import in.shop2020.metamodel.core.EntityState;
4
import in.shop2020.metamodel.core.EntityStatus;
5
import in.shop2020.metamodel.core.EntityStatus;
5
import in.shop2020.metamodel.util.CreationUtils;
6
import in.shop2020.metamodel.util.CreationUtils;
6
import in.shop2020.metamodel.util.ExpandedEntity;
7
import in.shop2020.metamodel.util.ExpandedEntity;
Line 32... Line 33...
32
import org.apache.commons.cli.CommandLineParser;
33
import org.apache.commons.cli.CommandLineParser;
33
import org.apache.commons.cli.HelpFormatter;
34
import org.apache.commons.cli.HelpFormatter;
34
import org.apache.commons.cli.Options;
35
import org.apache.commons.cli.Options;
35
import org.apache.commons.cli.ParseException;
36
import org.apache.commons.cli.ParseException;
36
import org.apache.commons.cli.PosixParser;
37
import org.apache.commons.cli.PosixParser;
-
 
38
import org.apache.commons.logging.Log;
-
 
39
import org.apache.commons.logging.LogFactory;
37
 
40
 
-
 
41
public class ContentGenerationUtility {
-
 
42
    private static final String   UPDATE_TYPE_CATALOG         = "CATALOG";
38
 
43
 
-
 
44
    private static final String   UPDATE_TYPE_CONTENT         = "CONTENT";
39
 
45
 
-
 
46
    private static final String   COMMAND_LINE                = "java ContentGenerationUtility.class -t { ALL | INCREMENTAL | ONE } -u { CONTENT | CATALOG } -e {EntityId} ";
-
 
47
 
-
 
48
    private static Log            log                         = LogFactory
-
 
49
                                                                      .getLog(ContentGenerationUtility.class);
-
 
50
 
40
public class ContentGenerationUtility {
51
    // Commandline options
41
    private static Options options = null; // Command line options
52
    private static Options        options                     = null;
42
    
-
 
-
 
53
    private static final String   GENERATION_TYPE_INCREMENTAL = "INCREMENTAL";
-
 
54
    private static final String   GENERATION_TYPE_ALL         = "ALL";
-
 
55
    private static final String   GENERATION_TYPE_ONE         = "ONE";
43
    private static final String UPDATE_TYPE_OPTION = "u";
56
    private static final String   UPDATE_TYPE_OPTION          = "u";
44
    private static final String GENERATION_TYPE_OPTION = "t";
57
    private static final String   GENERATION_TYPE_OPTION      = "t";
45
    private static final String ENTITY_ID_OPTION = "e";
58
    private static final String   ENTITY_ID_OPTION            = "e";
46
    
59
 
-
 
60
    // Default values of cmdline options
47
    private static String UPDATE_TYPE = "CONTENT";
61
    private static String         UPDATE_TYPE                 = UPDATE_TYPE_CONTENT;
48
    private static String GENERATION_TYPE = "INCREMENTAL";
62
    private static String         GENERATION_TYPE             = GENERATION_TYPE_INCREMENTAL;
49
    private static String ENTITY_ID = "ALL";
63
    private static String         ENTITY_ID                   = "ALL";
-
 
64
 
-
 
65
    private CommandLine           cmd                         = null;
50
    Map<Long, Entity> entities;
66
    private Map<Long, List<Item>> entityIdItemMap             = new LinkedHashMap<Long, List<Item>>();
51
    List<Item> items;
67
    private Long                  lastGenerationTime          = 0l;
52
    List<Item> contentCompleteItems  = new ArrayList<Item>();
68
    private Map<Long, Entity>     entities;
53
    List<Item> phasedOutItems;
69
    private List<Item>            items;
54
    List<Source> sources;
70
    private List<Source>          sources;
55
    CatalogClient csc;
71
    private CatalogClient         csc;
56
    Client client;
72
    private Client                client;
57
    Map<Long, List<Item>> entityIdItemMap = new LinkedHashMap<Long, List<Item>>();
-
 
-
 
73
 
58
    private CommandLine cmd = null; // Command Line arguments
74
    private long                  newLastGenerationTime;
59
    Long lastGenerationTime;
-
 
60
    
75
 
61
    static{
76
    static {
62
        options = new Options();
77
        options = new Options();
63
        options.addOption(GENERATION_TYPE_OPTION, true, "Generation type");
78
        options.addOption(GENERATION_TYPE_OPTION, true, "Generation type");
64
        options.addOption(UPDATE_TYPE_OPTION, true, "Default is : " + UPDATE_TYPE);
79
        options.addOption(UPDATE_TYPE_OPTION, true, "Default is : "
-
 
80
                + UPDATE_TYPE);
65
        options.addOption(ENTITY_ID_OPTION, true, "all entities " + ENTITY_ID + " by default");
81
        options.addOption(ENTITY_ID_OPTION, true, "all entities " + ENTITY_ID
-
 
82
                + " by default");
66
    }
83
    }
67
    
84
 
68
    public ContentGenerationUtility() throws Exception{
85
    public ContentGenerationUtility() throws Exception {
69
        csc = new CatalogClient();
86
        csc = new CatalogClient();
70
        client = csc.getClient();
87
        client = csc.getClient();
71
        sources = client.getAllSources();
88
        sources = client.getAllSources();
72
    }
89
    }
73
    
-
 
74
 
90
 
75
    /**
91
    /**
76
     * @param args
92
     * @param args
77
     * @throws Exception 
93
     * @throws Exception
78
     */
94
     */
79
    public static void main(String[] args) throws Exception{
95
    public static void main(String[] args) throws Exception {
80
        ContentGenerationUtility cgu = new ContentGenerationUtility();
96
        ContentGenerationUtility cgu = new ContentGenerationUtility();
-
 
97
 
81
        //Load arguments
98
        // Load arguments
82
        cgu.loadArgs(args);
99
        cgu.loadArgs(args);
-
 
100
 
83
        //Call method based on arguments
101
        // Call method based on arguments
84
        cgu.callMethod();
102
        cgu.callMethod();
85
    }
103
    }
86
 
104
 
87
    
105
    /**
88
	/**
-
 
89
     * Validate and set command line arguments.
106
     * Validate and set command line arguments. Exit after printing usage if
90
     * Exit after printing usage if anything is astray
107
     * anything is astray
-
 
108
     * 
-
 
109
     * @param args
91
     * @param args String[] args as featured in public static void main()
110
     *            String[] args as featured in public static void main()
92
     */
111
     */
93
    private void loadArgs(String[] args){
112
    private void loadArgs(String[] args) {
94
        CommandLineParser parser = new PosixParser();
113
        CommandLineParser parser = new PosixParser();
-
 
114
 
95
        try {
115
        try {
96
            cmd = parser.parse(options, args);
116
            cmd = parser.parse(options, args);
97
        } catch (ParseException e) {
117
        } catch (ParseException e) {
98
            System.err.println("Error parsing arguments");
118
            log.error("Error parsing arguments", e);
99
            e.printStackTrace();
-
 
100
            System.exit(1);
119
            System.exit(1);
101
        }
120
        }
102
        
121
 
103
        // Check for mandatory args
122
        // Check for mandatory args
104
        
-
 
105
        if (!( cmd.hasOption(GENERATION_TYPE_OPTION)  &&  cmd.hasOption(UPDATE_TYPE_OPTION))){
123
        if (!(cmd.hasOption(GENERATION_TYPE_OPTION) && cmd
-
 
124
                .hasOption(UPDATE_TYPE_OPTION))) {
106
            HelpFormatter formatter = new HelpFormatter();
125
            HelpFormatter formatter = new HelpFormatter();
107
            formatter.printHelp("java ContentGenerationUtility.class -t { ALL | INCREMENTAL | ONE } -u { CONTENT | CATALOG } -e {EntityId} ", options);
126
            formatter.printHelp(COMMAND_LINE, options);
108
            System.exit(1);
127
            System.exit(1);
109
        }
128
        }
110
        
129
 
111
        GENERATION_TYPE = cmd.getOptionValue(GENERATION_TYPE_OPTION);
130
        GENERATION_TYPE = cmd.getOptionValue(GENERATION_TYPE_OPTION);
112
        
-
 
113
        UPDATE_TYPE = cmd.getOptionValue(UPDATE_TYPE_OPTION);
131
        UPDATE_TYPE = cmd.getOptionValue(UPDATE_TYPE_OPTION);
114
        
132
 
115
        // Look for optional args.
133
        // Look for optional args.
116
        if(GENERATION_TYPE.equals("ONE")){
134
        if (GENERATION_TYPE.equals(GENERATION_TYPE_ONE)) {
117
            if (cmd.hasOption(ENTITY_ID_OPTION)){
135
            if (cmd.hasOption(ENTITY_ID_OPTION)) {
118
                ENTITY_ID = cmd.getOptionValue(ENTITY_ID_OPTION);
136
                ENTITY_ID = cmd.getOptionValue(ENTITY_ID_OPTION);
119
            }else{
137
            } else {
120
                HelpFormatter formatter = new HelpFormatter();
138
                HelpFormatter formatter = new HelpFormatter();
121
                formatter.printHelp("java ContentGenerationUtility.class -t { ALL | INCREMENTAL | ONE } -u { CONTENT | CATALOG } -e {EntityId} ", options);
139
                formatter.printHelp(COMMAND_LINE, options);
122
                System.exit(1);
140
                System.exit(1);
123
            }
141
            }
124
        }
142
        }
125
    }
143
    }
126
    
144
 
127
    /**
145
    /**
128
     * Call method based on arguments
146
     * Call method based on arguments
-
 
147
     * 
129
     * @throws Exception
148
     * @throws Exception
130
     */
149
     */
131
    private void callMethod(){
150
    private void callMethod() {
132
    	boolean isSuccess = false;
151
        boolean isSuccess = false;
133
    	String logfile = "/tmp/content-from-cms.log";
152
        String logfile = "/tmp/content-from-cms.log";
134
    	if(UPDATE_TYPE.equals("CONTENT")){
153
        if (UPDATE_TYPE.equals(UPDATE_TYPE_CONTENT)) {
135
    		logfile = "/tmp/content-from-cms.log";
154
            logfile = "/tmp/content-from-cms.log";
136
    		try{
155
            try {
137
    			this.generateContent();
156
                this.generateContent();
138
    			isSuccess = true;
157
                isSuccess = true;
139
    		}catch (Exception e) {
158
            } catch (Exception e) {
140
    			e.printStackTrace();
159
                log.error("Error generating content", e);
141
			}
160
            }
142
    	}
161
        }
143
    	
162
 
144
    	if(UPDATE_TYPE.equals("CATALOG")){
163
        if (UPDATE_TYPE.equals(UPDATE_TYPE_CATALOG)) {
145
    		logfile = "/tmp/content-from-catalog.log";
164
            logfile = "/tmp/content-from-catalog.log";
146
    		try{
165
            try {
147
    			this.updatePrices();
166
                this.updatePrices();
148
    			isSuccess = true;
167
                isSuccess = true;
149
    		}catch (Exception e) {
168
            } catch (Exception e) {
150
    			e.printStackTrace();
169
                log.error("Error updating prices", e);
151
			}
170
            }
152
    	}
171
        }
153
    	
172
 
154
    	GmailUtils gm = new GmailUtils();
173
//        GmailUtils gm = new GmailUtils();
-
 
174
//        String[] sendTo = { "chandranshu.s@shop2020.in",
155
		String[] sendTo = { "chandranshu.s@shop2020.in", "rajveer.singh@shop2020.in", "mandeep.dhir@shop2020.in" };
175
//                "rajveer.singh@shop2020.in", "mandeep.dhir@shop2020.in" };
-
 
176
//
156
		try {
177
//        try {
-
 
178
//            gm.sendSSLMessage(sendTo, "Content Generation Successful ? : "
157
			gm.sendSSLMessage(sendTo, "Content Generation Successful ? : " + isSuccess, "Content generation completed at time : " + Calendar.getInstance().getTime().toString(), "build@shop2020.in", "shop2020", logfile);
179
//                    + isSuccess, "Content generation completed at time : "
-
 
180
//                    + Calendar.getInstance().getTime().toString(),
-
 
181
//                    "build@shop2020.in", "shop2020", logfile);
158
		} catch (MessagingException e) {
182
//        } catch (MessagingException e) {
159
			// TODO Auto-generated catch block
183
//            log.error("Could not send status mail", e);
160
			e.printStackTrace();
-
 
161
		}
-
 
162
    }
184
//        }
163
    
185
    }
164
 
186
 
165
    
-
 
166
	public boolean cleanDir(File dir, boolean deleteSelf) {
187
    public boolean cleanDir(File dir, boolean deleteSelf) {
167
	    if (dir.isDirectory()) {
188
        if (dir.isDirectory()) {
168
	        String[] children = dir.list();
189
            String[] children = dir.list();
169
	        for (int i=0; i<children.length; i++) {
190
            for (int i = 0; i < children.length; i++) {
170
	            boolean success = cleanDir(new File(dir, children[i]), true);
191
                boolean success = cleanDir(new File(dir, children[i]), true);
171
	            if (!success) {
192
                if (!success) {
172
	                return false;
193
                    return false;
173
	            }
194
                }
174
	        }
195
            }
175
	    }
196
        }
-
 
197
 
176
	    // The directory is now empty so delete it
198
        // The directory is now empty so delete it
177
	    if(deleteSelf){
199
        if (deleteSelf) {
178
	    	return dir.delete();
200
            return dir.delete();
179
	    }
201
        }
-
 
202
 
180
	    return true;
203
        return true;
181
	}
204
    }
182
	
-
 
183
	
205
 
184
	private void removeOldResources() throws IOException{
206
    private void removeOldResources() throws IOException {
185
		File f = new File(Utils.EXPORT_SOLR_PATH);
207
        File f = new File(Utils.EXPORT_SOLR_PATH);
186
		if(f.exists()){
208
        if (f.exists()) {
187
			cleanDir(f, false);
209
            cleanDir(f, false);
188
		}
210
        }
189
		
211
 
190
		File f1 = new File(Utils.EXPORT_ENTITIES_PATH_LOCALHOST);
212
        File f1 = new File(Utils.EXPORT_ENTITIES_PATH_LOCALHOST);
191
		if(f1.exists()){
213
        if (f1.exists()) {
192
			cleanDir(f1, false);
214
            cleanDir(f1, false);
193
		}
215
        }
194
		
216
 
195
		File f2 = new File(Utils.EXPORT_ENTITIES_PATH_SAHOLIC);
217
        File f2 = new File(Utils.EXPORT_ENTITIES_PATH_SAHOLIC);
196
		if(f2.exists()){
218
        if (f2.exists()) {
197
			cleanDir(f2, false);
219
            cleanDir(f2, false);
198
		}
220
        }
199
		
221
 
200
		File f3 = new File(Utils.EXPORT_ENTITIES_PATH_SHOP2020);
222
        File f3 = new File(Utils.EXPORT_ENTITIES_PATH_SHOP2020);
201
		if(f3.exists()){
223
        if (f3.exists()) {
202
			cleanDir(f3, false);
224
            cleanDir(f3, false);
203
		}
225
        }
204
	}
226
    }
205
	
227
 
206
    /**
228
    /**
207
     * Update the prices in the generated content
229
     * Update the prices in the generated content
-
 
230
     * 
208
     * @throws Exception
231
     * @throws Exception
209
     */
232
     */
210
    private void updatePrices() throws Exception {
233
    private void updatePrices() throws Exception {
211
    	lastGenerationTime = new Long(0);
234
        lastGenerationTime = new Long(0);
212
        if(GENERATION_TYPE.equals("ONE")) {
235
        if (GENERATION_TYPE.equals(GENERATION_TYPE_ONE)) {
213
            items = client.getItemsByCatalogId(Long.parseLong(ENTITY_ID));
236
            items = client.getItemsByCatalogId(Long.parseLong(ENTITY_ID));
214
        }else{
237
        } else {
215
            items = client.getAllItemsByStatus(status.ACTIVE);
238
            items = client.getAllItemsByStatus(status.ACTIVE);
216
            items.addAll(client.getAllItemsByStatus(status.PAUSED));
239
            items.addAll(client.getAllItemsByStatus(status.PAUSED));
217
 
240
 
218
            //Clean up the data from the solr directories.
241
            // Clean up the data from the solr directories.
219
            removeOldResources();
242
            removeOldResources();
220
 
243
 
221
        }
244
        }
222
        //this still needs to be evolved. Must not be used.
-
 
223
        if(GENERATION_TYPE.equals("INCREMENTAL")) {
-
 
224
        }
-
 
225
 
245
 
-
 
246
        // this still needs to be evolved. Must not be used.
-
 
247
        if (GENERATION_TYPE.equals(GENERATION_TYPE_INCREMENTAL)) {
-
 
248
        }
226
 
249
 
227
        
-
 
228
        //Populate the entityIdIemMap 
250
        // Populate the entityIdIemMap
229
        populateEntityIdItemMap();
251
        populateEntityIdItemMap();
230
        
252
 
231
        PriceInsertor priceInserter = new PriceInsertor();
253
        PriceInsertor priceInserter = new PriceInsertor();
232
		
-
 
233
        for(Map.Entry<Long, List<Item>> entry: entityIdItemMap.entrySet()){
-
 
234
        	long entityId = entry.getKey();
-
 
235
        	List<Item> items = entry.getValue();
-
 
236
        	//TODO Domain name and destination  directory should be read from properties file
-
 
237
        	double minPrice = priceInserter.insertPriceInHtml(items, entityId, "saholic.com", Utils.EXPORT_ENTITIES_PATH_SAHOLIC, null);
-
 
238
        	priceInserter.insertPriceInHtml(items, entityId, "shop2020.in", Utils.EXPORT_ENTITIES_PATH_SHOP2020, null);
-
 
239
        	priceInserter.insertPriceInHtml(items, entityId, "localhost:8090", Utils.EXPORT_ENTITIES_PATH_LOCALHOST, null);
-
 
240
        	StringBuilder priceString = new StringBuilder("<field name=\"F_50002\">"+ minPrice +"</field>"); 
-
 
241
        	
-
 
242
        	if(sources != null){
-
 
243
        		for(Source source: sources){
-
 
244
            		minPrice = priceInserter.insertPriceInHtml(items, entityId, "saholic.com", Utils.EXPORT_ENTITIES_PATH_SAHOLIC, source);
-
 
245
            		priceInserter.insertPriceInHtml(items, entityId, "shop2020.in", Utils.EXPORT_ENTITIES_PATH_SHOP2020, source);
-
 
246
                	priceInserter.insertPriceInHtml(items, entityId, "localhost:8090", Utils.EXPORT_ENTITIES_PATH_LOCALHOST, source);
-
 
247
                	priceString.append("<field name=\"F_50002_" + source.getId() + "\">" + minPrice +"</field>");
-
 
248
            	}
-
 
249
        	}
-
 
250
        	
-
 
251
        	priceInserter.insertPriceInSolrData(entityId, priceString.toString());
-
 
252
        }
-
 
253
        
-
 
254
        //Generate partners and json objects for phones only
-
 
255
        if(!GENERATION_TYPE.equals("ONE")) {
-
 
256
        	ProductListGenerator generator = new ProductListGenerator(entityIdItemMap);
-
 
257
			generator.generateProductsListXML();
-
 
258
			generator.generateProductListJavascript();
-
 
259
        }
-
 
260
 
254
 
-
 
255
        for (Map.Entry<Long, List<Item>> entry : entityIdItemMap.entrySet()) {
261
    }
256
            long entityId = entry.getKey();
-
 
257
            List<Item> items = entry.getValue();
-
 
258
            // TODO Domain name and destination directory should be read from
262
    
259
            // properties file
-
 
260
            double minPrice = priceInserter.insertPriceInHtml(items, entityId,
-
 
261
                    "saholic.com", Utils.EXPORT_ENTITIES_PATH_SAHOLIC, null);
-
 
262
            priceInserter.insertPriceInHtml(items, entityId, "shop2020.in",
-
 
263
                    Utils.EXPORT_ENTITIES_PATH_SHOP2020, null);
-
 
264
            priceInserter.insertPriceInHtml(items, entityId, "localhost:8090",
-
 
265
                    Utils.EXPORT_ENTITIES_PATH_LOCALHOST, null);
-
 
266
            StringBuilder priceString = new StringBuilder(
-
 
267
                    "<field name=\"F_50002\">" + minPrice + "</field>");
263
    
268
 
264
    /**
-
 
265
     * 
269
            if (sources != null) {
266
     * @param items
270
                for (Source source : sources) {
267
     * @return the minimum price of the items
271
                    minPrice = priceInserter.insertPriceInHtml(items, entityId,
-
 
272
                            "saholic.com", Utils.EXPORT_ENTITIES_PATH_SAHOLIC,
268
     */
273
                            source);
269
	private double getMinPrice(List<Item> items){
274
                    priceInserter.insertPriceInHtml(items, entityId,
-
 
275
                            "shop2020.in", Utils.EXPORT_ENTITIES_PATH_SHOP2020,
270
        double minPrice = Double.MAX_VALUE;
276
                            source);
271
        for(Item item: items){
277
                    priceInserter.insertPriceInHtml(items, entityId,
272
            if(minPrice > item.getSellingPrice()){
278
                            "localhost:8090",
-
 
279
                            Utils.EXPORT_ENTITIES_PATH_LOCALHOST, source);
273
                minPrice = item.getSellingPrice();
280
                    priceString.append("<field name=\"F_50002_"
-
 
281
                            + source.getId() + "\">" + minPrice + "</field>");
-
 
282
                }
274
            }
283
            }
-
 
284
 
-
 
285
            priceInserter.insertPriceInSolrData(entityId,
-
 
286
                    priceString.toString());
275
        }
287
        }
276
        return minPrice;
-
 
277
    }
-
 
278
 
288
 
-
 
289
        // Generate partners and json objects for phones only
-
 
290
        if (!GENERATION_TYPE.equals(GENERATION_TYPE_ONE)) {
-
 
291
            ProductListGenerator generator = new ProductListGenerator(
-
 
292
                    entityIdItemMap);
-
 
293
            generator.generateProductsListXML();
-
 
294
            generator.generateProductListJavascript();
-
 
295
        }
-
 
296
    }
279
 
297
 
280
	/**
298
    /**
281
     * Generates content for the specified entity embedding links to the
299
     * Generates content for the specified entity embedding links to the
282
     * specified domain name.
300
     * specified domain name.
283
     * 
301
     * 
284
     * The method will not generate content if one of the following conditions is met:
302
     * The method will not generate content if one of the following conditions
-
 
303
     * is met:
285
     * <ol>
304
     * <ol>
286
     * <li>The entity is not ready.
305
     * <li>The entity is not ready.
287
     * <li>The category has not been updated yet. (Set to -1).
306
     * <li>The category has not been updated yet. (Set to -1).
288
     * <li>The content has not been updated.
307
     * <li>The content has not been updated.
289
     * </ol>
308
     * </ol>
290
     *
309
     * 
291
     * @throws
310
     * @throws
292
     */
311
     */
293
    private void generateContent() throws Exception{
312
    private void generateContent() throws Exception {
294
        if(GENERATION_TYPE.equals("ALL")) {
313
        if (GENERATION_TYPE.equals(GENERATION_TYPE_ALL)) {
295
        	entities = CreationUtils.getEntities();
314
            entities = CreationUtils.getEntities();
296
            lastGenerationTime = new Long(0);
315
            lastGenerationTime = new Long(0);
297
        }else if(GENERATION_TYPE.equals("ONE")) {
316
        } else if (GENERATION_TYPE.equals(GENERATION_TYPE_ONE)) {
298
        	entities = new HashMap<Long, Entity>();
317
            entities = new HashMap<Long, Entity>();
-
 
318
            entities.put(Long.parseLong(ENTITY_ID),
299
        	entities.put(Long.parseLong(ENTITY_ID), CreationUtils.getEntity(Long.parseLong(ENTITY_ID)));
319
                    CreationUtils.getEntity(Long.parseLong(ENTITY_ID)));
300
            lastGenerationTime = new Long(0);   
320
            lastGenerationTime = new Long(0);
301
        }else{
321
        } else {
302
        	entities = CreationUtils.getEntities();
322
            entities = CreationUtils.getEntities();
-
 
323
            newLastGenerationTime = new Date().getTime();
303
            lastGenerationTime = CreationUtils.getLastContentGenerationTime();
324
            lastGenerationTime = CreationUtils.getLastContentGenerationTime();
-
 
325
            log.info("lastGenerationTime: " + lastGenerationTime);
304
            if(lastGenerationTime==null){
326
            if (lastGenerationTime == null) {
305
                lastGenerationTime = new Long(0);
327
                lastGenerationTime = new Long(0);
306
            }    
328
            }
307
        }
329
        }
-
 
330
 
308
        //Filter invalid entities here
331
        // Filter invalid entities here
309
        List<Entity> validEntities = new ArrayList<Entity>();
332
        List<Entity> validEntities = new ArrayList<Entity>();
310
        for(long entityID: entities.keySet()){
333
        for (long entityID : entities.keySet()) {
311
        	if(isValidEntity(entities.get(entityID))){
334
            if (isValidEntity(entities.get(entityID))) {
312
        		validEntities.add(entities.get(entityID));
335
                validEntities.add(entities.get(entityID));
313
        	}
336
            }
314
        }
337
        }
-
 
338
 
315
        //Calculate comparison scores
339
        // Calculate comparison scores
-
 
340
        log.info("Calculating comparison scores");
316
        NewCMP cmp = new NewCMP(validEntities);
341
        NewCMP cmp = new NewCMP(validEntities);
317
        Map<Long, Map<Long, Double>> slideScoresByEntity = cmp.getSlideScores();
342
        Map<Long, Map<Long, Double>> slideScoresByEntity = cmp.getSlideScores();
318
        CreationUtils.storeSlideScores(slideScoresByEntity);
343
        CreationUtils.storeSlideScores(slideScoresByEntity);
319
 
344
 
320
        // Fetch comparison statistics everyday and store them in BDB
345
        // Fetch comparison statistics everyday and store them in BDB
-
 
346
        log.info("Fetching comparison statistics");
321
        ComparisonStatsFetcher csf = new ComparisonStatsFetcher();
347
        ComparisonStatsFetcher csf = new ComparisonStatsFetcher();
322
        csf.fetchAndStoreComparisonStats();
348
        csf.fetchAndStoreComparisonStats();
323
        	
349
 
324
        // Upload catalod to Google App Engine.
350
        // Upload catalog to Google App Engine.
325
        if(GENERATION_TYPE.equals("ALL")) {
351
        if (GENERATION_TYPE.equals(GENERATION_TYPE_ALL)) {
-
 
352
            log.info("Uploading Catalog to Google app engine");
326
            List<Item> allItems = client.getAllItems(false);
353
            List<Item> allItems = client.getAllItems(false);
327
            allItems.addAll(client.getAllItems(true));
354
            allItems.addAll(client.getAllItems(true));
328
            CatalogUploderToGAE catalogUploaderToGAE = new CatalogUploderToGAE();
355
            CatalogUploderToGAE catalogUploaderToGAE = new CatalogUploderToGAE();
329
            catalogUploaderToGAE.uploadItems(allItems);
356
            catalogUploaderToGAE.uploadItems(allItems);
330
        }
357
        }
331
        
358
 
332
        items = client.getAllItemsByStatus(status.ACTIVE);
359
        items = client.getAllItemsByStatus(status.ACTIVE);
333
        items.addAll(client.getAllItemsByStatus(status.PAUSED));
360
        items.addAll(client.getAllItemsByStatus(status.PAUSED));
334
        items.addAll(client.getAllItemsByStatus(status.CONTENT_COMPLETE));
361
        items.addAll(client.getAllItemsByStatus(status.CONTENT_COMPLETE));
335
        populateEntityIdItemMap();
362
        populateEntityIdItemMap();
-
 
363
 
336
        
364
        log.info("Finding accessories");
337
        AccessoriesFinder af = new AccessoriesFinder(entityIdItemMap.keySet());
365
        AccessoriesFinder af = new AccessoriesFinder(entityIdItemMap.keySet());
338
		Map<Long, Map<Long, List<Long>>> relatedAccessories = af.findAccessories();
366
        Map<Long, Map<Long, List<Long>>> relatedAccessories = af.findAccessories();
339
		CreationUtils.storeRelatedAccessories(relatedAccessories);
367
        CreationUtils.storeRelatedAccessories(relatedAccessories);
340
 
368
 
-
 
369
        log.info("Writing JSON file for special pages");
341
        SpecialPageJSONConvertor bjc = new SpecialPageJSONConvertor();
370
        SpecialPageJSONConvertor bjc = new SpecialPageJSONConvertor();
342
        bjc.writeToJSONFile(new File(Utils.EXPORT_JAVASCRIPT_CONTENT_PATH + "special-pages.json"));
371
        bjc.writeToJSONFile(new File(Utils.EXPORT_JAVASCRIPT_CONTENT_PATH
-
 
372
                + "special-pages.json"));
343
 
373
 
-
 
374
        log.info("Generating velocity templates, images, documents etc.");
344
        NewVUI vui = new NewVUI(lastGenerationTime);
375
        NewVUI vui = new NewVUI(lastGenerationTime);
345
        for(Entity entity: validEntities){
376
        for (Entity entity : validEntities) {
-
 
377
            log.info("Processing Entityid: " + entity.getID());
346
        		vui.generateContentForOneEntity(entity, Utils.EXPORT_VELOCITY_PATH);
378
            vui.generateContentForOneEntity(entity, Utils.EXPORT_VELOCITY_PATH);
-
 
379
        }
-
 
380
 
-
 
381
        // Generate synonyms list. This will be used in PriceComparisonTool to
-
 
382
        // resolve the product names.
-
 
383
        log.info("Generating synonyms");
-
 
384
        SynonymExporter sx = new SynonymExporter(validEntities);
-
 
385
        sx.storeSynonyms();
-
 
386
 
-
 
387
        if (newLastGenerationTime != 0) {
-
 
388
            CreationUtils.storeLastContentGenerationTime(newLastGenerationTime);
347
        }
389
        }
-
 
390
 
-
 
391
 
348
        CreationUtils.storeLastContentGenerationTime((new Date()).getTime());
392
        log.info("Generating Solr files");
349
        
-
 
350
        
-
 
351
        NewIR ir = new NewIR(validEntities);
393
        NewIR ir = new NewIR(validEntities);
352
        ir.exportIRData();
394
        ir.exportIRData();
353
        //ir.transformIrDataXMLtoSolrXML();
395
        // ir.transformIrDataXMLtoSolrXML();
354
        ir.exportIRMetaData();
396
        ir.exportIRMetaData();
355
        ir.transformIrMetaDataXMLSolrSchemaXML();
397
        ir.transformIrMetaDataXMLSolrSchemaXML();
356
 
398
 
357
        // Generate synonyms list. This will be used in PriceComparisonTool to resolve the product names.
-
 
358
        SynonymExporter sx = new SynonymExporter(validEntities);
-
 
359
        sx.storeSynonyms();
-
 
360
        
-
 
361
        for(Map.Entry<Long, List<Item>> entry: entityIdItemMap.entrySet()){
399
        for (Map.Entry<Long, List<Item>> entry : entityIdItemMap.entrySet()) {
362
        	List<Item> items = entry.getValue();
400
            List<Item> items = entry.getValue();
363
        	for(Item item: items){
401
            for (Item item : items) {
364
        		if(item.getItemStatus()==status.CONTENT_COMPLETE){
402
                if (item.getItemStatus() == status.CONTENT_COMPLETE) {
365
                    item.setItemStatus(status.ACTIVE);
403
                    item.setItemStatus(status.ACTIVE);
366
                    item.setStatus_description("This item is active");
404
                    item.setStatus_description("This item is active");
367
                    client.updateItem(item);
405
                    client.updateItem(item);
368
        		}
406
                }
369
        	}
407
            }
370
        }
408
        }
371
        
-
 
372
    }
409
    }
373
 
410
 
374
    
-
 
375
    /**
411
    /**
376
     * Checks weather entity is valid or not. Entity will be invalid in one of these cases:
412
     * Checks weather entity is valid or not. Entity will be invalid in one of
-
 
413
     * these cases:
377
     * <ol>
414
     * <ol>
378
     * <li>The entity is not ready.
415
     * <li>The entity is not ready.
379
     * <li>The category has not been updated yet. (Set to -1).
416
     * <li>The category has not been updated yet. (Set to -1).
380
     * <li>Content has not been updated after last content generation timestamp.
417
     * <li>Content has not been updated after last content generation timestamp.
381
     * </ol>
418
     * </ol>
382
     * 
419
     * 
383
     * @param entity
420
     * @param entity
384
     * @return
421
     * @return
385
     * @throws Exception
422
     * @throws Exception
386
     */
423
     */
387
    private boolean isValidEntity(Entity entity) throws Exception{
424
    private boolean isValidEntity(Entity entity) throws Exception {
388
        ExpandedEntity expEntity = new ExpandedEntity(entity);
425
        ExpandedEntity expEntity = new ExpandedEntity(entity);
389
        EntityState state = CreationUtils.getEntityState(entity.getID());
426
        EntityState state = CreationUtils.getEntityState(entity.getID());
390
        long categoryID = expEntity.getCategoryID();
427
        long categoryID = expEntity.getCategoryID();
391
        
428
 
392
        if(state.getStatus() != EntityStatus.READY ||  categoryID == -1){
429
        if (state.getStatus() != EntityStatus.READY || categoryID == -1) {
393
            return false;
430
            return false;
394
        }
431
        }
395
        if(state.getMerkedReadyOn().getTime() < this.lastGenerationTime){
432
        if (state.getMerkedReadyOn().getTime() < this.lastGenerationTime) {
396
            return false;
433
            return false;
397
        }
434
        }
398
        return true;
435
        return true;
399
    }
436
    }
400
 
437
 
401
    
-
 
402
    private void populateEntityIdItemMap(){
438
    private void populateEntityIdItemMap() {
403
        Date todate = new Date();
439
        Date todate = new Date();
404
        for(Item item: items){
440
        for (Item item : items) {
405
            //TODO Can be removed as we are checking in calling function
441
            // TODO Can be removed as we are checking in calling function
-
 
442
            if (!(item.getItemStatus() == status.ACTIVE
406
            if(!(item.getItemStatus()==status.ACTIVE || item.getItemStatus()==status.CONTENT_COMPLETE || item.getItemStatus() == status.PAUSED)){
443
                    || item.getItemStatus() == status.CONTENT_COMPLETE || item
-
 
444
                    .getItemStatus() == status.PAUSED)) {
407
                continue;
445
                continue;
408
            }
446
            }
409
            if(todate.getTime() < item.getStartDate() ||  item.getSellingPrice() == 0){
447
            if (todate.getTime() < item.getStartDate()
-
 
448
                    || item.getSellingPrice() == 0) {
410
                continue;
449
                continue;
411
            }
450
            }
412
            List<Item> itemList = entityIdItemMap.get(item.getCatalogItemId());
451
            List<Item> itemList = entityIdItemMap.get(item.getCatalogItemId());
413
            if(itemList == null){
452
            if (itemList == null) {
414
                itemList = new ArrayList<Item>();
453
                itemList = new ArrayList<Item>();
415
            }
454
            }
416
            itemList.add(item);
455
            itemList.add(item);
417
            entityIdItemMap.put(item.getCatalogItemId(), itemList);
456
            entityIdItemMap.put(item.getCatalogItemId(), itemList);
418
        }
457
        }
419
 
458
 
420
        //Remove all items which have not been updated since last content generation.
459
        // Remove all items which have not been updated since last content
-
 
460
        // generation.
421
        List<Long> removeEntities = new ArrayList<Long>();
461
        List<Long> removeEntities = new ArrayList<Long>();
422
        for(Long entityId:entityIdItemMap.keySet()){
462
        for (Long entityId : entityIdItemMap.keySet()) {
423
            boolean isValidEntity = false;
463
            boolean isValidEntity = false;
424
            //If any one of the items has been updated before current timestamp, than we generate content for whole entity
464
            // If any one of the items has been updated before current
-
 
465
            // timestamp, than we generate content for whole entity
425
            for(Item item: entityIdItemMap.get(entityId)){
466
            for (Item item : entityIdItemMap.get(entityId)) {
426
                if(item.getUpdatedOn() > lastGenerationTime){
467
                if (item.getUpdatedOn() > lastGenerationTime) {
427
                    isValidEntity = true;
468
                    isValidEntity = true;
428
                }
469
                }
429
            }
470
            }
430
            if(!isValidEntity){
471
            if (!isValidEntity) {
431
                removeEntities.add(entityId);
472
                removeEntities.add(entityId);
432
            }
473
            }
433
        }
474
        }
434
        for(Long entityId: removeEntities){
475
        for (Long entityId : removeEntities) {
435
            entityIdItemMap.remove(entityId);
476
            entityIdItemMap.remove(entityId);
436
        }
477
        }
437
    }
478
    }
438
    
-
 
439
}
479
}