Subversion Repositories SmartDukaan

Rev

Rev 19687 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
19685 kshitij.so 1
package in.shop2020.creation.controllers;
2
 
3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.IOException;
6
import java.util.ArrayList;
7
import java.util.Collection;
8
import java.util.HashMap;
9
import java.util.Iterator;
10
import java.util.List;
11
import java.util.Map;
12
 
13
import org.apache.commons.io.FileUtils;
14
import org.apache.juli.logging.Log;
15
import org.apache.juli.logging.LogFactory;
16
import org.apache.poi.hssf.usermodel.HSSFSheet;
17
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
18
import org.apache.struts2.convention.annotation.InterceptorRef;
19
import org.apache.struts2.convention.annotation.InterceptorRefs;
20
import org.apache.thrift.TException;
21
import org.apache.thrift.transport.TTransportException;
22
 
23
import in.shop2020.content.BulkContentUpload;
24
import in.shop2020.content.BulkContentUploadResult;
25
import in.shop2020.content.ContentService.Client;
26
import in.shop2020.content.security.UserManager;
27
import in.shop2020.creation.util.ContentValidator;
28
import in.shop2020.thrift.clients.CatalogClient;
29
import in.shop2020.thrift.clients.ContentClient;
30
import in.shop2020.util.EntityUtils;
31
 
32
import in.shop2020.metamodel.core.Bullet;
33
import in.shop2020.metamodel.core.Entity;
34
import in.shop2020.metamodel.core.EntityState;
35
import in.shop2020.metamodel.core.Feature;
36
import in.shop2020.metamodel.core.PrimitiveDataObject;
37
import in.shop2020.metamodel.core.Slide;
38
import in.shop2020.metamodel.definitions.Catalog;
39
import in.shop2020.metamodel.definitions.DefinitionsContainer;
40
import in.shop2020.metamodel.util.CreationUtils;
41
import in.shop2020.model.v1.catalog.BulkUploadCatalog;
42
import in.shop2020.model.v1.catalog.CatalogServiceException;
43
 
44
@InterceptorRefs({
45
	@InterceptorRef("myDefault"),
46
	@InterceptorRef("login")
47
})
48
 
49
 
