Subversion Repositories SmartDukaan

Rev

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