Subversion Repositories SmartDukaan

Rev

Rev 5930 | Rev 6241 | 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;
3560 rajveer 11
import in.shop2020.thrift.clients.CatalogClient;
2367 rajveer 12
import in.shop2020.util.Utils;
13
 
14
import java.io.BufferedWriter;
15
import java.io.File;
16
import java.io.FileOutputStream;
17
import java.io.IOException;
18
import java.io.OutputStreamWriter;
19
import java.util.ArrayList;
20
import java.util.HashMap;
21
import java.util.List;
22
import java.util.Map;
23
import java.util.Properties;
24
 
25
import org.apache.commons.io.FileUtils;
3560 rajveer 26
import org.apache.thrift.TException;
27
import org.apache.thrift.transport.TTransportException;
2367 rajveer 28
import org.apache.velocity.Template;
29
import org.apache.velocity.VelocityContext;
30
import org.apache.velocity.app.Velocity;
31
import org.apache.velocity.exception.ParseErrorException;
32
import org.apache.velocity.exception.ResourceNotFoundException;
33
 
34
 
35
/**
36
 * utility class to generated all html stuff from java objects
37
 * Driver class to merge Java data objects with VTL script. 
38
 * 
39
 * @author rajveer
40
 *
41
 */
42
public class PriceInsertor {
3560 rajveer 43
    CatalogClient csc;
44
    Client client;
2367 rajveer 45
 
3560 rajveer 46
	public PriceInsertor() throws TTransportException{
47
        csc = new CatalogClient();
48
        client = csc.getClient();
49
	}
50
 
4058 rajveer 51
	public void copySolrSchemaFiles() throws IOException{
52
		String source = Utils.EXPORT_PATH + "xml/final/irmetadata_solrschema.xml";
53
		String destination = Utils.EXPORT_PATH + "solr/irmetadata_solrschema.xml";
54
		FileUtils.copyFile(new File(source), new File(destination));
55
 
56
		source = Utils.EXPORT_PATH + "xml/final/irmetadata_catchall.xml";
57
		destination = Utils.EXPORT_PATH + "solr/irmetadata_catchall.xml";
58
		FileUtils.copyFile(new File(source), new File(destination));
59
	}
60
 
3560 rajveer 61
	public void insertPriceInSolrData(long catalogId, String priceString) throws IOException{
2367 rajveer 62
		String irSolrFilename = Utils.EXPORT_PATH + "xml/final/" + catalogId + "_irdata_solr.xml";
63
		String finalFile = Utils.EXPORT_PATH + "solr/" + catalogId + "_irdata_solr.xml";
64
		File f = new File(irSolrFilename);
65
		String s = FileUtils.readFileToString(f);
3560 rajveer 66
		s = s.replaceAll("<field name=\"F_50002\">.*</field>", priceString);
2367 rajveer 67
		File f1 = new File(finalFile);
68
		FileUtils.writeStringToFile(f1, s);
69
	}
70
 
4025 rajveer 71
	/**
72
	 * 
73
	 * 
74
	 * @param items
75
	 * @param catalogId
76
	 * @param domain
77
	 * @param exportPath
78
	 * @param source
79
	 * @return
80
	 */
3560 rajveer 81
	public double insertPriceInHtml(List<Item> items, long catalogId, String domain, String exportPath, Source source){
4025 rajveer 82
		List<Map<String, String>> itemDetails = new ArrayList<Map<String,String>>();
83
		String offerText = null;
5233 amit.gupta 84
		String afterArrival = null;
2367 rajveer 85
		Item minPriceItem = null;
86
		for(Item item: items){
87
			Map<String, String> itemDetail = new HashMap<String, String>();
88
			boolean showmrp = true;
3560 rajveer 89
			double sellingPrice = item.getSellingPrice();
90
			double mrp = item.getMrp();
91
 
92
			if(source != null){
93
				try {
94
					SourceItemPricing sip = client.getItemPricingBySource(item.getId(), source.getId());
95
					sellingPrice = sip.getSellingPrice();
96
					mrp = sip.getMrp();
97
				} catch (TException e) {
3575 rajveer 98
 
5945 mandeep.dh 99
				} catch (CatalogServiceException e) {
3575 rajveer 100
 
3560 rajveer 101
				}
102
			}
103
 
104
			if (mrp <= sellingPrice) {
2367 rajveer 105
				showmrp = false;
106
			}
107
			if(minPriceItem == null || minPriceItem.getSellingPrice() > item.getSellingPrice()){
108
				minPriceItem = item;
109
			}
3560 rajveer 110
 
2367 rajveer 111
			double saving = Math.round((mrp-sellingPrice)/mrp*100);
112
			itemDetail.put("ITEM_ID", ((int)item.getId())+"");	
3560 rajveer 113
			itemDetail.put("SP", ((int)sellingPrice)+"");
114
			itemDetail.put("MRP", ((int)mrp)+"");
2367 rajveer 115
			itemDetail.put("SAVING", ((int)saving)+"");
116
			itemDetail.put("COLOR", item.getColor());
117
			itemDetail.put("CATALOG_ID", ((int)item.getCatalogItemId())+"");
118
			itemDetail.put("SHOWMRP", showmrp +"");
119
			itemDetail.put("IS_SELECTED", item.isDefaultForEntity() ? "SELECTED" : "");
120
			itemDetails.add(itemDetail);
4025 rajveer 121
 
5233 amit.gupta 122
			if(item.getItemStatus().equals(status.COMING_SOON)){
123
				afterArrival = "after arrival";
124
			}
4025 rajveer 125
			String bestDealsText = item.getBestDealText();
5227 amit.gupta 126
			if( bestDealsText != null && bestDealsText.length() > 0 && offerText == null && status.ACTIVE.equals(item.getItemStatus())){
4025 rajveer 127
				offerText = bestDealsText;
128
			}
2367 rajveer 129
		}
3575 rajveer 130
 
131
 
132
 
3576 rajveer 133
 
134
		if(source == null){
135
			exportPath = exportPath + catalogId;
136
		}else{
3575 rajveer 137
			exportPath = exportPath + catalogId + File.separator + source.getId();
3560 rajveer 138
		}
2367 rajveer 139
 
3575 rajveer 140
		File exportDir = new File(exportPath);
141
 
2367 rajveer 142
		if(!exportDir.exists()) {
143
			exportDir.mkdir();
144
		}
145
 
146
		// This URL is used to ensure that images such as icon and thumbnail for
147
		// a particular entity are always downloaded from the same location.
148
		String staticurl = "http://static" + catalogId%3 + "." + domain;
149
 
150
 
151
		VelocityContext context = new VelocityContext();
152
		context.put("itemDetails", itemDetails);
153
		context.put("minPriceItem", minPriceItem);
154
		context.put("domain", domain);
155
		context.put("staticurl", staticurl);
4025 rajveer 156
		context.put("OFFER_TEXT", offerText);
5233 amit.gupta 157
		context.put("AFTER_ARRIVAL", afterArrival);
2367 rajveer 158
		List<String> filenames = new ArrayList<String>();
159
		filenames.add("ProductDetail");
160
		filenames.add("WidgetSnippet");
161
		filenames.add("HomeSnippet");
162
		filenames.add("SearchSnippet");
163
		filenames.add("CategorySnippet");
164
		filenames.add("SlideGuide");
165
		filenames.add("ProductPropertiesSnippet");
2716 varun.gupt 166
		filenames.add("MyResearchSnippet");
3018 rajveer 167
		filenames.add("AfterSales");
2716 varun.gupt 168
 
4805 rajveer 169
		Category category = Catalog.getInstance().getDefinitionsContainer().getCategory(items.get(0).getCategory());
4752 rajveer 170
		if(category!=null){
4802 amit.gupta 171
			Category parentCategory = category.getParentCategory();
172
 
173
			if(parentCategory.getID() == Utils.MOBILE_PHONES_CATAGORY)	{
174
				filenames.add("PhonesIOwnSnippet");
175
			}
176
			if(parentCategory.isHasAccessories()){
177
				filenames.add("RelatedAccessories");
178
			}
5930 amit.gupta 179
			filenames.add("CompareProductSnippet");
180
			filenames.add("SlideNamesSnippet");
181
			filenames.add("ComparisonSnippet");
182
			if(category.isComparable()) {
4802 amit.gupta 183
				filenames.add("MostComparedProducts");
4752 rajveer 184
				filenames.add("CompareProductSummarySnippet");
5552 phani.kuma 185
				filenames.add("MostComparedSnippet");
4752 rajveer 186
			}
2367 rajveer 187
		}
188
		getHtmlFromVelocity(filenames,exportPath,context,catalogId);
3560 rajveer 189
		return minPriceItem.getSellingPrice();
2367 rajveer 190
	}
191
 
192
	public void getHtmlFromVelocity(List<String> filenames, String exportPath, VelocityContext context, long catalogId){
193
		try {
194
			Properties p = new Properties();
195
			p.setProperty("resource.loader", "file");
196
			p.setProperty("file.resource.loader.class","org.apache.velocity.runtime.resource.loader.FileResourceLoader");
197
			p.setProperty( "file.resource.loader.path", Utils.EXPORT_VELOCITY_PATH);
198
			Velocity.init(p);
199
			for(String filename: filenames){
200
				Template template = Velocity.getTemplate(catalogId + File.separator + filename + ".vm");
3575 rajveer 201
				BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportPath + File.separator +filename+".html")));
2367 rajveer 202
				template.merge(context, writer);
203
				writer.flush();
204
				writer.close();	
205
			}
206
		}catch (ResourceNotFoundException e) {
207
			// TODO Auto-generated catch block
208
			e.printStackTrace();
209
		} catch (IOException e) {
210
			// TODO Auto-generated catch block
211
			e.printStackTrace();
212
		} catch (ParseErrorException e) {
213
			// TODO Auto-generated catch block
214
			e.printStackTrace();
215
		} catch (Exception e) {
216
			// TODO Auto-generated catch block
217
			e.printStackTrace();
218
		}
219
	}
220
}
221