restructuring producer
[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     String returnField = null;
131     if (field!=null)
132     {
133       if (field.equals("date_formatted"))
134       {
135                   if (hasValueForField("date"))
136         returnField = StringUtil.webdbDate2readableDate(getValue("date"));
137                 }
138       else if (field.equals("description_parsed"))
139         returnField = getDescriptionParsed();
140       else if (field.equals("content_data_parsed"))
141         returnField = getContentDataParsed();
142       else
143         return super.getValue(field);
144     }
145     return returnField;
146
147
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   private String getContentDataParsed() {
168     String returnField = getValue("content_data");
169     if (returnField!=null & returnField.length()>0 ) {
170       returnField=StringUtil.deleteForbiddenTags(returnField);
171       //create http-links and email-links
172       if (getValue("is_html").equals("0")) {
173         /** @todo the config stuff should be moved to StringUtil */
174         String extLinkName = MirConfig.getProp("Producer.ExtLinkName");
175         String intLinkName = MirConfig.getProp("Producer.IntLinkName");
176         String mailLinkName = MirConfig.getProp("Producer.MailLinkName");
177         String imageRoot = MirConfig.getProp("Producer.ImageRoot");
178         returnField = StringUtil.createHTML(returnField,imageRoot,mailLinkName,extLinkName,intLinkName);
179       }
180       returnField = StringUtil.decodeHTMLinTags(returnField);
181     }
182     return returnField;
183   }
184
185
186   private String getDescriptionParsed() {
187     String returnField = getValue("description");
188     if (returnField != null && returnField.length()>0) {
189       returnField = StringUtil.deleteForbiddenTags(returnField);
190       if (getValue("is_html").equals("0")) {
191         /** @todo the config stuff should be moved to StringUtil */
192         String extLinkName = MirConfig.getProp("Producer.ExtLinkName");
193         String intLinkName = MirConfig.getProp("Producer.IntLinkName");
194         String mailLinkName = MirConfig.getProp("Producer.MailLinkName");
195         String imageRoot = MirConfig.getProp("Producer.ImageRoot");
196         returnField = StringUtil.createHTML(returnField,imageRoot,mailLinkName,extLinkName,intLinkName);
197       }
198       returnField = StringUtil.decodeHTMLinTags(returnField);
199     }
200     return returnField;
201   }
202
203         /**
204          * fetches all the comments belonging to an article
205          *
206          * @return freemarker.template.SimpleList
207          */
208         public SimpleList getComments() throws StorageObjectException {
209                 return ((DatabaseContent)theStorageObject).getComments(this);
210         }
211
212
213 }