Subversion Repositories SmartDukaan

Rev

Rev 19685 | Go to most recent revision | 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
 
97
		File fileToCreate = new File("/tmp/", "entity.xls");
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
			}
135
 
136
			i.setId(String.valueOf(b.getNew_entity_id()));
137
			Map<String, String[]> reqparams =  new HashMap<String, String[]>();
138
			reqparams.put("action", new String[]{"dup-entity"});
139
			reqparams.put("entity-old", new String[]{String.valueOf(b.getExisting_entity_id())});
140
			i.setParameters(reqparams);
141
			i.show();
142
 
143
			if (!i.getActionErrors().isEmpty()){
144
				BulkContentUploadResult result = new BulkContentUploadResult();
145
				result.setEntity_id(b.getNew_entity_id());
146
				String local_message = "";
147
				Iterator<String> it = i.getActionErrors().iterator();
148
				while(it.hasNext()) {
149
					local_message = local_message + it.next().toString();
150
				}
151
				result.setMessage(local_message);
152
				resultList.add(result);
153
				continue;
154
			}
155
 
156
			Entity newEntity = CreationUtils.getEntity(b.getNew_entity_id());
157
			newEntity.setCategoryID(b.getCategory_id());
158
 
159
			newEntity.setBrand(b.getBrand().trim());
160
			newEntity.setModelName(b.getModel_name().trim());
161
			newEntity.setModelNumber(b.getModel_number().trim());
162
 
163
			EntityState entityState = CreationUtils.getEntityState(newEntity.getID());
164
			entityState.setCategoryID(b.getCategory_id());
165
			entityState.setBrand(b.getBrand().trim());
166
			entityState.setModelName(b.getModel_name().trim());
167
			entityState.setModelNumber(b.getModel_number().trim());
168
			updateTitleAndMetaDescription(newEntity);
169
			CreationUtils.updateEntity(newEntity);
170
			CreationUtils.updateEntityState(entityState);
171
		}
172
 
173
		log.info("Done with cloning");
174
		log.info("Error result list "+resultList.size()+ " "+resultList.toString());
175
		for (BulkContentUploadResult resultSet : resultList){
176
			for (BulkContentUpload b : bulkContentList){
177
				if (b.getNew_entity_id()==resultSet.getEntity_id()){
178
					bulkContentList.remove(b);
179
					log.info("Removing "+b.toString());
180
					break;
181
				}
182
			}
183
		}
184
		Client contentClient  =  new ContentClient().getClient();
185
		List<BulkContentUploadResult> notUpdated = contentClient.uploadContent(bulkContentList);
186
		log.info("Done with service call");
187
		log.info("Before removing not updated Error result list "+resultList.size()+ " "+resultList.toString());
188
		for (BulkContentUploadResult serviceResult : notUpdated){
189
			resultList.add(serviceResult);
190
			for (BulkContentUpload b : bulkContentList){
191
				if (b.getNew_entity_id()==serviceResult.getEntity_id()){
192
					bulkContentList.remove(b);
193
					break;
194
				}
195
			}
196
		}
197
		log.info("Error result list "+resultList.size()+ " "+resultList.toString());
198
		CatalogClient csc = new CatalogClient();
199
		in.shop2020.model.v1.catalog.CatalogService.Client iclient = csc.getClient();
200
		for (BulkContentUpload b : bulkContentList){
201
			Entity entity = CreationUtils.getEntity(b.getNew_entity_id());
202
			ContentValidator validator = new ContentValidator();
203
			if(!validator.validate(entity)){
204
				BulkContentUploadResult result = new BulkContentUploadResult();
205
				result.setEntity_id(b.getNew_entity_id());
206
				result.setMessage("Mandary fields not filled");
207
				resultList.add(result);
208
				continue;
209
			}
210
			EntityState state = CreationUtils.getEntityState(b.getNew_entity_id());
211
			state.assignEntity(this.getUsername(), this.getUsername());
212
			state.completeEntity(this.getUsername());
213
			iclient.markItemAsContentComplete(b.getNew_entity_id(), state.getCategoryID(), state.getBrand(), state.getModelName(), state.getModelNumber(), false);	
214
			state.readyEntity(getUsername());
215
			CreationUtils.updateEntityState(state);
216
 
217
		}
218
		removeEntityFrominconsistentEntities(bulkContentList);
219
		return "result";
220
	}
221
 
222
	private String updateCatalog(HSSFSheet sheet) throws CatalogServiceException, TException {
223
		List<BulkUploadCatalog> bulkCatalogList= new ArrayList<BulkUploadCatalog>();
224
		for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
225
			long newEntityId = (long)sheet.getRow(iterator).getCell(NEW_ENTITY_ID_INDEX).getNumericCellValue();
226
			String brand = sheet.getRow(iterator).getCell(BRAND_INDEX).getStringCellValue();
227
			String model_name = sheet.getRow(iterator).getCell(MODEL_NAME_INDEX).getStringCellValue();
228
			String model_number = sheet.getRow(iterator).getCell(MODEL_NUMBER_INDEX).getStringCellValue();
229
			BulkUploadCatalog b = new BulkUploadCatalog();
230
			b.setCatalog_item_id(newEntityId);
231
			b.setBrand(brand);
232
			b.setModel_name(model_name);
233
			b.setModel_number(model_number);
234
			bulkCatalogList.add(b);
235
		}
236
		CatalogClient csc = new CatalogClient();
237
		in.shop2020.model.v1.catalog.CatalogService.Client cclient = csc.getClient();
238
		cclient.bulkUpdateCatalog(bulkCatalogList);
239
		return "result";
240
 
241
	}
242
 
