Subversion Repositories SmartDukaan

Rev

Rev 4805 | Rev 5233 | 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;
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;
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;
2367 rajveer 84
		Item minPriceItem = null;
85
		for(Item item: items){
86
			Map<String, String> itemDetail = new HashMap<String, String>();
87
			boolean showmrp = true;
3560 rajveer 88
			double sellingPrice = item.getSellingPrice();
89
			double mrp = item.getMrp();
90
 
91
			if(source != null){
92
				try {
93
					SourceItemPricing sip = client.getItemPricingBySource(item.getId(), source.getId());
94
					sellingPrice = sip.getSellingPrice();
95
					mrp = sip.getMrp();
96
				} catch (TException e) {
3575 rajveer 97
 
3560 rajveer 98
				} catch (InventoryServiceException e) {
3575 rajveer 99
 
3560 rajveer 100
				}
101
			}
102
 
103
			if (mrp <= sellingPrice) {
2367 rajveer 104
				showmrp = false;
105
			}
106
			if(minPriceItem == null || minPriceItem.getSellingPrice() > item.getSellingPrice()){
107
				minPriceItem = item;
108
			}
3560 rajveer 109
 
2367 rajveer 110
			double saving = Math.round((mrp-sellingPrice)/mrp*100);
111
			itemDetail.put("ITEM_ID", ((int)item.getId())+"");	
3560 rajveer 112
			itemDetail.put("SP", ((int)sellingPrice)+"");
113
			itemDetail.put("MRP", ((int)mrp)+"");
2367 rajveer 114
			itemDetail.put("SAVING", ((int)saving)+"");
115
			itemDetail.put("COLOR", item.getColor());
116
			itemDetail.put("CATALOG_ID", ((int)item.getCatalogItemId())+"");
117
			itemDetail.put("SHOWMRP", showmrp +"");
118
			itemDetail.put("IS_SELECTED", item.isDefaultForEntity() ? "SELECTED" : "");
119
			itemDetails.add(itemDetail);
4025 rajveer 120
 
121
			String bestDealsText = item.getBestDealText();
5227 amit.gupta 122
			if( bestDealsText != null && bestDealsText.length() > 0 && offerText == null && status.ACTIVE.equals(item.getItemStatus())){
4025 rajveer 123
				offerText = bestDealsText;
124
			}
2367 rajveer 125
		}
3575 rajveer 126
 
127
 
128
 
3576 rajveer 129
 
130
		if(source == null){
131
			exportPath = exportPath + catalogId;
132
		}else{
3575 rajveer 133
			exportPath = exportPath + catalogId + File.separator + source.getId();
3560 rajveer 134
		}
2367 rajveer 135
 
3575 rajveer 136
		File exportDir = new File(exportPath);
137
 
2367 rajveer 138
		if(!exportDir.exists()) {
139
			exportDir.mkdir();
140
		}
141
 
142
		// This URL is used to ensure that images such as icon and thumbnail for
143
		// a particular entity are always downloaded from the same location.
144
		String staticurl = "http://static" + catalogId%3 + "." + domain;
145
 
146
 
147
		VelocityContext context = new VelocityContext();
148
		context.put("itemDetails", itemDetails);
149
		context.put("minPriceItem", minPriceItem);
150
		context.put("domain", domain);
151
		context.put("staticurl", staticurl);
4025 rajveer 152
		context.put("OFFER_TEXT", offerText);
153
 
2367 rajveer 154
		List<String> filenames = new ArrayList<String>();
155
		filenames.add("ProductDetail");
156
		filenames.add("WidgetSnippet");
157
		filenames.add("HomeSnippet");
158
		filenames.add("SearchSnippet");
159
		filenames.add("CategorySnippet");
160
		filenames.add("SlideGuide");
161
		filenames.add("ProductPropertiesSnippet");
2716 varun.gupt 162
		filenames.add("MyResearchSnippet");
3018 rajveer 163
		filenames.add("AfterSales");
2716 varun.gupt 164
 
4805 rajveer 165
		Category category = Catalog.getInstance().getDefinitionsContainer().getCategory(items.get(0).getCategory());
4752 rajveer 166
		if(category!=null){
4802 amit.gupta 167
			Category parentCategory = category.getParentCategory();
168
 
169
			if(parentCategory.getID() == Utils.MOBILE_PHONES_CATAGORY)	{
170
				filenames.add("PhonesIOwnSnippet");
171
			}
172
			if(parentCategory.isHasAccessories()){
173
				filenames.add("RelatedAccessories");
174
			}
175
			if(parentCategory.isComparable()) {
176
				filenames.add("MostComparedProducts");
4752 rajveer 177
				filenames.add("CompareProductSnippet");
178
				filenames.add("ComparisonSnippet");
179
				filenames.add("CompareProductSummarySnippet");
180
				filenames.add("SlideNamesSnippet");
181
			}
2367 rajveer 182
		}
183
		getHtmlFromVelocity(filenames,exportPath,context,catalogId);
3560 rajveer 184
		return minPriceItem.getSellingPrice();
2367 rajveer 185
	}
186
 
187
	public void getHtmlFromVelocity(List<String> filenames, String exportPath, VelocityContext context, long catalogId){
188
		try {
189
			Properties p = new Properties();
190
			p.setProperty("resource.loader", "file");
191
			p.setProperty("file.resource.loader.class","org.apache.velocity.runtime.resource.loader.FileResourceLoader");
192
			p.setProperty( "file.resource.loader.path", Utils.EXPORT_VELOCITY_PATH);
193
			Velocity.init(p);
194
			for(String filename: filenames){
195
				Template template = Velocity.getTemplate(catalogId + File.separator + filename + ".vm");
3575 rajveer 196
				BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(exportPath + File.separator +filename+".html")));
2367 rajveer 197
				template.merge(context, writer);
198
				writer.flush();
199
				writer.close();	
200
			}
201
		}catch (ResourceNotFoundException e) {
202
			// TODO Auto-generated catch block
203
			e.printStackTrace();
204
		} catch (IOException e) {
205
			// TODO Auto-generated catch block
206
			e.printStackTrace();
207
		} catch (ParseErrorException e) {
208
			// TODO Auto-generated catch block
209
			e.printStackTrace();
210
		} catch (Exception e) {
211
			// TODO Auto-generated catch block
212
			e.printStackTrace();
213
		}
214
	}
215
}
216