Subversion Repositories SmartDukaan

Rev

Rev 7317 | Rev 7814 | 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
 
6602 amit.gupta 61
	public void insertPriceInSolrData(long catalogId, String priceString, String availabilityString) 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);
6602 amit.gupta 67
		s = s.replaceAll("<field name=\"F_50028\">.*</field>", availabilityString);
2367 rajveer 68
		File f1 = new File(finalFile);
69
		FileUtils.writeStringToFile(f1, s);
70
	}
71
 
4025 rajveer 72
	/**
73
	 * 
74
	 * 
75
	 * @param items
76
	 * @param catalogId
77
	 * @param domain
78
	 * @param exportPath
79
	 * @param source
80
	 * @return
81
	 */
3560 rajveer 82
	public double insertPriceInHtml(List<Item> items, long catalogId, String domain, String exportPath, Source source){
4025 rajveer 83
		List<Map<String, String>> itemDetails = new ArrayList<Map<String,String>>();
84
		String offerText = null;
6792 vikram.rag 85
		String offerDetailText = null;
86
		String offerDetailLink = null;
5233 amit.gupta 87
		String afterArrival = null;
6241 amit.gupta 88
		String showPrice = "TRUE";
2367 rajveer 89
		Item minPriceItem = null;
90
		for(Item item: items){
91
			Map<String, String> itemDetail = new HashMap<String, String>();
92
			boolean showmrp = true;
3560 rajveer 93
			double sellingPrice = item.getSellingPrice();
94
			double mrp = item.getMrp();
95
 
96
			if(source != null){
97
				try {
98
					SourceItemPricing sip = client.getItemPricingBySource(item.getId(), source.getId());
99
					sellingPrice = sip.getSellingPrice();
100
					mrp = sip.getMrp();
101
				} catch (TException e) {
3575 rajveer 102
 
5945 mandeep.dh 103
				} catch (CatalogServiceException e) {
3575 rajveer 104
 
3560 rajveer 105
				}
106
			}
107
 
108
			if (mrp <= sellingPrice) {
2367 rajveer 109
				showmrp = false;
110
			}
111
			if(minPriceItem == null || minPriceItem.getSellingPrice() > item.getSellingPrice()){
112
				minPriceItem = item;
113
			}
3560 rajveer 114
 
2367 rajveer 115
			double saving = Math.round((mrp-sellingPrice)/mrp*100);
116
			itemDetail.put("ITEM_ID", ((int)item.getId())+"");	
3560 rajveer 117
			itemDetail.put("SP", ((int)sellingPrice)+"");
118
			itemDetail.put("MRP", ((int)mrp)+"");
2367 rajveer 119
			itemDetail.put("SAVING", ((int)saving)+"");
120
			itemDetail.put("COLOR", item.getColor());
121
			itemDetail.put("CATALOG_ID", ((int)item.getCatalogItemId())+"");
122
			itemDetail.put("SHOWMRP", showmrp +"");
123
			itemDetail.put("IS_SELECTED", item.isDefaultForEntity() ? "SELECTED" : "");
124
			itemDetails.add(itemDetail);
4025 rajveer 125
 
5233 amit.gupta 126
			if(item.getItemStatus().equals(status.COMING_SOON)){
127
				afterArrival = "after arrival";
6241 amit.gupta 128
				if(!item.isShowSellingPrice()){
129
					showPrice = "FALSE";
130
				}
5233 amit.gupta 131
			}
4025 rajveer 132
			String bestDealsText = item.getBestDealText();
5227 amit.gupta 133
			if( bestDealsText != null && bestDealsText.length() > 0 && offerText == null && status.ACTIVE.equals(item.getItemStatus())){
4025 rajveer 134
				offerText = bestDealsText;
6792 vikram.rag 135
				String bestDealsDetailsText = item.getBestDealsDetailsText();
136
				if( bestDealsDetailsText != null && bestDealsDetailsText.length() > 0){
137
					offerDetailText = bestDealsDetailsText;
138
				}
139
				String bestDealsDetailsLink = item.getBestDealsDetailsLink();
140
				if( bestDealsDetailsLink != null && bestDealsDetailsLink.length() > 0){
141
					offerDetailLink = bestDealsDetailsLink;
142
				}
4025 rajveer 143
			}
2367 rajveer 144
		}
3575 rajveer 145
 
146
 
147
 
3576 rajveer 148
 
149
		if(source == null){
150
			exportPath = exportPath + catalogId;
151
		}else{
3575 rajveer 152
			exportPath = exportPath + catalogId + File.separator + source.getId();
3560 rajveer 153
		}
2367 rajveer 154
 
3575 rajveer 155
		File exportDir = new File(exportPath);
156
 
2367 rajveer 157
		if(!exportDir.exists()) {
158
			exportDir.mkdir();
159
		}
160
 
161
		// This URL is used to ensure that images such as icon and thumbnail for
162
		// a particular entity are always downloaded from the same location.
7336 amit.gupta 163
		String staticurl = "http://static" + catalogId%3 + "." + (domain.contains("store") ? "saholic.com":domain);
2367 rajveer 164
 
165
 
166
		VelocityContext context = new VelocityContext();
7336 amit.gupta 167
		context.put("domain", domain.contains("store") ? "saholic.com":domain);
2367 rajveer 168
		context.put("itemDetails", itemDetails);
169
		context.put("minPriceItem", minPriceItem);
170
		context.put("staticurl", staticurl);
4025 rajveer 171
		context.put("OFFER_TEXT", offerText);
6792 vikram.rag 172
		context.put("OFFER_DETAIL_TEXT", offerDetailText);
173
		context.put("OFFER_DETAIL_LINK", offerDetailLink);
5233 amit.gupta 174
		context.put("AFTER_ARRIVAL", afterArrival);
7336 amit.gupta 175
		context.put("SHOW_PRICE", domain.contains("store")?"FALSE":showPrice);
7317 amit.gupta 176
		context.put("IS_STORE", domain.contains("store")?"TRUE":"FALSE");
2367 rajveer 177
		List<String> filenames = new ArrayList<String>();
178
		filenames.add("ProductDetail");
179
		filenames.add("WidgetSnippet");
180
		filenames.add("HomeSnippet");
181
		filenames.add("SearchSnippet");
182
		filenames.add("CategorySnippet");
183
		filenames.add("SlideGuide");
184
		filenames.add("ProductPropertiesSnippet");
2716 varun.gupt 185
		filenames.add("MyResearchSnippet");
3018 rajveer 186
		filenames.add("AfterSales");
2716 varun.gupt 187
 
4805 rajveer 188
		Category category = Catalog.getInstance().getDefinitionsContainer().getCategory(items.get(0).getCategory());
4752 rajveer 189
		if(category!=null){
4802 amit.gupta 190
			Category parentCategory = category.getParentCategory();
191
 
192
			if(parentCategory.getID() == Utils.MOBILE_PHONES_CATAGORY)	{
193
				filenames.add("PhonesIOwnSnippet");
194
			}
195
			if(parentCategory.isHasAccessories()){
196
				filenames.add("RelatedAccessories");
197
			}
5930 amit.gupta 198
			filenames.add("CompareProductSnippet");
199
			filenames.add("SlideNamesSnippet");
200
			filenames.add("ComparisonSnippet");
201
			if(category.isComparable()) {
4802 amit.gupta 202
				filenames.add("MostComparedProducts");
4752 rajveer 203
				filenames.add("CompareProductSummarySnippet");
5552 phani.kuma 204
				filenames.add("MostComparedSnippet");
4752 rajveer 205
			}
2367 rajveer 206
		}
207
		getHtmlFromVelocity(filenames,exportPath,context,catalogId);
3560 rajveer 208
		return minPriceItem.getSellingPrice();
2367 rajveer 209
	}
