Subversion Repositories SmartDukaan

Rev

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