50
public class EntityUploadController extends BaseController {
51
 
52
	/**
53
	 * 
54
	 */
55
	private static final long serialVersionUID = 1L;
56
 
57
	private static Log log = LogFactory.getLog(EntityUploadController.class);
58
	@SuppressWarnings("serial")
59
	public static final Map<String, String> CATEGORY_MAP = 
60
		new HashMap<String, String>(){
61
		{
62
			put("1","10018");
63
			put("2","10020");
64
			put("3","10014");
65
		}
66
	};
67
	private DefinitionsContainer defs = Catalog.getInstance().getDefinitionsContainer();
68
	private File upload;
69
	private String category;
70
	private static final int NEW_ENTITY_ID_INDEX = 0;
71
	private static final int BRAND_INDEX = 1;
72
	private static final int MODEL_NAME_INDEX = 2;
73
	private static final int MODEL_NUMBER_INDEX = 3;
74
	private static final int EXISTING_ENTITY_ID_INDEX = 4;
75
	private static final int COMPATIBILITY_INDEX = 5;
76
	private static final int BATTERY_TYPE_INDEX = 6;
77
	private static final int BATTERY_CAPACITY_INDEX = 7;
78
	private List<BulkContentUploadResult> resultList;
19687 kshitij.so 79
	private String changeCatalog="0";
19685 kshitij.so 80
 
81
	public String index(){
82
 
83
		if(!UserManager.getUserManager().canUploadContentSheet(this.getUsername())){
84
			return "deny";
85
		}
86
 
87
		return "entity-upload";
88
	}
89
 
90
	public String create() throws Exception{
91
 
92
		if(!UserManager.getUserManager().canUploadContentSheet(this.getUsername())){
93
			addActionError("You dont have permission to access this resource");
94
			return "deny";
95
		}
96
 
19840 kshitij.so 97
		File fileToCreate = new File("/tmp/", "entity.xls"+String.valueOf(getTimeInMilliseconds()));
19685 kshitij.so 98
		FileUtils.copyFile(this.upload, fileToCreate);
99
		FileInputStream iFile = new FileInputStream(new File(upload.getAbsolutePath()));
100
		HSSFWorkbook workbook = new HSSFWorkbook(iFile);
101
		HSSFSheet sheet = workbook.getSheetAt(0);
102
 
103
		if (changeCatalog.equals("1")){
104
			return updateCatalog(sheet);
105
		}
106
 
107
		if (CATEGORY_MAP.get(category) == null){
108
			addActionError("Category not selected");
109
			return "deny";
110
		}
111
 
112
		resultList = new ArrayList<BulkContentUploadResult>();
113
		List<BulkContentUpload> bulkContentList = to_thrift(sheet, Integer.valueOf(CATEGORY_MAP.get(category)));
114
		for (BulkContentUpload b : bulkContentList){
115
			ItemUpdateController i = new ItemUpdateController();
116
			i.setSession(session);
117
			Entity existingEntity = CreationUtils.getEntity(b.getExisting_entity_id());
118
 
119
			if (existingEntity == null){
120
				BulkContentUploadResult result = new BulkContentUploadResult();
121
				result.setEntity_id(b.getNew_entity_id());
122
				result.setMessage("Existing entity id is wrong");
123
				resultList.add(result);
124
				continue;
125
			}
126
 
127
 
128
			if (!(existingEntity.getCategoryID()==b.getCategory_id())){
129
				BulkContentUploadResult result = new BulkContentUploadResult();
130
				result.setEntity_id(b.getNew_entity_id());
131
				result.setMessage("Category doesn't match");
132
				resultList.add(result);
133
				continue;
134
			}
19840 kshitij.so 135
 
136
			if ((existingEntity.getCategoryID()==b.getNew_entity_id())){
137
				BulkContentUploadResult result = new BulkContentUploadResult();
138
				result.setEntity_id(b.getNew_entity_id());
139
				result.setMessage("New and old entities are same - Ignoring.");
140
				resultList.add(result);
141
				continue;
142
			}
19685 kshitij.so 143
 
144
			i.setId(String.valueOf(b.getNew_entity_id()));
145
			Map<String, String[]> reqparams =  new HashMap<String, String[]>();
146
			reqparams.put("action", new String[]{"dup-entity"});
147
			reqparams.put("entity-old", new String[]{String.valueOf(b.getExisting_entity_id())});
148
			i.setParameters(reqparams);
149
			i.show();
150
 
151
			if (!i.getActionErrors().isEmpty()){
152
				BulkContentUploadResult result = new BulkContentUploadResult();
153
				result.setEntity_id(b.getNew_entity_id());
154
				String local_message = "";
155
				Iterator<String> it = i.getActionErrors().iterator();
156
				while(it.hasNext()) {
157
					local_message = local_message + it.next().toString();
158
				}
159
				result.setMessage(local_message);
160
				resultList.add(result);
161
				continue;
162
			}
163
 
164
			Entity newEntity = CreationUtils.getEntity(b.getNew_entity_id());
165
			newEntity.setCategoryID(b.getCategory_id());
166
 
167
			newEntity.setBrand(b.getBrand().trim());
168
			newEntity.setModelName(b.getModel_name().trim());
169
			newEntity.setModelNumber(b.getModel_number().trim());
170
 
171
			EntityState entityState = CreationUtils.getEntityState(newEntity.getID());
172
			entityState.setCategoryID(b.getCategory_id());
173
			entityState.setBrand(b.getBrand().trim());
174
			entityState.setModelName(b.getModel_name().trim());
175
			entityState.setModelNumber(b.getModel_number().trim());
176
			updateTitleAndMetaDescription(newEntity);
177
			CreationUtils.updateEntity(newEntity);
178
			CreationUtils.updateEntityState(entityState);
179
		}
180
 
181
		log.info("Done with cloning");
182
		log.info("Error result list "+resultList.size()+ " "+resultList.toString());
183
		for (BulkContentUploadResult resultSet : resultList){
184
			for (BulkContentUpload b : bulkContentList){
185
				if (b.getNew_entity_id()==resultSet.getEntity_id()){
186
					bulkContentList.remove(b);
187
					log.info("Removing "+b.toString());
188
					break;
189
				}
190
			}
191
		}
192
		Client contentClient  =  new ContentClient().getClient();
193
		List<BulkContentUploadResult> notUpdated = contentClient.uploadContent(bulkContentList);
194
		log.info("Done with service call");
195
		log.info("Before removing not updated Error result list "+resultList.size()+ " "+resultList.toString());
196
		for (BulkContentUploadResult serviceResult : notUpdated){
197
			resultList.add(serviceResult);
198
			for (BulkContentUpload b : bulkContentList){
199
				if (b.getNew_entity_id()==serviceResult.getEntity_id()){
200
					bulkContentList.remove(b);
201
					break;
202
				}
203
			}
204
		}
205
		log.info("Error result list "+resultList.size()+ " "+resultList.toString());
206
		CatalogClient csc = new CatalogClient();
207
		in.shop2020.model.v1.catalog.CatalogService.Client iclient = csc.getClient();
208
		for (BulkContentUpload b : bulkContentList){
209
			Entity entity = CreationUtils.getEntity(b.getNew_entity_id());
210
			ContentValidator validator = new ContentValidator();
211
			if(!validator.validate(entity)){
212
				BulkContentUploadResult result = new BulkContentUploadResult();
213
				result.setEntity_id(b.getNew_entity_id());
214
				result.setMessage("Mandary fields not filled");
215
				resultList.add(result);
216
				continue;
217
			}
218
			EntityState state = CreationUtils.getEntityState(b.getNew_entity_id());
219
			state.assignEntity(this.getUsername(), this.getUsername());
220
			state.completeEntity(this.getUsername());
221
			iclient.markItemAsContentComplete(b.getNew_entity_id(), state.getCategoryID(), state.getBrand(), state.getModelName(), state.getModelNumber(), false);	
222
			state.readyEntity(getUsername());
223
			CreationUtils.updateEntityState(state);
224
 
225
		}
226
		removeEntityFrominconsistentEntities(bulkContentList);
227
		return "result";
228
	}
229
 
230
	private String updateCatalog(HSSFSheet sheet) throws CatalogServiceException, TException {
231
		List<BulkUploadCatalog> bulkCatalogList= new ArrayList<BulkUploadCatalog>();
232
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
233
			long newEntityId = (long)sheet.getRow(iterator).getCell(NEW_ENTITY_ID_INDEX).getNumericCellValue();
234
			String brand = sheet.getRow(iterator).getCell(BRAND_INDEX).getStringCellValue();
235
			String model_name = sheet.getRow(iterator).getCell(MODEL_NAME_INDEX).getStringCellValue();
236
			String model_number = sheet.getRow(iterator).getCell(MODEL_NUMBER_INDEX).getStringCellValue();
237
			BulkUploadCatalog b = new BulkUploadCatalog();
238
			b.setCatalog_item_id(newEntityId);
239
			b.setBrand(brand);
240
			b.setModel_name(model_name);
241
			b.setModel_number(model_number);
242
			bulkCatalogList.add(b);
243
		}
244
		CatalogClient csc = new CatalogClient();
245
		in.shop2020.model.v1.catalog.CatalogService.Client cclient = csc.getClient();
246
		cclient.bulkUpdateCatalog(bulkCatalogList);
247
		return "result";
248
 
249
	}
