001/**
002 *  '$RCSfile$'
003 *  '$Author: crawl $'
004 *  '$Date: 2013-02-21 19:13:44 +0000 (Thu, 21 Feb 2013) $'
005 *  '$Revision: 31472 $'
006 *
007 *  For Details:
008 *  http://www.kepler-project.org
009 *
010 *  Copyright (c) 2010 The Regents of the
011 *  University of California. All rights reserved. Permission is hereby granted,
012 *  without written agreement and without license or royalty fees, to use, copy,
013 *  modify, and distribute this software and its documentation for any purpose,
014 *  provided that the above copyright notice and the following two paragraphs
015 *  appear in all copies of this software. IN NO EVENT SHALL THE UNIVERSITY OF
016 *  CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL,
017 *  OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
018 *  DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE
019 *  POSSIBILITY OF SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY
020 *  DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
022 *  SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
023 *  CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
024 *  ENHANCEMENTS, OR MODIFICATIONS.
025 */
026
027package org.kepler.kar;
028
029import java.io.File;
030import java.sql.ResultSet;
031import java.sql.SQLException;
032import java.sql.Statement;
033import java.util.Vector;
034
035import org.apache.commons.logging.Log;
036import org.apache.commons.logging.LogFactory;
037import org.kepler.objectmanager.cache.CacheContent;
038import org.kepler.objectmanager.cache.CacheManager;
039import org.kepler.objectmanager.lsid.KeplerLSID;
040import org.kepler.sms.SemanticType;
041
042/**
043 * This class holds the information for one row of the KAR_CACHE_CONTENT table.
044 * 
045 * @author Aaron Schultz
046 */
047public class KARCacheContent {
048        private static final Log log = LogFactory.getLog(KARCacheContent.class.getName());
049        private static final boolean isDebugging = log.isDebugEnabled();
050
051        private String _name;
052        private String _type;
053
054        private KARCached _karCached;
055        private CacheContent _cacheContent;
056
057        private Vector<KeplerLSID> _semanticTypes;
058
059        public KARCacheContent() {
060                _semanticTypes = new Vector<KeplerLSID>();
061        }
062
063        /**
064         * Populate this object from the database. setKarLsid() and setLsid() must
065         * be called before running this method.
066         * 
067         * @param stmt
068         * @throws Exception
069         */
070        public void populate(File karFile, KeplerLSID lsid, Statement stmt) throws Exception {
071
072                String query = "SELECT NAME,TYPE FROM "
073                                + KARCacheManager.KAR_CONTENTS_TABLE_NAME
074                                + " WHERE FILE = '" + karFile + "' AND LSID = '"
075                                + lsid.toString() + "'";
076                if (isDebugging) log.debug(query);
077                ResultSet rs = stmt.executeQuery(query);
078                if (rs == null)
079                        throw new SQLException("Query Failed: " + query);
080                if (rs.next()) {
081                        String name = rs.getString(1);
082                        String type = rs.getString(2);
083
084                        setName(name);
085                        setType(type);
086
087                        // Populate the foreign key object
088                        KARCached kc = new KARCached();
089                        kc.populate(karFile,stmt);
090                        setKarCached(kc);
091                        
092                        CacheContent cc = new CacheContent();
093                        cc.populate(getLsid(),stmt);
094                        setCacheContent(cc);
095                        
096                        // Populate the semantic types
097                        populateSemanticTypes(stmt);
098                } else {
099                        throw new SQLException(karFile + " - " + lsid.toString()
100                                        + " was not found in "
101                                        + KARCacheManager.KAR_CONTENTS_TABLE_NAME);
102                }
103                rs.close();
104
105        }
106        
107        /**
108         * @return File that points to the kar file
109         */
110        public File getKarFile() {
111                return getKarCached().getFile();
112        }
113
114        /**
115         * @return the lsid of the cache content
116         */
117        public KeplerLSID getLsid() {
118                return getCacheContent().getLsid();
119        }
120
121        /**
122         * @return the name
123         */
124        public String getName() {
125                return _name;
126        }
127
128        /**
129         * @param name
130         *            the name to set
131         */
132        public void setName(String name) {
133                this._name = name;
134        }
135
136        /**
137         * @return the type
138         */
139        public String getType() {
140                return _type;
141        }
142
143        /**
144         * @param type
145         *            the type to set
146         */
147        public void setType(String type) {
148                this._type = type;
149        }
150
151        /**
152         * @return the karCached
153         */
154        public KARCached getKarCached() {
155                return _karCached;
156        }
157
158        /**
159         * @param karCached
160         *            the karCached to set
161         */
162        public void setKarCached(KARCached karCached) {
163                this._karCached = karCached;
164        }
165        
166        public CacheContent getCacheContent() {
167                return _cacheContent;
168        }
169        
170        public void setCacheContent(CacheContent cacheContent) {
171                _cacheContent = cacheContent;
172        }
173        
174        /**
175         * @return the _semanticTypes
176         */
177        public Vector<KeplerLSID> getSemanticTypes() {
178                return _semanticTypes;
179        }
180
181        /**
182         * @param semanticTypes the _semanticTypes to set
183         */
184        public void setSemanticTypes(Vector<KeplerLSID> semanticTypes) {
185                _semanticTypes = semanticTypes;
186        }
187        
188        /**
189         * Populate the semantic types from the CACHE_SEMTYPES table.
190         * @param stmt
191         * @throws SQLException
192         */
193        public void populateSemanticTypes(Statement stmt) throws SQLException {
194                String query = "SELECT SEMTYPE FROM " + CacheManager.CACHE_SEMTYPES_TABLE_NAME
195                        + " WHERE LSID = '" + getLsid().toString() + "'";
196                ResultSet rs = null;
197                try {
198                        rs = stmt.executeQuery(query);
199                        if (rs == null) throw new SQLException("Query Failed: " + query);
200                        while (rs.next()) {
201                                try {
202                                        String rawEntry = rs.getString(1);
203                                        SemanticType st = new SemanticType();
204                                        st.setExpression(rawEntry);
205                                        KeplerLSID semType = new KeplerLSID(st.getConceptUri());
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}