Subversion Repositories SmartDukaan

Rev

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