jlint debugging...
[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         // constructors
29
30         public EntityContent()
31         {
32                 super();
33     //content_data is now filed-type "text"
34                 //streamedInput = new ArrayList();
35                 //streamedInput.add("content_data");
36         }
37
38         public EntityContent(StorageObject theStorage) {
39                 this();
40                 setStorage(theStorage);
41         }
42
43         //
44         // methods
45
46  /**
47         * set is_produced flag for the article
48         */
49
50         public void setProduced(boolean yesno) throws StorageObjectException
51         {
52                 Connection con=null;Statement stmt=null;
53                 String value = (yesno) ? "1":"0";
54                 String sql = "update content set is_produced='" + value + "' where id='" + getId()+"'";
55                 try {
56                         con = theStorageObject.getPooledCon();
57                         /** @todo should be preparedStatement: faster!! */
58                         stmt = con.createStatement();
59                         theStorageObject.executeUpdate(stmt,sql);
60                 } catch (StorageObjectException e) {
61             throwStorageObjectException(e, "\n -- set produced failed");
62                 } catch (SQLException e) {
63             throwStorageObjectException(e, "\n -- set produced failed");
64                 } finally {
65                         theStorageObject.freeConnection(con,stmt);
66                 }
67         }
68
69
70  /**
71         * make openposting to newswire
72         */
73
74         public void newswire() throws StorageObjectException
75         {
76                 String sql = "update content set to_article_type='1', is_produced='0' where id='" + getId()+"'";
77                 try {
78                                 theStorageObject.executeUpdate(sql);
79                 } catch (StorageObjectException e) {
80             throwStorageObjectException(e, "\n -- newswire failed");
81                 } catch (SQLException e) {
82             throwStorageObjectException(e, "\n -- newswire failed");
83                 }
84         }
85
86
87  /**
88         * dettach from media
89         */
90         public void dettach(String cid,String mid) throws StorageObjectException
91         {
92                 if (mid!=null){
93                         try{
94                                 DatabaseContentToMedia.getInstance().delete(cid,mid);
95                         } catch (Exception e){
96                 throwStorageObjectException(e, "\n -- failed to get instance");
97                         }
98                         //set Content to unproduced
99                         setProduced(false);
100                 }
101         }
102
103  /**
104         * attach to media
105         */
106
107         public void attach(String mid) throws StorageObjectException
108         {
109                 if (mid!=null) {
110                         //write media-id mid and content-id in table content_x_media
111                         try{
112                                 DatabaseContentToMedia.getInstance().addMedia(getId(),mid);
113                         } catch(StorageObjectException e){
114                                 throwStorageObjectException(e, "attach: could not get the instance");
115                         }
116                         //set Content to unproduced
117                         setProduced(false);
118                 }       else {
119                         theLog.printError("EntityContent: attach without mid");
120                 }
121         }
122
123         /**
124          * overridden method getValue to include formatted date into every
125          * entityContent
126          */
127
128         public String getValue(String field)
129   {
130     if (field!=null && field.equals("date_formatted"))
131     {
132                 if (hasValueForField("date"))
133         return StringUtil.webdbDate2readableDate(getValue("date"));
134       else return null;
135                 }
136     else
137       return super.getValue(field);
138         }
139
140         /**
141          * overridden method setValues to patch creator_main_url
142          */
143         public void setValues(HashMap theStringValues) {
144                 if (theStringValues != null) {
145                         if (theStringValues.containsKey("creator_main_url")){
146                                 if (((String)theStringValues.get("creator_main_url")).equalsIgnoreCase("http://")){
147                                         theStringValues.remove("creator_main_url");
148         } else if (!((String)theStringValues.get("creator_main_url")).startsWith("http://")){
149           theStringValues.put("creator_main_url","http://"+((String)theStringValues.get("creator_main_url")));
150         }
151       }
152                 }
153                 super.setValues(theStringValues);
154         }
155
156         /**
157          * return the content_data as string
158          * is obsolete, because content_data is now sql-type text
159
160   public String getContentData()
161         {
162                 Connection con=null;Statement stmt=null;
163                 byte[] content_data=null;
164
165                 try {
166                         con = theStorageObject.getPooledCon();
167                         con.setAutoCommit(false);
168                         stmt = con.createStatement();
169                         ResultSet rs = theStorageObject.executeSql(stmt,"select content_data from content where id="+getId());
170                         if(rs!=null) {
171                                 if (rs.next()) {
172                                          content_data = rs.getBytes(1);
173                                 }
174                                 rs.close();
175                         }
176                 }
177                 catch (Exception e) {theLog.printError("EntityContent :: getContent failed! "+e.toString());}
178                 finally {
179                         try {con.setAutoCommit(true); } catch (Exception e) {;}
180                         theStorageObject.freeConnection(con,stmt); }
181
182                 return StringUtil.encodeHtml(StringUtil.unquote( new  String(content_data) ));
183         }
184 */
185
186         /**
187          * fetches all the comments belonging to an article
188          *
189          * @return freemarker.template.SimpleList
190          */
191         public SimpleList getComments() throws StorageObjectException {
192                 return ((DatabaseContent)theStorageObject).getComments(this);
193         }
194
195
196 }