View Javadoc

1   /*
2    * Created on 06-Apr-2005
3    */
4   package com.webstersmalley.mediacollection.model;
5   
6   import java.sql.PreparedStatement;
7   import java.sql.ResultSet;
8   import java.sql.SQLException;
9   
10  import com.webstersmalley.mediacollection.database.DatabaseConnection;
11  
12  /**
13   */
14  public class SimpleMediaCollectionBean
15  {
16  	public final static String[] columns = {"Format", "Collection", "Artist", "Name"};
17  
18  	private String format;
19  	private String collection;
20  	private String artist;
21  	private String name;
22  	/**
23  	 * @return Returns the artist.
24  	 */
25  	public String getArtist()
26  	{
27  		return artist;
28  	}
29  	/**
30  	 * @param artist The artist to set.
31  	 */
32  	public void setArtist(String artist)
33  	{
34  		this.artist = artist;
35  	}
36  	/**
37  	 * @return Returns the collection.
38  	 */
39  	public String getCollection()
40  	{
41  		return collection;
42  	}
43  	/**
44  	 * @param collection The collection to set.
45  	 */
46  	public void setCollection(String collection)
47  	{
48  		this.collection = collection;
49  	}
50  	/**
51  	 * @return Returns the format.
52  	 */
53  	public String getFormat()
54  	{
55  		return format;
56  	}
57  	/**
58  	 * @param format The format to set.
59  	 */
60  	public void setFormat(String format)
61  	{
62  		this.format = format;
63  	}
64  	/**
65  	 * @return Returns the name.
66  	 */
67  	public String getName()
68  	{
69  		return name;
70  	}
71  	/**
72  	 * @param name The name to set.
73  	 */
74  	public void setName(String name)
75  	{
76  		this.name = name;
77  	}
78  	
79  	public String getColumn(int col)
80  	{
81  		if (col == 0)
82  		{
83  			return getFormat();
84  		}
85  		else if (col == 1)
86  		{
87  			return getCollection();
88  		}
89  		else if (col == 2)
90  		{
91  			return getArtist();
92  		}
93  		else if (col == 3)
94  		{
95  			return getName();
96  		}
97  		else
98  		{
99  			throw new RuntimeException("Error: column index out of bounds (" + col + ")");
100 		}
101 	}
102 	
103 	public String toString()
104 	{
105 		return format + ":" + collection + ":" + artist + ":" + name;
106 	}
107 	
108 	public void persist(DatabaseConnection connection) throws SQLException
109 	{
110 		PreparedStatement stmt = connection.getConnection().prepareStatement("select * from Collections where formatname = ? and collectionname = ? and artistname = ? and workname = ?");
111 		stmt.setString(1, getFormat());
112 		stmt.setString(2, getCollection());
113 		stmt.setString(3, getArtist());
114 		stmt.setString(4, getName());
115 		ResultSet rs = stmt.executeQuery();
116 		if (!rs.next())
117 		{
118 			stmt = connection.getConnection().prepareStatement("insert into collections (formatname, collectionname, artistname, workname) values (?,?,?,?)");
119 			stmt.setString(1, getFormat());
120 			stmt.setString(2, getCollection());
121 			stmt.setString(3, getArtist());
122 			stmt.setString(4, getName());
123 			stmt.execute();
124 		}
125 	}
126 	
127 	/**
128 	 * @param rs
129 	 * @return
130 	 * @throws SQLException
131 	 */
132 	public static SimpleMediaCollectionBean createFromResultSet(ResultSet rs) throws SQLException
133 	{
134 		SimpleMediaCollectionBean bean = new SimpleMediaCollectionBean();
135 		bean.setArtist(rs.getString("ArtistName"));
136 		bean.setCollection(rs.getString("CollectionName"));
137 		bean.setFormat(rs.getString("FormatName"));
138 		bean.setName(rs.getString("WorkName"));
139 		return bean;
140 	}
141 }