View Javadoc

1   /*
2    * SummaryRowImpl.java
3    * Created on 24 Июль 2006 г., 22:34
4    */
5   
6   package org.weda.store.impl;
7   
8   import java.util.ArrayList;
9   import java.util.HashMap;
10  import java.util.List;
11  import java.util.Map;
12  import org.weda.store.ObjectSource;
13  import org.weda.store.ObjectSourceListener;
14  import org.weda.store.RowsChangeEvent;
15  import org.weda.store.SummaryRow;
16  import org.weda.store.SummaryRowException;
17  
18  /**
19   *
20   * @author Mikhail Titov
21   */
22  public class SummaryRowImpl 
23          implements SummaryRow, ObjectSourceListener, Cloneable
24  {    
25      List<SummaryElement> elements;
26      private Map<
27              Integer/*object position*/
28              , Map<String/*property name*/, SummaryElement>> mapedElements;
29      private boolean recalculate = true;
30      private ObjectSource objectSource;
31      
32      public void init() throws SummaryRowException {
33          try{
34              mapedElements = new HashMap<Integer, Map<String, SummaryElement>>();
35              if (elements!=null){
36                  for (SummaryElement element: elements){
37                      if (element.getObjectPosition()==-1){
38                          element.setObjectPosition(
39                                  objectSource.getBaseClassPosition());
40                          element.setObjectClass(objectSource.getBaseClass());
41                      }
42                      //
43                      element.init();
44                      //
45                      Map<String, SummaryElement> map = 
46                              mapedElements.get(element.getObjectPosition());
47                      if (map==null){
48                          map = new HashMap<String, SummaryElement>();
49                          mapedElements.put(element.getObjectPosition(), map);
50                      }
51                      map.put(element.getPropertyName(), element);
52                  }
53              }
54              objectSource.addListener(this);
55          }catch(Exception e){
56              throw new SummaryRowException(
57                      "Error while initializing summary row", e);
58          }
59      }
60      
61      public boolean hasElement(int objectPosition, String propertyName) {
62          Map<String, SummaryElement> map = mapedElements.get(objectPosition);
63          if (map != null)
64              return map.containsKey(propertyName);
65          else
66              return false;
67      }
68      
69      public Object getResultValue(int objectPosition, String propertyName)
70          throws SummaryRowException
71      {
72          try{
73              if (recalculate)
74                  makeCalculations();
75              return mapedElements.get(objectPosition).get(propertyName)
76                          .getFunction().getResultValue();
77          }catch(Exception e){
78              throw new SummaryRowException(
79                      String.format(
80                          "Error while getting summary value for property (%s)" +
81                          " of object in position (%d)"
82                          , propertyName, objectPosition)
83                      , e);
84          }
85      }
86      
87      private void makeCalculations() throws SummaryElementException {
88          for (SummaryElement element: elements)
89              element.startCalculation();
90          for (int i=0; i<objectSource.getRowCount(); i++)
91              for (SummaryElement element: elements)
92                  element.nextCalculation(objectSource.getRowAt(i));
93          for (SummaryElement element: elements)
94              element.finishCalculation();
95          recalculate = false;
96      }
97  
98      public void rowsChanged(RowsChangeEvent event) {
99          recalculate = true;
100     }
101     
102     public void addSummaryElement(SummaryElement element){
103         if (elements==null)
104             elements = new ArrayList<SummaryElement>();
105         elements.add(element);
106     }
107 
108     public Object clone() throws CloneNotSupportedException {
109         SummaryRowImpl clone = new SummaryRowImpl();
110         if (elements!=null){
111             clone.elements = new ArrayList<SummaryElement>(elements.size());
112             for (SummaryElement element: elements)
113                 clone.elements.add((SummaryElement)element.clone());
114         }
115         return clone;
116     }
117 
118     public ObjectSource getObjectSource() {
119         return objectSource;
120     }
121 
122     public void setObjectSource(ObjectSource objectSource) {
123         this.objectSource = objectSource;
124     }
125 
126     public void rowsChanging(RowsChangeEvent event) {
127     }
128 
129 }