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