001/*
002 * Copyright (c) 2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: crawl $'
006 * '$Date: 2013-02-21 19:13:44 +0000 (Thu, 21 Feb 2013) $' 
007 * '$Revision: 31472 $'
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.objectmanager.cache;
031
032import java.io.File;
033import java.sql.Date;
034import java.sql.ResultSet;
035import java.sql.SQLException;
036import java.sql.Statement;
037import java.util.Vector;
038
039import org.apache.commons.logging.Log;
040import org.apache.commons.logging.LogFactory;
041import org.kepler.objectmanager.lsid.KeplerLSID;
042
043/**
044 * This class represents one row of the CACHECONTENTTABLE.
045 * 
046 * @author Aaron Schultz
047 */
048public class CacheContent {
049
050        private static final Log log = LogFactory.getLog(CacheContent.class
051                        .getName());
052        private static final boolean isDebugging = log.isDebugEnabled();
053
054        private String _name;
055        private KeplerLSID _lsid;
056        private Date _dateChanged;
057        private File _file;
058        private String _type;
059        private String _className;
060
061        private Vector<KeplerLSID> _semanticTypes;
062
063        public CacheContent() {
064                _semanticTypes = new Vector<KeplerLSID>();
065        }
066
067        public void populate(KeplerLSID lsid, Statement stmt) throws Exception {
068
069                String query = "SELECT NAME,LSID,DATE,FILE,TYPE,CLASSNAME FROM "
070                                + CacheManager.CACHETABLENAME + " WHERE LSID = '"
071                                + lsid.toString() + "'";
072                if (isDebugging) log.debug(query);
073                ResultSet rs = stmt.executeQuery(query);
074                if (rs == null)
075                        throw new SQLException("Query Failed: " + query);
076                if (rs.next()) {
077                        String name = rs.getString(1);
078                        String lsidStr = rs.getString(2);
079                        Long d = rs.getLong(3);
080                        String file = rs.getString(4);
081                        String type = rs.getString(5);
082                        String className = rs.getString(6);
083
084                        setName(name);
085                        setLsid(new KeplerLSID(lsidStr));
086                        setDateChanged(new Date(d));
087                        setFile(new File(file));
088                        setType(type);
089                        setClassName(className);
090                } else {
091                        throw new SQLException(lsid + " was not found in "
092                                        + CacheManager.CACHETABLENAME);
093                }
094                rs.close();
095
096        }
097
098        /**
099         * @return the name
100         */
101        public String getName() {
102                return _name;
103        }
104
105        /**
106         * @param name
107         *            the name to set
108         */
109        public void setName(String name) {
110                _name = name;
111        }
112
113        /**
114         * @return the lsid
115         */
116        public KeplerLSID getLsid() {
117                return _lsid;
118        }
119
120        /**
121         * @param lsid
122         *            the lsid to set
123         */
124        public void setLsid(KeplerLSID lsid) {
125                _lsid = lsid;
126        }
127
128        /**
129         * @return the dateChanged
130         */
131        public Date getDateChanged() {
132                return _dateChanged;
133        }
134
135        /**
136         * @param dateChanged
137         *            the dateChanged to set
138         */
139        public void setDateChanged(Date dateChanged) {
140                _dateChanged = dateChanged;
141        }
142
143        /**
144         * @return the file
145         */
146        public File getFile() {
147                return _file;
148        }
149
150        /**
151         * @param file
152         *            the file to set
153         */
154        public void setFile(File file) {
155                _file = file;
156        }
157
158        /**
159         * @return the type
160         */
161        public String getType() {
162                return _type;
163        }
164
165        /**
166         * @param type
167         *            the type to set
168         */
169        public void setType(String type) {
170                _type = type;
171        }
172        
173        public String getClassName() {
174                return _className;
175        }
176        
177        public void setClassName(String className) {
178                _className = className;
179        }
180
181        /**
182         * @return the semanticTypes
183         */
184        public Vector<KeplerLSID> getSemanticTypes() {
185                return _semanticTypes;
186        }
187
188        /**
189         * Populate the semantic types from the CACHE_SEMTYPES table.
190         * 
191         * @param stmt
192         * @throws SQLException
193         */
194        public void populateSemanticTypes(Statement stmt) throws SQLException {
195                String query = "SELECT SEMTYPE FROM "
196                                + CacheManager.CACHE_SEMTYPES_TABLE_NAME + " WHERE LSID = '"
197                                + getLsid().toString() + "'";
198                ResultSet rs = null;
199                try {
200                        rs = stmt.executeQuery(query);
201                        if (rs == null)
202                                throw new SQLException("Query Failed: " + query);
203                        while (rs.next()) {
204                                try {
205                                        KeplerLSID semType = new KeplerLSID(rs.getString(1));
206                                        _semanticTypes.add(semType);
207                                } catch (Exception e) {
208                                        e.printStackTrace();
209                                }
210                        }
211                } finally {
212                        if(rs != null) {
213                                rs.close();
214                        }
215                }
216        }
217
218}