Subversion Repositories SmartDukaan

Rev

Rev 3575 | Rev 3719 | 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) {
3575 rajveer 74
 
3560 rajveer 75
				} catch (InventoryServiceException e) {
3575 rajveer 76
 
3560 rajveer 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
		}
3575 rajveer 98
 
99
 
100
 
3576 rajveer 101
 
102
		if(source == null){
103
			exportPath = exportPath + catalogId;
104
		}else{
3575 rajveer 105
			exportPath = exportPath + catalogId + File.separator + source.getId();
3560 rajveer 106
		}
2367 rajveer 107
 
3575 rajveer 108
		File exportDir = new File(exportPath);
109
 
2367 rajveer 110
		if(!exportDir.exists()) {
111
			exportDir.mkdir();
112
		}
113
 
114
		// This URL is used to ensure that images such as icon and thumbnail for
115
		// a particular entity are always downloaded from the same location.
116
		String staticurl = "http://static" + catalogId%3 + "." + domain;
117
 
118
 
119
		VelocityContext context = new VelocityContext();
120
		context.put("itemDetails", itemDetails);
121
		context.put("minPriceItem", minPriceItem);
122
		context.put("domain", domain);
123
		context.put("staticurl", staticurl);
124
 
125
		List<String> filenames = new ArrayList<String>();
126
		filenames.add("ProductDetail");
127
		filenames.add("WidgetSnippet");
128
		filenames.add("HomeSnippet");
129
		filenames.add("SearchSnippet");
130
		filenames.add("CategorySnippet");
131
		filenames.add("SlideGuide");
132
		filenames.add("ProductPropertiesSnippet");
2716 varun.gupt 133
		filenames.add("MyResearchSnippet");
3018 rajveer 134
		filenames.add("AfterSales");
2716 varun.gupt 135
 
2547 rajveer 136
		if(CategoryManager.getCategoryManager().getCategory(items.get(0).getCategory()).getParent_category_id() != Utils.MOBILE_ACCESSORIES_CATEGORY){
2488 rajveer 137
			filenames.add("PhonesIOwnSnippet");
2367 rajveer 138
			filenames.add("CompareProductSnippet");
139
			filenames.add("ComparisonSnippet");
140
			filenames.add("CompareProductSummarySnippet");
141
			filenames.add("SlideNamesSnippet");
2651 rajveer 142
			filenames.add("RelatedAccessories");
2743 rajveer 143
			filenames.add("MostComparedProducts");
2367 rajveer 144
		}
145
		getHtmlFromVelocity(filenames,exportPath,context,catalogId);
3560 rajveer 146
		return minPriceItem.getSellingPrice();
2367 rajveer 147
	}
148
 
149
	public void getHtmlFromVelocity(List<String> filenames, String exportPath, VelocityContext context, long catalogId){
150
		try {
151
			Properties p = new Properties();
152
			p.setProperty("resource.loader", "file");
153
			p.setProperty("file.resource.loader.class","org.apache.velocity.runtime.resource.loader.FileResourceLoader");
154
			p.setProperty( "file.resource.loader.path", Utils.EXPORT_VELOCITY_PATH);
155
			Velocity.init(p);
156
			for(String filename: filenames){
157
				Template template = Velocity.getTemplate(catalogId + File.separator + filename + ".vm");
3575 rajveer 158
				BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportPath + File.separator +filename+".html")));
2367 rajveer 159
				template.merge(context, writer);
160
				writer.flush();
161
				writer.close();	
162
			}
163
		}catch (ResourceNotFoundException e) {
164
			// TODO Auto-generated catch block
165
			e.printStackTrace();
166
		} catch (IOException e) {
167
			// TODO Auto-generated catch block
168
			e.printStackTrace();
169
		} catch (ParseErrorException e) {
170
			// TODO Auto-generated catch block
171
			e.printStackTrace();
172
		} catch (Exception e) {
173
			// TODO Auto-generated catch block
174
			e.printStackTrace();
175
		}
176
	}
177
}
178