Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
2367 rajveer 1
package in.shop2020.ui.util;
2
 
3719 mandeep.dh 3
import in.shop2020.model.v1.catalog.Category;
3560 rajveer 4
import in.shop2020.model.v1.catalog.InventoryServiceException;
2367 rajveer 5
import in.shop2020.model.v1.catalog.Item;
3560 rajveer 6
import in.shop2020.model.v1.catalog.Source;
7
import in.shop2020.model.v1.catalog.InventoryService.Client;
8
import in.shop2020.model.v1.catalog.SourceItemPricing;
9
import in.shop2020.thrift.clients.CatalogClient;
2367 rajveer 10
import in.shop2020.util.Utils;
11
import in.shop2020.utils.CategoryManager;
12
 
13
import java.io.BufferedWriter;
14
import java.io.File;
15
import java.io.FileOutputStream;
16
import java.io.IOException;
17
import java.io.OutputStreamWriter;
18
import java.util.ArrayList;
19
import java.util.HashMap;
20
import java.util.List;
21
import java.util.Map;
22
import java.util.Properties;
23
 
24
import org.apache.commons.io.FileUtils;
3560 rajveer 25
import org.apache.thrift.TException;
26
import org.apache.thrift.transport.TTransportException;
2367 rajveer 27
import org.apache.velocity.Template;
28
import org.apache.velocity.VelocityContext;
29
import org.apache.velocity.app.Velocity;
30
import org.apache.velocity.exception.ParseErrorException;
31
import org.apache.velocity.exception.ResourceNotFoundException;
32
 
33
 
34
/**
35
 * utility class to generated all html stuff from java objects
36
 * Driver class to merge Java data objects with VTL script. 
37
 * 
38
 * @author rajveer
39
 *
40
 */
