Subversion Repositories SmartDukaan

Rev

Rev 7814 | Rev 7860 | 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
 
4805 rajveer 3
import in.shop2020.metamodel.definitions.Catalog;
4802 amit.gupta 4
import in.shop2020.metamodel.definitions.Category;
5945 mandeep.dh 5
import in.shop2020.model.v1.catalog.CatalogService.Client;
6
import in.shop2020.model.v1.catalog.CatalogServiceException;
2367 rajveer 7
import in.shop2020.model.v1.catalog.Item;
3560 rajveer 8
import in.shop2020.model.v1.catalog.Source;
9
import in.shop2020.model.v1.catalog.SourceItemPricing;
5227 amit.gupta 10
import in.shop2020.model.v1.catalog.status;
7853 amit.gupta 11
import in.shop2020.model.v1.order.EmiScheme;
3560 rajveer 12
import in.shop2020.thrift.clients.CatalogClient;
7853 amit.gupta 13
import in.shop2020.thrift.clients.TransactionClient;
2367 rajveer 14
import in.shop2020.util.Utils;
15
 
16
import java.io.BufferedWriter;
17
import java.io.File;
18
import java.io.FileOutputStream;
19
import java.io.IOException;
20
import java.io.OutputStreamWriter;
21
import java.util.ArrayList;
22
import java.util.HashMap;
23
import java.util.List;
7853 amit.gupta 24
import java.util.Locale;
2367 rajveer 25
import java.util.Map;
26
import java.util.Properties;
7853 amit.gupta 27
import java.util.TreeMap;
2367 rajveer 28
 
29
import org.apache.commons.io.FileUtils;
3560 rajveer 30
import org.apache.thrift.TException;
31
import org.apache.thrift.transport.TTransportException;
2367 rajveer 32
import org.apache.velocity.Template;
33
import org.apache.velocity.VelocityContext;
34
import org.apache.velocity.app.Velocity;
35
import org.apache.velocity.exception.ParseErrorException;
36
import org.apache.velocity.exception.ResourceNotFoundException;
37
 
38
 
39
/**
40
 * utility class to generated all html stuff from java objects
41
 * Driver class to merge Java data objects with VTL script. 
42
 * 
43
 * @author rajveer
44
 *
45
 */
