mass update mir codeswitch
[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)
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()
100         {
101                 String sql = "update content set to_media=null, is_produced='0' where id='" + getId()+"'";
102                 try {
103                                 theStorageObject.executeUpdate(sql);
104                 } catch (StorageObjectException e) {
105                         theLog.printError(e.toString() + "dettach failed");
106                 } catch (SQLException e) {
107                         theLog.printError(e.toString() + "dettach failed");
108                 }
109         }
110
111  /**
112         * attach to media
113         */
114
115         public void attach(String mid)
116         {
117                 if (mid!=null) {
118                         String sql = "update content set to_media='" + mid + "', is_produced='0' where id='" + getId()+"'";
119                         try {
120                                         theStorageObject.executeUpdate(sql);
121                         } catch (StorageObjectException e) {
122                                 theLog.printError(e.toString() + "attach failed");
123                         } catch (SQLException e) {
124                                 theLog.printError(e.toString() + "attach failed");
125                         }
126                 }
127                 else
128                         theLog.printError("EntityContent: attach without mid");
129         }
130
131         /**
132          * overridden method getValues to include formatted date into every
133          * entityContent
134          */
135
136         public HashMap getValues() {
137                 HashMap returnHash = super.getValues();
138                 String date=null;
139
140                 if ((date=(String)returnHash.get("date"))!=null)
141                         returnHash.put("date_formatted", StringUtil.webdbDate2readableDate(date));
142                 if ((date=(String)returnHash.get("webdb_create"))!=null)
143                         returnHash.put("webdb_create_formatted", StringUtil.dateToReadableDate(date));
144                 if ((date=(String)returnHash.get("webdb_lastchange"))!=null)
145                         returnHash.put("webdb_lastchange_formatted", StringUtil.dateToReadableDate(date));
146                 return returnHash;
147         }
148
149         /**
150          * overridden method setValues to patch creator_main_url
151          */
152         public void setValues(HashMap theStringValues) {
153                 if (theStringValues != null) {
154                         if (theStringValues.containsKey("creator_main_url")){
155                                 if (((String)theStringValues.get("creator_main_url")).equalsIgnoreCase("http://")){
156                                         theStringValues.remove("creator_main_url");
157         } else if (!((String)theStringValues.get("creator_main_url")).startsWith("http://")){
158           theStringValues.put("creator_main_url","http://"+((String)theStringValues.get("creator_main_url")));
159         }
160       }
161                 }
162                 super.setValues(theStringValues);
163         }
164
165         /**
166          * return the content_data as string
167          * is obsolete, because content_data is now sql-type text
168
169   public String getContentData()
170         {
171                 Connection con=null;Statement stmt=null;
172                 byte[] content_data=null;
173
174                 try {
175                         con = theStorageObject.getPooledCon();
176                         con.setAutoCommit(false);
177                         stmt = con.createStatement();
178                         ResultSet rs = theStorageObject.executeSql(stmt,"select content_data from content where id="+getId());
179                         if(rs!=null) {
180                                 if (rs.next()) {
181                                          content_data = rs.getBytes(1);
182                                 }
183                                 rs.close();
184                         }
185                 }
186                 catch (Exception e) {theLog.printError("EntityContent :: getContent failed! "+e.toString());}
187                 finally {
188                         try {con.setAutoCommit(true); } catch (Exception e) {;}
189                         theStorageObject.freeConnection(con,stmt); }
190
191                 return StringUtil.encodeHtml(StringUtil.unquote( new  String(content_data) ));
192         }
193 */
194
195         /**
196          * fetches all the comments belonging to an article
197          *
198          * @return freemarker.template.SimpleList
199          */
200         public SimpleList getComments() {
201                 return ((DatabaseContent)theStorageObject).getComments(this);
202         }
203
204
205 }