X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmircoders%2Fentity%2FEntityContent.java;h=96ab2dc9b29a75d8b41a8cdbc48beab677e37acc;hb=d74a52d5f65389e5cf89d3fcf14d07afbb78a9f9;hp=5eb2bf58a127962599d2f29665ef812e30b53a2b;hpb=a459f111d85598df56c6ab711cec11632676b39c;p=mir.git diff --git a/source/mircoders/entity/EntityContent.java b/source/mircoders/entity/EntityContent.java index 5eb2bf58..96ab2dc9 100755 --- a/source/mircoders/entity/EntityContent.java +++ b/source/mircoders/entity/EntityContent.java @@ -18,13 +18,13 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * In addition, as a special exception, The Mir-coders gives permission to link - * the code of this program with any library licensed under the Apache Software License, - * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library - * (or with modified versions of the above that use the same license as the above), - * and distribute linked combinations including the two. You must obey the - * GNU General Public License in all respects for all of the code used other than - * the above mentioned libraries. If you modify this file, you may extend this - * exception to your version of the file, but you are not obligated to do so. + * the code of this program with any library licensed under the Apache Software License, + * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library + * (or with modified versions of the above that use the same license as the above), + * and distribute linked combinations including the two. You must obey the + * GNU General Public License in all respects for all of the code used other than + * the above mentioned libraries. If you modify this file, you may extend this + * exception to your version of the file, but you are not obligated to do so. * If you do not wish to do so, delete this exception statement from your version. */ @@ -33,89 +33,48 @@ package mircoders.entity; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; -import java.util.HashMap; import java.util.Map; -import mir.entity.Entity; -import mir.entity.EntityList; +import mir.entity.AbstractEntity; import mir.log.LoggerWrapper; -import mir.storage.StorageObject; -import mir.storage.StorageObjectExc; -import mir.storage.StorageObjectFailure; -import mircoders.storage.DatabaseContent; +import mir.storage.DatabaseFailure; import mircoders.storage.DatabaseContentToMedia; -import mircoders.storage.DatabaseContentToTopics; -import freemarker.template.SimpleScalar; -import freemarker.template.TemplateModel; -import freemarker.template.TemplateModelException; /** * this class implements mapping of one line of the database table content * to a java object * - * @version $Id: EntityContent.java,v 1.19 2003/04/21 12:42:54 idfx Exp $ + * @version $Id: EntityContent.java,v 1.19.2.12 2005/10/30 00:46:58 zapata Exp $ * @author mir-coders group * */ -public class EntityContent extends Entity -{ - - String mirconf_extLinkName = configuration.getString("Producer.ExtLinkName"); - String mirconf_intLinkName = configuration.getString("Producer.IntLinkName"); - String mirconf_mailLinkName = configuration.getString("Producer.MailLinkName"); - String mirconf_imageRoot = configuration.getString("Producer.ImageRoot"); - - //this should always be transient i.e it can never be stored in the db - //or ObjectStore. (so the ObjectStore should only be caching what comes - //directly out of the DB. @todo confirm this with rk. -mh - Map _entCache = new HashMap(); - Boolean _hasMedia = null; - - // constructors - - public EntityContent() - { - super(); - - logger = new LoggerWrapper("Entity.Content"); - } - - public EntityContent(StorageObject theStorage) { - this(); - - setStorage(theStorage); - } - - // - // methods - +public class EntityContent extends AbstractEntity { /** * set is_produced flag for the article */ - - public void setProduced(boolean yesno) throws StorageObjectFailure + public void setProduced(boolean yesno) throws DatabaseFailure { String value = (yesno) ? "1":"0"; - if (value.equals( getValue("is_produced") )) return; + if (value.equals( getFieldValue("is_produced") )) return; Connection con=null;Statement stmt=null; String sql = "update content set is_produced='" + value + "' where id='" + getId()+"'"; try { - con = theStorageObject.getPooledCon(); - /** @todo should be preparedStatement: faster!! */ + con = database.obtainConnection(); + /** todo should be preparedStatement: faster!! */ stmt = con.createStatement(); - theStorageObject.executeUpdate(stmt,sql); + database.executeUpdate(stmt,sql); } - catch (StorageObjectFailure e) { - throwStorageObjectFailure(e, "\n -- set produced failed"); + catch (DatabaseFailure e) { + throw e; } catch (SQLException e) { - throwStorageObjectFailure(e, "\n -- set produced failed"); + throw new DatabaseFailure(e); } finally { - theStorageObject.freeConnection(con,stmt); + database.freeConnection(con,stmt); } } @@ -124,130 +83,36 @@ public class EntityContent extends Entity * * @param anArticleId * @param aMediaId - * @throws StorageObjectFailure + * @throws DatabaseFailure */ - public void dettach(String anArticleId, String aMediaId) throws StorageObjectFailure + public void dettach(String anArticleId, String aMediaId) throws DatabaseFailure { if (aMediaId!=null){ - try{ - DatabaseContentToMedia.getInstance().delete(anArticleId, aMediaId); - } - catch (Exception e){ - throwStorageObjectFailure(e, "\n -- failed to get instance"); - } + DatabaseContentToMedia.getInstance().delete(anArticleId, aMediaId); setProduced(false); } } /** - * Attaches media to an article - * - * @param mid - * @throws StorageObjectFailure + * Attaches media to the article */ - public void attach(String aMediaId) throws StorageObjectFailure + public void attach(String aMediaId) throws DatabaseFailure { if (aMediaId!=null) { - try{ - DatabaseContentToMedia.getInstance().addMedia(getId(),aMediaId); - } - catch(StorageObjectFailure e){ - throwStorageObjectFailure(e, "attach: could not get the instance"); - } + DatabaseContentToMedia.getInstance().addMedia(getId(),aMediaId); setProduced(false); } else { - logger.error("EntityContent: attach without mid"); - } - } - - /** - * overridden method getValue to include formatted date into every - * entityContent - */ - - public TemplateModel get(java.lang.String key) throws TemplateModelException - { - if (key!=null) { - if (_entCache.containsKey(key)) { - return (TemplateModel)_entCache.get(key); - } - if (key.equals("to_comments")) { - try { - _entCache.put(key, getComments()); - return (TemplateModel)_entCache.get(key); - } - catch (Exception ex) { - logger.warn("EntityContent.getComments: could not fetch data " + ex.toString()); - - throw new TemplateModelException(ex.toString()); - } - } - if (key.equals("to_media_images")) { - try { - _entCache.put(key, getImagesForContent()); - return (TemplateModel)_entCache.get(key); - } - catch (Exception ex) { - logger.warn("EntityContent.getImagesForContent: could not fetch data " + ex.toString()); - throw new TemplateModelException(ex.toString()); - } - } - if (key.equals("to_media_audio")) { - try { - _entCache.put(key, getAudioForContent()); - return (TemplateModel)_entCache.get(key); - } - catch (Exception ex) { - logger.warn("EntityContent.getAudioForContent: could not fetch data " + ex.toString()); - throw new TemplateModelException(ex.toString()); - } - } - if (key.equals("to_media_video")) { - try { - _entCache.put(key, getVideoForContent()); - return (TemplateModel)_entCache.get(key); - } - catch (Exception ex) { - logger.warn("EntityContent.getVideoForContent: could not fetch data " + ex.toString()); - throw new TemplateModelException(ex.toString()); - } - } - if (key.equals("to_media_other")) { - try { - _entCache.put(key, getOtherMediaForContent()); - return (TemplateModel)_entCache.get(key); - } - catch (Exception ex) { - logger.warn("EntityContent.getOtherMediaForContent: could not fetch data " + ex.toString()); - throw new TemplateModelException(ex.toString()); - } - } - else if (key.equals("to_topics")) { - try { - _entCache.put(key, - DatabaseContentToTopics.getInstance().getTopics(this)); - return (TemplateModel)_entCache.get(key); - } - catch (Exception ex) { - logger.warn("EntityContent.getTopics: could not fetch data " + ex.toString()); - throw new TemplateModelException(ex.toString()); - } - } - else { - return new SimpleScalar(getValue(key)); - } - + getLogger().error("EntityContent: attach without mid"); } - return null; } /** - * overridden method setValues to patch creator_main_url + * overridden method setFieldValues to patch creator_main_url */ - public void setValues(Map theStringValues) { + public void setFieldValues(Map theStringValues) { if (theStringValues != null) { if (theStringValues.containsKey("creator_main_url")){ if (((String)theStringValues.get("creator_main_url")).equalsIgnoreCase("http://")){ @@ -258,67 +123,6 @@ public class EntityContent extends Entity } } } - super.setValues(theStringValues); + super.setFieldValues(theStringValues); } - - /** - * fetches all the comments belonging to an article - * - * @return freemarker.template.SimpleList - */ - private EntityList getComments() throws StorageObjectFailure { - return ((DatabaseContent)theStorageObject).getComments(this); - } - - private boolean hasMedia() throws StorageObjectFailure - { - if (_hasMedia == null) { - try { - _hasMedia = - new Boolean(DatabaseContentToMedia.getInstance().hasMedia(this)); - } catch (StorageObjectExc e) { - throw new StorageObjectFailure(e); - } - } - return _hasMedia.booleanValue(); - } - - //######## @todo all of the following getBlahForContent should have - // and optimized version where LIMIT=1 sql for list view. - private EntityList getImagesForContent() - throws StorageObjectFailure, TemplateModelException - { - if (hasMedia()) - return DatabaseContentToMedia.getInstance().getImages(this); - else - return null; - } - - private EntityList getAudioForContent() - throws StorageObjectFailure, TemplateModelException - { - if (hasMedia()) - return DatabaseContentToMedia.getInstance().getAudio(this) ; - else - return null; - } - - private EntityList getVideoForContent() - throws StorageObjectFailure, TemplateModelException - { - if (hasMedia()) - return DatabaseContentToMedia.getInstance().getVideo(this) ; - else - return null; - } - - private EntityList getOtherMediaForContent() - throws StorageObjectFailure, TemplateModelException - { - if (hasMedia()) - return DatabaseContentToMedia.getInstance().getOther(this); - else - return null; - } - }