41
public class PriceInsertor {
3560 rajveer 42
    CatalogClient csc;
43
    Client client;
2367 rajveer 44
 
3560 rajveer 45
	public PriceInsertor() throws TTransportException{
46
        csc = new CatalogClient();
47
        client = csc.getClient();
48
	}
49
 
50
	public void insertPriceInSolrData(long catalogId, String priceString) throws IOException{
2367 rajveer 51
		String irSolrFilename = Utils.EXPORT_PATH + "xml/final/" + catalogId + "_irdata_solr.xml";
52
		String finalFile = Utils.EXPORT_PATH + "solr/" + catalogId + "_irdata_solr.xml";
53
		File f = new File(irSolrFilename);
54
		String s = FileUtils.readFileToString(f);
3560 rajveer 55
		s = s.replaceAll("<field name=\"F_50002\">.*</field>", priceString);
2367 rajveer 56
		File f1 = new File(finalFile);
57
		FileUtils.writeStringToFile(f1, s);
58
	}
59
 
3560 rajveer 60
	public double insertPriceInHtml(List<Item> items, long catalogId, String domain, String exportPath, Source source){
2367 rajveer 61
		List<Map<String, String>> itemDetails = new ArrayList<Map<String,String>>(); 
62
		Item minPriceItem = null;
63
		for(Item item: items){
64
			Map<String, String> itemDetail = new HashMap<String, String>();
65
			boolean showmrp = true;
3560 rajveer 66
			double sellingPrice = item.getSellingPrice();
67
			double mrp = item.getMrp();
68
 
69
			if(source != null){
70
				try {
71
					SourceItemPricing sip = client.getItemPricingBySource(item.getId(), source.getId());
72
					sellingPrice = sip.getSellingPrice();
73
					mrp = sip.getMrp();
74
				} catch (TException e) {
3575 rajveer 75
 
3560 rajveer 76
				} catch (InventoryServiceException e) {
3575 rajveer 77
 
3560 rajveer 78
				}
79
			}
80
 
81
			if (mrp <= sellingPrice) {
2367 rajveer 82
				showmrp = false;
83
			}
84
			if(minPriceItem == null || minPriceItem.getSellingPrice() > item.getSellingPrice()){
85
				minPriceItem = item;
86
			}
3560 rajveer 87
 
2367 rajveer 88
			double saving = Math.round((mrp-sellingPrice)/mrp*100);
89
			itemDetail.put("ITEM_ID", ((int)item.getId())+"");	
3560 rajveer 90
			itemDetail.put("SP", ((int)sellingPrice)+"");
91
			itemDetail.put("MRP", ((int)mrp)+"");
2367 rajveer 92
			itemDetail.put("SAVING", ((int)saving)+"");
93
			itemDetail.put("COLOR", item.getColor());
94
			itemDetail.put("CATALOG_ID", ((int)item.getCatalogItemId())+"");
95
			itemDetail.put("SHOWMRP", showmrp +"");
96
			itemDetail.put("IS_SELECTED", item.isDefaultForEntity() ? "SELECTED" : "");
97
			itemDetails.add(itemDetail);
98
		}
3575 rajveer 99
 
100
 
101
 
3576 rajveer 102
 
103
		if(source == null){
104
			exportPath = exportPath + catalogId;
105
		}else{
3575 rajveer 106
			exportPath = exportPath + catalogId + File.separator + source.getId();
3560 rajveer 107
		}
2367 rajveer 108
 
3575 rajveer 109
		File exportDir = new File(exportPath);
110
 
2367 rajveer 111
		if(!exportDir.exists()) {
112
			exportDir.mkdir();
113
		}
114
 
115
		// This URL is used to ensure that images such as icon and thumbnail for
116
		// a particular entity are always downloaded from the same location.
117
		String staticurl = "http://static" + catalogId%3 + "." + domain;
118
 
119
 
120
		VelocityContext context = new VelocityContext();
121
		context.put("itemDetails", itemDetails);
122
		context.put("minPriceItem", minPriceItem);
123
		context.put("domain", domain);
124
		context.put("staticurl", staticurl);
125
 
126
		List<String> filenames = new ArrayList<String>();
127
		filenames.add("ProductDetail");
128
		filenames.add("WidgetSnippet");
129
		filenames.add("HomeSnippet");
130
		filenames.add("SearchSnippet");
131
		filenames.add("CategorySnippet");
132
		filenames.add("SlideGuide");
133
		filenames.add("ProductPropertiesSnippet");
2716 varun.gupt 134
		filenames.add("MyResearchSnippet");
3018 rajveer 135
		filenames.add("AfterSales");
2716 varun.gupt 136
 
3719 mandeep.dh 137
		Category category = CategoryManager.getCategoryManager().getCategory(items.get(0).getCategory());
138
        if(category.getParent_category_id() != Utils.MOBILE_ACCESSORIES_CATEGORY && category.getId() != Utils.LAPTOPS_CATEGORY) {
2488 rajveer 139
			filenames.add("PhonesIOwnSnippet");
2367 rajveer 140
			filenames.add("CompareProductSnippet");
141
			filenames.add("ComparisonSnippet");
142
			filenames.add("CompareProductSummarySnippet");
143
			filenames.add("SlideNamesSnippet");
2651 rajveer 144
			filenames.add("RelatedAccessories");
2743 rajveer 145
			filenames.add("MostComparedProducts");
2367 rajveer 146
		}
147
		getHtmlFromVelocity(filenames,exportPath,context,catalogId);
3560 rajveer 148
		return minPriceItem.getSellingPrice();
2367 rajveer 149
	}
150
 
151
	public void getHtmlFromVelocity(List<String> filenames, String exportPath, VelocityContext context, long catalogId){
152
		try {
153
			Properties p = new Properties();
154
			p.setProperty("resource.loader", "file");
155
			p.setProperty("file.resource.loader.class","org.apache.velocity.runtime.resource.loader.FileResourceLoader");
156
			p.setProperty( "file.resource.loader.path", Utils.EXPORT_VELOCITY_PATH);
157
			Velocity.init(p);
158
			for(String filename: filenames){
159
				Template template = Velocity.getTemplate(catalogId + File.separator + filename + ".vm");
3575 rajveer 160
				BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportPath + File.separator +filename+".html")));
2367 rajveer 161
				template.merge(context, writer);
162
				writer.flush();
163
				writer.close();	
164
			}
165
		}catch (ResourceNotFoundException e) {
166
			// TODO Auto-generated catch block
167
			e.printStackTrace();
168
		} catch (IOException e) {
169
			// TODO Auto-generated catch block
170
			e.printStackTrace();
171
		} catch (ParseErrorException e) {
172
			// TODO Auto-generated catch block
173
			e.printStackTrace();
174
		} catch (Exception e) {
175
			// TODO Auto-generated catch block
176
			e.printStackTrace();
177
		}
178
	}
179
}
180