Subversion Repositories SmartDukaan

Rev

Rev 8086 | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.creation.controllers;

import in.shop2020.content.security.Role;
import in.shop2020.content.security.UserManager;
import in.shop2020.metamodel.core.Entity;
import in.shop2020.metamodel.core.EntityState;
import in.shop2020.metamodel.core.EntityStatus;
import in.shop2020.metamodel.definitions.Catalog;
import in.shop2020.metamodel.util.CreationUtils;
import in.shop2020.metamodel.util.ExpandedEntity;
import in.shop2020.util.EntityUtils;
import in.shop2020.util.NewCMP;

import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

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="redirect", location="${url}", type="redirect")
})

public class CmpScoresController extends BaseController {
        
        /**
         * 
         */
        private String id;
        private String entityName;
        private Entity entity;
        private String url;

        
        public String getEntityName() {
                return entityName;
        }

        private static final long serialVersionUID = 1L;
        
        public String show(){
                return "show";
        }

        public void setId(String id) {
                this.id = id;
                ExpandedEntity expEntity;
                try {
                        expEntity = CreationUtils.getExpandedEntity(Long.parseLong(id));
                        this.entity = expEntity;
                        this.entityName = EntityUtils.getProductName(expEntity);
                } catch (NumberFormatException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }

        public String getId() {
                return id;
        }
        
        public boolean canEdit() {
                return UserManager.getUserManager().getUserRole(getUsername()).equals(Role.EDITOR) || UserManager.getUserManager().getUserRole(getUsername()).equals(Role.ADMIN);
        }
        
    public Map<String, Map<Long, Double>> getScores() throws Exception {
        Long entityId = Long.parseLong(this.id);
        Map<Long,Double> actualScore = new HashMap<Long, Double>();
        Map<Long,Double> customScore = new HashMap<Long, Double>();
        Map<String, Map<Long,Double>> scores = new HashMap<String, Map<Long, Double>>();
        EntityState es = CreationUtils.getEntityState(entityId);
        if (es!=null && es.getCategoryID() > 0) {
                if(es.getCategoryID() == 10006l && !es.getStatus().equals(EntityStatus.UNASSIGNED)){
                        NewCMP cmp = new NewCMP(Arrays.asList(this.entity));
                                try {
                                        //get Scores on the basis of evaluation
                                        Map<Long, Map<Long, Double>> scoreMap = cmp.getSlideScores(0);
                                        for(Map.Entry<Long, Double> entry1 : scoreMap.get(entityId).entrySet()){
                                                actualScore.put(entry1.getKey(), entry1.getValue());
                                        }
                                        actualScore.put(0l, cmp.getFinalScores(scoreMap).get(entityId)/10);
                                        
                                        Map<Long, Double> customScores = CreationUtils.getCustomSlideComparisonScores(entityId);
                                        if(customScores != null){
                                                //now update scoreMap if any of the slide score has been customised
                                                for (Map.Entry<Long, Double> entry : scoreMap.get(entityId).entrySet()){
                                                        if(customScores.containsKey(entry.getKey())){
                                                                entry.setValue(customScores.get(entry.getKey()));
                                                                customScore.put(entry.getKey(), customScores.get(entry.getKey()));
                                                        }
                                                }
                                                customScore.put(0l, cmp.getFinalScores(scoreMap).get(entityId)/10);
                                        }
                                        
                                } catch (Exception e) {
                                        System.out.println("Some error occurred");
                                        e.printStackTrace();
                                }
                                DecimalFormat oneDForm = new DecimalFormat("#.#");
                                for(Map.Entry<Long, Double> scoreEntry : customScore.entrySet()){
                                        scoreEntry.setValue(Double.valueOf(oneDForm.format(scoreEntry.getValue())));
                                }
                                for(Map.Entry<Long, Double> scoreEntry : actualScore.entrySet()){
                                        scoreEntry.setValue(Double.valueOf(oneDForm.format(scoreEntry.getValue())));
                                }
                                scores.put("actual", actualScore);
                                scores.put("custom", customScore);
                                return scores;
                } else {
                        return new HashMap<String, Map<Long, Double>>();
                }
        } else {
                return new HashMap<String, Map<Long, Double>>();
        }
    }
    
    public String getSlideLabel(long slideId){
        if(slideId == 0){
                return "Total Score";
        }
        try {
                        return Catalog.getInstance().getDefinitionsContainer().getSlideDefinition(slideId).getLabel();
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return "";
    }
    public String update() throws Exception {
        Map<Long, Double> customSlideScores = new HashMap<Long, Double>();
        for (String slideStr :this.reqparams.keySet()){
                if(slideStr.equals("id") || this.reqparams.get(slideStr)[0].equals("")){
                        continue;
                }
                Long slideId = Long.parseLong(slideStr);
                double score = Double.parseDouble(this.reqparams.get(slideStr)[0]);
                customSlideScores.put(slideId, score    );
        }
                CreationUtils.storeCustomSlideComparisonScores(this.entity.getID(), customSlideScores );
                touchEntityState(this.entity.getID());
        setUrl("/cmp-scores/" + this.id);
        return "redirect";
    }
    
        private void touchEntityState(Long eId) throws Exception {
                EntityState es = CreationUtils.getEntityState(eId);
                es.setMerkedReadyOn(new Date());
                CreationUtils.updateEntityState(es);
                System.out.println("Entity touched:" + eId);
        }
    
        public void setUrl(String url) {
                this.url = url;
        }
        
        public String getUrl() {
                return this.url;
        }
}