proper handling of exceptions all around. especially for the media stuff.
[mir.git] / source / mircoders / entity / EntityContent.java
1 package mircoders.entity;
2
3 import java.lang.*;
4 import java.io.*;
5 import java.util.*;
6 import java.sql.*;
7
8 import freemarker.template.*;
9
10 import mir.entity.*;
11 import mir.misc.*;
12 import mir.storage.*;
13
14 import mircoders.storage.*;
15
16 /**
17  * this class implements mapping of one line of the database table content
18  * to a java object
19  *
20  * @author RK
21  * @version 2001
22  */
23
24
25 public class EntityContent extends AbstractEntity implements Entity
26 {
27
28         private static int      instances;
29
30         // constructors
31
32         public EntityContent()
33         {
34                 super();
35                 instances++;
36     //content_data is now filed-type "text"
37                 //streamedInput = new ArrayList();
38                 //streamedInput.add("content_data");
39         }
40
41         public EntityContent(StorageObject theStorage) {
42                 this();
43                 setStorage(theStorage);
44         }
45
46         public void finalize() {
47     instances--;
48     super.finalize();
49   }
50
51         //
52         // methods
53
54  /**
55         * set is_produced flag for the article
56         */
57
58         public void setProduced(boolean yesno) throws StorageObjectException
59         {
60                 Connection con=null;Statement stmt=null;
61                 String value = (yesno) ? "1":"0";
62                 String sql = "update content set is_produced='" + value + "' where id='" + getId()+"'";
63                 try {
64                         con = theStorageObject.getPooledCon();
65                         /** @todo should be preparedStatement: faster!! */
66                         stmt = con.createStatement();
67                         theStorageObject.executeUpdate(stmt,sql);
68                 } catch (StorageObjectException e) {
69                         theLog.printDebugInfo(e.toString() + "\n -- set produced failed");
70                 } catch (SQLException e) {
71                         theLog.printDebugInfo(e.toString() + "\n -- set produced failed");
72                 } finally {
73                         theStorageObject.freeConnection(con,stmt);
74                 }
75         }
76
77
78  /**
79         * make openposting to newswire
80         */
81
82         public void newswire()
83         {
84                 String sql = "update content set to_article_type='1', is_produced='0' where id='" + getId()+"'";
85                 try {
86                                 theStorageObject.executeUpdate(sql);
87                 } catch (StorageObjectException e) {
88                         theLog.printError(e.toString() + "newswire failed");
89                 } catch (SQLException e) {
90                         theLog.printError(e.toString() + "newswire failed");
91                 }
92         }
93
94
95  /**
96         * dettach from media
97         */
98
99         public void dettach(String cid,String mid) throws StorageObjectException
100         {
101                 if (mid!=null){
102                         try{
103                                 DatabaseContentToMedia.getInstance().delete(cid,mid);
104                         } catch (Exception e){
105                                 theLog.printError("failed to get instance");
106                         }
107                         //set Content to unproduced
108                         setProduced(false);
109                 }
110         }
111
112  /**
113         * attach to media
114         */
115
116         public void attach(String mid) throws StorageObjectException
117         {
118                 if (mid!=null) {
119                         //write media-id mid and content-id in table content_x_media
120                         try{
121                                 DatabaseContentToMedia.getInstance().addMedia(getId(),mid);
122                         } catch(StorageObjectException e){
123                                 theLog.printError("attach: could not get the instance");
124                         }
125                         //set Content to unproduced
126                         setProduced(false);
127                 }       else {
128                         theLog.printError("EntityContent: attach without mid");
129                 }
130         }
131
132         /**
133          * overridden method getValues to include formatted date into every
134          * entityContent
135          */
136
137         public HashMap getValues() {
138                 HashMap returnHash = super.getValues();
139                 String date=null;
140
141                 if ((date=(String)returnHash.get("date"))!=null)
142                         returnHash.put("date_formatted", StringUtil.webdbDate2readableDate(date));
143                 if ((date=(String)returnHash.get("webdb_create"))!=null)
144                         returnHash.put("webdb_create_formatted", StringUtil.dateToReadableDate(date));
145                 if ((date=(String)returnHash.get("webdb_lastchange"))!=null)
146                         returnHash.put("webdb_lastchange_formatted", StringUtil.dateToReadableDate(date));
147                 return returnHash;
148         }
149
150         /**
151          * overridden method setValues to patch creator_main_url
152          */
153         public void setValues(HashMap theStringValues) {
154                 if (theStringValues != null) {
155                         if (theStringValues.containsKey("creator_main_url")){
156                                 if (((String)theStringValues.get("creator_main_url")).equalsIgnoreCase("http://")){
157                                         theStringValues.remove("creator_main_url");
158         } else if (!((String)theStringValues.get("creator_main_url")).startsWith("http://")){
159           theStringValues.put("creator_main_url","http://"+((String)theStringValues.get("creator_main_url")));
160         }
161       }
162                 }
163                 super.setValues(theStringValues);
164         }
165
166         /**
167          * return the content_data as string
168          * is obsolete, because content_data is now sql-type text
169
170   public String getContentData()
171         {
172                 Connection con=null;Statement stmt=null;
173                 byte[] content_data=null;
174
175                 try {
176                         con = theStorageObject.getPooledCon();
177                         con.setAutoCommit(false);
178                         stmt = con.createStatement();
179                         ResultSet rs = theStorageObject.executeSql(stmt,"select content_data from content where id="+getId());
180                         if(rs!=null) {
181                                 if (rs.next()) {
182                                          content_data = rs.getBytes(1);
183                                 }
184                                 rs.close();
185                         }
186                 }
187                 catch (Exception e) {theLog.printError("EntityContent :: getContent failed! "+e.toString());}
188                 finally {
189                         try {con.setAutoCommit(true); } catch (Exception e) {;}
190                         theStorageObject.freeConnection(con,stmt); }
191
192                 return StringUtil.encodeHtml(StringUtil.unquote( new  String(content_data) ));
193         }
194 */
195
196         /**
197          * fetches all the comments belonging to an article
198          *
199          * @return freemarker.template.SimpleList
200          */
201         public SimpleList getComments() {
202                 return ((DatabaseContent)theStorageObject).getComments(this);
203         }
204
205
206 }