Subversion Repositories SmartDukaan

Rev

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