Source code: jmmv/progs/DiskCat/RegisterFile.java
1 /*
2 * DiskCat - Disk Cataloguer
3 * Copyright (C) 2002 Julio Merino <slink@unixbsd.org>
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
9
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 * USA
19 */
20
21 package jmmv.progs.DiskCat;
22
23 import jmmv.Global;
24 import jmmv.db.Database;
25 import jmmv.db.InvalidDataException;
26 import jmmv.db.Register;
27
28 import java.sql.Date;
29 import java.sql.ResultSet;
30 import java.sql.SQLException;
31 import java.sql.Statement;
32
33 final class RegisterFile extends Register {
34 public long mIDFile;
35 public long mIDDisk;
36 public String mDirectory;
37 public String mFilename;
38
39 private final static int DIRECTORY_MAXLENGTH = 500;
40 private final static int FILENAME_MAXLENGTH = 255;
41
42 public void setNewValues() {
43 mIDFile = 0;
44 mIDDisk = 0;
45 mDirectory = "";
46 mFilename = "";
47 }
48
49 public long getIDFile() { return mIDFile; }
50 public long getIDDisk() { return mIDDisk; }
51 public String getDirectory() { return mDirectory; }
52 public String getFilename() { return mFilename; }
53
54 public void setIDFile(long l) {
55 mIDFile = l;
56 }
57
58 public void setIDDisk(long l) throws InvalidDataException, SQLException {
59 Statement st = Database.getInstance().getConnection().createStatement();
60 ResultSet rs = st.executeQuery("SELECT iddisk FROM disks WHERE iddisk='" + l + "'");
61
62 rs.last();
63 if (rs.getRow() != 1)
64 throw new InvalidDataException(Global.getResources().getString("FILE_INVALIDDATA_IDDISK_MISSING"));
65
66 mIDDisk = l;
67 }
68
69 public void setDirectory(String s) throws InvalidDataException {
70 if (s.length() == 0 || s.length() > DIRECTORY_MAXLENGTH)
71 throw new InvalidDataException(Global.getResources().getString("FILE_INVALIDDATA_DIRECTORY"));
72 mDirectory = s;
73 }
74
75 public void setFilename(String s) throws InvalidDataException {
76 if (s.length() == 0 || s.length() > FILENAME_MAXLENGTH)
77 throw new InvalidDataException(Global.getResources().getString("FILE_INVALIDDATA_FILENAME"));
78 mFilename = s;
79 }
80
81 public void parseResultSet(ResultSet rs) throws SQLException {
82 mIDFile = rs.getLong("idfile");
83 mIDDisk = rs.getLong("iddisk");
84 mDirectory = rs.getString("directory");
85 mFilename = rs.getString("filename");
86 }
87
88 public final String whereSentence() {
89 String s;
90 //s = "idfile='" + mIDFile + "' AND ";
91 s = "iddisk='" + mIDDisk + "' AND ";
92 s += "directory='" + mDirectory + "' AND ";
93 s += "filename='" + mFilename + "'";
94 return s;
95 }
96
97 public final void insert() throws InvalidDataException, SQLException {
98 // Check if there is already another register with the same ID
99 Statement st = Database.getInstance().getConnection().createStatement();
100 ResultSet rs = st.executeQuery("SELECT idfile FROM files WHERE " + whereSentence());
101
102 rs.last();
103 if (rs.getRow() != 0)
104 throw new InvalidDataException
105 (Global.getResources().getString("FILE_INVALIDDATA_IDFILE_EXISTS"));
106 st.close();
107
108 // Insert it
109 st = Database.getInstance().getConnection().createStatement();
110 st.executeUpdate("INSERT INTO files (iddisk,directory,filename)" +
111 " VALUES (" +
112 mIDDisk + ", '" + mDirectory + "', '" + mFilename + "')");
113 st.close();
114 }
115
116 public final void delete() throws SQLException {
117 Statement st = Database.getInstance().getConnection().createStatement();
118 st.executeUpdate("DELETE FROM files WHERE " + whereSentence());
119 st.close();
120 }
121
122 public final void update(Register old) throws SQLException {
123 Statement st = Database.getInstance().getConnection().createStatement();
124 st.executeUpdate("UPDATE files SET " +
125 "iddisk='" + mIDDisk + "', directory='" + mDirectory +
126 "', filename='" + mFilename + "' WHERE " + old.whereSentence());
127 st.close();
128 }
129 }