001/*
002 * Copyright (c) 2003-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.roml;
031
032import java.io.Serializable;
033import java.util.ArrayList;
034import java.util.List;
035
036import javax.swing.DefaultListModel;
037
038import org.kepler.objectmanager.lsid.KeplerLSID;
039import org.kepler.objectmanager.lsid.LSIDGenerator;
040import org.kepler.reporting.rio.Item;
041import org.kepler.reporting.rio.ItemLabel;
042import org.kepler.reporting.rio.ItemValue;
043import org.kepler.reporting.rio.ReportSection;
044
045import ptolemy.actor.gui.TableauFrame;
046
047
048public class ReportLayout implements Serializable {
049        
050        protected KeplerLSID lsid = null;
051        protected String name;
052        protected String title = "";
053        protected String headerGraphic;
054        protected KeplerLSID workflowLsid;
055        protected List<ReportSection> sections = new ArrayList<ReportSection>();
056
057        private boolean modified = false;
058        private boolean locked = false;
059        private DefaultListModel dlm = new DefaultListModel();
060        
061        
062        public ReportLayout() {
063                sections.add(new ReportSection());
064                if (lsid == null){
065                        try {
066                                lsid = LSIDGenerator.getInstance().getNewLSID();
067                        } catch (Exception e) {
068                                // TODO Auto-generated catch block
069                                e.printStackTrace();
070                        }
071                }
072        }
073        
074        public boolean isBlank() {
075                //no items and no title
076                boolean empty = 
077                        (
078                        this.getSections().get(0).getColumns().get(0).getItems().isEmpty() 
079                        && 
080                        (this.getTitle() == null || this.getTitle().length() == 0)
081                        );
082                return empty;
083        }
084        
085        public boolean isModified() {
086                return modified;
087        }
088        
089
090        /**
091         * Mark TableauFrame modified so user will be prompted to save,
092         * and increment ReportLayout LSID. This is a bit of a hack.
093         * @param tableauFrame
094         */
095        public void setModified(TableauFrame tableauFrame) {
096                tableauFrame.setModified(true);
097                incrementLSID();
098        }
099        
100        private void incrementLSID() {
101                if (lsid.isLocalToInstance()){
102                        lsid = LSIDGenerator.getInstance().updateLsidRevision(lsid);
103                }
104                else{
105                        //TODO maintain referral list - currently just throwing away old LSID. 
106                        try {
107                                lsid = LSIDGenerator.getInstance().getNewLSID();
108                        } catch (Exception e) {
109                                // TODO Auto-generated catch block
110                                e.printStackTrace();
111                        }
112                }
113        }
114        
115        public boolean isLocked() {
116                return locked;
117        }
118        
119        public void setLocked(boolean flag) {
120                locked = flag;
121        }
122        
123        public KeplerLSID getLsid() {
124                return lsid;
125        }
126
127        public void setLsid(KeplerLSID lsid) {
128                this.lsid = lsid;
129        }
130
131        public KeplerLSID getWorkflowLSID() {
132                return workflowLsid;
133        }
134
135        public void setWorkflowLSID(KeplerLSID workflowLsid) {
136                this.workflowLsid = workflowLsid;
137        }
138
139        public String getName() {
140                return name;
141        }
142
143        public void setName(String name) {
144                this.name = name;
145        }
146
147        public List<ReportSection> getSections() {
148                return sections;
149        }
150        
151        public String getTitle() {
152                return title;
153        }
154        
155        public void setTitle(String title) {
156                this.title = title;
157        }
158        
159        public String getHeaderGraphic() {
160                return headerGraphic;
161        }
162
163        public void setHeaderGraphic(String headerGraphic) {
164                this.headerGraphic = headerGraphic;
165        }
166
167        public DefaultListModel getAllItemsAsListModel() {
168                dlm.clear();
169                //items
170                List<Item> items = this.getSections().get(0).getColumns().get(0).getItems();
171                for (int i = 0; i < items.size(); i++) {
172                        Item item = items.get(i);
173                        //labels (does not take into consideration the placement/properties)
174                        dlm.add(i, item.getName());
175                }
176                
177                return dlm;
178        }
179        
180        public String toString() {
181                
182                StringBuffer buf = new StringBuffer();
183                buf.append(getTitle());
184                //items
185                List<Item> items = this.getSections().get(0).getColumns().get(0).getItems();
186                for (int i = 0; i < items.size(); i++) {
187                        //labels (does not take into consideration the placement/properties)
188                        List<ItemLabel> labels = items.get(i).getLabels();
189                        for (int k = 0; k < labels.size(); k++) {
190                                buf.append(labels.get(k).getValue().toString());
191                        }
192                        //values
193                        List<ItemValue> values = items.get(i).getValues();
194                        for (int j = 0; j < values.size(); j++) {
195                                buf.append(values.get(j).getValue().toString());
196                        }
197                }
198                
199                return buf.toString();
200        }
201        
202}