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