243
	private void removeEntityFrominconsistentEntities(List<BulkContentUpload> bulkContentList) throws Exception{
244
		for (BulkContentUploadResult r: resultList){
245
			for (BulkContentUpload b :bulkContentList){
246
				if (r.getEntity_id()==b.getExisting_entity_id()){
247
					bulkContentList.remove(b);
248
					break;
249
				}
250
			}
251
		}
252
 
253
		List<Long> inconsistentEntities = CreationUtils.getInconsistentEntities();
254
		for (BulkContentUpload b :bulkContentList){
255
			if(inconsistentEntities != null){
256
				inconsistentEntities.remove(b.getNew_entity_id());
257
			}
258
		}
259
 
260
		CreationUtils.storeInconsistentEntities(inconsistentEntities);
261
	}
262
 
263
	private List<BulkContentUpload> to_thrift(HSSFSheet sheet, int category_id){
264
		List<BulkContentUpload> bulkContentList= new ArrayList<BulkContentUpload>();
265
		switch(category_id){
266
		//Carrying case
267
		case 10018:
268
			//Screen Guard
269
		case 10020:
270
			for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
271
				long newEntityId = (long)sheet.getRow(iterator).getCell(NEW_ENTITY_ID_INDEX).getNumericCellValue();
272
				long existing_entity_id = (long)sheet.getRow(iterator).getCell(EXISTING_ENTITY_ID_INDEX).getNumericCellValue();
273
				String compatibility = sheet.getRow(iterator).getCell(COMPATIBILITY_INDEX).getStringCellValue();
274
				String brand = sheet.getRow(iterator).getCell(BRAND_INDEX).getStringCellValue();
275
				String model_name = sheet.getRow(iterator).getCell(MODEL_NAME_INDEX).getStringCellValue();
276
				String model_number = sheet.getRow(iterator).getCell(MODEL_NUMBER_INDEX).getStringCellValue();
277
				BulkContentUpload b =  new BulkContentUpload();
278
				b.setNew_entity_id(newEntityId);
279
				b.setCategory_id((long)category_id);
280
				b.setCompatibility(compatibility);
281
				b.setExisting_entity_id(existing_entity_id);
282
				b.setBrand(brand);
283
				b.setModel_name(model_name);
284
				b.setModel_number(model_number);
285
				bulkContentList.add(b);
286
			}
287
			break;
288
			//Battery
289
		case 10014:
290
			for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){
291
				long newEntityId = (long)sheet.getRow(iterator).getCell(NEW_ENTITY_ID_INDEX).getNumericCellValue();
292
				long existing_entity_id = (long)sheet.getRow(iterator).getCell(EXISTING_ENTITY_ID_INDEX).getNumericCellValue();
293
				String compatibility = sheet.getRow(iterator).getCell(COMPATIBILITY_INDEX).getStringCellValue();
294
				String brand = sheet.getRow(iterator).getCell(BRAND_INDEX).getStringCellValue();
295
				String model_name = sheet.getRow(iterator).getCell(MODEL_NAME_INDEX).getStringCellValue();
296
				String model_number = sheet.getRow(iterator).getCell(MODEL_NUMBER_INDEX).getStringCellValue();
297
				int battery_type = (int)sheet.getRow(iterator).getCell(BATTERY_TYPE_INDEX).getNumericCellValue();
298
				String battery_capacity = sheet.getRow(iterator).getCell(BATTERY_CAPACITY_INDEX).getStringCellValue();
299
				BulkContentUpload b =  new BulkContentUpload();
300
				b.setNew_entity_id(newEntityId);
301
				b.setCategory_id((long)category_id);
302
				b.setCompatibility(compatibility);
303
				b.setBattery_type(battery_type);
304
				b.setBattery_capacity(battery_capacity);
305
				b.setExisting_entity_id(existing_entity_id);
306
				b.setBrand(brand);
307
				b.setModel_name(model_name);
308
				b.setModel_number(model_number);
309
				bulkContentList.add(b);
310
			}
311
		}
312
		return bulkContentList;
313
	}
314
 
315
	private void updateTitleAndMetaDescription(Entity entity) {
316
		Slide summarySlide = entity.getSlide(130054l);
317
		if (summarySlide != null){
318
			List<Feature> features = summarySlide.getFeatures();
319
			if(features != null){
320
				for(Feature f : features) {
321
					if(f.getFeatureDefinitionID()==120132l){
322
						if(f.getBullets() == null){
323
							f.setBullets(new ArrayList<Bullet>());
324
						}else {
325
							f.getBullets().clear();
326
						}
327
						Bullet titleBullet = new Bullet(new PrimitiveDataObject(EntityUtils.getTitle(entity)));
328
						f.getBullets().add(titleBullet);
329
					}
330
					else if(f.getFeatureDefinitionID()==120133l){
331
						if(f.getBullets() == null){
332
							f.setBullets(new ArrayList<Bullet>());
333
						}else {
334
							f.getBullets().clear();
335
						}
336
						Bullet metaDescBullet = new Bullet(new PrimitiveDataObject(EntityUtils.getMetaDescription(entity, defs)));
337
						f.getBullets().add(metaDescBullet);
338
					}
339
				}
340
			}
341
		}
342
	}
343
 
344
	public String getCategory() {
345
		return category;
346
	}
347
 
348
 
349
	public void setCategory(String category) {
350
		this.category = category;
351
	}
352
 
353
	public void setUpload(File upload) {
354
		this.upload = upload;
355
	}
356
 
357
	public File getUpload() {
358
		return upload;
359
	}
360
 
361
	public List<BulkContentUploadResult> getResultList() {
362
		return resultList;
363
	}
364
 
365
	public String getChangeCatalog() {
366
		return changeCatalog;
367
	}
368
 
369
	public void setChangeCatalog(String changeCatalog) {
370
		this.changeCatalog = changeCatalog;
371
	}
372
 
373
}