Subversion Repositories SmartDukaan

Rev

Rev 8086 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
8024 amit.gupta 1
package in.shop2020.creation.controllers;
2
 
3
import in.shop2020.content.security.Role;
4
import in.shop2020.content.security.UserManager;
5
import in.shop2020.metamodel.core.Entity;
6
import in.shop2020.metamodel.core.EntityState;
7
import in.shop2020.metamodel.core.EntityStatus;
8
import in.shop2020.metamodel.definitions.Catalog;
9
import in.shop2020.metamodel.util.CreationUtils;
10
import in.shop2020.metamodel.util.ExpandedEntity;
11
import in.shop2020.util.EntityUtils;
12
import in.shop2020.util.NewCMP;
13
 
14
import java.text.DecimalFormat;
15
import java.util.Arrays;
16
import java.util.HashMap;
17
import java.util.Map;
18
 
19
import org.apache.struts2.convention.annotation.InterceptorRef;
20
import org.apache.struts2.convention.annotation.InterceptorRefs;
21
import org.apache.struts2.convention.annotation.Result;
22
import org.apache.struts2.convention.annotation.Results;
23
 
24
 
25
@InterceptorRefs({
26
    @InterceptorRef("myDefault"),
27
    @InterceptorRef("login")
28
})
29
 
30
@Results({
31
    @Result(name="redirect", location="${url}", type="redirect")
32
})
33
 
34
public class CmpScoresController extends BaseController {
35
 
36
	/**
37
	 * 
38
	 */
39
	private String id;
40
	private String entityName;
41
	private Entity entity;
42
	private String url;
43
 
44
 
45
	public String getEntityName() {
46
		return entityName;
47
	}
48
 
49
	private static final long serialVersionUID = 1L;
50
 
51
	public String show(){
52
		return "show";
53
	}
54
 
55
	public void setId(String id) {
56
		this.id = id;
57
		ExpandedEntity expEntity;
58
		try {
59
			expEntity = CreationUtils.getExpandedEntity(Long.parseLong(id));
60
			this.entity = expEntity;
61
			this.entityName = EntityUtils.getProductName(expEntity);
62
		} catch (NumberFormatException e) {
63
			// TODO Auto-generated catch block
64
			e.printStackTrace();
65
		} catch (Exception e) {
66
			// TODO Auto-generated catch block
67
			e.printStackTrace();
68
		}
69
	}
70
 
71
	public String getId() {
72
		return id;
73
	}
74
 
75
	public boolean canEdit() {
76
		return UserManager.getUserManager().getUserRole(getUsername()).equals(Role.EDITOR) || UserManager.getUserManager().getUserRole(getUsername()).equals(Role.ADMIN);
77
	}
78
 
79
    public Map<String, Map<Long, Double>> getScores() throws Exception {
80
    	Long entityId = Long.parseLong(this.id);
81
    	Map<Long,Double> actualScore = new HashMap<Long, Double>();
82
    	Map<Long,Double> customScore = new HashMap<Long, Double>();
83
    	Map<String, Map<Long,Double>> scores = new HashMap<String, Map<Long, Double>>();
84
    	EntityState es = CreationUtils.getEntityState(entityId);
85
    	if (es!=null && es.getCategoryID() > 0) {
86
	    	if((es.getCategoryID() == 10002l || es.getCategoryID() == 10003l || es.getCategoryID() == 10004l || es.getCategoryID() == 10005l) && !es.getStatus().equals(EntityStatus.UNASSIGNED)){
87
		    	NewCMP cmp = new NewCMP(Arrays.asList(this.entity));
88
				try {
89
					//get Scores on the basis of evaluation
90
					Map<Long, Map<Long, Double>> scoreMap = cmp.getSlideScores(0);
91
					for(Map.Entry<Long, Double> entry1 : scoreMap.get(entityId).entrySet()){
92
						actualScore.put(entry1.getKey(), entry1.getValue());
93
					}
94
					actualScore.put(0l, cmp.getFinalScores(scoreMap).get(entityId)/10);
95
 
96
					Map<Long, Double> customScores = CreationUtils.getCustomSlideComparisonScores(entityId);
97
					if(customScores != null){
98
						//now update scoreMap if any of the slide score has been customised
99
						for (Map.Entry<Long, Double> entry : scoreMap.get(entityId).entrySet()){
100
							if(customScores.containsKey(entry.getKey())){
101
								entry.setValue(customScores.get(entry.getKey()));
102
								customScore.put(entry.getKey(), customScores.get(entry.getKey()));
103
							}
104
						}
105
						customScore.put(0l, cmp.getFinalScores(scoreMap).get(entityId)/10);
106
					}
107
 
108
				} catch (Exception e) {
109
					System.out.println("Some error occurred");
110
					e.printStackTrace();
111
				}
112
				DecimalFormat oneDForm = new DecimalFormat("#.#");
113
				for(Map.Entry<Long, Double> scoreEntry : customScore.entrySet()){
114
					scoreEntry.setValue(Double.valueOf(oneDForm.format(scoreEntry.getValue())));
115
				}
116
				for(Map.Entry<Long, Double> scoreEntry : actualScore.entrySet()){
117
					scoreEntry.setValue(Double.valueOf(oneDForm.format(scoreEntry.getValue())));
118
				}
119
				scores.put("actual", actualScore);
120
				scores.put("custom", customScore);
121
				return scores;
122
	    	} else {
123
	    		return new HashMap<String, Map<Long, Double>>();
124
	    	}
125
    	} else {
126
    		return new HashMap<String, Map<Long, Double>>();
127
    	}
128
    }
129
 
130
    public String getSlideLabel(long slideId){
131
    	if(slideId == 0){
132
    		return "Total Score";
133
    	}
134
    	try {
135
			return Catalog.getInstance().getDefinitionsContainer().getSlideDefinition(slideId).getLabel();
136
		} catch (Exception e) {
137
			e.printStackTrace();
138
		}
139
		return "";
140
    }
141
    public String update() throws Exception {
142
    	Map<Long, Double> customSlideScores = new HashMap<Long, Double>();
143
    	for (String slideStr :this.reqparams.keySet()){
144
    		if(slideStr.equals("id") || this.reqparams.get(slideStr)[0].equals("")){
145
    			continue;
146
    		}
147
    		Long slideId = Long.parseLong(slideStr);
148
    		double score = Double.parseDouble(this.reqparams.get(slideStr)[0]);
149
    		customSlideScores.put(slideId, score	);
150
    	}
151
		CreationUtils.storeCustomSlideComparisonScores(this.entity.getID(), customSlideScores );
152
    	setUrl("/cmp-scores/" + this.id);
153
    	return "redirect";
154
    }
155
 
156
	public void setUrl(String url) {
157
		this.url = url;
158
	}
159
 
160
	public String getUrl() {
161
		return this.url;
162
	}
163
}