fixes:
[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.sql.Connection;
35 import java.sql.SQLException;
36 import java.sql.Statement;
37 import java.util.Map;
38 import java.util.HashMap;
39
40 import freemarker.template.SimpleScalar;
41 import freemarker.template.TemplateModel;
42 import freemarker.template.TemplateModelException;
43
44 import mir.log.LoggerWrapper;
45 import mir.entity.Entity;
46 import mir.entity.EntityList;
47 import mir.storage.StorageObject;
48 import mir.storage.StorageObjectExc;
49 import mir.storage.StorageObjectFailure;
50 import mircoders.storage.DatabaseContent;
51 import mircoders.storage.DatabaseContentToMedia;
52 import mircoders.storage.DatabaseContentToTopics;
53
54 /**
55  * this class implements mapping of one line of the database table content
56  * to a java object
57  *
58  * @version $Id: EntityContent.java,v 1.16 2003/03/04 22:00:53 zapata Exp $
59  * @author mir-coders group
60  *
61  */
62
63
64 public class EntityContent extends Entity
65 {
66
67   String mirconf_extLinkName  = configuration.getString("Producer.ExtLinkName");
68   String mirconf_intLinkName  = configuration.getString("Producer.IntLinkName");
69   String mirconf_mailLinkName = configuration.getString("Producer.MailLinkName");
70   String mirconf_imageRoot    = configuration.getString("Producer.ImageRoot");
71
72   //this should always be transient i.e it can never be stored in the db
73   //or ObjectStore. (so the ObjectStore should only be caching what comes
74   //directly out of the DB. @todo confirm this with rk. -mh
75   Map _entCache = new HashMap();
76   Boolean _hasMedia = null;
77
78   // constructors
79
80   public EntityContent()
81   {
82     super();
83
84     logger = new LoggerWrapper("Entity.Content");
85   }
86
87   public EntityContent(StorageObject theStorage) {
88     this();
89     setStorage(theStorage);
90   }
91
92   //
93   // methods
94
95   /**
96    * set is_produced flag for the article
97    */
98
99   public void setProduced(boolean yesno) throws StorageObjectFailure
100   {
101     String value = (yesno) ? "1":"0";
102     if (value.equals( getValue("is_produced") )) return;
103
104     Connection con=null;Statement stmt=null;
105     String sql = "update content set is_produced='" + value + "' where id='" + getId()+"'";
106     try {
107       con = theStorageObject.getPooledCon();
108       /** @todo should be preparedStatement: faster!! */
109       stmt = con.createStatement();
110       theStorageObject.executeUpdate(stmt,sql);
111     }
112     catch (StorageObjectFailure e) {
113       throwStorageObjectFailure(e, "\n -- set produced failed");
114     }
115     catch (SQLException e) {
116       throwStorageObjectFailure(e, "\n -- set produced failed");
117     }
118     finally {
119       theStorageObject.freeConnection(con,stmt);
120     }
121   }
122
123
124   /**
125    * make openposting to newswire
126    */
127
128   public void newswire() throws StorageObjectFailure
129   {
130     String sql = "update content set to_article_type='1', is_produced='0' where id='" + getId()+"'";
131     try {
132       theStorageObject.executeUpdate(sql);
133     } catch (StorageObjectFailure e) {
134       throwStorageObjectFailure(e, "\n -- newswire failed");
135     } catch (SQLException e) {
136       throwStorageObjectFailure(e, "\n -- newswire failed");
137     }
138   }
139
140
141   /**
142    * dettach from media
143    */
144   public void dettach(String cid,String mid) throws StorageObjectFailure
145   {
146     if (mid!=null){
147       try{
148         DatabaseContentToMedia.getInstance().delete(cid,mid);
149       }
150       catch (Exception e){
151         throwStorageObjectFailure(e, "\n -- failed to get instance");
152       }
153
154       //set Content to unproduced
155       setProduced(false);
156     }
157   }
158
159   /**
160    * attach to media
161    */
162
163   public void attach(String mid) throws StorageObjectFailure
164   {
165     if (mid!=null) {
166       //write media-id mid and content-id in table content_x_media
167       try{
168         DatabaseContentToMedia.getInstance().addMedia(getId(),mid);
169       } catch(StorageObjectFailure e){
170         throwStorageObjectFailure(e, "attach: could not get the instance");
171       }
172       //set Content to unproduced
173       setProduced(false);
174     }
175     else {
176       logger.error("EntityContent: attach without mid");
177     }
178   }
179
180   /**
181    * overridden method getValue to include formatted date into every
182    * entityContent
183    */
184
185   public TemplateModel get(java.lang.String key) throws TemplateModelException
186   {
187     if (key!=null) {
188       if (_entCache.containsKey(key)) {
189         return (TemplateModel)_entCache.get(key);
190       }
191       if (key.equals("to_comments")) {
192         try {
193           _entCache.put(key, getComments());
194           return (TemplateModel)_entCache.get(key);
195         }
196         catch (Exception ex) {
197           logger.warn("EntityContent.getComments: could not fetch data " + ex.toString());
198
199           throw new TemplateModelException(ex.toString());
200         }
201       }
202       if (key.equals("to_media_images")) {
203         try {
204           _entCache.put(key, getImagesForContent());
205           return (TemplateModel)_entCache.get(key);
206         }
207         catch (Exception ex) {
208           logger.warn("EntityContent.getImagesForContent: could not fetch data " + ex.toString());
209           throw new TemplateModelException(ex.toString());
210         }
211       }
212       if (key.equals("to_media_audio")) {
213         try {
214           _entCache.put(key, getAudioForContent());
215           return (TemplateModel)_entCache.get(key);
216         }
217         catch (Exception ex) {
218           logger.warn("EntityContent.getAudioForContent: could not fetch data " + ex.toString());
219           throw new TemplateModelException(ex.toString());
220         }
221       }
222       if (key.equals("to_media_video")) {
223         try {
224           _entCache.put(key, getVideoForContent());
225           return (TemplateModel)_entCache.get(key);
226         }
227         catch (Exception ex) {
228           logger.warn("EntityContent.getVideoForContent: could not fetch data " + ex.toString());
229           throw new TemplateModelException(ex.toString());
230         }
231       }
232       if (key.equals("to_media_other")) {
233         try {
234           _entCache.put(key, getOtherMediaForContent());
235           return (TemplateModel)_entCache.get(key);
236         }
237         catch (Exception ex) {
238           logger.warn("EntityContent.getOtherMediaForContent: could not fetch data " + ex.toString());
239           throw new TemplateModelException(ex.toString());
240         }
241       }
242       else if (key.equals("to_topics")) {
243         try {
244           _entCache.put(key,
245                         DatabaseContentToTopics.getInstance().getTopics(this));
246           return (TemplateModel)_entCache.get(key);
247         }
248         catch (Exception ex) {
249           logger.warn("EntityContent.getTopics: could not fetch data " + ex.toString());
250           throw new TemplateModelException(ex.toString());
251         }
252       }
253       else {
254         return new SimpleScalar(getValue(key));
255       }
256
257     }
258     return null;
259   }
260
261   /**
262    * overridden method setValues to patch creator_main_url
263    */
264   public void setValues(Map theStringValues) {
265     if (theStringValues != null) {
266       if (theStringValues.containsKey("creator_main_url")){
267         if (((String)theStringValues.get("creator_main_url")).equalsIgnoreCase("http://")){
268           theStringValues.remove("creator_main_url");
269         }
270         else if (!((String)theStringValues.get("creator_main_url")).startsWith("http://")){
271           theStringValues.put("creator_main_url","http://"+((String)theStringValues.get("creator_main_url")));
272         }
273       }
274     }
275     super.setValues(theStringValues);
276   }
277
278   /**
279    * fetches all the comments belonging to an article
280    *
281    * @return freemarker.template.SimpleList
282    */
283   private EntityList getComments() throws StorageObjectFailure {
284     return ((DatabaseContent)theStorageObject).getComments(this);
285   }
286
287   private boolean hasMedia() throws StorageObjectFailure
288   {
289     if (_hasMedia == null) {
290       try {
291         _hasMedia =
292             new Boolean(DatabaseContentToMedia.getInstance().hasMedia(this));
293       } catch (StorageObjectExc e) {
294         throw new StorageObjectFailure(e);
295       }
296     }
297     return _hasMedia.booleanValue();
298   }
299
300   //######## @todo all of the following getBlahForContent should have
301   // and optimized version where LIMIT=1 sql for list view.
302   private EntityList getImagesForContent()
303       throws StorageObjectFailure, TemplateModelException
304   {
305     if (hasMedia())
306       return DatabaseContentToMedia.getInstance().getImages(this);
307     else
308       return null;
309   }
310
311   private EntityList getAudioForContent()
312       throws StorageObjectFailure, TemplateModelException
313   {
314     if (hasMedia())
315       return DatabaseContentToMedia.getInstance().getAudio(this) ;
316     else
317       return null;
318   }
319
320   private EntityList getVideoForContent()
321       throws StorageObjectFailure, TemplateModelException
322   {
323     if (hasMedia())
324       return DatabaseContentToMedia.getInstance().getVideo(this) ;
325     else
326       return null;
327   }
328
329   private EntityList getOtherMediaForContent()
330       throws StorageObjectFailure, TemplateModelException
331   {
332     if (hasMedia())
333       return DatabaseContentToMedia.getInstance().getOther(this);
334     else
335       return null;
336   }
337
338 }