210
 
211
	public void getHtmlFromVelocity(List<String> filenames, String exportPath, VelocityContext context, long catalogId){
212
		try {
213
			Properties p = new Properties();
214
			p.setProperty("resource.loader", "file");
215
			p.setProperty("file.resource.loader.class","org.apache.velocity.runtime.resource.loader.FileResourceLoader");
216
			p.setProperty( "file.resource.loader.path", Utils.EXPORT_VELOCITY_PATH);
217
			Velocity.init(p);
218
			for(String filename: filenames){
219
				Template template = Velocity.getTemplate(catalogId + File.separator + filename + ".vm");
3575 rajveer 220
				BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportPath + File.separator +filename+".html")));
2367 rajveer 221
				template.merge(context, writer);
222
				writer.flush();
223
				writer.close();	
224
			}
225
		}catch (ResourceNotFoundException e) {
226
			// TODO Auto-generated catch block
227
			e.printStackTrace();
228
		} catch (IOException e) {
229
			// TODO Auto-generated catch block
230
			e.printStackTrace();
231
		} catch (ParseErrorException e) {
232
			// TODO Auto-generated catch block
233
			e.printStackTrace();
234
		} catch (Exception e) {
235
			// TODO Auto-generated catch block
236
			e.printStackTrace();
237
		}
238
	}
239
}
240