Subversion Repositories SmartDukaan

Rev

Rev 4025 | Rev 4058 | 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
 
4025 rajveer 60
	/**
61
	 * 
62
	 * 
63
	 * @param items
64
	 * @param catalogId
65
	 * @param domain
66
	 * @param exportPath
67
	 * @param source
68
	 * @return
69
	 */
3560 rajveer 70
	public double insertPriceInHtml(List<Item> items, long catalogId, String domain, String exportPath, Source source){
4025 rajveer 71
		List<Map<String, String>> itemDetails = new ArrayList<Map<String,String>>();
72
		String offerText = null;
2367 rajveer 73
		Item minPriceItem = null;
74
		for(Item item: items){
75
			Map<String, String> itemDetail = new HashMap<String, String>();
76
			boolean showmrp = true;
3560 rajveer 77
			double sellingPrice = item.getSellingPrice();
78
			double mrp = item.getMrp();
79
 
80
			if(source != null){
81
				try {
82
					SourceItemPricing sip = client.getItemPricingBySource(item.getId(), source.getId());
83
					sellingPrice = sip.getSellingPrice();
84
					mrp = sip.getMrp();
85
				} catch (TException e) {
3575 rajveer 86
 
3560 rajveer 87
				} catch (InventoryServiceException e) {
3575 rajveer 88
 
3560 rajveer 89
				}
90
			}
91
 
92
			if (mrp <= sellingPrice) {
2367 rajveer 93
				showmrp = false;
94
			}
95
			if(minPriceItem == null || minPriceItem.getSellingPrice() > item.getSellingPrice()){
96
				minPriceItem = item;
97
			}
3560 rajveer 98
 
2367 rajveer 99
			double saving = Math.round((mrp-sellingPrice)/mrp*100);
100
			itemDetail.put("ITEM_ID", ((int)item.getId())+"");	
3560 rajveer 101
			itemDetail.put("SP", ((int)sellingPrice)+"");
102
			itemDetail.put("MRP", ((int)mrp)+"");
2367 rajveer 103
			itemDetail.put("SAVING", ((int)saving)+"");
104
			itemDetail.put("COLOR", item.getColor());
105
			itemDetail.put("CATALOG_ID", ((int)item.getCatalogItemId())+"");
106
			itemDetail.put("SHOWMRP", showmrp +"");
107
			itemDetail.put("IS_SELECTED", item.isDefaultForEntity() ? "SELECTED" : "");
108
			itemDetails.add(itemDetail);
4025 rajveer 109
 
110
			String bestDealsText = item.getBestDealText();
111
			if( bestDealsText != null && bestDealsText.length() > 0 && offerText == null){
112
				offerText = bestDealsText;
113
			}
2367 rajveer 114
		}
3575 rajveer 115
 
116
 
117
 
3576 rajveer 118
 
119
		if(source == null){
120
			exportPath = exportPath + catalogId;
121
		}else{
3575 rajveer 122
			exportPath = exportPath + catalogId + File.separator + source.getId();
3560 rajveer 123
		}
2367 rajveer 124
 
3575 rajveer 125
		File exportDir = new File(exportPath);
126
 
2367 rajveer 127
		if(!exportDir.exists()) {
128
			exportDir.mkdir();
129
		}
130
 
131
		// This URL is used to ensure that images such as icon and thumbnail for
132
		// a particular entity are always downloaded from the same location.
133
		String staticurl = "http://static" + catalogId%3 + "." + domain;
134
 
135
 
136
		VelocityContext context = new VelocityContext();
137
		context.put("itemDetails", itemDetails);
138
		context.put("minPriceItem", minPriceItem);
139
		context.put("domain", domain);
140
		context.put("staticurl", staticurl);
4025 rajveer 141
		context.put("OFFER_TEXT", offerText);
142
 
2367 rajveer 143
		List<String> filenames = new ArrayList<String>();
144
		filenames.add("ProductDetail");
145
		filenames.add("WidgetSnippet");
146
		filenames.add("HomeSnippet");
147
		filenames.add("SearchSnippet");
148
		filenames.add("CategorySnippet");
149
		filenames.add("SlideGuide");
150
		filenames.add("ProductPropertiesSnippet");
2716 varun.gupt 151
		filenames.add("MyResearchSnippet");
3018 rajveer 152
		filenames.add("AfterSales");
2716 varun.gupt 153
 
3719 mandeep.dh 154
		Category category = CategoryManager.getCategoryManager().getCategory(items.get(0).getCategory());
4036 varun.gupt 155
        if(category.getParent_category_id() != Utils.MOBILE_ACCESSORIES_CATEGORY) {
156
 
157
        	if(category.getId() != Utils.LAPTOPS_CATEGORY)	{
158
        		filenames.add("PhonesIOwnSnippet");
159
    			filenames.add("RelatedAccessories");
160
    			filenames.add("MostComparedProducts");
161
        	}
2367 rajveer 162
			filenames.add("CompareProductSnippet");
163
			filenames.add("ComparisonSnippet");
164
			filenames.add("CompareProductSummarySnippet");
165
			filenames.add("SlideNamesSnippet");
166
		}
167
		getHtmlFromVelocity(filenames,exportPath,context,catalogId);
3560 rajveer 168
		return minPriceItem.getSellingPrice();
2367 rajveer 169
	}
170
 
171
	public void getHtmlFromVelocity(List<String> filenames, String exportPath, VelocityContext context, long catalogId){
172
		try {
173
			Properties p = new Properties();
174
			p.setProperty("resource.loader", "file");
175
			p.setProperty("file.resource.loader.class","org.apache.velocity.runtime.resource.loader.FileResourceLoader");
176
			p.setProperty( "file.resource.loader.path", Utils.EXPORT_VELOCITY_PATH);
177
			Velocity.init(p);
178
			for(String filename: filenames){
179
				Template template = Velocity.getTemplate(catalogId + File.separator + filename + ".vm");
3575 rajveer 180
				BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportPath + File.separator +filename+".html")));
2367 rajveer 181
				template.merge(context, writer);
182
				writer.flush();
183
				writer.close();	
184
			}
185
		}catch (ResourceNotFoundException e) {
186
			// TODO Auto-generated catch block
187
			e.printStackTrace();
188
		} catch (IOException e) {
189
			// TODO Auto-generated catch block
190
			e.printStackTrace();
191
		} catch (ParseErrorException e) {
192
			// TODO Auto-generated catch block
193
			e.printStackTrace();
194
		} catch (Exception e) {
195
			// TODO Auto-generated catch block
196
			e.printStackTrace();
197
		}
198
	}
199
}
200