001/**
002 *  '$Author: jianwu $'
003 *  '$Date: 2013-04-10 17:13:14 +0000 (Wed, 10 Apr 2013) $'
004 *  '$Revision: 31883 $'
005 *
006 *  For Details:
007 *  http://www.kepler-project.org
008 *
009 *  Copyright (c) 2009-2010 The Regents of the
010 *  University of California. All rights reserved. Permission is hereby granted,
011 *  without written agreement and without license or royalty fees, to use, copy,
012 *  modify, and distribute this software and its documentation for any purpose,
013 *  provided that the above copyright notice and the following two paragraphs
014 *  appear in all copies of this software. IN NO EVENT SHALL THE UNIVERSITY OF
015 *  CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL,
016 *  OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
017 *  DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE
018 *  POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY
019 *  DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
020 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
021 *  SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
022 *  CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
023 *  ENHANCEMENTS, OR MODIFICATIONS.
024 */
025
026package org.kepler.workflowrunmanager.gui;
027
028import java.util.ArrayList;
029
030import javax.swing.table.AbstractTableModel;
031
032import org.apache.commons.logging.Log;
033import org.apache.commons.logging.LogFactory;
034import org.kepler.authentication.AuthenticationException;
035import org.kepler.util.WorkflowRun;
036import org.kepler.workflowrunmanager.WRMDefaults;
037import org.kepler.workflowrunmanager.WorkflowRunManager;
038import org.kepler.workflowrunmanager.WorkflowRunManagerManager;
039
040import ptolemy.actor.gui.TableauFrame;
041
042public class WorkflowRunManagerTableModel extends AbstractTableModel {
043
044        private WRMDefaults wrmdefaults = WRMDefaults.getInstance();
045        private ArrayList<WorkflowRun> workflowRuns;
046        private TableauFrame tableauFrame = null;
047
048        private static final Log log = LogFactory
049                        .getLog(WorkflowRunManagerTableModel.class.getName());
050        
051        /**
052         * 
053         * @param frame
054         */
055        public WorkflowRunManagerTableModel(TableauFrame frame) {
056                tableauFrame = frame;
057        }
058
059        /**
060         * 
061         * @param workflowName
062         * @param userName
063         * @param startTimeSearchString
064         * @param durationSearchString
065         * @param execIdSearchString
066         * @param tagSearchString
067         * @throws AuthenticationException 
068         */
069        public void updateDataUsingWRMQuery(String workflowName, String userName,
070                        String startTimeSearchString, String durationSearchString,
071                        String execIdSearchString, String tagSearchString, boolean authenticate) throws AuthenticationException {
072
073                WorkflowRunManager workflowRunManager = WorkflowRunManagerManager.getInstance().getWRM(tableauFrame);
074                workflowRuns = workflowRunManager.queryForAndSetRuns(workflowName, userName,
075                                startTimeSearchString, durationSearchString,
076                                execIdSearchString, tagSearchString, authenticate);
077                log.debug("queryForAndSetRuns returned " + workflowRuns.size() + " runs");
078                
079                fireTableDataChanged();
080        }
081        
082        /**
083         * Clear all the runs. This should probably only be called by WorkflowRunManager.clear().
084         */
085        public void clearAllRuns() {
086
087                if (workflowRuns != null){
088                        workflowRuns.clear();
089                        fireTableDataChanged();
090                }
091        }
092        
093        /**
094         * Filter entire list of workflow runs (kept in workflowRunManager) so that we may
095         * display only those runs that pass the filter.
096         * @param wfNameSearchString
097         * @param userSearchString
098         * @param startTimeSearchString
099         * @param durationSearchString
100         * @param execIdSearchString
101         * @param tagsSearchString
102         */
103        public void filterRuns(String wfNameSearchString, String userSearchString,
104                        String startTimeSearchString, String durationSearchString,
105                        String execIdSearchString, String tagsSearchString) {
106                
107                WorkflowRunManager workflowRunManager = WorkflowRunManagerManager.getInstance().getWRM(tableauFrame);
108                if (workflowRunManager != null){
109                        workflowRuns = workflowRunManager.filterRuns(wfNameSearchString, userSearchString,
110                                startTimeSearchString, durationSearchString,
111                                execIdSearchString, tagsSearchString);
112                
113                        fireTableDataChanged();
114                }
115                else{
116                        System.out.println("WARN WorkflowRunManagerTableModel workflowRunManager is null for frame:"+tableauFrame.getName()
117                                        +" Something probably wasn't disposed of properly -- is WorkflowRunManagerPanel.windowClosed still being called?");
118                }
119        }
120        
121        
122        public int getColumnCount() {
123                return wrmdefaults.getColumnCount();
124        }
125
126        public int getRowCount() {
127                if (workflowRuns == null) {
128                        return 0;
129                }
130                return workflowRuns.size();
131        }
132
133        public Object getValueAt(int row, int col) {
134
135                try {
136                        WorkflowRun run = workflowRuns.get(row);
137
138                        if (col == wrmdefaults.getDefaultColIndex(WRMDefaults.TAGS)) {
139                                return run.getTagsAsFormattedString();
140                        }
141                        if (col == wrmdefaults
142                                        .getDefaultColIndex(WRMDefaults.WORKFLOW_NAME)) {
143                                return run.getWorkflowName();
144                        } else if (col == wrmdefaults
145                                        .getDefaultColIndex(WRMDefaults.START_TIME)) {
146                                return run.getStartTime();
147                        } else if (col == wrmdefaults
148                                        .getDefaultColIndex(WRMDefaults.DURATION)) {
149                                return run.getDuration();
150                        } else if (col == wrmdefaults.getDefaultColIndex(WRMDefaults.USER)) {
151                                return run.getUser();
152                        } else if (col == wrmdefaults
153                                        .getDefaultColIndex(WRMDefaults.EXEC_ID)) {
154                                return run.getExecId();
155                        }
156                } catch (Exception e) {
157                        // TODO Auto-generated catch block
158                        //e.printStackTrace();
159                }
160
161                return null;
162        }
163
164        // /public void setValueAt(Object value, int row, int col) {
165        // / fireTableCellUpdated(row, col);
166        // /}
167
168        // public void tableChanged(TableModelEvent e) {
169        // System.out.println("WorkflowRunManagerTableModel tableChanged event");
170        // TODO Auto-generated method stub
171        // }
172
173        public boolean isCellEditable(int rowIndex, int columnIndex) {
174                return false;
175        }
176
177        /*
178         * JTable uses this method to determine the default renderer/ editor for
179         * each cell.
180         */
181        public Class getColumnClass(int c) {
182                return getValueAt(0, c).getClass();
183        }
184
185        /**
186         * get workflowRun(s) for rows
187         * 
188         * @param rows
189         * @return
190         */
191        public ArrayList<WorkflowRun> getWorkflowRunsForRows(int[] rows) {
192                ArrayList<WorkflowRun> runs = new ArrayList<WorkflowRun>();
193                WorkflowRun run = null;
194                for (int i = 0; i < rows.length; i++) {
195                        // System.out.println("getWorkflowRunsFromMap rows["+i+"]:"+rows[i]);
196
197                        try{
198                                run = workflowRuns.get(rows[i]);
199                        }
200                        catch(IndexOutOfBoundsException iobe){
201                                System.out.println("WorkflowRunManagerTableModel IndexOutOfBoundsException trying to get workflowRuns for row:"+rows[i]);
202                                //iobe.printStackTrace();
203                        }
204                        runs.add(run);
205                }
206                return runs;
207        }
208
209        /**
210         * get workflowRun for row
211         * 
212         * @param row
213         * @return
214         */
215        public WorkflowRun getWorkflowRunForRow(int row) {
216                WorkflowRun run = workflowRuns.get(row);
217                return run;
218        }
219
220}