commented out cache initialization until it works properly.
[mir.git] / source / mircoders / storage / DatabaseLinksImcs.java
1 package  mircoders.storage;
2
3 import  java.lang.*;
4 import  java.sql.*;
5 import  java.io.*;
6 import  java.util.*;
7 import  freemarker.template.*;
8 import  mir.storage.*;
9 import  mir.entity.*;
10 import  mir.misc.*;
11
12
13 /**
14  * <b>Diese Klasse implementiert die Datenbankverbindung zur MetaObjekt-Tabelle
15  *
16  *
17  */
18 public class DatabaseLinksImcs extends Database
19     implements StorageObject {
20   private static DatabaseLinksImcs instance;
21
22   /**
23    * put your documentation comment here
24    * @return
25    * @exception StorageObjectException
26    */
27   public static DatabaseLinksImcs getInstance () throws StorageObjectException {
28     if (instance == null) {
29       instance = new DatabaseLinksImcs();
30       instance.myselfDatabase = instance;
31     }
32     return  instance;
33   }
34
35   /**
36    * put your documentation comment here
37    */
38   private DatabaseLinksImcs () throws StorageObjectException
39   {
40     super();
41     ////this.cache = new HashMap();
42     this.hasTimestamp = false;
43     this.theTable = "links_imcs";
44     try {
45       this.theEntityClass = Class.forName("mircoders.entity.EntityLinksImcs");
46     } catch (Exception e) {
47       throw  new StorageObjectException(e.toString());
48     }
49   }
50
51   public String insert (Entity theEntity) throws StorageObjectException {
52                 String returnId = "0";
53                 Connection con = null;
54                 PreparedStatement pstmt = null;
55                 //cache
56                 invalidatePopupCache();
57                 try {
58                         HashMap theEntityValues = theEntity.getValues();
59                         ArrayList streamedInput = theEntity.streamedInput();
60                         StringBuffer f = new StringBuffer();
61                         StringBuffer v = new StringBuffer();
62                         String aField, aValue;
63                         boolean firstField = true;
64                         // make sql-string
65                         for (int i = 0; i < getFields().size(); i++) {
66                                 aField = (String)getFields().get(i);
67                                 if (!aField.equals(thePKeyName)) {
68                                         aValue = null;
69                                         // sonderfaelle
70                                         if (aField.equals("webdb_create")) {
71                                                 aValue = "NOW()";
72                                         }
73                                         else {
74                                                 if (streamedInput != null && streamedInput.contains(aField)) {
75                                                         aValue = "?";
76                                                 }
77                                                 else {
78                                                         if (theEntityValues.containsKey(aField)) {
79                 if (aField.equals("to_parent_id")) {
80                   aValue = StringUtil.quote((String)theEntityValues.get(aField));
81                 } else {
82                                                                   aValue = "'" + StringUtil.quote((String)theEntityValues.get(aField)) + "'";
83                 }
84                                                         }
85                                                 }
86                                         }
87                                         // wenn Wert gegeben, dann einbauen
88                                         if (aValue != null) {
89                                                 if (firstField == false) {
90                                                         f.append(",");
91                                                         v.append(",");
92                                                 }
93                                                 else {
94                                                         firstField = false;
95                                                 }
96                                                 f.append(aField);
97                                                 v.append(aValue);
98                                         }
99                                 }
100                         }         // end for
101                         // insert into db
102                         StringBuffer sqlBuf = new StringBuffer("insert into ").append(theTable).append("(").append(f).append(") values (").append(v).append(")");
103                         String sql = sqlBuf.toString();
104                         theLog.printInfo("INSERT: " + sql);
105                         con = getPooledCon();
106                         con.setAutoCommit(false);
107                         pstmt = con.prepareStatement(sql);
108                         if (streamedInput != null) {
109                                 for (int i = 0; i < streamedInput.size(); i++) {
110                                         String inputString = (String)theEntityValues.get(streamedInput.get(i));
111                                         pstmt.setBytes(i + 1, inputString.getBytes());
112                                 }
113                         }
114                         pstmt.execute();
115                         pstmt = con.prepareStatement(theAdaptor.getLastInsertSQL((Database)myselfDatabase));
116                         ResultSet rs = pstmt.executeQuery();
117                         rs.next();
118                         returnId = rs.getString(1);
119                         theEntity.setId(returnId);
120                 } catch (SQLException sqe) {
121                         throwSQLException(sqe, "insert");
122                 } finally {
123                         try {
124                                 con.setAutoCommit(true);
125                         } catch (Exception e) {
126                                 ;
127                         }
128                         freeConnection(con, pstmt);
129                 }
130                 return  returnId;
131         }
132
133   public void update (Entity theEntity) throws StorageObjectException {
134     Connection con = null;
135     PreparedStatement pstmt = null;
136     ArrayList streamedInput = theEntity.streamedInput();
137     HashMap theEntityValues = theEntity.getValues();
138     String id = theEntity.getId();
139     String aField;
140     StringBuffer fv = new StringBuffer();
141     boolean firstField = true;
142     //cache
143     invalidatePopupCache();
144     // build sql statement
145     for (int i = 0; i < getFields().size(); i++) {
146       aField = (String)metadataFields.get(i);
147       // only normal cases
148       if (!(aField.equals(thePKeyName) || aField.equals("webdb_create") ||
149           aField.equals("webdb_lastchange") || (streamedInput != null && streamedInput.contains(aField)))) {
150         if (theEntityValues.containsKey(aField)) {
151           if (firstField == false) {
152             fv.append(", ");
153           }
154           else {
155             firstField = false;
156           }
157           if (aField.equals("to_parent_id")) {
158             fv.append(aField).append("=").append(StringUtil.quote((String)theEntityValues.get(aField)));
159           } else {
160             fv.append(aField).append("='").append(StringUtil.quote((String)theEntityValues.get(aField))).append("'");
161           }
162         }
163       }
164     }
165     StringBuffer sql = new StringBuffer("update ").append(theTable).append(" set ").append(fv);
166     // exceptions
167     if (metadataFields.contains("webdb_lastchange")) {
168       sql.append(",webdb_lastchange=NOW()");
169     }
170     if (streamedInput != null) {
171       for (int i = 0; i < streamedInput.size(); i++) {
172         sql.append(",").append(streamedInput.get(i)).append("=?");
173       }
174     }
175     sql.append(" where id=").append(id);
176     theLog.printInfo("UPDATE: " + sql);
177     // execute sql
178     try {
179       con = getPooledCon();
180       con.setAutoCommit(false);
181       pstmt = con.prepareStatement(sql.toString());
182       if (streamedInput != null) {
183         for (int i = 0; i < streamedInput.size(); i++) {
184           String inputString = (String)theEntityValues.get(streamedInput.get(i));
185           pstmt.setBytes(i + 1, inputString.getBytes());
186         }
187       }
188       pstmt.executeUpdate();
189     } catch (SQLException sqe) {
190       throwSQLException(sqe, "update");
191     } finally {
192       try {
193         con.setAutoCommit(true);
194       } catch (Exception e) {
195         ;
196       }
197       freeConnection(con, pstmt);
198     }
199   }
200
201   /**
202    * put your documentation comment here
203    * @return
204    */
205   public SimpleHash getHashData () {
206     return  getHashData();
207   }
208
209   public SimpleList getPopupData () throws StorageObjectException {
210     return  getPopupData();
211   }
212 }
213
214
215