restructuring producer content
[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 import java.lang.reflect.*;
8
9 import freemarker.template.*;
10
11 import mir.entity.*;
12 import mir.misc.*;
13 import mir.media.*;
14 import mir.storage.*;
15
16 import mircoders.storage.*;
17
18 /**
19  * this class implements mapping of one line of the database table content
20  * to a java object
21  *
22  * @author RK
23  * @version 2001
24  */
25
26
27 public class EntityContent extends Entity
28 {
29
30   String mirconf_extLinkName  = MirConfig.getProp("Producer.ExtLinkName");
31   String mirconf_intLinkName  = MirConfig.getProp("Producer.IntLinkName");
32   String mirconf_mailLinkName = MirConfig.getProp("Producer.MailLinkName");
33   String mirconf_imageRoot    = MirConfig.getProp("Producer.ImageRoot");
34
35         // constructors
36
37         public EntityContent()
38         {
39                 super();
40     //content_data is now filed-type "text"
41                 //streamedInput = new ArrayList();
42                 //streamedInput.add("content_data");
43         }
44
45         public EntityContent(StorageObject theStorage) {
46                 this();
47                 setStorage(theStorage);
48         }
49
50         //
51         // methods
52
53  /**
54         * set is_produced flag for the article
55         */
56
57         public void setProduced(boolean yesno) throws StorageObjectException
58         {
59                 Connection con=null;Statement stmt=null;
60                 String value = (yesno) ? "1":"0";
61                 String sql = "update content set is_produced='" + value + "' where id='" + getId()+"'";
62                 try {
63                         con = theStorageObject.getPooledCon();
64                         /** @todo should be preparedStatement: faster!! */
65                         stmt = con.createStatement();
66                         theStorageObject.executeUpdate(stmt,sql);
67                 } catch (StorageObjectException e) {
68             throwStorageObjectException(e, "\n -- set produced failed");
69                 } catch (SQLException e) {
70             throwStorageObjectException(e, "\n -- set produced failed");
71                 } finally {
72                         theStorageObject.freeConnection(con,stmt);
73                 }
74         }
75
76
77  /**
78         * make openposting to newswire
79         */
80
81         public void newswire() throws StorageObjectException
82         {
83                 String sql = "update content set to_article_type='1', is_produced='0' where id='" + getId()+"'";
84                 try {
85                                 theStorageObject.executeUpdate(sql);
86                 } catch (StorageObjectException e) {
87             throwStorageObjectException(e, "\n -- newswire failed");
88                 } catch (SQLException e) {
89             throwStorageObjectException(e, "\n -- newswire failed");
90                 }
91         }
92
93
94  /**
95         * dettach from media
96         */
97         public void dettach(String cid,String mid) throws StorageObjectException
98         {
99                 if (mid!=null){
100                         try{
101                                 DatabaseContentToMedia.getInstance().delete(cid,mid);
102                         } catch (Exception e){
103                 throwStorageObjectException(e, "\n -- failed to get instance");
104                         }
105                         //set Content to unproduced
106                         setProduced(false);
107                 }
108         }
109
110  /**
111         * attach to media
112         */
113
114         public void attach(String mid) throws StorageObjectException
115         {
116                 if (mid!=null) {
117                         //write media-id mid and content-id in table content_x_media
118                         try{
119                                 DatabaseContentToMedia.getInstance().addMedia(getId(),mid);
120                         } catch(StorageObjectException e){
121                                 throwStorageObjectException(e, "attach: could not get the instance");
122                         }
123                         //set Content to unproduced
124                         setProduced(false);
125                 }       else {
126                         theLog.printError("EntityContent: attach without mid");
127                 }
128         }
129
130         /**
131          * overridden method getValue to include formatted date into every
132          * entityContent
133          */
134
135         public String getValue(String field)
136   {
137     String returnField = null;
138     if (field!=null)
139     {
140       if (field.equals("date_formatted"))
141       {
142                   if (hasValueForField("date"))
143         returnField = StringUtil.webdbDate2readableDate(getValue("date"));
144                 }
145       else if (field.equals("description_parsed"))
146         returnField = getDescriptionParsed();
147       else if (field.equals("content_data_parsed"))
148         returnField = getContentDataParsed();
149       else
150         return super.getValue(field);
151     }
152     return returnField;
153         }
154
155   public TemplateModel get(java.lang.String key) throws TemplateModelException
156   {
157     if (key!=null) {
158       if (key.equals("to_comments")) {
159         try {
160           return getComments();
161         }
162         catch (Exception ex) {
163           theLog.printWarning("-- getComments: could not fetch data " + ex.toString());
164           throw new TemplateModelException(ex.toString());
165         }
166       }
167       if (key.equals("to_media")) {
168         try {
169           return getUploadedMediaForContent();
170         }
171         catch (Exception ex) {
172           theLog.printWarning("-- getUploadedMediaForContent: could not fetch data " + ex.toString());
173           throw new TemplateModelException(ex.toString());
174         }
175       }
176       else if (key.equals("to_topics")) {
177         try {
178           HTMLTemplateProcessor.makeSimpleList(DatabaseContentToTopics.getInstance().getTopics(this));
179         }
180         catch (Exception ex) {
181           theLog.printWarning("-- getTopics: could not fetch data " + ex.toString());
182           throw new TemplateModelException(ex.toString());
183         }
184       }
185       else return new SimpleScalar(getValue(key));
186
187     }
188     return null;
189   }
190
191
192
193
194         /**
195          * overridden method setValues to patch creator_main_url
196          */
197         public void setValues(HashMap theStringValues) {
198                 if (theStringValues != null) {
199                         if (theStringValues.containsKey("creator_main_url")){
200                                 if (((String)theStringValues.get("creator_main_url")).equalsIgnoreCase("http://")){
201                                         theStringValues.remove("creator_main_url");
202         } else if (!((String)theStringValues.get("creator_main_url")).startsWith("http://")){
203           theStringValues.put("creator_main_url","http://"+((String)theStringValues.get("creator_main_url")));
204         }
205       }
206                 }
207                 super.setValues(theStringValues);
208         }
209
210
211   private String getContentDataParsed() {
212     String returnField = getValue("content_data");
213     if (returnField!=null & returnField.length()>0 ) {
214       returnField=StringUtil.deleteForbiddenTags(returnField);
215       //create http-links and email-links
216       if (getValue("is_html").equals("0")) {
217         returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
218                                             mirconf_mailLinkName,mirconf_extLinkName,
219                                             mirconf_intLinkName);
220       }
221       returnField = StringUtil.decodeHTMLinTags(returnField);
222     }
223     return returnField;
224   }
225
226
227   private String getDescriptionParsed() {
228     String returnField = getValue("description");
229     if (returnField != null && returnField.length()>0) {
230       returnField = StringUtil.deleteForbiddenTags(returnField);
231       if (getValue("is_html").equals("0")) {
232         returnField = StringUtil.createHTML(returnField,mirconf_imageRoot,
233                                             mirconf_mailLinkName,mirconf_extLinkName,
234                                             mirconf_intLinkName);
235       }
236       returnField = StringUtil.decodeHTMLinTags(returnField);
237     }
238     return returnField;
239   }
240
241         /**
242          * fetches all the comments belonging to an article
243          *
244          * @return freemarker.template.SimpleList
245          */
246         public SimpleList getComments() throws StorageObjectException {
247                 return ((DatabaseContent)theStorageObject).getComments(this);
248         }
249
250   private SimpleList getUploadedMediaForList() throws StorageObjectException
251   { return null; }
252
253   private SimpleList getUploadedMediaForContent()
254     throws StorageObjectException, TemplateModelException
255   {
256     SimpleList returnList=null;
257     EntityList currentMediaList = DatabaseContentToMedia.getInstance().getUploadedMedia(this);
258     if (currentMediaList!=null && currentMediaList.getCount()>=1)
259     {
260       Entity              upMedia,mediaType;
261       SimpleHash          upMediaSimpleHash;
262       Class               mediaHandlerClass,mediaStorageClass;
263       String              mediaStorageName,mediaHandlerName=null;
264       MirMedia            mediaHandler=null;
265       Database            mediaStorage=null;
266
267       returnList = new SimpleList();
268
269       for (int n=0; n < currentMediaList.size();n++) {
270         upMedia = currentMediaList.elementAt(n);
271         upMediaSimpleHash = HTMLTemplateProcessor.makeSimpleHash(upMedia);
272         mediaType = ((EntityMedia)upMedia).getMediaType();
273         //must be a non-existant to_media_type entry..
274         if (mediaType != null) {
275           mediaHandlerName = mediaType.getValue("classname");
276           mediaStorageName = mediaType.getValue("tablename");
277           try {
278             mediaStorageClass = Class.forName("mircoders.storage.Database"+mediaStorageName);
279             mediaHandlerClass = Class.forName("mir.media.MediaHandler"+mediaHandlerName);
280             mediaHandler = (MirMedia)mediaHandlerClass.newInstance();
281             Method m = mediaStorageClass.getMethod("getInstance", null);
282             mediaStorage = (Database)m.invoke(null, null);
283           }
284           catch (Exception ex) {
285             throw new TemplateModelException(ex.toString());
286           }
287
288           //we most likely need further info
289           upMedia = mediaStorage.selectById(upMedia.getId());
290           try {
291             upMediaSimpleHash.put("url", mediaHandler.getURL(upMedia, mediaType));
292           }
293           catch (Exception ex) {
294             throw new TemplateModelException(ex.toString());
295           }
296           upMediaSimpleHash.put("type",mediaType.getValue("classname"));
297           returnList.add(upMediaSimpleHash);
298         } //end if media_type != null
299       } //end for
300     } //end if currentMediaList != null
301     return returnList;
302   }
303
304 }