46
public class PriceInsertor {
7853 amit.gupta 47
	static Map<Long, EmiScheme> EMI_SCHEMES = new TreeMap<Long, EmiScheme>();
48
 
49
	static {
50
		try {
51
			TransactionClient transactionClient = new TransactionClient();
52
	        in.shop2020.model.v1.order.TransactionService.Client trxClient = transactionClient.getClient();
53
	        List<EmiScheme> emiSchemes = trxClient.getAvailableEmiSchemes();
54
	        for (EmiScheme emiScheme : emiSchemes){
55
	        	if(!EMI_SCHEMES.containsKey(emiScheme.getMinAmount())) {
56
	        		EMI_SCHEMES.put(emiScheme.getMinAmount(), emiScheme);
57
	        	} else {
58
	        		EmiScheme emiSchemeOld = EMI_SCHEMES.get(emiScheme.getMinAmount());
59
	        		if(Double.compare((1 + (emiSchemeOld.getChargeValue()/100))/emiSchemeOld.getTenure(), (1 + (emiScheme.getChargeValue()/100))/emiScheme.getTenure()) > 0){
60
	        			EMI_SCHEMES.put(emiScheme.getMinAmount(), emiScheme);
61
	        		}
62
	        	}
63
	        }
64
		} catch (Exception e) {
65
			e.printStackTrace();
66
		}
67
	}
3560 rajveer 68
    CatalogClient csc;
69
    Client client;
2367 rajveer 70
 
3560 rajveer 71
	public PriceInsertor() throws TTransportException{
72
        csc = new CatalogClient();
73
        client = csc.getClient();
74
	}
75
 
4058 rajveer 76
	public void copySolrSchemaFiles() throws IOException{
77
		String source = Utils.EXPORT_PATH + "xml/final/irmetadata_solrschema.xml";
78
		String destination = Utils.EXPORT_PATH + "solr/irmetadata_solrschema.xml";
79
		FileUtils.copyFile(new File(source), new File(destination));
80
 
81
		source = Utils.EXPORT_PATH + "xml/final/irmetadata_catchall.xml";
82
		destination = Utils.EXPORT_PATH + "solr/irmetadata_catchall.xml";
83
		FileUtils.copyFile(new File(source), new File(destination));
84
	}
85
 
6602 amit.gupta 86
	public void insertPriceInSolrData(long catalogId, String priceString, String availabilityString) throws IOException{
2367 rajveer 87
		String irSolrFilename = Utils.EXPORT_PATH + "xml/final/" + catalogId + "_irdata_solr.xml";
88
		String finalFile = Utils.EXPORT_PATH + "solr/" + catalogId + "_irdata_solr.xml";
89
		File f = new File(irSolrFilename);
90
		String s = FileUtils.readFileToString(f);
3560 rajveer 91
		s = s.replaceAll("<field name=\"F_50002\">.*</field>", priceString);
6602 amit.gupta 92
		s = s.replaceAll("<field name=\"F_50028\">.*</field>", availabilityString);
2367 rajveer 93
		File f1 = new File(finalFile);
94
		FileUtils.writeStringToFile(f1, s);
95
	}
96
 
4025 rajveer 97
	/**
98
	 * 
99
	 * 
100
	 * @param items
101
	 * @param catalogId
102
	 * @param domain
103
	 * @param exportPath
104
	 * @param source
105
	 * @return
106
	 */
3560 rajveer 107
	public double insertPriceInHtml(List<Item> items, long catalogId, String domain, String exportPath, Source source){
4025 rajveer 108
		List<Map<String, String>> itemDetails = new ArrayList<Map<String,String>>();
109
		String offerText = null;
6792 vikram.rag 110
		String offerDetailText = null;
111
		String offerDetailLink = null;
5233 amit.gupta 112
		String afterArrival = null;
6241 amit.gupta 113
		String showPrice = "TRUE";
2367 rajveer 114
		Item minPriceItem = null;
115
		for(Item item: items){
116
			Map<String, String> itemDetail = new HashMap<String, String>();
117
			boolean showmrp = true;
3560 rajveer 118
			double sellingPrice = item.getSellingPrice();
119
			double mrp = item.getMrp();
120
 
121
			if(source != null){
122
				try {
123
					SourceItemPricing sip = client.getItemPricingBySource(item.getId(), source.getId());
124
					sellingPrice = sip.getSellingPrice();
125
					mrp = sip.getMrp();
126
				} catch (TException e) {
3575 rajveer 127
 
5945 mandeep.dh 128
				} catch (CatalogServiceException e) {
3575 rajveer 129
 
3560 rajveer 130
				}
131
			}
132
 
133
			if (mrp <= sellingPrice) {
2367 rajveer 134
				showmrp = false;
135
			}
136
			if(minPriceItem == null || minPriceItem.getSellingPrice() > item.getSellingPrice()){
137
				minPriceItem = item;
138
			}
3560 rajveer 139
 
2367 rajveer 140
			double saving = Math.round((mrp-sellingPrice)/mrp*100);
141
			itemDetail.put("ITEM_ID", ((int)item.getId())+"");	
3560 rajveer 142
			itemDetail.put("SP", ((int)sellingPrice)+"");
143
			itemDetail.put("MRP", ((int)mrp)+"");
2367 rajveer 144
			itemDetail.put("SAVING", ((int)saving)+"");
145
			itemDetail.put("COLOR", item.getColor());
146
			itemDetail.put("CATALOG_ID", ((int)item.getCatalogItemId())+"");
147
			itemDetail.put("SHOWMRP", showmrp +"");
148
			itemDetail.put("IS_SELECTED", item.isDefaultForEntity() ? "SELECTED" : "");
149
			itemDetails.add(itemDetail);
4025 rajveer 150
 
5233 amit.gupta 151
			if(item.getItemStatus().equals(status.COMING_SOON)){
152
				afterArrival = "after arrival";
6241 amit.gupta 153
				if(!item.isShowSellingPrice()){
154
					showPrice = "FALSE";
155
				}
5233 amit.gupta 156
			}
4025 rajveer 157
			String bestDealsText = item.getBestDealText();
5227 amit.gupta 158
			if( bestDealsText != null && bestDealsText.length() > 0 && offerText == null && status.ACTIVE.equals(item.getItemStatus())){
4025 rajveer 159
				offerText = bestDealsText;
6792 vikram.rag 160
				String bestDealsDetailsText = item.getBestDealsDetailsText();
161
				if( bestDealsDetailsText != null && bestDealsDetailsText.length() > 0){
162
					offerDetailText = bestDealsDetailsText;
163
				}
164
				String bestDealsDetailsLink = item.getBestDealsDetailsLink();
165
				if( bestDealsDetailsLink != null && bestDealsDetailsLink.length() > 0){
166
					offerDetailLink = bestDealsDetailsLink;
167
				}
4025 rajveer 168
			}
2367 rajveer 169
		}
3575 rajveer 170
 
171
 
172
 
3576 rajveer 173
 
174
		if(source == null){
175
			exportPath = exportPath + catalogId;
176
		}else{
3575 rajveer 177
			exportPath = exportPath + catalogId + File.separator + source.getId();
3560 rajveer 178
		}
2367 rajveer 179
 
3575 rajveer 180
		File exportDir = new File(exportPath);
181
 
2367 rajveer 182
		if(!exportDir.exists()) {
183
			exportDir.mkdir();
184
		}
185
 
186
		// This URL is used to ensure that images such as icon and thumbnail for
187
		// a particular entity are always downloaded from the same location.
7336 amit.gupta 188
		String staticurl = "http://static" + catalogId%3 + "." + (domain.contains("store") ? "saholic.com":domain);
2367 rajveer 189
 
190
 
191
		VelocityContext context = new VelocityContext();
7336 amit.gupta 192
		context.put("domain", domain.contains("store") ? "saholic.com":domain);
2367 rajveer 193
		context.put("itemDetails", itemDetails);
194
		context.put("minPriceItem", minPriceItem);
195
		context.put("staticurl", staticurl);
4025 rajveer 196
		context.put("OFFER_TEXT", offerText);
6792 vikram.rag 197
		context.put("OFFER_DETAIL_TEXT", offerDetailText);
198
		context.put("OFFER_DETAIL_LINK", offerDetailLink);
5233 amit.gupta 199
		context.put("AFTER_ARRIVAL", afterArrival);
7336 amit.gupta 200
		context.put("SHOW_PRICE", domain.contains("store")?"FALSE":showPrice);
7317 amit.gupta 201
		context.put("IS_STORE", domain.contains("store")?"TRUE":"FALSE");
7853 amit.gupta 202
		context.put("EMI", getEMI(items.get(0).getSellingPrice()));
2367 rajveer 203
		List<String> filenames = new ArrayList<String>();
7814 amit.gupta 204
		if(!domain.contains("store")){		
205
			filenames.add("WidgetSnippet");
206
			filenames.add("ProductPropertiesSnippet");
207
			filenames.add("MyResearchSnippet");
208
			filenames.add("AfterSales");
209
			filenames.add("SearchSnippet");
210
			filenames.add("CategorySnippet");
211
			filenames.add("HomeSnippet");
212
			filenames.add("ProductDetail");
213
			filenames.add("SlideGuide");
214
		}else {
215
			filenames.add("store/" + catalogId + "/SearchSnippet");
216
			filenames.add("store/" + catalogId + "/CategorySnippet");
217
			filenames.add("store/" + catalogId + "/HomeSnippet");
218
			filenames.add("store/" + catalogId + "/ProductDetail");
219
			filenames.add("store/" + catalogId + "/SlideGuide");
220
		}
2716 varun.gupt 221
 
4805 rajveer 222
		Category category = Catalog.getInstance().getDefinitionsContainer().getCategory(items.get(0).getCategory());
7814 amit.gupta 223
		if(category!=null && !domain.contains("store")){
4802 amit.gupta 224
			Category parentCategory = category.getParentCategory();
225
 
226
			if(parentCategory.getID() == Utils.MOBILE_PHONES_CATAGORY)	{
227
				filenames.add("PhonesIOwnSnippet");
228
			}
229
			if(parentCategory.isHasAccessories()){
230
				filenames.add("RelatedAccessories");
231
			}
5930 amit.gupta 232
			filenames.add("CompareProductSnippet");
233
			filenames.add("SlideNamesSnippet");
234
			filenames.add("ComparisonSnippet");
235
			if(category.isComparable()) {
4802 amit.gupta 236
				filenames.add("MostComparedProducts");
4752 rajveer 237
				filenames.add("CompareProductSummarySnippet");
5552 phani.kuma 238
				filenames.add("MostComparedSnippet");
4752 rajveer 239
			}
2367 rajveer 240
		}
241
		getHtmlFromVelocity(filenames,exportPath,context,catalogId);
3560 rajveer 242
		return minPriceItem.getSellingPrice();
2367 rajveer 243
	}
244
 
7853 amit.gupta 245
	private static String getEMI(double sellingPrice) {
246
		String returnString = null;
247
		EmiScheme finalEmi = null;
248
		for (EmiScheme emiScheme : EMI_SCHEMES.values() ){
249
			if(emiScheme.getMinAmount() <= sellingPrice) {
250
				if(finalEmi == null){
251
					finalEmi = emiScheme; 
252
				} else if(Double.compare((1 + (finalEmi.getChargeValue()/100))/finalEmi.getTenure(), (1 + (emiScheme.getChargeValue()/100))/emiScheme.getTenure()) > 0){
253
					finalEmi = emiScheme;
254
				}
255
			}
256
		}
257
		if(finalEmi != null) {
258
			returnString = String.format(Locale.getDefault(),"%.0f",sellingPrice * (1 + (finalEmi.getChargeValue()/100))/finalEmi.getTenure());
259
		}
260
		return returnString;
261
	}
262
 
2367 rajveer 263
	public void getHtmlFromVelocity(List<String> filenames, String exportPath, VelocityContext context, long catalogId){
264
		try {
265
			Properties p = new Properties();
266
			p.setProperty("resource.loader", "file");
267
			p.setProperty("file.resource.loader.class","org.apache.velocity.runtime.resource.loader.FileResourceLoader");
268
			p.setProperty( "file.resource.loader.path", Utils.EXPORT_VELOCITY_PATH);
269
			Velocity.init(p);
270
			for(String filename: filenames){
7814 amit.gupta 271
				if(filename.contains("store/")){
272
					Template template = Velocity.getTemplate(filename + ".vm");
273
					BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportPath + File.separator + filename.split(""+ catalogId +"/")[1] +".html")));
274
					template.merge(context, writer);
275
					writer.flush();
276
					writer.close();
277
 
278
				} else {
279
					Template template = Velocity.getTemplate(catalogId + File.separator + filename + ".vm");
280
					BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportPath + File.separator + filename +".html")));
281
					template.merge(context, writer);
282
					writer.flush();
283
					writer.close();
284
				}
2367 rajveer 285
			}
286
		}catch (ResourceNotFoundException e) {
287
			// TODO Auto-generated catch block
288
			e.printStackTrace();
289
		} catch (IOException e) {
290
			// TODO Auto-generated catch block
291
			e.printStackTrace();
292
		} catch (ParseErrorException e) {
293
			// TODO Auto-generated catch block
294
			e.printStackTrace();
295
		} catch (Exception e) {
296
			// TODO Auto-generated catch block
297
			e.printStackTrace();
298
		}
299
	}
300
}
301