Rev 19687 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.creation.controllers;import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.util.ArrayList;import java.util.Collection;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import org.apache.commons.io.FileUtils;import org.apache.juli.logging.Log;import org.apache.juli.logging.LogFactory;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.struts2.convention.annotation.InterceptorRef;import org.apache.struts2.convention.annotation.InterceptorRefs;import org.apache.thrift.TException;import org.apache.thrift.transport.TTransportException;import in.shop2020.content.BulkContentUpload;import in.shop2020.content.BulkContentUploadResult;import in.shop2020.content.ContentService.Client;import in.shop2020.content.security.UserManager;import in.shop2020.creation.util.ContentValidator;import in.shop2020.thrift.clients.CatalogClient;import in.shop2020.thrift.clients.ContentClient;import in.shop2020.util.EntityUtils;import in.shop2020.metamodel.core.Bullet;import in.shop2020.metamodel.core.Entity;import in.shop2020.metamodel.core.EntityState;import in.shop2020.metamodel.core.Feature;import in.shop2020.metamodel.core.PrimitiveDataObject;import in.shop2020.metamodel.core.Slide;import in.shop2020.metamodel.definitions.Catalog;import in.shop2020.metamodel.definitions.DefinitionsContainer;import in.shop2020.metamodel.util.CreationUtils;import in.shop2020.model.v1.catalog.BulkUploadCatalog;import in.shop2020.model.v1.catalog.CatalogServiceException;@InterceptorRefs({@InterceptorRef("myDefault"),@InterceptorRef("login")})public class EntityUploadController extends BaseController {/****/private static final long serialVersionUID = 1L;private static Log log = LogFactory.getLog(EntityUploadController.class);@SuppressWarnings("serial")public static final Map<String, String> CATEGORY_MAP =new HashMap<String, String>(){{put("1","10018");put("2","10020");put("3","10014");}};private DefinitionsContainer defs = Catalog.getInstance().getDefinitionsContainer();private File upload;private String category;private static final int NEW_ENTITY_ID_INDEX = 0;private static final int BRAND_INDEX = 1;private static final int MODEL_NAME_INDEX = 2;private static final int MODEL_NUMBER_INDEX = 3;private static final int EXISTING_ENTITY_ID_INDEX = 4;private static final int COMPATIBILITY_INDEX = 5;private static final int BATTERY_TYPE_INDEX = 6;private static final int BATTERY_CAPACITY_INDEX = 7;private List<BulkContentUploadResult> resultList;private String changeCatalog="0";public String index(){if(!UserManager.getUserManager().canUploadContentSheet(this.getUsername())){return "deny";}return "entity-upload";}public String create() throws Exception{if(!UserManager.getUserManager().canUploadContentSheet(this.getUsername())){addActionError("You dont have permission to access this resource");return "deny";}File fileToCreate = new File("/tmp/", "entity.xls"+String.valueOf(getTimeInMilliseconds()));FileUtils.copyFile(this.upload, fileToCreate);FileInputStream iFile = new FileInputStream(new File(upload.getAbsolutePath()));HSSFWorkbook workbook = new HSSFWorkbook(iFile);HSSFSheet sheet = workbook.getSheetAt(0);if (changeCatalog.equals("1")){return updateCatalog(sheet);}if (CATEGORY_MAP.get(category) == null){addActionError("Category not selected");return "deny";}resultList = new ArrayList<BulkContentUploadResult>();List<BulkContentUpload> bulkContentList = to_thrift(sheet, Integer.valueOf(CATEGORY_MAP.get(category)));for (BulkContentUpload b : bulkContentList){ItemUpdateController i = new ItemUpdateController();i.setSession(session);Entity existingEntity = CreationUtils.getEntity(b.getExisting_entity_id());if (existingEntity == null){BulkContentUploadResult result = new BulkContentUploadResult();result.setEntity_id(b.getNew_entity_id());result.setMessage("Existing entity id is wrong");resultList.add(result);continue;}if (!(existingEntity.getCategoryID()==b.getCategory_id())){BulkContentUploadResult result = new BulkContentUploadResult();result.setEntity_id(b.getNew_entity_id());result.setMessage("Category doesn't match");resultList.add(result);continue;}if ((existingEntity.getCategoryID()==b.getNew_entity_id())){BulkContentUploadResult result = new BulkContentUploadResult();result.setEntity_id(b.getNew_entity_id());result.setMessage("New and old entities are same - Ignoring.");resultList.add(result);continue;}i.setId(String.valueOf(b.getNew_entity_id()));Map<String, String[]> reqparams = new HashMap<String, String[]>();reqparams.put("action", new String[]{"dup-entity"});reqparams.put("entity-old", new String[]{String.valueOf(b.getExisting_entity_id())});i.setParameters(reqparams);i.show();if (!i.getActionErrors().isEmpty()){BulkContentUploadResult result = new BulkContentUploadResult();result.setEntity_id(b.getNew_entity_id());String local_message = "";Iterator<String> it = i.getActionErrors().iterator();while(it.hasNext()) {local_message = local_message + it.next().toString();}result.setMessage(local_message);resultList.add(result);continue;}Entity newEntity = CreationUtils.getEntity(b.getNew_entity_id());newEntity.setCategoryID(b.getCategory_id());newEntity.setBrand(b.getBrand().trim());newEntity.setModelName(b.getModel_name().trim());newEntity.setModelNumber(b.getModel_number().trim());EntityState entityState = CreationUtils.getEntityState(newEntity.getID());entityState.setCategoryID(b.getCategory_id());entityState.setBrand(b.getBrand().trim());entityState.setModelName(b.getModel_name().trim());entityState.setModelNumber(b.getModel_number().trim());updateTitleAndMetaDescription(newEntity);CreationUtils.updateEntity(newEntity);CreationUtils.updateEntityState(entityState);}log.info("Done with cloning");log.info("Error result list "+resultList.size()+ " "+resultList.toString());for (BulkContentUploadResult resultSet : resultList){for (BulkContentUpload b : bulkContentList){if (b.getNew_entity_id()==resultSet.getEntity_id()){bulkContentList.remove(b);log.info("Removing "+b.toString());break;}}}Client contentClient = new ContentClient().getClient();List<BulkContentUploadResult> notUpdated = contentClient.uploadContent(bulkContentList);log.info("Done with service call");log.info("Before removing not updated Error result list "+resultList.size()+ " "+resultList.toString());for (BulkContentUploadResult serviceResult : notUpdated){resultList.add(serviceResult);for (BulkContentUpload b : bulkContentList){if (b.getNew_entity_id()==serviceResult.getEntity_id()){bulkContentList.remove(b);break;}}}log.info("Error result list "+resultList.size()+ " "+resultList.toString());CatalogClient csc = new CatalogClient();in.shop2020.model.v1.catalog.CatalogService.Client iclient = csc.getClient();for (BulkContentUpload b : bulkContentList){Entity entity = CreationUtils.getEntity(b.getNew_entity_id());ContentValidator validator = new ContentValidator();if(!validator.validate(entity)){BulkContentUploadResult result = new BulkContentUploadResult();result.setEntity_id(b.getNew_entity_id());result.setMessage("Mandary fields not filled");resultList.add(result);continue;}EntityState state = CreationUtils.getEntityState(b.getNew_entity_id());state.assignEntity(this.getUsername(), this.getUsername());state.completeEntity(this.getUsername());iclient.markItemAsContentComplete(b.getNew_entity_id(), state.getCategoryID(), state.getBrand(), state.getModelName(), state.getModelNumber(), false);state.readyEntity(getUsername());CreationUtils.updateEntityState(state);}removeEntityFrominconsistentEntities(bulkContentList);return "result";}private String updateCatalog(HSSFSheet sheet) throws CatalogServiceException, TException {List<BulkUploadCatalog> bulkCatalogList= new ArrayList<BulkUploadCatalog>();for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){long newEntityId = (long)sheet.getRow(iterator).getCell(NEW_ENTITY_ID_INDEX).getNumericCellValue();String brand = sheet.getRow(iterator).getCell(BRAND_INDEX).getStringCellValue();String model_name = sheet.getRow(iterator).getCell(MODEL_NAME_INDEX).getStringCellValue();String model_number = sheet.getRow(iterator).getCell(MODEL_NUMBER_INDEX).getStringCellValue();BulkUploadCatalog b = new BulkUploadCatalog();b.setCatalog_item_id(newEntityId);b.setBrand(brand);b.setModel_name(model_name);b.setModel_number(model_number);bulkCatalogList.add(b);}CatalogClient csc = new CatalogClient();in.shop2020.model.v1.catalog.CatalogService.Client cclient = csc.getClient();cclient.bulkUpdateCatalog(bulkCatalogList);return "result";}private void removeEntityFrominconsistentEntities(List<BulkContentUpload> bulkContentList) throws Exception{for (BulkContentUploadResult r: resultList){for (BulkContentUpload b :bulkContentList){if (r.getEntity_id()==b.getExisting_entity_id()){bulkContentList.remove(b);break;}}}List<Long> inconsistentEntities = CreationUtils.getInconsistentEntities();for (BulkContentUpload b :bulkContentList){if(inconsistentEntities != null){inconsistentEntities.remove(b.getNew_entity_id());}}CreationUtils.storeInconsistentEntities(inconsistentEntities);}private List<BulkContentUpload> to_thrift(HSSFSheet sheet, int category_id){List<BulkContentUpload> bulkContentList= new ArrayList<BulkContentUpload>();switch(category_id){//Carrying casecase 10018://Screen Guardcase 10020:for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){long newEntityId = (long)sheet.getRow(iterator).getCell(NEW_ENTITY_ID_INDEX).getNumericCellValue();long existing_entity_id = (long)sheet.getRow(iterator).getCell(EXISTING_ENTITY_ID_INDEX).getNumericCellValue();String compatibility = sheet.getRow(iterator).getCell(COMPATIBILITY_INDEX).getStringCellValue();String brand = sheet.getRow(iterator).getCell(BRAND_INDEX).getStringCellValue();String model_name = sheet.getRow(iterator).getCell(MODEL_NAME_INDEX).getStringCellValue();String model_number = sheet.getRow(iterator).getCell(MODEL_NUMBER_INDEX).getStringCellValue();BulkContentUpload b = new BulkContentUpload();b.setNew_entity_id(newEntityId);b.setCategory_id((long)category_id);b.setCompatibility(compatibility);b.setExisting_entity_id(existing_entity_id);b.setBrand(brand);b.setModel_name(model_name);b.setModel_number(model_number);bulkContentList.add(b);}break;//Batterycase 10014:for (int iterator=sheet.getFirstRowNum()+1;iterator<=sheet.getLastRowNum();iterator++){long newEntityId = (long)sheet.getRow(iterator).getCell(NEW_ENTITY_ID_INDEX).getNumericCellValue();long existing_entity_id = (long)sheet.getRow(iterator).getCell(EXISTING_ENTITY_ID_INDEX).getNumericCellValue();String compatibility = sheet.getRow(iterator).getCell(COMPATIBILITY_INDEX).getStringCellValue();String brand = sheet.getRow(iterator).getCell(BRAND_INDEX).getStringCellValue();String model_name = sheet.getRow(iterator).getCell(MODEL_NAME_INDEX).getStringCellValue();String model_number = sheet.getRow(iterator).getCell(MODEL_NUMBER_INDEX).getStringCellValue();int battery_type = (int)sheet.getRow(iterator).getCell(BATTERY_TYPE_INDEX).getNumericCellValue();String battery_capacity = sheet.getRow(iterator).getCell(BATTERY_CAPACITY_INDEX).getStringCellValue();BulkContentUpload b = new BulkContentUpload();b.setNew_entity_id(newEntityId);b.setCategory_id((long)category_id);b.setCompatibility(compatibility);b.setBattery_type(battery_type);b.setBattery_capacity(battery_capacity);b.setExisting_entity_id(existing_entity_id);b.setBrand(brand);b.setModel_name(model_name);b.setModel_number(model_number);bulkContentList.add(b);}}return bulkContentList;}private void updateTitleAndMetaDescription(Entity entity) {Slide summarySlide = entity.getSlide(130054l);if (summarySlide != null){List<Feature> features = summarySlide.getFeatures();if(features != null){for(Feature f : features) {if(f.getFeatureDefinitionID()==120132l){if(f.getBullets() == null){f.setBullets(new ArrayList<Bullet>());}else {f.getBullets().clear();}Bullet titleBullet = new Bullet(new PrimitiveDataObject(EntityUtils.getTitle(entity)));f.getBullets().add(titleBullet);}else if(f.getFeatureDefinitionID()==120133l){if(f.getBullets() == null){f.setBullets(new ArrayList<Bullet>());}else {f.getBullets().clear();}Bullet metaDescBullet = new Bullet(new PrimitiveDataObject(EntityUtils.getMetaDescription(entity, defs)));f.getBullets().add(metaDescBullet);}}}}}public String getCategory() {return category;}public void setCategory(String category) {this.category = category;}public void setUpload(File upload) {this.upload = upload;}public File getUpload() {return upload;}public List<BulkContentUploadResult> getResultList() {return resultList;}public String getChangeCatalog() {return changeCatalog;}public void setChangeCatalog(String changeCatalog) {this.changeCatalog = changeCatalog;}}