001/**
002 *  '$RCSfile$'
003 *  '$Author: crawl $'
004 *  '$Date: 2012-06-13 16:44:25 +0000 (Wed, 13 Jun 2012) $'
005 *  '$Revision: 29932 $'
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;
033
034import org.apache.commons.logging.Log;
035import org.apache.commons.logging.LogFactory;
036import org.kepler.objectmanager.lsid.KeplerLSID;
037
038/**
039 * This class holds the information for one row of the KARS_CACHED table.
040 * 
041 * @author Aaron Schultz
042 */
043public class KARCached {
044        private static final Log log = LogFactory.getLog(KARCached.class.getName());
045        private static final boolean isDebugging = log.isDebugEnabled();
046
047        private File _file;
048        private KeplerLSID _lsid;
049        private String _version;
050        private String _repoName;
051
052        public KARCached() {
053        }
054
055        /**
056         * Populate this object from the database. setLsid() must be called before
057         * running this method.
058         * 
059         * @param stmt
060         * @throws Exception
061         */
062        public void populate(File karFile, Statement stmt) throws Exception {
063
064                String query = "SELECT LSID,VERSION,REPONAME FROM "
065                                + KARCacheManager.KARS_CACHED_TABLE_NAME + " WHERE FILE = '"
066                                + karFile + "'";
067                if (isDebugging)
068                        log.debug(query);
069                ResultSet rs = stmt.executeQuery(query);
070                if (rs == null)
071                        throw new SQLException("Query Failed: " + query);
072                if (rs.next()) {
073                        String lsidStr = rs.getString(2);
074                        String version = rs.getString(3);
075                        String reponame = rs.getString(4);
076                        if (rs.wasNull()) {
077                                reponame = null;
078                        }
079
080                        if (!karFile.isFile()) {
081                                throw new Exception(
082                                                "KARS_CACHED table data in database is corrupt: "
083                                                + "KAR file is not a file");
084                        }
085                        KeplerLSID lsid = new KeplerLSID(lsidStr);
086                        if (lsid == null) {
087                                throw new Exception(
088                                                "KARS_CACHED table data in database is corrupt: "
089                                                + "KAR lsid is null");
090                        }
091                        setFile(karFile);
092                        setLsid(lsid);
093                        setVersion(version);
094                        setRepoName(reponame);
095                } else {
096                        throw new SQLException(getFile() + " was not found in "
097                                        + KARCacheManager.KARS_CACHED_TABLE_NAME);
098                }
099                rs.close();
100        }
101
102        public File getFile() {
103                return _file;
104        }
105
106        public void setFile(File file) {
107                _file = file;
108        }
109
110        /**
111         * @return the lsid
112         */
113        public KeplerLSID getLsid() {
114                return _lsid;
115        }
116
117        /**
118         * @param lsid
119         *            the lsid to set
120         */
121        public void setLsid(KeplerLSID lsid) {
122                this._lsid = lsid;
123        }
124
125        /**
126         * @return the name
127         */
128        public String getName() {
129                return _file.getName();
130        }
131
132        /**
133         * @return the version
134         */
135        public String getVersion() {
136                return _version;
137        }
138
139        /**
140         * @param version
141         *            the version to set
142         */
143        public void setVersion(String version) {
144                this._version = version;
145        }
146
147        /**
148         * @return the repoName
149         */
150        public String getRepoName() {
151                return _repoName;
152        }
153
154        /**
155         * @param repoName
156         *            the repoName to set
157         */
158        public void setRepoName(String repoName) {
159                this._repoName = repoName;
160        }
161
162        /**
163         * @return the path
164         */
165        public File getPath() {
166                return getFile().getParentFile();
167        }
168
169}