Subversion Repositories SmartDukaan

Rev

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