Subversion Repositories SmartDukaan

Rev

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