1
2
3
4
5
6 package org.weda.store.impl;
7
8 import org.weda.enhance.InjectHivemindObject;
9 import org.weda.property.ObjectDescriptorRegistry;
10 import org.weda.property.PropertyDescriptor;
11 import org.weda.property.PropertyValue;
12 import org.weda.store.SummaryFunction;
13 import org.weda.store.SummaryFunctionRegistry;
14 import org.weda.store.SummaryFunctionRegistryException;
15
16 /**
17 *
18 * @author Mikhail Titov
19 */
20 public class SummaryElement implements Cloneable{
21 private String functionName;
22 private SummaryFunction function;
23 private String propertyName;
24 private Class propertyClass;
25 private int objectPosition = -1;
26 private Class objectClass;
27 private Integer getterId;
28 @InjectHivemindObject()
29 private static SummaryFunctionRegistry functionRegistry;
30 @InjectHivemindObject()
31 private static PropertyValue propertyValue;
32 @InjectHivemindObject()
33 private static ObjectDescriptorRegistry descRegistry;
34
35 public void init() throws SummaryElementException {
36 try{
37 if (objectPosition<0)
38 throw new SummaryElementException(
39 String.format(
40 "The value (%s) of the property (objectPosition) " +
41 "must be positive integer number", objectPosition));
42 function = functionRegistry.getSummaryFunction(functionName);
43 if (propertyName==null){
44 propertyClass = objectClass;
45 }else{
46 getterId =
47 propertyValue.compileGetter(objectClass, propertyName);
48 PropertyDescriptor desc =
49 descRegistry.getPropertyDescriptor(
50 objectClass, propertyName);
51 propertyClass = desc.getPropertyClass();
52 }
53 }catch(Exception e){
54 throw new SummaryElementException(
55 String.format(
56 "Error while initializing summary element " +
57 "(object position: %d, object class: %s, " +
58 "property name: %s)"
59 , objectPosition, objectClass.getName(), propertyName)
60 , e);
61 }
62 }
63
64 public void startCalculation() throws SummaryElementException {
65 try{
66 function.startCalculation(propertyClass);
67 }catch(Exception e){
68 throw new SummaryElementException(
69 String.format(
70 "Error while starting calculation in summary element " +
71 "(object position: %d, object class: %s, " +
72 "property name: %s)"
73 , objectPosition, objectClass.getName(), propertyName)
74 , e);
75 }
76 }
77
78 public void nextCalculation(Object[] row) throws SummaryElementException
79 {
80 try{
81 if (objectPosition >= row.length)
82 throw new SummaryElementException(
83 String.format(
84 "Invalid object position (%d). " +
85 "Object position must between 0 and %d"
86 , objectPosition, row.length));
87 Object value;
88 if (propertyName!=null)
89 value = propertyValue.getValue(row[objectPosition], getterId);
90 else
91 value = row[objectPosition];
92 function.nextCalculation(value);
93 }catch(Exception e){
94 throw new SummaryElementException(
95 String.format(
96 "Error while calculating next value for " +
97 "summary element " +
98 "(object position: %d, object class: %s, " +
99 "property name: %s)"
100 , objectPosition, objectClass.getName()
101 , propertyName)
102 , e);
103 }
104 }
105
106 public void finishCalculation(){
107 function.finishCalculation();
108 }
109
110 public String getFunctionName() {
111 return functionName;
112 }
113
114 public void setFunctionName(String functionName) {
115 this.functionName = functionName;
116 }
117
118 public SummaryFunction getFunction() {
119 return function;
120 }
121
122 public void setFunction(SummaryFunction function) {
123 this.function = function;
124 }
125
126 public String getPropertyName() {
127 return propertyName;
128 }
129
130 public void setPropertyName(String propertyName) {
131 this.propertyName = propertyName;
132 }
133
134 public int getObjectPosition() {
135 return objectPosition;
136 }
137
138 public void setObjectPosition(int objectPosition) {
139 this.objectPosition = objectPosition;
140 }
141
142 public Integer getGetterId() {
143 return getterId;
144 }
145
146 public void setGetterId(Integer getterId) {
147 this.getterId = getterId;
148 }
149
150 public Object clone()
151 throws CloneNotSupportedException
152 {
153 SummaryElement clone = (SummaryElement)super.clone();
154 return clone;
155 }
156
157 public Class getObjectClass() {
158 return objectClass;
159 }
160
161 public void setObjectClass(Class objectClass) {
162 this.objectClass = objectClass;
163 }
164 }