250
 
251
	private void removeEntityFrominconsistentEntities(List<BulkContentUpload> bulkContentList) throws Exception{
252
		for (BulkContentUploadResult r: resultList){
253
			for (BulkContentUpload b :bulkContentList){
254
				if (r.getEntity_id()==b.getExisting_entity_id()){
255
					bulkContentList.remove(b);
256
					break;
257
				}
258
			}
259
		}
260
 
261
		List<Long> inconsistentEntities = CreationUtils.getInconsistentEntities();
262
		for (BulkContentUpload b :bulkContentList){
263
			if(inconsistentEntities != null){
264
				inconsistentEntities.remove(b.getNew_entity_id());
265
			}
266
		}
267
 
268
		CreationUtils.storeInconsistentEntities(inconsistentEntities);
269
	}
270
 
271
	private List<BulkContentUpload> to_thrift(HSSFSheet sheet, int category_id){
272
		List<BulkContentUpload> bulkContentList= new ArrayList<BulkContentUpload>();
273
		switch(category_id){
274
		//Carrying case
275
		case 10018:
276
			//Screen Guard
277
		case 10020:
278
			for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
279
				long newEntityId = (long)sheet.getRow(iterator).getCell(NEW_ENTITY_ID_INDEX).getNumericCellValue();
280
				long existing_entity_id = (long)sheet.getRow(iterator).getCell(EXISTING_ENTITY_ID_INDEX).getNumericCellValue();
281
				String compatibility = sheet.getRow(iterator).getCell(COMPATIBILITY_INDEX).getStringCellValue();
282
				String brand = sheet.getRow(iterator).getCell(BRAND_INDEX).getStringCellValue();
283
				String model_name = sheet.getRow(iterator).getCell(MODEL_NAME_INDEX).getStringCellValue();
284
				String model_number = sheet.getRow(iterator).getCell(MODEL_NUMBER_INDEX).getStringCellValue();
285
				BulkContentUpload b =  new BulkContentUpload();
286
				b.setNew_entity_id(newEntityId);
287
				b.setCategory_id((long)category_id);
288
				b.setCompatibility(compatibility);
289
				b.setExisting_entity_id(existing_entity_id);
290
				b.setBrand(brand);
291
				b.setModel_name(model_name);
292
				b.setModel_number(model_number);
293
				bulkContentList.add(b);
294
			}
295
			break;
296
			//Battery
297
		case 10014:
298
			for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
299
				long newEntityId = (long)sheet.getRow(iterator).getCell(NEW_ENTITY_ID_INDEX).getNumericCellValue();
300
				long existing_entity_id = (long)sheet.getRow(iterator).getCell(EXISTING_ENTITY_ID_INDEX).getNumericCellValue();
301
				String compatibility = sheet.getRow(iterator).getCell(COMPATIBILITY_INDEX).getStringCellValue();
302
				String brand = sheet.getRow(iterator).getCell(BRAND_INDEX).getStringCellValue();
303
				String model_name = sheet.getRow(iterator).getCell(MODEL_NAME_INDEX).getStringCellValue();
304
				String model_number = sheet.getRow(iterator).getCell(MODEL_NUMBER_INDEX).getStringCellValue();
305
				int battery_type = (int)sheet.getRow(iterator).getCell(BATTERY_TYPE_INDEX).getNumericCellValue();
306
				String battery_capacity = sheet.getRow(iterator).getCell(BATTERY_CAPACITY_INDEX).getStringCellValue();
307
				BulkContentUpload b =  new BulkContentUpload();
308
				b.setNew_entity_id(newEntityId);
309
				b.setCategory_id((long)category_id);
310
				b.setCompatibility(compatibility);
311
				b.setBattery_type(battery_type);
312
				b.setBattery_capacity(battery_capacity);
313
				b.setExisting_entity_id(existing_entity_id);
314
				b.setBrand(brand);
315
				b.setModel_name(model_name);
316
				b.setModel_number(model_number);
317
				bulkContentList.add(b);
318
			}
