39b0428bdfaffdc602f5c6bac3d344d95047692b
[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 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             throwStorageObjectException(e, "\n -- set produced failed");
70                 } catch (SQLException e) {
71             throwStorageObjectException(e, "\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() throws StorageObjectException
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             throwStorageObjectException(e, "\n -- newswire failed");
89                 } catch (SQLException e) {
90             throwStorageObjectException(e, "\n -- newswire failed");
91                 }
92         }
93
94
95  /**
96         * dettach from media
97         */
98         public void dettach(String cid,String mid) throws StorageObjectException
99         {
100                 if (mid!=null){
101                         try{
102                                 DatabaseContentToMedia.getInstance().delete(cid,mid);
103                         } catch (Exception e){
104                 throwStorageObjectException(e, "\n -- failed to get instance");
105                         }
106                         //set Content to unproduced
107                         setProduced(false);
108                 }
109         }
110
111  /**
112         * attach to media
113         */
114
115         public void attach(String mid) throws StorageObjectException
116         {
117                 if (mid!=null) {
118                         //write media-id mid and content-id in table content_x_media
119                         try{
120                                 DatabaseContentToMedia.getInstance().addMedia(getId(),mid);
121                         } catch(StorageObjectException e){
122                                 throwStorageObjectException(e, "attach: could not get the instance");
123                         }
124                         //set Content to unproduced
125                         setProduced(false);
126                 }       else {
127                         theLog.printError("EntityContent: attach without mid");
128                 }
129         }
130
131         /**
132          * overridden method getValue to include formatted date into every
133          * entityContent
134          */
135
136         public String getValue(String field)
137   {
138     if (field!=null && field.equals("date_formatted"))
139     {
140                 if (hasValueForField("date"))
141         return StringUtil.webdbDate2readableDate(getValue("date"));
142       else return null;
143                 }
144     else
145       return super.getValue(field);
146         }
147
148         /**
149          * overridden method setValues to patch creator_main_url
150          */
151         public void setValues(HashMap theStringValues) {
152                 if (theStringValues != null) {
153                         if (theStringValues.containsKey("creator_main_url")){
154                                 if (((String)theStringValues.get("creator_main_url")).equalsIgnoreCase("http://")){
155                                         theStringValues.remove("creator_main_url");
156         } else if (!((String)theStringValues.get("creator_main_url")).startsWith("http://")){
157           theStringValues.put("creator_main_url","http://"+((String)theStringValues.get("creator_main_url")));
158         }
159       }
160                 }
161                 super.setValues(theStringValues);
162         }
163
164         /**
165          * return the content_data as string
166          * is obsolete, because content_data is now sql-type text
167
168   public String getContentData()
169         {
170                 Connection con=null;Statement stmt=null;
171                 byte[] content_data=null;
172
173                 try {
174                         con = theStorageObject.getPooledCon();
175                         con.setAutoCommit(false);
176                         stmt = con.createStatement();
177                         ResultSet rs = theStorageObject.executeSql(stmt,"select content_data from content where id="+getId());
178                         if(rs!=null) {
179                                 if (rs.next()) {
180                                          content_data = rs.getBytes(1);
181                                 }
182                                 rs.close();
183                         }
184                 }
185                 catch (Exception e) {theLog.printError("EntityContent :: getContent failed! "+e.toString());}
186                 finally {
187                         try {con.setAutoCommit(true); } catch (Exception e) {;}
188                         theStorageObject.freeConnection(con,stmt); }
189
190                 return StringUtil.encodeHtml(StringUtil.unquote( new  String(content_data) ));
191         }
192 */
193
194         /**
195          * fetches all the comments belonging to an article
196          *
197          * @return freemarker.template.SimpleList
198          */
199         public SimpleList getComments() throws StorageObjectException {
200                 return ((DatabaseContent)theStorageObject).getComments(this);
201         }
202
203
204 }