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