9a475751899656b8d2703d00fa90ddfbccc5abf6
[mir.git] / DatabaseContentToMedia.java
1 package mircoders.storage;
2
3 import java.lang.*;
4 import java.sql.*;
5 import java.io.*;
6 import java.util.*;
7
8 import freemarker.template.*;
9
10 import mir.storage.*;
11 import mir.entity.*;
12 import mir.misc.*;
13
14 import mircoders.entity.*;
15
16 /**
17  * <b>Diese Klasse implementiert die Datenbankverbindung zur MetaObjekt-Tabelle
18  *
19  *
20  */
21
22 public class DatabaseContentToMedia extends Database implements StorageObject{
23
24   private static DatabaseContentToMedia instance;
25
26   public static DatabaseContentToMedia getInstance()
27     throws StorageObjectException {
28     if (instance == null) {
29       instance = new DatabaseContentToMedia();
30       instance.myselfDatabase = instance;
31     }
32     return instance;
33   }
34
35   private DatabaseContentToMedia()
36     throws StorageObjectException {
37
38     super();
39     this.hasTimestamp = false;
40     this.theTable="content_x_media";
41   }
42
43
44   public EntityList getMedia(EntityContent content) {
45     EntityList returnList=null;
46     if (content != null) {
47       // get all to_topic from media_x_topic
48       String id = content.getId();
49       //this is not supported by mysql
50       String subselect = "id in (select media_id from " + theTable + " where content_id=" + id+")";
51
52       try {
53         returnList = DatabaseMedia.getInstance().selectByWhereClause(subselect,-1);
54       } catch (Exception e) {
55         theLog.printDebugInfo("-- get media failed " + e.toString());
56       }
57     }
58     return returnList;
59   }
60
61
62   public void setMedia(String contentId, String[] mediaId) {
63     if (contentId == null){
64       return;
65     }
66     if (mediaId==null || mediaId[0]==null) {
67       return;
68     }
69     //first delete all row with content_id=contentId
70     String sql = "delete from "+ theTable +" where content_id=" + contentId;
71   
72     Connection con=null;Statement stmt=null;
73     try {
74       con = getPooledCon();
75       // should be a preparedStatement because is faster
76       stmt = con.createStatement();
77       ResultSet rs = executeSql(stmt,sql);
78     } catch (Exception e) {
79       theLog.printDebugInfo("-- set media failed -- delete");
80     } finally {
81       freeConnection(con,stmt);
82     }
83   
84     //now insert
85     //first delete all row with content_id=contentId
86     for (int i=0;i<mediaId.length;i++) {
87       sql = "insert into "+ theTable +" (content_id,media_id) values ("
88             + contentId + "," + mediaId[i] + ")";
89       try {
90         con = getPooledCon();
91         // should be a preparedStatement because is faster
92         stmt = con.createStatement();
93         ResultSet rs = executeSql(stmt,sql);
94       } catch (Exception e) {
95         theLog.printDebugInfo("-- set topics failed -- insert");
96       } finally {
97         freeConnection(con,stmt);
98       }
99     }
100   }
101
102
103   public void setMedia(String contentId, String mediaId) {
104     if (contentId == null && mediaId == null) {
105       return;
106     }
107     //first delete all row with content_id=contentId
108     String sql = "delete from "+ theTable +" where content_id=" + contentId;
109
110     Connection con=null;Statement stmt=null;
111     try {
112       con = getPooledCon();
113       // should be a preparedStatement because is faster
114       stmt = con.createStatement();
115       int rs = executeUpdate(stmt,sql);
116     } catch (Exception e) {
117       theLog.printDebugInfo("-- set media failed -- delete");
118     } finally {
119       freeConnection(con,stmt);
120     }
121
122     //now insert
123     //first delete all row with content_id=contentId
124
125     sql = "insert into "+ theTable +" (content_id,media_id) values ("
126           + contentId + "," + mediaId + ")";
127     try {
128       con = getPooledCon();
129       // should be a preparedStatement because is faster
130       stmt = con.createStatement();
131       int rs = executeUpdate(stmt,sql);
132     } catch (Exception e) {
133       theLog.printDebugInfo("-- set media failed -- insert");
134     } finally {
135       freeConnection(con,stmt);
136     }
137   }
138
139   public void deleteByContentId(String contentId) {
140     if (contentId == null) {
141       //theLog.printDebugInfo("-- delete topics failed -- no content id");
142       return;
143     }
144     //delete all row with content_id=contentId
145     String sql = "delete from "+ theTable +" where content_id=" + contentId;
146
147     Connection con=null;Statement stmt=null;
148     try {
149       con = getPooledCon();
150       // should be a preparedStatement because is faster
151       stmt = con.createStatement();
152       ResultSet rs = executeSql(stmt,sql);
153     } catch (Exception e) {
154       //theLog.printDebugInfo("-- delete topics failed  ");
155     } finally {
156       freeConnection(con,stmt);
157     }
158   }
159
160   public void deleteByMediaId(String mediaId) {
161     if (mediaId == null) {
162       //theLog.printDebugInfo("-- delete topics failed -- no topic id");
163       return;
164     }
165     //delete all row with content_id=contentId
166     String sql = "delete from "+ theTable +" where topic_id=" + mediaId;
167
168     Connection con=null;Statement stmt=null;
169     try {
170       con = getPooledCon();
171       // should be a preparedStatement because is faster
172       stmt = con.createStatement();
173       ResultSet rs = executeSql(stmt,sql);
174       theLog.printDebugInfo("-- delete media success ");
175     } catch (Exception e) {
176       theLog.printDebugInfo("-- delete media failed ");
177     } finally {
178       freeConnection(con,stmt);
179     }
180   }
181
182
183   public EntityList getContent(EntityMedia media) {
184     EntityList returnList=null;
185     if (media != null) {
186       String id = media.getId();
187       String select = "select content_id from " + theTable + " where media_id=" + id;
188
189       // execute select statement
190       Connection con=null;Statement stmt=null;
191       try {
192         con = getPooledCon();
193         // should be a preparedStatement because is faster
194         stmt = con.createStatement();
195         ResultSet rs = executeSql(stmt,select);
196         if (rs!=null) {
197           String mediaSelect= "id IN (";
198           boolean first=true;
199           while (rs.next()) {
200             if (first==false) mediaSelect+=",";
201             mediaSelect += rs.getString(1);
202             first=false;
203           }
204           mediaSelect+=")";
205           if (first==false)
206             returnList = DatabaseContent.getInstance().selectByWhereClause(mediaSelect,-1);
207         }
208       }
209       catch (Exception e) {theLog.printDebugInfo("-- get content failed");}
210       finally { freeConnection(con,stmt);}
211     }
212     return returnList;
213   }
214
215 }