Subversion Repositories SmartDukaan

Rev

Rev 5564 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5522 varun.gupt 1
package in.shop2020.util;
2
 
3
import java.io.File;
4
import java.util.ArrayList;
5
import java.util.HashMap;
6
import java.util.List;
7
import java.util.Map;
8
 
9
import org.apache.commons.lang.StringUtils;
10
 
11
import in.shop2020.metamodel.core.Bullet;
12
import in.shop2020.metamodel.core.Entity;
13
import in.shop2020.metamodel.core.Feature;
14
import in.shop2020.metamodel.core.PrimitiveDataObject;
15
import in.shop2020.metamodel.core.Slide;
16
import in.shop2020.metamodel.definitions.Catalog;
17
import in.shop2020.metamodel.definitions.DefinitionsContainer;
18
import in.shop2020.metamodel.util.CreationUtils;
19
import in.shop2020.model.v1.catalog.Item;
20
import in.shop2020.model.v1.catalog.status;
21
import in.shop2020.model.v1.catalog.InventoryService.Client;
22
import in.shop2020.thrift.clients.CatalogClient;
23
 
24
public class MobileSiteDataXMLGenerator {
25
 
26
	private List<Entity> entities;
27
	private List<Item> items;
28
	private Map<Long, Item> entityItemMap = new HashMap<Long, Item>();
29
	private String[] xmlIndentation = {"", "    ", "        ", "            ","                "};
30
 
31
	private DefinitionsContainer defContainer = Catalog.getInstance().getDefinitionsContainer();
32
    private CatalogClient csc;
33
    private Client client;
34
 
35
	public MobileSiteDataXMLGenerator(List<Entity> entities) throws Exception {
36
		this.entities = entities;
37
        csc = new CatalogClient();
38
        client = csc.getClient();
39
 
40
        items = client.getAllItemsByStatus(status.ACTIVE);
41
        items.addAll(client.getAllItemsByStatus(status.COMING_SOON));
42
        items.addAll(client.getAllItemsByStatus(status.PAUSED));
43
 
44
        for (Item item: items)	{
45
        	if(!entityItemMap.containsKey(item.getCatalogItemId()))	{
46
 
47
        		entityItemMap.put(item.getCatalogItemId(), item);
48
        	}
49
        }
50
	}
51
 
52
	void generate() throws Exception	{
53
    	List<String> productXMLSnippets = new ArrayList<String>();
54
		productXMLSnippets.add("<Products>");
55
 
56
		for (Entity entity: entities)	{
57
			try	{
58
				long categoryId = defContainer.getCategory(entity.getCategoryID()).getParentCategory().getID();
59
 
60
				if(categoryId != Utils.MOBILE_PHONES_CATAGORY && categoryId != Utils.TABLETS_CATEGORY && categoryId != Utils.LAPTOPS_CATEGORY)	{
61
					continue;
62
				}
63
			} catch (NullPointerException e) {
64
				continue;
65
			}
66
 
67
			String description = "";
68
			Slide slide = entity.getSlide(130054);
69
 
70
			if (slide == null)	continue;
71
 
72
			List<Feature> features = slide.getFeatures();
73
 
74
			if (features == null)	continue;
75
 
76
			for(Feature feature: slide.getFeatures())	{
77
 
78
				if (feature.getFeatureDefinitionID() == 120081) {
79
	                List<Bullet> bullets = feature.getBullets();
80
 
81
	                if (bullets == null)	continue;
82
 
83
	                int counter = 0;
84
 
85
	                for (Bullet bullet : bullets) {
86
 
87
	                    PrimitiveDataObject dataObject = (PrimitiveDataObject) bullet.getDataObject();
88
 
89
	                    description += dataObject.getValue();
90
	                    if(counter < 2)	description += ", ";
91
	                    counter ++;
92
	                }
93
	            }
94
			}
95
			if (! entityItemMap.containsKey(entity.getID()))	continue;
96
 
97
			Item item = entityItemMap.get(entity.getID());
98
			String productType = "";
99
			String url = getProductURL(entity.getID(), item);
100
			String imageUrl = "http://www.saholic.com/images/website" + File.separator + entity.getID() + File.separator + "thumbnail.jpg";
101
 
102
			if (entity.getCategoryID() == 10001 || entity.getCategoryID() == 10002 || entity.getCategoryID() == 10003 || entity.getCategoryID() == 10004 || entity.getCategoryID() == 10005)	{
103
				productType = "Mobile Phone";
104
 
105
			} else if (entity.getCategoryID() == 10010)	{
106
				productType = "Tablet";
107
 
108
			} else if (entity.getCategoryID() == 10050)	{
109
				productType = "Laptop";
110
			}
111
			productXMLSnippets.add(this.xmlIndentation[1] + "<Product>");	
112
 
113
			productXMLSnippets.add(this.xmlIndentation[2] + "<ID>" + entity.getID() + "</ID>");
114
			productXMLSnippets.add(this.xmlIndentation[2] + "<Type>" + productType + "</Type>");
115
			productXMLSnippets.add(this.xmlIndentation[2] + "<Brand>" + item.getBrand() + "</Brand>");
116
			productXMLSnippets.add(this.xmlIndentation[2] + "<ModelName>" + item.getModelName() + "</ModelName>");
117
			productXMLSnippets.add(this.xmlIndentation[2] + "<ModelNumber>" + item.getModelNumber() + "</ModelNumber>");
118
			productXMLSnippets.add(this.xmlIndentation[2] + "<URL>" + url + "</URL>");
119
			productXMLSnippets.add(this.xmlIndentation[2] + "<ImageURL>" + imageUrl + "</ImageURL>");
120
			productXMLSnippets.add(this.xmlIndentation[2] + "<ShortDesc>" + description + "</ShortDesc>");
121
			productXMLSnippets.add(this.xmlIndentation[2] + "<MRP>" + item.getMrp() + "</MRP>");
122
			productXMLSnippets.add(this.xmlIndentation[2] + "<SellingPrice>" + (int)item.getSellingPrice() + "</SellingPrice>");
123
 
124
			productXMLSnippets.add(this.xmlIndentation[1] + "</Product>");
125
		}
126
		productXMLSnippets.add("</Products>");
127
		String productDataXML = StringUtils.join(productXMLSnippets, "\n");
128
 
129
		Utils.info(productDataXML);
130
 
131
		// Write it to file
132
		String productXMLFilename = Utils.EXPORT_PARTNERS_CONTENT_PATH + "msitedata.xml";
133
		DBUtils.store(productDataXML, productXMLFilename);
134
	}
135
 
136
	private String getProductURL(long entityId, Item item){
137
		//long rootCategoryId = catm.getCategory(item.getCategory()).getParent_category_id();
138
		in.shop2020.metamodel.definitions.Category parentCategory = defContainer.getCategory(item.getCategory()).getParentCategory();
139
 
140
		String url = "http://www.saholic.com/" + parentCategory.getLabel().toLowerCase().replace(' ', '-') + "/";
141
		String productUrl = ((item.getBrand() != null) ? item.getBrand().trim() + " " : "").toLowerCase().replace(' ', '-')
142
        + "-" + ((item.getModelName() != null) ? item.getModelName().trim() + " " : "").toLowerCase().replace(' ', '-') 
143
	    + "-" + (( item.getModelNumber() != null ) ? item.getModelNumber().trim() + " ": "" ).toLowerCase().replace(' ', '-')
144
        + "-" + entityId;
145
		productUrl = productUrl.replaceAll("/", "-");
146
		url = url + productUrl;
147
		url = url.replaceAll("-+", "-");
148
		return url;
149
	}
150
 
151
	public static void main(String[] args) {
152
		try {
153
			List<Entity> entities = new ArrayList<Entity>(CreationUtils.getEntities().values());
154
			MobileSiteDataXMLGenerator generator = new MobileSiteDataXMLGenerator(entities);
155
			generator.generate();
156
		} catch (Exception e) {
157
			e.printStackTrace();
158
		}
159
	}
160
}