001/*
002 * Copyright (c) 2003-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: barseghian $'
006 * '$Date: 2011-01-12 02:49:21 +0000 (Wed, 12 Jan 2011) $' 
007 * '$Revision: 26675 $'
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
032import java.util.List;
033
034import org.apache.commons.logging.Log;
035import org.apache.commons.logging.LogFactory;
036import org.kepler.objectmanager.lsid.KeplerLSID;
037import org.kepler.objectmanager.lsid.LSIDGenerator;
038import org.kepler.provenance.Queryable;
039import org.kepler.reporting.rio.util.ProvenanceUtil;
040import org.kepler.reporting.roml.ReportLayout;
041
042import ptolemy.actor.gui.TableauFrame;
043
044/**
045 * This class assists in assembling Report objects with 
046 * their Items fully fleshed with values (from Provenance)
047 */
048public class ReportAssembler {
049
050        public static Log log = LogFactory.getLog(ReportAssembler.class);
051        
052        //public static ReportInstance populateReportWithLatest(ReportLayout reportLayout, TableauFrame tableauFrame) {
053                
054        //      KeplerLSID lsid = reportLayout.getWorkflowLSID();
055        //      
056        //      return populateReportWithLatest(reportLayout, lsid, tableauFrame);
057        //}
058        
059        public static ReportInstance populateReportWithLatest(Queryable queryable, ReportLayout reportLayout, KeplerLSID lsid,
060                        TableauFrame tableauFrame) {
061
062                Integer execId = null;
063                try {
064                        //execId = ProvenanceUtil.getQueryable(lsid, tableauFrame).getLastExecutionForWorkflow(lsid);
065                        execId = queryable.getLastExecutionForWorkflow(lsid);
066                } catch (Exception e) {
067                        // TODO Auto-generated catch block
068                        e.printStackTrace();
069                }
070                finally {
071                        ProvenanceUtil.returnQueryable(lsid);
072                }
073                return populateReport(queryable, reportLayout, lsid, execId, tableauFrame);
074        }
075        
076        public static ReportInstance populateReport(Queryable queryable, ReportLayout reportLayout, KeplerLSID lsid, int execId,
077                        TableauFrame tableauFrame) {
078                
079                //TODO can this cause a NotSerializable error?
080                ReportInstance reportInstance = new ReportInstance(reportLayout);
081                
082                // LSID for the instance
083                try {
084                        KeplerLSID rioLSID = LSIDGenerator.getInstance().getNewLSID();
085                        reportInstance.setLsid(rioLSID);
086                } catch (Exception e) {
087                        log.error("error generating LSID for Report Instance: " + e.getMessage());
088                        e.printStackTrace();
089                }
090                
091                // go through the items and have them do their thing
092                List<Item> items = reportInstance.getSections().get(0).getColumns().get(0).getItems();
093                for (int i = 0; i < items.size(); i++) {
094                        Item item = items.get(i);
095                        item.generateValues(queryable, lsid, execId, tableauFrame);
096                }
097                return reportInstance;
098        }
099        
100}