Source code: jmmv/progs/DiskCat/RegisterMovie.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 RegisterMovie extends Register {
34 public long mIDMovie;
35 public long mIDDisk;
36 public long mIDFile;
37 public String mTitle;
38 public String mFormat;
39
40 private final static int TITLE_MAXLENGTH = 100;
41 private final static int FORMAT_MAXLENGTH = 10;
42
43 public void setNewValues() {
44 mIDMovie = 0;
45 mIDDisk = 0;
46 mIDFile = 0;
47 mTitle = "";
48 mFormat = "Other";
49 }
50
51 public long getIDMovie() { return mIDMovie; }
52 public long getIDDisk() { return mIDDisk; }
53 public long getIDFile() { return mIDFile; }
54 public String getTitle() { return mTitle; }
55 public String getFormat() { return mFormat; }
56
57 public void setIDMovie(long l) {
58 mIDMovie = l;
59 }
60
61 public void setIDDisk(long l) throws InvalidDataException, SQLException {
62 Statement st = Database.getInstance().getConnection().createStatement();
63 ResultSet rs = st.executeQuery("SELECT iddisk FROM disks WHERE iddisk='" + l + "'");
64
65 rs.last();
66 if (rs.getRow() != 1)
67 throw new InvalidDataException
68 (Global.getResources().getString("MOVIE_INVALIDDATA_IDDISK_MISSING"));
69
70 mIDDisk = l;
71 }
72
73 public void setIDFile(long l) {
74 mIDFile = l;
75 }
76
77 public void setTitle(String s) throws InvalidDataException {
78 if (s.length() > TITLE_MAXLENGTH)
79 throw new InvalidDataException(Global.getResources().getString("MOVIE_INVALIDDATA_TITLE"));
80 mTitle = s;
81 }
82
83 public void setFormat(String s) throws InvalidDataException {
84 if (s.length() == 0 || s.length() > FORMAT_MAXLENGTH)
85 throw new InvalidDataException(Global.getResources().getString("MOVIE_INVALIDDATA_FORMAT"));
86 mFormat = s;
87 }
88
89 public void parseResultSet(ResultSet rs) throws SQLException {
90 mIDMovie = rs.getLong("idmovie");
91 mIDDisk = rs.getLong("iddisk");
92 mIDFile = rs.getLong("idfile");
93 mTitle = rs.getString("title");
94 mFormat = rs.getString("format");
95 }
96
97 public final String whereSentence() {
98 String s;
99 //s = "idmovie='" + mIDMovie + "' AND ";
100 s = "iddisk='" + mIDDisk + "' AND ";
101 s += "idfile='" + mIDFile + "' AND ";
102 s += "title='" + mTitle + "' AND ";
103 s += "format='" + mFormat + "'";
104 return s;
105 }
106
107 public final void insert() throws InvalidDataException, SQLException {
108 // Check if there is already another register with the same ID
109 Statement st =
110 Database.getInstance().getConnection().createStatement();
111 ResultSet rs = st.executeQuery("SELECT idmovie FROM movies " +
112 " WHERE " + whereSentence());
113
114 rs.last();
115 if (rs.getRow() != 0)
116 throw new
117 InvalidDataException(Global.getResources().getString
118 ("MOVIE_INVALIDDATA_IDMOVIE_EXISTS"));
119 st.close();
120
121 // Insert it
122 st = Database.getInstance().getConnection().createStatement();
123 st.executeUpdate("INSERT INTO movies (iddisk,idfile,artist," +
124 "title,format) VALUES (" +
125 mIDDisk + ", " + mIDFile + "', '" + mTitle +
126 "', '" + mFormat + "')");
127 st.close();
128 }
129
130 public final void delete() throws SQLException {
131 Statement st =
132 Database.getInstance().getConnection().createStatement();
133 st.executeUpdate("DELETE FROM movies WHERE " +
134 whereSentence());
135 st.close();
136 }
137
138 public final void update(Register old) throws SQLException {
139 Statement st =
140 Database.getInstance().getConnection().createStatement();
141 st.executeUpdate("UPDATE movies SET " +
142 "iddisk='" + mIDDisk + "', idfile='" +
143 mIDFile + "', title='" + mTitle +
144 "', format='" + mFormat + "' WHERE " +
145 old.whereSentence());
146 st.close();
147 }
148 }