Rev 3574 | Blame | Compare with Previous | Last modification | View Log | RSS feed
/****/package in.shop2020.creation.controllers;import in.shop2020.metamodel.core.Entity;import in.shop2020.metamodel.core.Slide;import in.shop2020.metamodel.definitions.Catalog;import in.shop2020.metamodel.definitions.Category;import in.shop2020.metamodel.definitions.CategorySlideDefinition;import in.shop2020.metamodel.definitions.DefinitionsContainer;import in.shop2020.metamodel.util.CreationUtils;import java.util.ArrayList;import java.util.Arrays;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.juli.logging.Log;import org.apache.juli.logging.LogFactory;import org.apache.struts2.convention.annotation.InterceptorRef;import org.apache.struts2.convention.annotation.InterceptorRefs;import org.apache.struts2.convention.annotation.Result;import org.apache.struts2.convention.annotation.Results;@InterceptorRefs({@InterceptorRef("myDefault"),@InterceptorRef("login")})@Results({@Result(name="success", type="redirectAction",params = {"actionName" , "slides"}),@Result(name="redirect", location="${url}", type="redirect")})public class SlidesController extends BaseController {/****/private static final long serialVersionUID = 1L;/****/private static Log log = LogFactory.getLog(SlidesController.class);/****/private String id;private String redirectURL;private Map<String, String[]> reqparams;private DefinitionsContainer defs = Catalog.getInstance().getDefinitionsContainer();private List<List<String[]>> allSlidesData = new ArrayList<List<String[]>>();/*** Key: Slide Definition Id* Value: Category Slide Definition*/private Map<Long, CategorySlideDefinition> mapAllCatSlideDefs =new HashMap<Long, CategorySlideDefinition>();private Entity entity;/**** @return*/public String edit() {log.info("SlidesController.edit");long entityID = Long.parseLong(this.getId());log.info("entityID:" + entityID);try {entity = CreationUtils.getEntity(entityID);//List<Long> selection = CreationUtils.getSlideSequence(entityID, entity.getCategoryID());List<Long> selection = entity.getSlideSequence();if(selection == null){entity.setSlideSequence(Catalog.getInstance().getDefinitionsContainer().getCategorySlideSequence(entity.getCategoryID()));CreationUtils.updateEntity(entity);selection = entity.getSlideSequence();}log.info("selection:" + selection);// Entity's category firstlong catID = entity.getCategoryID();List<String[]> slidesData = this.getSlidesData(catID, selection,true);this.allSlidesData.add(slidesData);} catch (Exception e) {log.error(CreationUtils.getStackTrace(e));this.setErrorString(CreationUtils.getStackTrace(e));return "fatal";}return "edit";}public String show(){if(this.reqparams.get("sequence") != null){String itemlist = this.reqparams.get("sequence")[0];long entityID = Long.parseLong(this.getId());try {this.entity = CreationUtils.getEntity(entityID);List<Long> slideSequence = entity.getSlideSequence();List<Long> newSlideSequence = new ArrayList<Long>();String[] items = itemlist.split("-");for(int i = 0; i < items.length; i++){newSlideSequence.add(slideSequence.get(Integer.parseInt(items[i])));}entity.setSlideSequence(newSlideSequence);CreationUtils.updateEntity(entity);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}this.redirectURL = "/slides/" + this.getId() + "/edit";return "redirect";}/**** @return*/public String update() {log.info("SlidesController.update");for(String reqparam: this.reqparams.keySet()){String[] values = this.reqparams.get(reqparam);for(String value: values){System.out.println("Param: " + reqparam + "Value: " + value);}}//FIXME/* Need to fix me while reorderingif(this.reqparams.get("slideid") != null){long slideId = Long.parseLong(this.reqparams.get("slideid")[0]);long afterSlideId = Long.parseLong(this.reqparams.get("afterslide")[0]);long entityID = Long.parseLong(this.getId());// List<Long> slideSequence = CreationUtils.getSlideSequence(entityID);// List<Long> newSlideSequence = new ArrayList<Long>();//// for(Long slide: slideSequence){// newSlideSequence.add(slide);// if(slide.equals(new Long(afterSlideId))){// newSlideSequence.add(new Long(slideId));// }// }try{entity = CreationUtils.getEntity(entityID);//Map<Long, List<Long>> catSlideSequence = CreationUtils.getRawSlideSequence(entityID);Map<Long, List<Long>> catSlideSequence = defs.getCategorySlideSequence();List<Long> allSlideSequence = catSlideSequence.get(new Long(-1));List<Long> newAllSlideSequence = new ArrayList<Long>();catSlideSequence.remove(new Long(-1));Map<Long, List<Long>> newCatSlideSequence = new HashMap<Long, List<Long>>();for(Long slide: allSlideSequence){newAllSlideSequence.add(slide);if(slide.equals(new Long(afterSlideId))){newAllSlideSequence.add(new Long(slideId));}}for(Long cat: catSlideSequence.keySet()){List<Long> newSlideSequence = new ArrayList<Long>();List<Long> slides = catSlideSequence.get(cat);boolean contains = false;List<CategorySlideDefinition> catdefs = defs.getCategorySlideDefinitions(cat);for(CategorySlideDefinition catdef: catdefs){if(catdef.getSlideDefintionID()==slideId){contains = true;}}for(Long slide: slides){newSlideSequence.add(slide);}if(contains){newSlideSequence.add(new Long(slideId));}newCatSlideSequence.put(cat, newSlideSequence);}newCatSlideSequence.put(new Long(-1), newAllSlideSequence);//CreationUtils.storeSlideSequence(entityID, newCatSlideSequence);entity.setSlideSequence(newAllSlideSequence);}catch(Exception e){e.printStackTrace();}this.redirectURL = "/slides/" + this.getId() + "/edit";return "redirect";}*/try {long entityID = Long.parseLong(this.getId());entity = CreationUtils.getEntity(entityID);Long catId = new Long(entity.getCategoryID());// Slides of entity's categoryString[] strSlideDefIDs = this.reqparams.get(new Long(entity.getCategoryID()).toString());log.info("strSlideDefIDs:" + Arrays.toString(strSlideDefIDs));List<Long> sequence= entity.getSlideSequence();log.info("S1"+sequence);if(sequence == null){sequence = new ArrayList<Long>();entity.setSlideSequence(sequence);}List<Long> newSequence = new ArrayList<Long>();// Add entity's category slides firstList<Long> slideDefIDs = new ArrayList<Long>();for(String strSlideDefID : strSlideDefIDs) {newSequence.add(new Long(Long.parseLong(strSlideDefID)));slideDefIDs.add(new Long(Long.parseLong(strSlideDefID)));if(!sequence.contains(new Long(Long.parseLong(strSlideDefID)))){sequence.add(new Long(Long.parseLong(strSlideDefID)));}}log.info("S2"+sequence);long parentCatID = (defs.getCategory(entity.getCategoryID())).getParentCategory().getID();List<Category> cats = defs.getChildrenCategories(parentCatID);// Add slides borrowed from other categoriesfor(Category cat : cats) {Long lCat = new Long(cat.getID());String[] strCatSlideDefIDs = this.reqparams.get(lCat.toString());if(strCatSlideDefIDs != null && strCatSlideDefIDs.length > 0) {for(String strCatSlideDefID : strCatSlideDefIDs) {newSequence.add(new Long(Long.parseLong(strCatSlideDefID)));if(!lCat.equals(catId) && !sequence.contains(new Long(Long.parseLong(strCatSlideDefID)))){sequence.add(new Long(Long.parseLong(strCatSlideDefID)));log.info("S3"+sequence);}}}}//FIXME To remove not required slide ids and slides// to remove obsolete elementsList<Long> removeList = new ArrayList<Long>();for(Long slideId: sequence){boolean isContains = false;if(newSequence.contains(slideId)){isContains = true;}if(!isContains){removeList.add(slideId);}}log.info("To remove"+removeList);for(Long slideId: removeList){sequence.remove(slideId);}// end// Delete slides no longer present in the listList<Slide> slides = entity.getSlides();if(slides != null) {List<Slide> deletedSlides = new ArrayList<Slide>();for(Slide slide : slides) {Long lSlideDefID = new Long(slide.getSlideDefinitionID());log.info(sequence);if(!sequence.contains(lSlideDefID)) {log.info("deleted lSlideDefID:" + lSlideDefID);deletedSlides.add(slide);}}for(Slide deletedSlide : deletedSlides) {slides.remove(deletedSlide);}}CreationUtils.updateEntity(entity);if(this.reqparams.containsKey("save")) {this.redirectURL = "/entity";}else {this.redirectURL = "/entity/" + this.getId() + "/edit" +"?slideDefID=" + slideDefIDs.get(0);}return "redirect";} catch (Exception e) {log.error(CreationUtils.getStackTrace(e));this.setErrorString(CreationUtils.getStackTrace(e));return "fatal";}}/****/@Overridepublic void setParameters(Map<String, String[]> reqmap) {log.info("setParameters:" + reqmap);this.reqparams = reqmap;}/*** @param id the id to set*/public void setId(String id) {this.id = id;}/*** @return the id*/public String getId() {return id;}/**** @return*/public List<List<String[]>> getAllSlidesData() {return this.allSlidesData;}/**** @return*/public String getUrl() {return this.redirectURL;}/*** @param errorString the exceptionString to set*/public void setErrorString(String errorString) {}/**** @return*/public List<String> getMandatorySlideLabels() {List<String[]> slidesdata = this.allSlidesData.get(0);List<String> mandatorySlideLabels = new ArrayList<String>();for(String[] data : slidesdata) {if("Mandatory".equals(data[4])) {mandatorySlideLabels.add(data[3]);}}return mandatorySlideLabels;}/**** @param catID* @param selection* @return* @throws Exception*/private List<String[]> getSlidesData(long catID, List<Long> selection,boolean isEntityCategory) throws Exception {log.info("catID:" + catID);List<Long> slideDefIDs = defs.getCategorySlideSequence(catID);log.info("slideDefIDs:" + slideDefIDs);List<CategorySlideDefinition> catSlideDefs =defs.getCategorySlideDefinitions(catID);log.info("catSlideDefs:" + catSlideDefs);// Convert it into Map, this will help put CategorySlideDefinition// objects in sequence// FIXME - Quick and dirtyList<Long> excludeList = new ArrayList<Long>();for(CategorySlideDefinition catSlideDef : catSlideDefs) {Long slideDefID = new Long(catSlideDef.getSlideDefintionID());if(!this.mapAllCatSlideDefs.containsKey(slideDefID)) {this.mapAllCatSlideDefs.put(slideDefID, catSlideDef);}else {excludeList.add(slideDefID);}}log.info("excludeList:" + excludeList);List<String[]> slidesData = new ArrayList<String[]>();String categoryLabel = defs.getCategory(catID).getLabel();for(Long slideDefID : slideDefIDs) {// Do not repeat if already coveredif(excludeList.contains(slideDefID)) {continue;}// Category slide definition for the slide def id.CategorySlideDefinition catSlideDef = this.mapAllCatSlideDefs.get(slideDefID);log.info("catSlideDef: " + catSlideDef);String slideLabel = defs.getSlideDefinition(catSlideDef.getSlideDefintionID()).getLabel();Long lSlideDefID = new Long(catSlideDef.getSlideDefintionID());String selected = "0";if(selection != null) {selected = selection.contains(lSlideDefID) ? "1" : "0";}else if(isEntityCategory) {String edImp = catSlideDef.getEditorialImportance().toString();selected = (edImp.equals("Mandatory") ||edImp.endsWith("Recommended")) ? "1" : "0";}String[] data = new String[] {new Long(catID).toString(),categoryLabel,lSlideDefID.toString(),slideLabel,catSlideDef.getEditorialImportance().toString(),selected};slidesData.add(data);}return slidesData;}public List<Long> getSlideSequence(){return this.entity.getSlideSequence();}}