Subversion Repositories SmartDukaan

Rev

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