Subversion Repositories SmartDukaan

Rev

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