001/*
002 * Copyright (c) 2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2012-11-26 22:23:27 +0000 (Mon, 26 Nov 2012) $' 
007 * '$Revision: 31127 $'
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.reporting.rio;
031
032
033import java.util.List;
034import java.util.Map;
035
036import org.kepler.objectmanager.lsid.KeplerLSID;
037import org.kepler.provenance.Queryable;
038import org.kepler.reporting.rio.util.ProvenanceUtil;
039
040import ptolemy.actor.gui.TableauFrame;
041import ptolemy.data.StringToken;
042import ptolemy.data.Token;
043
044
045public class DynamicReportItem extends Item {
046        
047        public static final int ROW_SCALE = 1;
048        public static final int ROW_ALIGNMENT = 0;
049
050        public enum DynamicItemProperties {
051                SCALE,
052                ALIGNMENT
053        }
054        
055        public enum ScaleValues {
056                
057                FULL_SIZE
058                 {
059                        @Override
060                        public String toString() {
061                                return "100%";
062                        }
063                 },
064                
065                NINETY_PERCENT{
066                        @Override
067                        public String toString() {
068                                return "90%";
069                        }
070                },
071                
072                
073                EIGHTY_PERCENT{
074                        @Override
075                        public String toString() {
076                                return "80%";
077                        }
078                        
079                },
080                
081                
082                SEVENTY_PERCENT{
083                        @Override
084                        public String toString() {
085                                return "70%";
086                        }
087                        
088                },
089                
090                
091                SIXTY_PERCENT{
092                        @Override
093                        public String toString() {
094                                return "60%";
095                        }
096                        
097                },
098                
099                FIFTY_PERCENT{
100                        @Override
101                        public String toString() {
102                                return "50%";
103                        }
104                        
105                },
106                
107                FOURTY_PERCENT{
108                        @Override
109                        public String toString() {
110                                return "40%";
111                        }
112                        
113                },
114                
115                THIRTY_PERCENT{
116                        @Override
117                        public String toString() {
118                                return "30%";
119                        }
120                        
121                },
122                
123                TWENTY_PERCENT{
124                        @Override
125                        public String toString() {
126                                return "20%";
127                        }
128                        
129                },
130                
131                TEN_PERCENT{
132                        @Override
133                        public String toString() {
134                                return "10%";
135                        }
136                        
137                },
138                
139        }
140        
141        /*private double scalepercentage;
142        
143        void ScaleValues(double scalepercentage) {
144                this.scalepercentage = scalepercentage;
145        }*/
146        
147        
148        public enum AlignmentValues {
149                LEFT,
150                RIGHT,
151                CENTER
152        }
153        
154        
155        private String userdefinedType;
156        private String sourceType;
157
158        public DynamicReportItem() {
159                super();
160                init();
161        }
162        
163        public DynamicReportItem(String name) {
164                super();
165                setName(name);
166                init();
167        }       
168        
169        private void init() {
170                DynamicItemProperties[] props = DynamicItemProperties.values();
171                for (DynamicItemProperties key: props) {
172                        this.itemProperties.setProperty(key.toString(), null);
173                }
174        }
175        
176        
177        public String getSourceType() {
178                return sourceType;
179        }
180        
181        public void setSourceType(String sourceType) {
182                this.sourceType = sourceType;
183        }
184        
185        
186        public String getUserDefinedType() {
187                return userdefinedType;
188        }
189        
190        
191        public void setUserDefinedType(String userdefinedType) {
192                this.userdefinedType = userdefinedType;
193        }
194        
195        
196        public void setUserDefinedType(String type, String userdefinedtype){
197                this.itemProperties.setProperty(type, userdefinedtype);
198        }
199        
200
201        
202        /**
203         * Retrieves values for a given execution
204         * @see getValues() - a method that will only return previously-
205         * retrieved values (accessor method)
206         * @param execId execution for which to retrieve values
207         * @return list of ItemValue objects for given execution
208         */
209        public void generateValues(Queryable queryable, KeplerLSID lsid, int execId, TableauFrame tableauFrame) {
210                //clear the values
211                values.clear();
212                
213                //look them up
214                ///Queryable queryable = null;
215                
216                try {
217                        ///queryable = ProvenanceUtil.getQueryable(lsid, tableauFrame);
218                        String entityName = getName();
219                        entityName = entityName.substring(entityName.indexOf(".", 1));
220                        Integer entityId = queryable.getEntityId(entityName, lsid);
221
222                        // make sure we found the entity id
223                        // if we didn't, it's probably due to a rename or deletion
224            if (entityId == null) {
225                System.out.println("Could not find " + entityName
226                        + " in provenance; perhaps it was deleted or renamed?");
227                values.add(new ItemValue(new StringToken("ERROR: " + entityName + " does not exist.")));
228                return;
229            }
230                        
231                        // what kind of data are we retrieving?
232                        String entityType = queryable.getEntityType(entityId);                                          
233                        // for PORTS
234                        if (entityType.equals("port")) {
235                                List<Integer> tokenIds = queryable.getTokensForExecution(execId, entityId, true);
236                                for (int i = 0; i < tokenIds.size(); i++) {
237                                        Integer tokenId = tokenIds.get(i);
238                                        Token token = queryable.getToken(tokenId);
239                                        ItemValue itemValue = new ItemValue(token);
240                                        values.add(itemValue);
241                                }
242                        }
243                        else {
244                                // this is a parameter
245                                // look up the parameters used 
246                                Map<String, String> parameters = queryable.getParameterNameValuesForExecution(execId);
247                                // get this particular one
248                                String value = parameters.get(entityName);
249                                if (value != null) {
250                                        Token token = new StringToken(value);
251                                        ItemValue itemValue = new ItemValue(token);
252                                        values.add(itemValue);
253                                }
254                        }
255                        
256                } catch (Exception e) {
257                        System.out.println("ERROR getting value for " + name
258                    + " \nPerhaps it was renamed or deleted? " +
259                    " Error message is " + e.getMessage());
260                }
261                finally {
262                        ProvenanceUtil.returnQueryable(lsid);
263                }
264                
265        }
266        
267        @Override
268        public String toString() {
269                return getName();
270        }
271        
272        @Override
273        public String getShortName() {
274                String delims = "[.]";
275                String[] nameList = name.split(delims);
276                
277                int size = nameList.length;
278                String tmp1 = nameList[(size - 2)];
279                
280                String shortname = "";
281                int lastobj = name.lastIndexOf('.');
282                
283                String lasttoken = name.substring(lastobj);
284        
285                shortname = tmp1 + lasttoken;
286                
287                return shortname;
288        }
289
290}