001/*
002 * Copyright (c) 2010-2011 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: jianwu $'
006 * '$Date: 2012-10-10 00:46:40 +0000 (Wed, 10 Oct 2012) $' 
007 * '$Revision: 30851 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.workflowrunmanager;
031
032import java.util.HashSet;
033import java.util.Iterator;
034import java.util.Map;
035import java.util.Set;
036import java.util.WeakHashMap;
037
038import org.kepler.gui.KeplerGraphFrame;
039import org.kepler.gui.KeplerGraphFrame.Components;
040import org.kepler.gui.KeplerGraphFrameUpdater;
041import org.kepler.util.ProvenanceStore;
042
043import ptolemy.actor.gui.TableauFrame;
044
045//singleton, manager for all WorkflowRunManagers
046public final class WorkflowRunManagerManager implements KeplerGraphFrameUpdater {
047
048        // an association of each TableauFrame to a particular WRM (often the same one)
049        private Map<TableauFrame, WorkflowRunManager> wrms = new WeakHashMap<TableauFrame, WorkflowRunManager>(); 
050        
051        static private WorkflowRunManagerManager _instance = null;
052        
053        /**
054         * @return singleton WorkflowRunManagerManager instance
055         */
056        public synchronized static WorkflowRunManagerManager getInstance() {
057                if (_instance == null) {
058                        _instance = new WorkflowRunManagerManager();
059                        // add ourself as an updater so we can remove tableauFrames
060                        // from mapping during dispose events.
061                        KeplerGraphFrame.addUpdater(_instance);
062                }
063                return _instance;
064        }
065        
066        /**
067         * 
068         * @param tableauFrame
069         * @return
070         */
071        public WorkflowRunManager getWRM(TableauFrame tableauFrame){
072                
073                if (wrms.get(tableauFrame) == null){
074                        System.out.println("WorkflowRunManagerManager getWRM - ERROR no wrm found for this TableauFrame!");
075                }
076                //printDebug();
077                return wrms.get(tableauFrame);
078        }
079        
080        public WorkflowRunManager getWRM(TableauFrame tableauFrame, ProvenanceStore provenanceStore){
081                
082                WorkflowRunManager existingWRM = null;
083                for (WorkflowRunManager wrm : wrms.values()){
084                        ProvenanceStore existingProvStore = wrm.getProvenanceStore();
085                        
086                        ///TODO determine why this equality returns false:
087                        ///if (existingProvStore.equals(provenanceStore)){
088                        if (existingProvStore.getType().equals(provenanceStore.getType())){
089                                if (existingProvStore.getType().equals(ProvenanceStore.PROVENANCE_REPOSITORY)){
090                                        if (existingProvStore.getProvenanceConfigurationProperty().
091                                                        equals(provenanceStore.getProvenanceConfigurationProperty())){
092                                                wrms.put(tableauFrame, wrm);
093                                                existingWRM = wrm;
094                                                //printDebug();
095                                                break;
096                                        }
097                                }
098                                else if (existingProvStore.getType().equals(ProvenanceStore.ECOGRID_REPOSITORY)){
099                                        if (existingProvStore.getEcogridRepository().
100                                                        equals(provenanceStore.getEcogridRepository())){
101                                                wrms.put(tableauFrame, wrm);
102                                                existingWRM = wrm;
103                                                //printDebug();
104                                                break;
105                                        }
106                                }
107                        }
108                }
109                
110                if (existingWRM != null){
111                        return existingWRM;
112                }
113                
114                WorkflowRunManager wrm = new WorkflowRunManager(provenanceStore);
115                wrms.put(tableauFrame, wrm);
116                //printDebug();
117                return wrm;
118        }
119        
120        public void printDebug(){
121                System.out.println("--------------------WorkflowRunManagerManager printDebug wrms:--------------------");
122                Iterator<TableauFrame> itr = wrms.keySet().iterator();
123                Iterator<WorkflowRunManager> wrmItr = wrms.values().iterator();
124                Set<WorkflowRunManager> set = new HashSet<WorkflowRunManager>();
125                while (wrmItr.hasNext()){
126                        set.add(wrmItr.next());
127                }
128                int numUniqueWRM = set.size();
129                
130                while (itr.hasNext()){
131                        TableauFrame tf = itr.next();
132                        WorkflowRunManager wrm = wrms.get(tf);
133                        ProvenanceStore provenanceStore = wrm.getProvenanceStore();
134                        if (tf == null && provenanceStore == null){
135                                System.out.println("tf:NULL => wrm(provenanceStore:NULL)");     
136                        }
137                        else if (provenanceStore == null){
138                                System.out.println("tf:"+tf.getName() + " => wrm(provenanceStore:NULL)");       
139                        }
140                        else if (tf == null){
141                                System.out.println("tf:NULL => wrm(provenanceStore type:"+provenanceStore.getType()+")");
142                                if (wrm.getQueryable() != null){
143                                        System.out.println("   with a queryable:"+wrm.getQueryable().getClass());
144                                }
145                        }
146                        else{
147                                System.out.println("tf:"+tf.getName() + " => wrm(provenanceStore type:"+provenanceStore.getType()+")"); 
148                                if (wrm.getQueryable() != null){
149                                        System.out.println("   with a queryable:"+wrm.getQueryable().getClass());
150                                }
151                        }
152                }
153                System.out.println(numUniqueWRM + " unique WorkflowRunManagers");
154        }
155
156        public int compareTo(KeplerGraphFrameUpdater arg0) {
157                return -333;
158        }
159
160        public void updateFrameComponents(Components components) {
161                // do nothing
162        }
163
164        public void dispose(KeplerGraphFrame frame) {
165                wrms.remove(frame);
166                //printDebug();
167        }
168        
169        
170        
171}