08b407e8218daceb6f32489b73392f84656859de
[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   // the following *has* to be sychronized cause this static method
31   // could get preemted and we could end up with 2 instances of DatabaseFoo.
32   // see the "Singletons with needles and thread" article at JavaWorld -mh
33   public synchronized static DatabaseContent getInstance()
34     throws StorageObjectException {
35
36     if (instance == null ) {
37       instance = new DatabaseContent();
38       instance.myselfDatabase = instance;
39     }
40     return instance;
41   }
42
43   private DatabaseContent()
44     throws StorageObjectException {
45
46     super();
47     this.theTable="content";
48     this.theCoreTable="media";
49
50     relationComments = new EntityRelation("id", "to_media", DatabaseComment.getInstance(), EntityRelation.TO_MANY);
51     relationFeature = new EntityRelation("id", "to_feature", DatabaseFeature.getInstance(), EntityRelation.TO_ONE);
52     try { this.theEntityClass = Class.forName("mircoders.entity.EntityContent"); }
53     catch (Exception e) { throw new StorageObjectException(e.toString()); }
54   }
55
56   // methods
57
58   /**
59    * sets the database flag is_produced to unproduced
60    */
61
62   public void setUnproduced(String where) throws StorageObjectException
63   {
64     Connection con=null;Statement stmt=null;
65     String sql = "update content set is_produced='0' where " + where;
66     theLog.printDebugInfo("set unproduced: "+where);
67     try {
68       con = getPooledCon();
69       // should be a preparedStatement because is faster
70       stmt = con.createStatement();
71       executeUpdate(stmt,sql);
72       theLog.printDebugInfo("set unproduced: "+where);
73     }
74     catch (Exception e) {_throwStorageObjectException(e, "-- set unproduced failed");}
75     finally { freeConnection(con,stmt);}
76   }
77
78   /**
79    * returns the comments that belong to the article (via entityrelation)
80    * where db-flag is_published is true
81    */
82   public EntityList getComments(EntityContent entC) throws StorageObjectException {
83     return relationComments.getMany(entC,"webdb_create","is_published='1'");
84   }
85
86   /**
87    * returns the features that belong to the article (via entityrelation)
88    */
89   public EntityList getFeature(EntityContent entC) throws StorageObjectException {
90     return relationFeature.getMany(entC);
91   }
92
93   public boolean delete(String id) throws StorageObjectException
94   {
95     DatabaseComment.getInstance().deleteByContentId(id);
96     super.delete(id);
97     return true;
98   }
99
100 }