View Javadoc
1   /**
2    * @author mchyzer
3    * $Id$
4    */
5   package edu.internet2.middleware.grouperClient.jdbc.tableSync;
6   
7   import java.util.ArrayList;
8   import java.util.HashMap;
9   import java.util.HashSet;
10  import java.util.LinkedHashSet;
11  import java.util.List;
12  import java.util.Map;
13  import java.util.Set;
14  
15  import edu.internet2.middleware.grouperClient.collections.MultiKey;
16  import edu.internet2.middleware.grouperClient.util.GrouperClientUtils;
17  
18  
19  /**
20   * data from a table
21   */
22  public class GcTableSyncTableData {
23  
24    /**
25     * take the data and find the max incremental progress value
26     * @param progressColumn
27     * @return the max value
28     */
29    public Object maxProgressValue(GcTableSyncColumnMetadata progressColumnMetadata) {
30      
31      if (GrouperClientUtils.length(this.rows) == 0) {
32        return null;
33      }
34      
35      Object maxIncrementalAllColumnsValue = null;
36      
37      for (GcTableSyncRowData gcTableSyncRowData : this.rows) {
38        Object currentIncrementalAllColumnsValue = gcTableSyncRowData.incrementalProgressValue(progressColumnMetadata);
39        if (maxIncrementalAllColumnsValue == null || ((Comparable)currentIncrementalAllColumnsValue).compareTo(maxIncrementalAllColumnsValue) > 0) {
40          maxIncrementalAllColumnsValue = currentIncrementalAllColumnsValue;
41        }
42      }
43      return maxIncrementalAllColumnsValue;
44    }
45    
46    /**
47     * column metadata (might be a subset of all columns)
48     */
49    private List<GcTableSyncColumnMetadata> columnMetadata;
50    
51    /**
52     * column metadata (might be a subset of all columns)
53     * @return columns
54     */
55    public List<GcTableSyncColumnMetadata> getColumnMetadata() {
56      return this.columnMetadata;
57    }
58  
59    /**
60     * column metadata (might be a subset of all columns)
61     * @param columnMetadata1
62     */
63    public void setColumnMetadata(List<GcTableSyncColumnMetadata> columnMetadata1) {
64      this.columnMetadata = columnMetadata1;
65    }
66  
67    /**
68     * construct
69     * @param gcTableSyncTableBean1
70     * @param data
71     */
72    public void init(GcTableSyncTableBean gcTableSyncTableBean1, List<GcTableSyncColumnMetadata> columnMetadata1, List<Object[]> data) {
73      this.gcTableSyncTableBean = gcTableSyncTableBean1;
74      this.columnMetadata = columnMetadata1;
75      this.indexByPrimaryKey = null;
76      this.rows = new ArrayList<GcTableSyncRowData>();
77      
78      for (Object[] row : GrouperClientUtils.nonNull(data)) {
79        
80        GcTableSyncRowDatableSync/GcTableSyncRowData.html#GcTableSyncRowData">GcTableSyncRowData gcTableSyncRowData = new GcTableSyncRowData();
81        gcTableSyncRowData.setGcTableSyncTableData(this);
82        gcTableSyncRowData.setData(row);
83        this.rows.add(gcTableSyncRowData);
84        
85      }
86      
87    }
88    
89    /**
90     * construct
91     * @param gcTableSyncTableBean1
92     * @param data
93     */
94    public void init(GcTableSyncTableBean gcTableSyncTableBean1, List<GcTableSyncColumnMetadata> columnMetadata1, Map<MultiKey, GcTableSyncRowData> data) {
95      this.gcTableSyncTableBean = gcTableSyncTableBean1;
96      this.columnMetadata = columnMetadata1;
97      this.indexByPrimaryKey = null;
98      this.rows = new ArrayList<GcTableSyncRowData>();
99      this.indexByPrimaryKey = data;
100     
101     for (MultiKey primaryKey : data.keySet()) {
102       GcTableSyncRowData gcTableSyncRowData = data.get(primaryKey);
103       this.rows.add(gcTableSyncRowData);
104     }
105     
106   }
107   
108   /**
109    * link back up to table bean
110    */
111   private GcTableSyncTableBean gcTableSyncTableBean;
112   
113   /**
114    * link back up to table bean
115    * @return the gcTableSyncTableBean
116    */
117   public GcTableSyncTableBean getGcTableSyncTableBean() {
118     return this.gcTableSyncTableBean;
119   }
120 
121   
122   /**
123    * link back up to table bean
124    * @param gcTableSyncTableBean1 the gcTableSyncTableBean to set
125    */
126   public void setGcTableSyncTableBean(GcTableSyncTableBean gcTableSyncTableBean1) {
127     this.gcTableSyncTableBean = gcTableSyncTableBean1;
128   }
129 
130   /**
131    * index the data by primary key
132    */
133   private Map<MultiKey, GcTableSyncRowData> indexByPrimaryKey = null;
134 
135   /**
136    * 
137    * @return the multikeys
138    */
139   public Set<MultiKey> allPrimaryKeys() {
140     
141     if (this.indexByPrimaryKey == null) {
142       this.indexData();
143     }
144     
145     return this.indexByPrimaryKey.keySet();
146     
147   }
148   
149   /**
150    * 
151    * @return the multikeys
152    */
153   public Set<MultiKey> allDataInColumns(List<GcTableSyncColumnMetadata> gcTableSyncColumnMetadatas) {
154 
155     Set<MultiKey> results = new LinkedHashSet<MultiKey>();
156     
157     GcTableSyncColumnMetadataumnMetadata.html#GcTableSyncColumnMetadata">GcTableSyncColumnMetadata[] gcTableSyncColumnMetadataThises = new GcTableSyncColumnMetadata[GrouperClientUtils.length(gcTableSyncColumnMetadatas)];
158     for (int i=0; i<GrouperClientUtils.length(gcTableSyncColumnMetadatas); i++) {
159       
160       GcTableSyncColumnMetadata gcTableSyncColumnMetadataOther = gcTableSyncColumnMetadatas.get(i);
161 
162       GcTableSyncColumnMetadata gcTableSyncColumnMetadataThis = 
163           this.getGcTableSyncTableBean().getTableMetadata().lookupColumn(gcTableSyncColumnMetadataOther.getColumnName(), true);
164       
165       gcTableSyncColumnMetadataThises[i] = gcTableSyncColumnMetadataThis;
166       
167     }
168 
169     for (GcTableSyncRowData row : GrouperClientUtils.nonNull(this.rows)) {
170       Object[] values = new Object[GrouperClientUtils.length(gcTableSyncColumnMetadatas)];
171       int i=0;
172       for (GcTableSyncColumnMetadata gcTableSyncColumnMetadata : gcTableSyncColumnMetadataThises) {
173         values[i++] = row.getData()[row.lookupColumnToIndexZeroIndexed(gcTableSyncColumnMetadata.getColumnName(), true)];
174       }
175       MultiKeyrouperClient/collections/MultiKey.html#MultiKey">MultiKey multiKey = new MultiKey(values);
176       results.add(multiKey);
177     }
178 
179     return results;
180     
181   }
182   
183   /**
184    * 
185    * @return the multikeys
186    */
187   public Map<MultiKey, GcTableSyncRowData> allIndexByPrimaryKey() {
188     
189     if (this.indexByPrimaryKey == null) {
190       this.indexData();
191     }
192     
193     return this.indexByPrimaryKey;
194     
195   }
196   
197   /**
198    * index the data by primary key
199    */
200   public void indexData() {
201     
202     this.indexByPrimaryKey = new HashMap<MultiKey, GcTableSyncRowData>();
203     
204     // are we looking at the from side? this is not a good test, we need to set a boolean if the from and to are same
205     // TODO in GcTableSyncFromData.sync()
206     boolean isFrom = this.gcTableSyncTableBean != null && this.gcTableSyncTableBean == this.gcTableSyncTableBean.getGcTableSync().getDataBeanFrom();
207       
208     for (GcTableSyncRowData row : GrouperClientUtils.nonNull(this.rows)) {
209       
210       MultiKey rowPrimaryKey = row.getPrimaryKey();
211       
212       if (this.indexByPrimaryKey.containsKey(rowPrimaryKey) && isFrom) {
213         GrouperClientUtils.mapAddValue(this.gcTableSyncTableBean.getGcTableSync().getDebugMap(), "duplicateSourceKeyCount", 1);
214       }
215       
216       this.indexByPrimaryKey.put(rowPrimaryKey, row);
217     }
218     
219   }
220 
221   /**
222    * if just selecting groups, these are the groupings
223    * @return the multikeys
224    */
225   public Set<Object> allGroupings() {
226     
227     Set<Object> groupings = new HashSet<Object>();
228     
229     for (GcTableSyncRowData row : GrouperClientUtils.nonNull(this.rows)) {
230       Object[] rowData = row.getData();
231       
232       if (GrouperClientUtils.length(rowData) > 1) {
233         throw new RuntimeException("Expecting 1 column for groupings, but was: " + GrouperClientUtils.length(rowData));
234       }
235       Object grouping = rowData[0];
236       if (grouping == null) {
237         throw new RuntimeException("Expecting non null grouping but was null");
238       }
239       groupings.add(grouping);
240     }
241     
242     return groupings;
243     
244   }
245   
246 
247   /**
248    * @param primaryKey
249    * @return the row
250    */
251   public GcTableSyncRowData findRowFromPrimaryKey(MultiKey primaryKey) {
252     if (this.indexByPrimaryKey == null) {
253       this.indexData();
254     }
255     return this.indexByPrimaryKey.get(primaryKey);
256   }
257   
258   /**
259    * 
260    */
261   public GcTableSyncTableData() {
262   }
263 
264   /**
265    * row data
266    */
267   private List<GcTableSyncRowData> rows;
268   
269   /**
270    * row data
271    * @return the rows
272    */
273   public List<GcTableSyncRowData> getRows() {
274     return this.rows;
275   }
276 
277   
278   /**
279    * row data
280    * @param rows1 the rows to set
281    */
282   public void setRows(List<GcTableSyncRowData> rows1) {
283     this.rows = rows1;
284   }
285   
286   
287 }