319
		}
320
		return bulkContentList;
321
	}
322
 
323
	private void updateTitleAndMetaDescription(Entity entity) {
324
		Slide summarySlide = entity.getSlide(130054l);
325
		if (summarySlide != null){
326
			List<Feature> features = summarySlide.getFeatures();
327
			if(features != null){
328
				for(Feature f : features) {
329
					if(f.getFeatureDefinitionID()==120132l){
330
						if(f.getBullets() == null){
331
							f.setBullets(new ArrayList<Bullet>());
332
						}else {
333
							f.getBullets().clear();
334
						}
335
						Bullet titleBullet = new Bullet(new PrimitiveDataObject(EntityUtils.getTitle(entity)));
336
						f.getBullets().add(titleBullet);
337
					}
338
					else if(f.getFeatureDefinitionID()==120133l){
339
						if(f.getBullets() == null){
340
							f.setBullets(new ArrayList<Bullet>());
341
						}else {
342
							f.getBullets().clear();
343
						}
344
						Bullet metaDescBullet = new Bullet(new PrimitiveDataObject(EntityUtils.getMetaDescription(entity, defs)));
345
						f.getBullets().add(metaDescBullet);
346
					}
347
				}
348
			}
349
		}
350
	}
351
 
352
	public String getCategory() {
353
		return category;
354
	}
355
 
356
 
357
	public void setCategory(String category) {
358
		this.category = category;
359
	}
360
 
361
	public void setUpload(File upload) {
362
		this.upload = upload;
363
	}
364
 
365
	public File getUpload() {
366
		return upload;
367
	}
368
 
369
	public List<BulkContentUploadResult> getResultList() {
370
		return resultList;
371
	}
372
 
373
	public String getChangeCatalog() {
374
		return changeCatalog;
375
	}
376
 
377
	public void setChangeCatalog(String changeCatalog) {
378
		this.changeCatalog = changeCatalog;
379
	}
380
 
381
}