7935b53ee3bfaeec5fe69f2c230f2b3e4bfb3d6a
[mir.git] / source / mircoders / entity / EntityContent.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31
32 package mircoders.entity;
33
34 import java.lang.*;
35 import java.io.*;
36 import java.util.*;
37 import java.sql.*;
38 import java.lang.reflect.*;
39
40 import freemarker.template.*;
41
42 import mir.entity.*;
43 import mir.misc.*;
44 import mir.media.*;
45 import mir.storage.*;
46
47 import mircoders.storage.*;
48
49 /**
50  * this class implements mapping of one line of the database table content
51  * to a java object
52  *
53  * @version $Id: EntityContent.java,v 1.13 2002/12/02 12:33:23 zapata Exp $
54  * @author mir-coders group
55  *
56  */
57
58
59 public class EntityContent extends Entity
60 {
61
62   String mirconf_extLinkName  = MirConfig.getProp("Producer.ExtLinkName");
63   String mirconf_intLinkName  = MirConfig.getProp("Producer.IntLinkName");
64   String mirconf_mailLinkName = MirConfig.getProp("Producer.MailLinkName");
65   String mirconf_imageRoot    = MirConfig.getProp("Producer.ImageRoot");
66
67   //this should always be transient i.e it can never be stored in the db
68   //or ObjectStore. (so the ObjectStore should only be caching what comes
69   //directly out of the DB. @todo confirm this with rk. -mh
70   HashMap _entCache = new HashMap();
71   Boolean _hasMedia = null;
72
73   // constructors
74
75   public EntityContent()
76   {
77     super();
78     //content_data is now filed-type "text"
79     //streamedInput = new ArrayList();
80     //streamedInput.add("content_data");
81   }
82
83   public EntityContent(StorageObject theStorage) {
84     this();
85     setStorage(theStorage);
86   }
87
88   //
89   // methods
90
91   /**
92    * set is_produced flag for the article
93    */
94
95   public void setProduced(boolean yesno) throws StorageObjectException
96   {
97     String value = (yesno) ? "1":"0";
98     if (value.equals( getValue("is_produced") )) return;
99
100     Connection con=null;Statement stmt=null;
101     String sql = "update content set is_produced='" + value + "' where id='" + getId()+"'";
102     try {
103       con = theStorageObject.getPooledCon();
104       /** @todo should be preparedStatement: faster!! */
105       stmt = con.createStatement();
106       theStorageObject.executeUpdate(stmt,sql);
107     }
108     catch (StorageObjectException e) {
109       throwStorageObjectException(e, "\n -- set produced failed");
110     }
111     catch (SQLException e) {
112       throwStorageObjectException(e, "\n -- set produced failed");
113     }
114     finally {
115       theStorageObject.freeConnection(con,stmt);
116     }
117   }
118
119
120   /**
121    * make openposting to newswire
122    */
123
124   public void newswire() throws StorageObjectException
125   {
126     String sql = "update content set to_article_type='1', is_produced='0' where id='" + getId()+"'";
127     try {
128       theStorageObject.executeUpdate(sql);
129     } catch (StorageObjectException e) {
130       throwStorageObjectException(e, "\n -- newswire failed");
131     } catch (SQLException e) {
132       throwStorageObjectException(e, "\n -- newswire failed");
133     }
134   }
135
136
137   /**
138    * dettach from media
139    */
140   public void dettach(String cid,String mid) throws StorageObjectException
141   {
142     if (mid!=null){
143       try{
144         DatabaseContentToMedia.getInstance().delete(cid,mid);
145       }
146       catch (Exception e){
147         throwStorageObjectException(e, "\n -- failed to get instance");
148       }
149
150       //set Content to unproduced
151       setProduced(false);
152     }
153   }
154
155   /**
156    * attach to media
157    */
158
159   public void attach(String mid) throws StorageObjectException
160   {
161     if (mid!=null) {
162       //write media-id mid and content-id in table content_x_media
163       try{
164         DatabaseContentToMedia.getInstance().addMedia(getId(),mid);
165       } catch(StorageObjectException e){
166         throwStorageObjectException(e, "attach: could not get the instance");
167       }
168       //set Content to unproduced
169       setProduced(false);
170     }   else {
171       theLog.printError("EntityContent: attach without mid");
172     }
173   }
174
175   /**
176    * overridden method getValue to include formatted date into every
177    * entityContent
178    */
179
180   public TemplateModel get(java.lang.String key) throws TemplateModelException
181   {
182     if (key!=null) {
183       if (_entCache.containsKey(key)) {
184         return (TemplateModel)_entCache.get(key);
185       }
186       if (key.equals("to_comments")) {
187         try {
188           _entCache.put(key, getComments());
189           return (TemplateModel)_entCache.get(key);
190         }
191         catch (Exception ex) {
192           theLog.printWarning("-- getComments: could not fetch data " + ex.toString());
193
194           throw new TemplateModelException(ex.toString());
195         }
196       }
197       if (key.equals("to_media_images")) {
198         try {
199           _entCache.put(key, getImagesForContent());
200           return (TemplateModel)_entCache.get(key);
201         }
202         catch (Exception ex) {
203           theLog.printWarning("-- getImagesForContent: could not fetch data " + ex.toString());
204           throw new TemplateModelException(ex.toString());
205         }
206       }
207       if (key.equals("to_media_audio")) {
208         try {
209           _entCache.put(key, getAudioForContent());
210           return (TemplateModel)_entCache.get(key);
211         }
212         catch (Exception ex) {
213           theLog.printWarning("-- getAudioForContent: could not fetch data " + ex.toString());
214           throw new TemplateModelException(ex.toString());
215         }
216       }
217       if (key.equals("to_media_video")) {
218         try {
219           _entCache.put(key, getVideoForContent());
220           return (TemplateModel)_entCache.get(key);
221         }
222         catch (Exception ex) {
223           theLog.printWarning("-- getVideoForContent: could not fetch data " + ex.toString());
224           throw new TemplateModelException(ex.toString());
225         }
226       }
227       if (key.equals("to_media_other")) {
228         try {
229           _entCache.put(key, getOtherMediaForContent());
230           return (TemplateModel)_entCache.get(key);
231         }
232         catch (Exception ex) {
233           theLog.printWarning("-- getOtherMediaForContent: could not fetch data " + ex.toString());
234           throw new TemplateModelException(ex.toString());
235         }
236       }
237       else if (key.equals("to_topics")) {
238         try {
239           _entCache.put(key,
240                         DatabaseContentToTopics.getInstance().getTopics(this));
241           return (TemplateModel)_entCache.get(key);
242         }
243         catch (Exception ex) {
244           theLog.printWarning("-- getTopics: could not fetch data " + ex.toString());
245           throw new TemplateModelException(ex.toString());
246         }
247       }
248       else {
249         return new SimpleScalar(getValue(key));
250       }
251
252     }
253     return null;
254   }
255
256   /**
257    * overridden method setValues to patch creator_main_url
258    */
259   public void setValues(HashMap theStringValues) {
260     if (theStringValues != null) {
261       if (theStringValues.containsKey("creator_main_url")){
262         if (((String)theStringValues.get("creator_main_url")).equalsIgnoreCase("http://")){
263           theStringValues.remove("creator_main_url");
264         }
265         else if (!((String)theStringValues.get("creator_main_url")).startsWith("http://")){
266           theStringValues.put("creator_main_url","http://"+((String)theStringValues.get("creator_main_url")));
267         }
268       }
269     }
270     super.setValues(theStringValues);
271   }
272
273   /**
274    * fetches all the comments belonging to an article
275    *
276    * @return freemarker.template.SimpleList
277    */
278   private EntityList getComments() throws StorageObjectException {
279     return ((DatabaseContent)theStorageObject).getComments(this);
280   }
281
282   private boolean hasMedia() throws StorageObjectException
283   {
284     if (_hasMedia == null) {
285       _hasMedia =
286           new Boolean(DatabaseContentToMedia.getInstance().hasMedia(this));
287     }
288     return _hasMedia.booleanValue();
289   }
290
291   //######## @todo all of the following getBlahForContent should have
292   // and optimized version where LIMIT=1 sql for list view.
293   private EntityList getImagesForContent()
294       throws StorageObjectException, TemplateModelException
295   {
296     if (hasMedia())
297       return DatabaseContentToMedia.getInstance().getImages(this);
298     else
299       return null;
300   }
301
302   private EntityList getAudioForContent()
303       throws StorageObjectException, TemplateModelException
304   {
305     if (hasMedia())
306       return DatabaseContentToMedia.getInstance().getAudio(this) ;
307     else
308       return null;
309   }
310
311   private EntityList getVideoForContent()
312       throws StorageObjectException, TemplateModelException
313   {
314     if (hasMedia())
315       return DatabaseContentToMedia.getInstance().getVideo(this) ;
316     else
317       return null;
318   }
319
320   private EntityList getOtherMediaForContent()
321       throws StorageObjectException, TemplateModelException
322   {
323     if (hasMedia())
324       return DatabaseContentToMedia.getInstance().getOther(this);
325     else
326       return null;
327   }
328
329 }