Subversion Repositories SmartDukaan

Rev

Rev 2171 | Rev 2362 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2171 rajveer 1
package in.shop2020.util;
2
import java.util.ArrayList;
3
import java.util.Date;
4
import java.util.LinkedHashMap;
5
import java.util.List;
6
import java.util.Map;
7
 
8
import in.shop2020.metamodel.core.Entity;
9
import in.shop2020.metamodel.core.EntityState;
10
import in.shop2020.metamodel.core.EntityStatus;
11
import in.shop2020.metamodel.util.CreationUtils;
12
import in.shop2020.metamodel.util.ExpandedEntity;
13
import in.shop2020.model.v1.catalog.InventoryService.Client;
14
import in.shop2020.model.v1.catalog.Item;
15
import in.shop2020.model.v1.catalog.status;
16
import in.shop2020.thrift.clients.CatalogServiceClient;
17
import in.shop2020.ui.util.NewVUI;
18
 
19
import org.apache.commons.cli.*;
20
 
21
 
22
 
23
public class ContentGenerationUtility {
24
    private static Options options = null; // Command line options
25
 
26
    private static final String GENERATION_TYPE_OPTION = "t";
27
    private static final String ENTITY_ID_OPTION = "e";
28
 
29
    private static String GENERATION_TYPE = "INCREMENTAL";
30
    private static String ENTITY_ID = "ALL";
31
    Map<Long, Entity> entities;
32
    List<Item> items;
33
    List<Item> contentCompleteItems;
34
    CatalogServiceClient csc;
35
    Client client;
36
    Map<Long, List<Item>> entityIdItemMap = new LinkedHashMap<Long, List<Item>>();
37
    private CommandLine cmd = null; // Command Line arguments
38
    Long lastGenerationTime;
39
 
40
    static{
41
        options = new Options();
42
        options.addOption(GENERATION_TYPE_OPTION, true, "Generation type");
43
        options.addOption(ENTITY_ID_OPTION, true, "all entities " + ENTITY_ID + " by default");
44
 
45
    }
46
 
47
    public ContentGenerationUtility() throws Exception{
48
        csc = new CatalogServiceClient();
49
        client = csc.getClient();
50
    }
51
 
52
    /**
53
     * @param args
54
     * @throws Exception 
55
     */
56
    public static void main(String[] args) throws Exception {
57
        ContentGenerationUtility cgu = new ContentGenerationUtility();
58
        cgu.loadArgs(args);
59
        cgu.generateContent();
60
    }
61
 
62
    /**
63
     * Validate and set command line arguments.
64
     * Exit after printing usage if anything is astray
65
     * @param args String[] args as featured in public static void main()
66
     */
67
    private void loadArgs(String[] args){
68
        CommandLineParser parser = new PosixParser();
69
        try {
70
            cmd = parser.parse(options, args);
71
        } catch (ParseException e) {
72
            System.err.println("Error parsing arguments");
73
            e.printStackTrace();
74
            System.exit(1);
75
        }
76
 
77
        // Check for mandatory args
78
 
79
        if (! cmd.hasOption(GENERATION_TYPE_OPTION)){
80
            HelpFormatter formatter = new HelpFormatter();
81
            formatter.printHelp("java ContentGenerationUtility.class -t {ALL,INCREMENTAL,ONE} -e {EntityId} ", options);
82
            System.exit(1);
83
        }
84
 
85
        GENERATION_TYPE = cmd.getOptionValue(GENERATION_TYPE_OPTION);
86
        // Look for optional args.
87
 
88
        if(GENERATION_TYPE.equals("ONE")){
89
            if (cmd.hasOption(ENTITY_ID_OPTION)){
90
                ENTITY_ID = cmd.getOptionValue(ENTITY_ID_OPTION);
91
            }else{
92
                HelpFormatter formatter = new HelpFormatter();
93
                formatter.printHelp("java ContentGenerationUtility.class -t {ALL,INCREMENTAL,ONE} -e {EntityId} ", options);
94
                System.exit(1);
95
            }
96
        }
97
 
98
    }
99
 
100
    /**
101
     * Generates content for the specified entity embedding links to the
102
     * specified domain name.
103
     * 
104
     * The method updates the member variable problems in any of the following
105
     * four cases:
106
     * <ol>
107
     * <li>The entity is not ready.
108
     * <li>The category has not been updated yet. (Set to -1).
109
     * <li>There are no items in the catalog corresponding to this entity.
110
     * <li>There are no active or content-complete items in the catalog
111
     * corresponding to this entity.
112
     * <li>Neither the items have been updated nor the content has been updated.
113
     * </ol>
114
     * 
115
     * @param entity
116
     *            - Entity for which the content has to be generated.
117
     * @param domain
118
     *            - The domain name to be used to serve static content.
119
     * @param exportPath
120
     *            - Local file system path where content has to be generated.
121
     * @return -True if content is generated successfully, False otherwise.
122
     * @throws Exception
123
     */
124
 
125
    /**
126
     * 
127
     */
128
    private void generateContent() throws Exception{
129
        if(GENERATION_TYPE.equals("ALL") || GENERATION_TYPE.equals("ONE")) {
130
            lastGenerationTime = new Long(0);   
131
        }else{
132
            lastGenerationTime = CreationUtils.getLastContentGenerationTime();
133
            if(lastGenerationTime==null){
134
                lastGenerationTime = new Long(0);
135
            }    
136
        }
137
 
138
        if(GENERATION_TYPE.equals("ONE")) {
139
            items = client.getItemsByCatalogId(Long.parseLong(ENTITY_ID));
140
            contentCompleteItems = new ArrayList<Item>();
141
            for(Item item: items){
142
                if(item.getItemStatus() == status.CONTENT_COMPLETE){
143
                    contentCompleteItems.add(item);
144
                }
145
 
146
            }
147
        }else{
148
            contentCompleteItems = client.getAllItemsByStatus(status.CONTENT_COMPLETE);
149
            items = client.getAllItemsByStatus(status.ACTIVE);
150
            items.addAll(client.getAllItemsByStatus(status.PAUSED));
151
            items.addAll(contentCompleteItems);
152
        }
153
        //If non incremental content needs to be generated
154
        if(GENERATION_TYPE.equals("INCREMENTAL")) {
155
 
156
        }
157
 
158
        removeInvalidItems();
159
 
160
        NewCMP cmp = new NewCMP(entityIdItemMap);
161
        Map<Long, Map<Long, Double>> slideScoresByEntity = cmp.getSlideScores();
162
//        Utils.info("slideScoresByEntity=" + slideScoresByEntity);
163
 
164
        CreationUtils.storeSlideScores(slideScoresByEntity);
165
 
166
        NewVUI vui = new NewVUI(lastGenerationTime);
167
        for(long entityID: entityIdItemMap.keySet()){
168
            //TODO Domain name and destination  directory should be read from properties file 
169
            vui.generateHtmlForOneEntity(CreationUtils.getEntity(entityID), "saholic.com", Utils.EXPORT_ENTITIES_PATH_SAHOLIC, entityIdItemMap.get(entityID));
170
            vui.generateHtmlForOneEntity(CreationUtils.getEntity(entityID), "shop2020.in", Utils.EXPORT_ENTITIES_PATH_SHOP2020, entityIdItemMap.get(entityID));
171
            vui.generateHtmlForOneEntity(CreationUtils.getEntity(entityID), "localhost:8090", Utils.EXPORT_ENTITIES_PATH_LOCALHOST, entityIdItemMap.get(entityID));
172
        }
173
 
174
        // Mark content-complete items as active after the content has been
175
        // generated
176
        for(Item item:contentCompleteItems){
177
            item.setItemStatus(status.ACTIVE);
178
            item.setStatus_description("This item is active");
179
            client.updateItem(item);
180
        }
181
 
2227 rajveer 182
        NewIR ir = new NewIR(entityIdItemMap);
183
        ir.exportIRData();
184
        ir.transformIrDataXMLtoSolrXML();
185
        ir.exportIRMetaData();
186
        ir.transformIrMetaDataXMLSolrSchemaXML();
187
 
2171 rajveer 188
    }
189
 
190
    /**
191
     * Iterate through all candidate items and remove item if one of following conditions met.
192
     * <li> If item is not in one  of three states ( ACTIVE, CONTENT_COMPLETE, PAUSED)
193
     * <li> If Entity is invalid 
194
     * <li> If neither content nor item is updated after last content generation
195
     *   
196
     * @throws Exception 
197
     *   
198
     */
199
    private void removeInvalidItems() throws Exception{
200
        entities =  CreationUtils.getEntities();
201
        Date todate = new Date();
202
        for(Item item: items){            
203
            List<Item> itemList = entityIdItemMap.get(item.getCatalogItemId());
204
            //TODO Can be removed as we are checking in calling function
205
            if(!(item.getItemStatus()==status.ACTIVE || item.getItemStatus()==status.CONTENT_COMPLETE || item.getItemStatus() == status.PAUSED)){
206
                continue;
207
            }
208
            if(todate.getTime() < item.getStartDate()){
209
                continue;
210
            }
211
            if(itemList == null){
212
 
213
                if(!isValidEntity(entities.get(item.getCatalogItemId()))){
214
                    continue;
215
                }
216
                itemList = new ArrayList<Item>();
217
            }
218
            itemList.add(item);
219
            entityIdItemMap.put(item.getCatalogItemId(), itemList);
220
        }
221
 
222
        /**
223
         * Remove all items which have not been updated ever.
224
         */
225
        List<Long> removeEntities = new ArrayList<Long>();
226
        for(Long entityId:entityIdItemMap.keySet()){
227
            boolean isValidEntity = false;
228
            //If any one of the items has been updated since last generation timestamp, than we generate content for whole entity
229
            for(Item item: entityIdItemMap.get(entityId)){
230
                if(item.getUpdatedOn() > this.lastGenerationTime){
231
                    isValidEntity = true;
232
                }
233
            }
234
            if(!isValidEntity){
235
                removeEntities.add(entityId);
236
            }
237
        }
238
        for(Long entityId: removeEntities){
239
            entityIdItemMap.remove(entityId);
240
        }
241
 
242
 
243
    }
244
 
245
    /**
246
     * Checks weather entity is valid or not. Entity will be invalid in one of these cases:
247
     * <ol>
248
     * <li>The entity is not ready.
249
     * <li>The category has not been updated yet. (Set to -1).
250
     * <li>Content has not been updated after last content generation timestamp.
251
     * </ol>
252
     * 
253
     * @param entity
254
     * @return
255
     * @throws Exception
256
     */
257
    private boolean isValidEntity(Entity entity) throws Exception{
258
        ExpandedEntity expEntity = new ExpandedEntity(entity);
259
        EntityState state = CreationUtils.getEntityState(entity.getID());
260
        long categoryID = expEntity.getCategoryID();
261
 
262
        if(state.getStatus() != EntityStatus.READY ||  categoryID == -1){
263
            return false;
264
        }
265
        if(state.getMerkedReadyOn().getTime() < this.lastGenerationTime){
266
            return false;
267
        }
268
        return true;
269
    }
270
 
271
}