Subversion Repositories SmartDukaan

Rev

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