more error handling.
[mir.git] / source / mircoders / storage / DatabaseContent.java
1 package mircoders.storage;
2
3 import java.lang.*;
4 import java.sql.*;
5 import java.io.*;
6 import java.util.*;
7
8 import freemarker.template.*;
9
10 import mir.storage.*;
11 import mir.entity.*;
12 import mir.misc.*;
13
14 import mircoders.entity.*;
15
16 /**
17  * <b>this class implements the access to the content-table</b>
18  *
19  *
20  */
21
22 public class DatabaseContent extends Database implements StorageObject {
23
24   private static DatabaseContent      instance;
25   private static EntityRelation       relationComments;
26   private static EntityRelation       relationFeature;
27
28   // Contructors / Singleton
29
30   public static DatabaseContent getInstance()
31     throws StorageObjectException {
32
33     if (instance == null ) {
34       instance = new DatabaseContent();
35       instance.myselfDatabase = instance;
36     }
37     return instance;
38   }
39
40   private DatabaseContent()
41     throws StorageObjectException {
42
43     super();
44     this.theTable="content";
45     this.theCoreTable="media";
46     relationComments = new EntityRelation("id", "to_media", DatabaseComment.getInstance(), EntityRelation.TO_MANY);
47     relationFeature = new EntityRelation("id", "to_feature", DatabaseFeature.getInstance(), EntityRelation.TO_ONE);
48     try { this.theEntityClass = Class.forName("mircoders.entity.EntityContent"); }
49     catch (Exception e) { throw new StorageObjectException(e.toString()); }
50   }
51
52   // methods
53
54   /**
55    * sets the database flag is_produced to unproduced
56    */
57
58   public void setUnproduced(String where) throws StorageObjectException
59   {
60     Connection con=null;Statement stmt=null;
61     String sql = "update content set is_produced='0' where " + where;
62     try {
63       con = getPooledCon();
64       // should be a preparedStatement because is faster
65       stmt = con.createStatement();
66       executeUpdate(stmt,sql);
67     }
68     catch (Exception e) {_throwStorageObjectException(e, "-- set unproduced failed");}
69     finally { freeConnection(con,stmt);}
70   }
71
72   /**
73    * returns the comments that belong to the article (via entityrelation)
74    * where db-flag is_published is true
75    */
76   public SimpleList getComments(EntityContent entC) throws StorageObjectException {
77     SimpleList comments=null;
78     try {
79       comments = relationComments.getManyAsSimpleList(entC,"webdb_create","is_published='1'");
80     }
81     catch (StorageObjectException e) {
82       _throwStorageObjectException(e, "DatabaseComments :: failed to get comments");
83     }
84     return comments;
85   }
86
87   /**
88    * returns the features that belong to the article (via entityrelation)
89    */
90   public SimpleList getFeature(EntityContent entC) throws StorageObjectException {
91     SimpleList feature=null;
92     try {
93       feature = relationFeature.getManyAsSimpleList(entC);
94     }
95     catch (StorageObjectException e) {
96       _throwStorageObjectException(e, "DatabaseComments :: failed to get features");
97     }
98     return feature;
99   }
100
101   public boolean delete(String id)
102     throws StorageObjectException {
103     DatabaseComment.getInstance().deleteByContentId(id);
104     super.delete(id);
105     return true;
106   }
107
108 }