44import DTOs .Relations .SourceRelationDTO ;
55import DTOs .Relations .TargetRelationDTO ;
66import com .upc .gessi .qrapids .app .config .QMAConnection ;
7+ import com .upc .gessi .qrapids .app .domain .controllers .QualityFactorsController ;
78import com .upc .gessi .qrapids .app .domain .controllers .StrategicIndicatorsController ;
9+ import com .upc .gessi .qrapids .app .domain .exceptions .CategoriesException ;
10+ import com .upc .gessi .qrapids .app .presentation .rest .dto .DTOMetric ;
11+ import com .upc .gessi .qrapids .app .presentation .rest .dto .DTOQualityFactor ;
12+ import com .upc .gessi .qrapids .app .presentation .rest .dto .DTOStrategicIndicatorEvaluation ;
813import com .upc .gessi .qrapids .app .presentation .rest .dto .relations .DTORelationsFactor ;
914import com .upc .gessi .qrapids .app .presentation .rest .dto .relations .DTORelationsMetric ;
1015import com .upc .gessi .qrapids .app .presentation .rest .dto .relations .DTORelationsSI ;
@@ -29,6 +34,9 @@ public class QMARelations {
2934 @ Autowired
3035 private StrategicIndicatorsController strategicIndicatorsController ;
3136
37+ @ Autowired
38+ private QualityFactorsController qualityFactorsController ;
39+
3240 private static final String SI_TYPE = "strategic_indicators" ;
3341 private static final String FACTORS_TYPE = "factors" ;
3442 private static final String METRICS_TYPE = "metrics" ;
@@ -60,18 +68,21 @@ private double[] convertFloatListToDoubleArray(List<Float> floatList) {
6068 return doubleArray ;
6169 }
6270
63- public List <DTORelationsSI > getRelations (String prj , LocalDate date ) throws IOException {
71+ public List <DTORelationsSI > getRelations (String prj , LocalDate date ) throws IOException , CategoriesException , ArithmeticException {
6472 qmacon .initConnexion ();
6573 List <RelationDTO > relationDTOS ;
74+ // get relations from elasticsearch
6675 if (date == null )
6776 relationDTOS = Relations .getRelations (prj );
6877 else
6978 relationDTOS = Relations .getRelations (prj , date );
70-
71- return RelationDTOToDTORelationSI (relationDTOS );
79+ // get current evaluations for SI and Quality Factors
80+ List <DTOStrategicIndicatorEvaluation > siEval = strategicIndicatorsController .getAllStrategicIndicatorsCurrentEvaluation (prj );
81+ List <DTOQualityFactor > qfEval = qualityFactorsController .getAllFactorsWithMetricsCurrentEvaluation (prj );
82+ return RelationDTOToDTORelationSI (relationDTOS , siEval , qfEval );
7283 }
7384
74- private List <DTORelationsSI > RelationDTOToDTORelationSI (List <RelationDTO > relationDTOS ) {
85+ private List <DTORelationsSI > RelationDTOToDTORelationSI (List <RelationDTO > relationDTOS , List < DTOStrategicIndicatorEvaluation > siEval , List < DTOQualityFactor > qfEval ) throws ArithmeticException {
7586 Map <String , DTORelationsSI > strategicIndicatorsMap = new HashMap <>();
7687 Map <String , DTORelationsFactor > factorsMap = new HashMap <>();
7788 Map <String , DTORelationsMetric > metricsMap = new HashMap <>();
@@ -81,22 +92,60 @@ private List<DTORelationsSI> RelationDTOToDTORelationSI (List<RelationDTO> relat
8192 SourceRelationDTO source = relation .getSource ();
8293 TargetRelationDTO target = relation .getTarget ();
8394 if (target .getType ().equals (FACTORS_TYPE ) && source .getType ().equals (METRICS_TYPE )) {
84- buildFactorMetricRelation (factorsMap , metricsMap , weight , source , target );
95+ buildFactorMetricRelation (factorsMap , metricsMap , weight , source , target , qfEval );
8596 }
8697 else if (target .getType ().equals (SI_TYPE ) && source .getType ().equals (FACTORS_TYPE )) {
87- buildSIFactorRelation (strategicIndicatorsMap , factorsMap , weight , source , target );
98+ buildSIFactorRelation (strategicIndicatorsMap , factorsMap , weight , source , target , siEval , qfEval );
8899 }
89100 }
90101
91- return new ArrayList <>(strategicIndicatorsMap .values ());
102+ List <DTORelationsSI > result = new ArrayList <>(strategicIndicatorsMap .values ());
103+ for (DTORelationsSI si : result ) {
104+ for (DTORelationsFactor f : si .getFactors ()) {
105+ // Define weight & weightedValue for factors legacy cases (0 & 1)
106+ float w = Float .parseFloat (f .getWeight ());
107+ if (w == 0f || w == 1f ) {
108+ float nFactors = si .getFactors ().size ();
109+ w = 1 /nFactors ;
110+ f .setWeight (String .valueOf (w ));
111+ f .setWeightedValue (String .valueOf (Float .parseFloat (f .getAssessmentValue ()) * Float .parseFloat (f .getWeight ())));
112+ } else if (w == -1 ) { // Define weightedValue for factor with BN case (-1)
113+ f .setWeightedValue (String .valueOf (Float .parseFloat (f .getAssessmentValue ()) * 1 /si .getFactors ().size ()));
114+ }
115+ float sum = sumMetricsWeights (f .getMetrics ());
116+ for (DTORelationsMetric m : f .getMetrics ()) {
117+ // Define weight percentage & weightedValue for metrics
118+ if (sum != 0 ) {
119+ m .setWeight (String .valueOf ((Float .parseFloat (m .getWeight ())/sum )));
120+ m .setWeightedValue (String .valueOf (Float .parseFloat (m .getAssessmentValue ())*Float .parseFloat (m .getWeight ())));
121+ } else {
122+ throw new ArithmeticException ("/ by 0: sum of metrics weights is zero." );
123+ }
124+ }
125+ }
126+ }
127+ return result ;
92128 }
93129
94- private void buildSIFactorRelation (Map <String , DTORelationsSI > strategicIndicatorsMap , Map <String , DTORelationsFactor > factorsMap , String weight , SourceRelationDTO source , TargetRelationDTO target ) {
130+ private float sumMetricsWeights (List <DTORelationsMetric > metrics ) {
131+ float totalWeight = 0f ;
132+ for (int i = 0 ; i < metrics .size (); i ++){
133+ totalWeight += Float .parseFloat (metrics .get (i ).getWeight ());
134+ }
135+ return totalWeight ;
136+ }
137+
138+ private void buildSIFactorRelation (Map <String , DTORelationsSI > strategicIndicatorsMap , Map <String , DTORelationsFactor > factorsMap , String weight , SourceRelationDTO source , TargetRelationDTO target , List <DTOStrategicIndicatorEvaluation > siEval , List <DTOQualityFactor > qfEval ) {
95139 DTORelationsSI strategicIndicator ;
140+ DTOStrategicIndicatorEvaluation thisSI = siEval .stream ()
141+ .filter (si -> target .getID ().equals (si .getId ()))
142+ .findAny ()
143+ .orElse (null );
96144 if (strategicIndicatorsMap .containsKey (target .getID ())) {
97145 strategicIndicator = strategicIndicatorsMap .get (target .getID ());
98146 } else {
99147 strategicIndicator = new DTORelationsSI (target .getID ());
148+ strategicIndicator .setName (thisSI .getName ());
100149 strategicIndicatorsMap .put (target .getID (), strategicIndicator );
101150 }
102151 strategicIndicator .setValue (target .getValue ());
@@ -106,8 +155,7 @@ private void buildSIFactorRelation(Map<String, DTORelationsSI> strategicIndicato
106155 String valueDescription = StrategicIndicatorsController .buildDescriptiveLabelAndValue (Pair .of (value , label ));
107156 strategicIndicator .setValueDescription (valueDescription );
108157 strategicIndicator .setColor (strategicIndicatorsController .getColorFromLabel (label ));
109- }
110- catch (NumberFormatException nfe ) {
158+ } catch (NumberFormatException nfe ) {
111159 String label = strategicIndicator .getValue ();
112160 Float value = strategicIndicatorsController .getValueFromLabel (label );
113161 String valueDescription = StrategicIndicatorsController .buildDescriptiveLabelAndValue (Pair .of (value , label ));
@@ -116,36 +164,61 @@ private void buildSIFactorRelation(Map<String, DTORelationsSI> strategicIndicato
116164 }
117165
118166 DTORelationsFactor factor ;
167+ DTOQualityFactor thisFactor = qfEval .stream ()
168+ .filter (qf -> source .getID ().equals (qf .getId ()))
169+ .findAny ()
170+ .orElse (null );
119171 if (factorsMap .containsKey (source .getID ())) {
120172 factor = factorsMap .get (source .getID ());
121173 } else {
122174 factor = new DTORelationsFactor (source .getID ());
175+ factor .setName (thisFactor .getName ());
123176 factorsMap .put (source .getID (), factor );
124177 }
125- factor .setWeight (weight );
126- factor .setValue (source .getValue ());
178+ // Special cases
179+ if (Float .parseFloat (weight ) == 0f || Float .parseFloat (weight ) == 1f || Float .parseFloat (weight ) == -1f ) {
180+ factor .setWeight (weight );
181+ factor .setWeightedValue (source .getValue ());
182+ factor .setAssessmentValue (source .getValue ());
183+ } else {
184+ factor .setWeight (weight );
185+ factor .setWeightedValue (source .getValue ());
186+ factor .setAssessmentValue (String .valueOf (Float .parseFloat (source .getValue ())/Float .parseFloat (weight )));
187+ }
127188
128189 strategicIndicator .setFactor (new DTORelationsFactor (factor ));
129190 }
130191
131- private void buildFactorMetricRelation (Map <String , DTORelationsFactor > factorsMap , Map <String , DTORelationsMetric > metricsMap , String weight , SourceRelationDTO source , TargetRelationDTO target ) {
192+ private void buildFactorMetricRelation (Map <String , DTORelationsFactor > factorsMap , Map <String , DTORelationsMetric > metricsMap , String weight , SourceRelationDTO source , TargetRelationDTO target , List < DTOQualityFactor > qfEval ) {
132193 DTORelationsFactor factor ;
194+ DTOQualityFactor thisFactor = qfEval .stream ()
195+ .filter (qf -> target .getID ().equals (qf .getId ()))
196+ .findAny ()
197+ .orElse (null );
133198 if (factorsMap .containsKey (target .getID ())) {
134199 factor = factorsMap .get (target .getID ());
135200 } else {
136201 factor = new DTORelationsFactor (target .getID ());
202+ factor .setName (thisFactor .getName ());
137203 factorsMap .put (target .getID (), factor );
138204 }
139205
140206 DTORelationsMetric metric ;
207+ List <DTOMetric > metrics = thisFactor .getMetrics ();
208+ DTOMetric thisMetric = metrics .stream ()
209+ .filter (m -> source .getID ().equals (m .getId ()))
210+ .findAny ()
211+ .orElse (null );
141212 if (metricsMap .containsKey (source .getID ())) {
142213 metric = metricsMap .get (source .getID ());
143214 } else {
144215 metric = new DTORelationsMetric (source .getID ());
216+ metric .setName (thisMetric .getName ());
145217 metricsMap .put (source .getID (), metric );
146218 }
147219 metric .setWeight (weight );
148- metric .setValue (source .getValue ());
220+ metric .setWeightedValue (source .getValue ());
221+ metric .setAssessmentValue (thisMetric .getValue ().toString ());
149222
150223 factor .setMetric (new DTORelationsMetric (metric ));
151224 }
0 commit comments