various fixes/cleanup: old producers are now completely gone, old logfile class too
[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 freemarker.template.SimpleScalar;
40 import freemarker.template.TemplateModel;
41 import freemarker.template.TemplateModelException;
42
43 import mir.log.LoggerWrapper;
44 import mir.entity.Entity;
45 import mir.entity.EntityList;
46 import mir.storage.StorageObject;
47 import mir.storage.StorageObjectExc;
48 import mir.storage.StorageObjectFailure;
49 import mircoders.storage.DatabaseContent;
50 import mircoders.storage.DatabaseContentToMedia;
51 import mircoders.storage.DatabaseContentToTopics;
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.15 2003/02/23 05:00:13 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   HashMap _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     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   /**
124    * make openposting to newswire
125    */
126
127   public void newswire() throws StorageObjectFailure
128   {
129     String sql = "update content set to_article_type='1', is_produced='0' where id='" + getId()+"'";
130     try {
131       theStorageObject.executeUpdate(sql);
132     } catch (StorageObjectFailure e) {
133       throwStorageObjectFailure(e, "\n -- newswire failed");
134     } catch (SQLException e) {
135       throwStorageObjectFailure(e, "\n -- newswire failed");
136     }
137   }
138
139
140   /**
141    * dettach from media
142    */
143   public void dettach(String cid,String mid) throws StorageObjectFailure
144   {
145     if (mid!=null){
146       try{
147         DatabaseContentToMedia.getInstance().delete(cid,mid);
148       }
149       catch (Exception e){
150         throwStorageObjectFailure(e, "\n -- failed to get instance");
151       }
152
153       //set Content to unproduced
154       setProduced(false);
155     }
156   }
157
158   /**
159    * attach to media
160    */
161
162   public void attach(String mid) throws StorageObjectFailure
163   {
164     if (mid!=null) {
165       //write media-id mid and content-id in table content_x_media
166       try{
167         DatabaseContentToMedia.getInstance().addMedia(getId(),mid);
168       } catch(StorageObjectFailure e){
169         throwStorageObjectFailure(e, "attach: could not get the instance");
170       }
171       //set Content to unproduced
172       setProduced(false);
173     }
174     else {
175       logger.error("EntityContent: attach without mid");
176     }
177   }
178
179   /**
180    * overridden method getValue to include formatted date into every
181    * entityContent
182    */
183
184   public TemplateModel get(java.lang.String key) throws TemplateModelException
185   {
186     if (key!=null) {
187       if (_entCache.containsKey(key)) {
188         return (TemplateModel)_entCache.get(key);
189       }
190       if (key.equals("to_comments")) {
191         try {
192           _entCache.put(key, getComments());
193           return (TemplateModel)_entCache.get(key);
194         }
195         catch (Exception ex) {
196           logger.warn("EntityContent.getComments: could not fetch data " + ex.toString());
197
198           throw new TemplateModelException(ex.toString());
199         }
200       }
201       if (key.equals("to_media_images")) {
202         try {
203           _entCache.put(key, getImagesForContent());
204           return (TemplateModel)_entCache.get(key);
205         }
206         catch (Exception ex) {
207           logger.warn("EntityContent.getImagesForContent: could not fetch data " + ex.toString());
208           throw new TemplateModelException(ex.toString());
209         }
210       }
211       if (key.equals("to_media_audio")) {
212         try {
213           _entCache.put(key, getAudioForContent());
214           return (TemplateModel)_entCache.get(key);
215         }
216         catch (Exception ex) {
217           logger.warn("EntityContent.getAudioForContent: could not fetch data " + ex.toString());
218           throw new TemplateModelException(ex.toString());
219         }
220       }
221       if (key.equals("to_media_video")) {
222         try {
223           _entCache.put(key, getVideoForContent());
224           return (TemplateModel)_entCache.get(key);
225         }
226         catch (Exception ex) {
227           logger.warn("EntityContent.getVideoForContent: could not fetch data " + ex.toString());
228           throw new TemplateModelException(ex.toString());
229         }
230       }
231       if (key.equals("to_media_other")) {
232         try {
233           _entCache.put(key, getOtherMediaForContent());
234           return (TemplateModel)_entCache.get(key);
235         }
236         catch (Exception ex) {
237           logger.warn("EntityContent.getOtherMediaForContent: could not fetch data " + ex.toString());
238           throw new TemplateModelException(ex.toString());
239         }
240       }
241       else if (key.equals("to_topics")) {
242         try {
243           _entCache.put(key,
244                         DatabaseContentToTopics.getInstance().getTopics(this));
245           return (TemplateModel)_entCache.get(key);
246         }
247         catch (Exception ex) {
248           logger.warn("EntityContent.getTopics: could not fetch data " + ex.toString());
249           throw new TemplateModelException(ex.toString());
250         }
251       }
252       else {
253         return new SimpleScalar(getValue(key));
254       }
255
256     }
257     return null;
258   }
259
260   /**
261    * overridden method setValues to patch creator_main_url
262    */
263   public void setValues(HashMap theStringValues) {
264     if (theStringValues != null) {
265       if (theStringValues.containsKey("creator_main_url")){
266         if (((String)theStringValues.get("creator_main_url")).equalsIgnoreCase("http://")){
267           theStringValues.remove("creator_main_url");
268         }
269         else if (!((String)theStringValues.get("creator_main_url")).startsWith("http://")){
270           theStringValues.put("creator_main_url","http://"+((String)theStringValues.get("creator_main_url")));
271         }
272       }
273     }
274     super.setValues(theStringValues);
275   }
276
277   /**
278    * fetches all the comments belonging to an article
279    *
280    * @return freemarker.template.SimpleList
281    */
282   private EntityList getComments() throws StorageObjectFailure {
283     return ((DatabaseContent)theStorageObject).getComments(this);
284   }
285
286   private boolean hasMedia() throws StorageObjectFailure
287   {
288     if (_hasMedia == null) {
289       try {
290         _hasMedia =
291             new Boolean(DatabaseContentToMedia.getInstance().hasMedia(this));
292       } catch (StorageObjectExc e) {
293         throw new StorageObjectFailure(e);
294       }
295     }
296     return _hasMedia.booleanValue();
297   }
298
299   //######## @todo all of the following getBlahForContent should have
300   // and optimized version where LIMIT=1 sql for list view.
301   private EntityList getImagesForContent()
302       throws StorageObjectFailure, TemplateModelException
303   {
304     if (hasMedia())
305       return DatabaseContentToMedia.getInstance().getImages(this);
306     else
307       return null;
308   }
309
310   private EntityList getAudioForContent()
311       throws StorageObjectFailure, TemplateModelException
312   {
313     if (hasMedia())
314       return DatabaseContentToMedia.getInstance().getAudio(this) ;
315     else
316       return null;
317   }
318
319   private EntityList getVideoForContent()
320       throws StorageObjectFailure, TemplateModelException
321   {
322     if (hasMedia())
323       return DatabaseContentToMedia.getInstance().getVideo(this) ;
324     else
325       return null;
326   }
327
328   private EntityList getOtherMediaForContent()
329       throws StorageObjectFailure, TemplateModelException
330   {
331     if (hasMedia())
332       return DatabaseContentToMedia.getInstance().getOther(this);
333     else
334       return null;
335   }
336
337 }