summaryrefslogtreecommitdiffstats
blob: 1eec6a94f62f772e5a4412b040b966ad0f70d836 (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*******************************************************************************
 * Copyright (c) 2011 SunGard CSA LLC and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    SunGard CSA LLC - initial API and implementation and/or initial documentation
 *******************************************************************************/
package org.eclipse.stardust.ui.web.viewscommon.process.history;

import java.util.List;
import java.util.Map;

import javax.faces.component.UICommand;
import javax.faces.event.ActionEvent;
import javax.swing.tree.DefaultTreeModel;

import org.eclipse.stardust.engine.api.runtime.ActivityInstance;
import org.eclipse.stardust.engine.api.runtime.ProcessInstance;
import org.eclipse.stardust.ui.web.common.column.ColumnPreference;
import org.eclipse.stardust.ui.web.common.filter.ITableDataFilterOnOff;
import org.eclipse.stardust.ui.web.common.filter.TableDataFilterOnOff;
import org.eclipse.stardust.ui.web.common.table.export.DataTableExportHandler;
import org.eclipse.stardust.ui.web.common.table.export.ExportType;
import org.eclipse.stardust.ui.web.common.treetable.NodeUserObject;
import org.eclipse.stardust.ui.web.common.treetable.TreeTable;
import org.eclipse.stardust.ui.web.common.treetable.TreeTableBean;
import org.eclipse.stardust.ui.web.common.treetable.TreeTableNode;
import org.eclipse.stardust.ui.web.common.treetable.TreeTableUserObject;
import org.eclipse.stardust.ui.web.viewscommon.common.FilterToolbarItem;
import org.eclipse.stardust.ui.web.viewscommon.descriptors.DescriptorColumnUtils;
import org.eclipse.stardust.ui.web.viewscommon.utils.DefaultColumnModelEventHandler;
import org.eclipse.stardust.ui.web.viewscommon.utils.ProcessInstanceUtils;



/**
 * @author Yogesh.Manware
 *
 */
public class ProcessTreeTable implements TreeTableBean
{
   private static final long serialVersionUID = 1L;
   private ActivityInstance currentActivityInstance;

   // tree default model, used as a value for the tree component
   private DefaultTreeModel model;

   private IProcessHistoryDataModel processHistoryDataModel;
   private IProcessHistoryTableEntry processHistoryTableRoot;
   private List<ProcessInstance> processInstances;
   private ProcessInstance currentProcessInstance;
   private ProcessTableEntryUserObject selectedRow;
   private TreeTable treeTable;
   private TreeTableNode processInstanceNode;
   private final ProcessUIBuilder processUIBuilder;
   private DefaultColumnModelEventHandler columnModelEventHandler;

   /**
    * 
    */
   public ProcessTreeTable()
   {
      columnModelEventHandler = new DefaultColumnModelEventHandler();
      columnModelEventHandler.setNeedRefresh(false);
      processHistoryDataModel = new ProcessHistoryDataModel();
      processUIBuilder = new ProcessUIBuilder(columnModelEventHandler);
      columnModelEventHandler.setNeedRefresh(true);
   }

   /**
    * This needs to be called by Caller to Initialize the Component
    */
   public void initialize()
   {     
      fetchProcessData();     
   }


   /**
    * @param event
    */
   public void filterTable(ActionEvent event)
   {
      UICommand commandObject = (UICommand) event.getComponent();
      Map<String, Object> attributesMap = commandObject.getAttributes();

      String filterName = (String) attributesMap.get("name");

      // Update Filter on UI
      FilterToolbarItem filterToolbarItem = processUIBuilder.getFilterToolbarItem(filterName);
      filterToolbarItem.toggle();

      // Update Data Filters
      ITableDataFilterOnOff onOffFilter = (ITableDataFilterOnOff) processUIBuilder.getOnOffFilters().getDataFilter(
            filterName);
      onOffFilter.toggle();

      treeTable.rebuildList();
   }

   
   /**
    * @param tableEntry
    * @param parent
    * @throws Exception
    */
   @SuppressWarnings("unchecked")
   private void buildProcessHistoryTree(IProcessHistoryTableEntry tableEntry, TreeTableNode parent)
   {
      List<IProcessHistoryTableEntry> listChildren = tableEntry.getChildren();
      if (listChildren != null)
      {
         boolean selectedRowFlag = false;
         for (IProcessHistoryTableEntry childEntry : listChildren)
         {
            TreeTableNode node = TreeNodeFactory.createProcessTreeNode(treeTable, this,
                  (ProcessInstanceHistoryItem) childEntry, childEntry.isNodePathToActivityInstance());
            parent.add(node);
            buildProcessHistoryTree(childEntry, node);
            if (!selectedRowFlag)
            {
               selectedRowFlag = setSelectedRow(node);
            }
         }
      }
   }

   /**
    * @param node
    */
   private boolean setSelectedRow(TreeTableNode node)
   {
      ProcessTableEntryUserObject processTableEntryUserObject = (ProcessTableEntryUserObject) node.getUserObject();

      ProcessInstance selectedProcessInstance = null;

      if (null != selectedRow)
      {
         selectedProcessInstance = selectedRow.getProcessInstance();
      }
      else
      {
         selectedProcessInstance = getCurrentProcessInstance();
      }

      if (processTableEntryUserObject.getProcessInstance().getOID() == selectedProcessInstance.getOID())
      {
         processTableEntryUserObject.setSelected(true);
         selectedRow = processTableEntryUserObject;
         return true;
      }
      return false;
   }

   /**
    * 
    */
   private void fetchProcessData()
   {
      // Works if either Process Instance or Activity Instance is set
      if ((getCurrentProcessInstance() != null) || (getCurrentActivityInstance() != null))
      {
         if (getCurrentProcessInstance() != null)
         {
            processHistoryTableRoot = processHistoryDataModel.getProcessHistoryDataModel(getCurrentProcessInstance(),
                  processInstances, true);
         }
         else
         {
            processHistoryTableRoot = processHistoryDataModel.getProcessHistoryDataModel(getCurrentActivityInstance(),
                  processInstances, true);
         }

         // Root Node
         processInstanceNode = TreeNodeFactory.createProcessTreeNode(treeTable, this,
               (ProcessInstanceHistoryItem) processHistoryTableRoot, true);

         // Now Create a Model & Tree Table
         model = new DefaultTreeModel(processInstanceNode);

         treeTable = new TreeTable(model, processUIBuilder.getProcessColumnFilterPopup(), processUIBuilder.getOnOffFilters());
         treeTable.setDataTableExportHandler(new ProcessTreeTableExportHandler());
         if ((processInstanceNode != null) && (processInstanceNode.getUserObject() != null))
         {
            processInstanceNode.getUserObject().setTreeTable(treeTable);
            setSelectedRow(processInstanceNode);
            // Build Tree
            buildProcessHistoryTree(processHistoryTableRoot, processInstanceNode);
         }
         
         treeTable.setAutoFilter(false);
         columnModelEventHandler.setNeedRefresh(false);
         // Build Tree
         treeTable.initialize();
         columnModelEventHandler.setNeedRefresh(true);

      // reset the filters
         for (FilterToolbarItem filterToolbarItem : processUIBuilder.getProcessFilterToolbarItems())
         {
            ((TableDataFilterOnOff) processUIBuilder.getOnOffFilters().getDataFilter(filterToolbarItem.getName()))
                  .setOn(!filterToolbarItem.isActive());
         }
         
         // Rebuild the Tree
         treeTable.rebuildList();
      }
   }

   /**
    * @author Sidharth.Singh
    * 
    */
   private class ProcessTreeTableExportHandler implements DataTableExportHandler<TreeTableUserObject>
   {
       /*
       * (non-Javadoc)
       * 
       * @see org.eclipse.stardust.ui.web.common.table.export.DataTableExportHandler#
       * handleHeaderCellExport
       * (org.eclipse.stardust.ui.web.common.table.export.DataTableExportHandler.ExportType,
       * org.eclipse.stardust.ui.web.common.column.ColumnPreference, java.lang.String)
       */
      public String handleHeaderCellExport(ExportType exportType, ColumnPreference column, String text)
      {
         return text;
      }
      
      /*
       * (non-Javadoc)
       * 
       * @see
       * org.eclipse.stardust.ui.web.common.table.export.DataTableExportHandler#handleCellExport
       * (org.eclipse.stardust.ui.web.common.table.export.ExportType,
       * org.eclipse.stardust.ui.web.common.column.ColumnPreference, java.lang.Object,
       * java.lang.Object)
       */
      public Object handleCellExport(ExportType exportType, ColumnPreference column, TreeTableUserObject row,
            Object value)
      {
         ProcessTableEntryUserObject thisRow = ((ProcessTableEntryUserObject) row);
         if ("Priority".equals(column.getColumnName()))
         {
            return ProcessInstanceUtils.getPriorityLabel(thisRow.getPriority());
         }
         else if ("Descriptors".equals(column.getColumnName()))
         {
            return DescriptorColumnUtils.exportDescriptors(thisRow.getProcessDescriptorsList(),
                  ExportType.EXCEL == exportType ? "\n" : ", ");
         }
         else
         {
            return value;
         }
      }

   }

   /**
    * 
    * @param node
    * @return
    */
   private boolean isPriorityModified(ProcessInstanceHistoryItem node)
   {
      boolean isPriorityModified = false;
      if (null != node)
      {
         if (node.getOldPriority() != node.getPriority())
         {
            return true;
         }

         List<IProcessHistoryTableEntry> childs = node.getChildren();

         for (IProcessHistoryTableEntry entry : childs)
         {
            if (entry instanceof ProcessInstanceHistoryItem)
            {
               ProcessInstanceHistoryItem item = (ProcessInstanceHistoryItem) entry;
               isPriorityModified = isPriorityModified(item);

               if (isPriorityModified)
               {
                  break;
               }
            }
         }
      }
      return isPriorityModified;
   }   

   public ActivityInstance getCurrentActivityInstance()
   {
      return currentActivityInstance;
   }

   public int getIncreasedLabelCount()
   {
      return 0;
   }

   public IProcessHistoryDataModel getProcessHistoryDataModel()
   {
      return processHistoryDataModel;
   }

   public ProcessTableEntryUserObject getSelectedRow()
   {
      return selectedRow;
   }
   
   public void setSelectedRow(ProcessTableEntryUserObject selectedRow)
   {
      this.selectedRow = selectedRow;
   }

   public boolean isPriorityChanged()
   {
      ProcessInstanceHistoryItem node = (ProcessInstanceHistoryItem) processHistoryTableRoot;

      return isPriorityModified(node);
   }

   public void setCurrentActivityInstance(ActivityInstance currentActivityInstance)
   {
      this.currentActivityInstance = currentActivityInstance;
   }

   public void setCurrentProcessInstance(ProcessInstance currentProcessInstance)
   {
      this.currentProcessInstance = currentProcessInstance;
   }

   public void setProcessHistoryDataModel(IProcessHistoryDataModel processHistoryDataModel)
   {
      this.processHistoryDataModel = processHistoryDataModel;
   }

   public void setProcessInstances(List<ProcessInstance> processInstances)
   {
      this.processInstances = processInstances;
   }

   public void setSelectedNodeLabel(String label)
   {}

   public void setSelectedNodeObject(NodeUserObject nodeObject)
   {}

   public TreeTable getTreeTable()
   {
      return treeTable;
   }

   public IProcessHistoryTableEntry getProcessHistoryTableRoot()
   {
      return processHistoryTableRoot;
   }

   public ProcessInstance getCurrentProcessInstance()
   {
      return currentProcessInstance;
   }

   public ProcessUIBuilder getProcessUIBuilder()
   {
      return processUIBuilder;
   }

   public DefaultColumnModelEventHandler getColumnModelEventHandler()
   {
      return columnModelEventHandler;
   }
   
}