-
Notifications
You must be signed in to change notification settings - Fork 188
/
ProportionalExpressionEvaluator.java
111 lines (94 loc) · 5.02 KB
/
ProportionalExpressionEvaluator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Copyright (c) 2017 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/
package com.evolveum.midpoint.model.common.expression.evaluator;
import javax.xml.namespace.QName;
import com.evolveum.midpoint.prism.*;
import com.evolveum.midpoint.prism.crypto.Protector;
import com.evolveum.midpoint.prism.delta.ItemDeltaUtil;
import com.evolveum.midpoint.prism.delta.PrismValueDeltaSetTriple;
import com.evolveum.midpoint.repo.common.expression.ExpressionEvaluationContext;
import com.evolveum.midpoint.repo.common.expression.evaluator.AbstractExpressionEvaluator;
import com.evolveum.midpoint.schema.constants.ExpressionConstants;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.util.exception.ExpressionEvaluationException;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.IntegerStatType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ProportionalExpressionEvaluatorType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ProportionalStyleType;
/**
* @author skublik
*/
public class ProportionalExpressionEvaluator<V extends PrismValue, D extends ItemDefinition>
extends AbstractExpressionEvaluator<V, D, ProportionalExpressionEvaluatorType> {
ProportionalExpressionEvaluator(QName elementName, ProportionalExpressionEvaluatorType proportionalEvaluatorBean,
D outputDefinition, Protector protector, PrismContext prismContext) {
super(elementName, proportionalEvaluatorBean, outputDefinition, protector, prismContext);
}
@Override
public PrismValueDeltaSetTriple<V> evaluate(ExpressionEvaluationContext context, OperationResult result)
throws SchemaException, ExpressionEvaluationException, ObjectNotFoundException {
ProportionalStyleType style = expressionEvaluatorBean.getStyle();
IntegerStatType integerStatType = context.getVariables().getValue(ExpressionConstants.VAR_INPUT, IntegerStatType.class);
if(integerStatType == null) {
throw new IllegalArgumentException("Proportional expression cannot by evaluated without input of type "
+ IntegerStatType.COMPLEX_TYPE);
}
String numberMessage;
Integer totalItems = integerStatType.getDomain();
Integer actualItems = integerStatType.getValue();
switch (style) {
case PERCENTAGE:
validateInputNumbers(totalItems, actualItems, ProportionalStyleType.PERCENTAGE);
float percentage = (totalItems==0 ? 0 : actualItems*100.0f/totalItems);
String format = "%.0f";
if (percentage < 100.0f && percentage % 10 != 0 && ((percentage % 10) % 1) != 0) {
format = "%.1f";
}
numberMessage = String.format(format, percentage) + " %";
integerStatType.setPercentage(percentage);
break;
case VALUE_OF_DOMAIN:
validateInputNumbers(totalItems, actualItems, ProportionalStyleType.VALUE_OF_DOMAIN);
numberMessage = actualItems + " of " + totalItems;
break;
case VALUE_SLASH_DOMAIN:
validateInputNumbers(totalItems, actualItems, ProportionalStyleType.VALUE_SLASH_DOMAIN);
numberMessage = actualItems + "/" + totalItems;
break;
case VALUE_ONLY:
if(actualItems == null) {
throw new IllegalArgumentException("Proportional expression with " + ProportionalStyleType.VALUE_ONLY.value()
+" style cannot by evaluated without value and domain numbers in input of type " + IntegerStatType.COMPLEX_TYPE);
}
numberMessage = String.valueOf(actualItems);
break;
default:
numberMessage = "";
break;
}
//noinspection unchecked
Item<V, D> output = outputDefinition.instantiate();
if (output instanceof PrismProperty) {
((PrismProperty<String>) output).addRealValue(numberMessage);
} else {
throw new UnsupportedOperationException(
"Can only generate values of property, not " + output.getClass());
}
return ItemDeltaUtil.toDeltaSetTriple(output, null, prismContext);
}
private void validateInputNumbers(Integer totalItems, Integer actualItems, ProportionalStyleType style) {
if (totalItems == null || actualItems == null) {
throw new IllegalArgumentException("Proportional expression with " + style.value() +" style cannot by evaluated"
+ " without value and domain numbers in input of type " + IntegerStatType.COMPLEX_TYPE);
}
}
@Override
public String shortDebugDump() {
return "const:"+expressionEvaluatorBean.getStyle();
}
}