some notes in preparation of ObjectStore and other things
[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         /** @todo toooo much copy/paste in this class //rk  */
52
53         public String insert (Entity theEntity) throws StorageObjectException {
54                 String returnId = "0";
55                 Connection con = null;
56                 PreparedStatement pstmt = null;
57                 //cache
58                 invalidatePopupCache();
59                 try {
60                         HashMap theEntityValues = theEntity.getValues();
61                         ArrayList streamedInput = theEntity.streamedInput();
62                         StringBuffer f = new StringBuffer();
63                         StringBuffer v = new StringBuffer();
64                         String aField, aValue;
65                         boolean firstField = true;
66                         // make sql-string
67                         for (int i = 0; i < getFields().size(); i++) {
68                                 aField = (String)getFields().get(i);
69                                 if (!aField.equals(thePKeyName)) {
70                                         aValue = null;
71                                         // sonderfaelle
72                                         if (aField.equals("webdb_create")) {
73                                                 aValue = "NOW()";
74                                         }
75                                         else {
76                                                 if (streamedInput != null && streamedInput.contains(aField)) {
77                                                         aValue = "?";
78                                                 }
79                                                 else {
80                                                         if (theEntityValues.containsKey(aField)) {
81                                                                 if (aField.equals("to_parent_id")) {
82                                                                         aValue = StringUtil.quote((String)theEntityValues.get(aField));
83                                                                 } else {
84                                                                         aValue = "'" + StringUtil.quote((String)theEntityValues.get(aField)) + "'";
85                                                                 }
86                                                         }
87                                                 }
88                                         }
89                                         // wenn Wert gegeben, dann einbauen
90                                         if (aValue != null) {
91                                                 if (firstField == false) {
92                                                         f.append(",");
93                                                         v.append(",");
94                                                 }
95                                                 else {
96                                                         firstField = false;
97                                                 }
98                                                 f.append(aField);
99                                                 v.append(aValue);
100                                         }
101                                 }
102                         }         // end for
103                         // insert into db
104                         StringBuffer sqlBuf = new StringBuffer("insert into ").append(theTable).append("(").append(f).append(") values (").append(v).append(")");
105                         String sql = sqlBuf.toString();
106                         theLog.printInfo("INSERT: " + sql);
107                         con = getPooledCon();
108                         con.setAutoCommit(false);
109                         pstmt = con.prepareStatement(sql);
110                         if (streamedInput != null) {
111                                 for (int i = 0; i < streamedInput.size(); i++) {
112                                         String inputString = (String)theEntityValues.get(streamedInput.get(i));
113                                         pstmt.setBytes(i + 1, inputString.getBytes());
114                                 }
115                         }
116                         pstmt.execute();
117                         pstmt = con.prepareStatement(theAdaptor.getLastInsertSQL((Database)myselfDatabase));
118                         ResultSet rs = pstmt.executeQuery();
119                         rs.next();
120                         returnId = rs.getString(1);
121                         theEntity.setId(returnId);
122                 } catch (SQLException sqe) {
123                         throwSQLException(sqe, "insert");
124                 } finally {
125                         try {
126                                 con.setAutoCommit(true);
127                         } catch (Exception e) {
128                                 ;
129                         }
130                         freeConnection(con, pstmt);
131                 }
132                 return  returnId;
133         }
134
135         public void update (Entity theEntity) throws StorageObjectException {
136                 Connection con = null;
137                 PreparedStatement pstmt = null;
138                 ArrayList streamedInput = theEntity.streamedInput();
139                 HashMap theEntityValues = theEntity.getValues();
140                 String id = theEntity.getId();
141                 String aField;
142                 StringBuffer fv = new StringBuffer();
143                 boolean firstField = true;
144                 //cache
145                 invalidatePopupCache();
146                 // build sql statement
147                 for (int i = 0; i < getFields().size(); i++) {
148                         aField = (String)metadataFields.get(i);
149                         // only normal cases
150                         if (!(aField.equals(thePKeyName) || aField.equals("webdb_create") ||
151                                         aField.equals("webdb_lastchange") || (streamedInput != null && streamedInput.contains(aField)))) {
152                                 if (theEntityValues.containsKey(aField)) {
153                                         if (firstField == false) {
154                                                 fv.append(", ");
155                                         }
156                                         else {
157                                                 firstField = false;
158                                         }
159                                         if (aField.equals("to_parent_id")) {
160                                                 fv.append(aField).append("=").append(StringUtil.quote((String)theEntityValues.get(aField)));
161                                         } else {
162                                                 fv.append(aField).append("='").append(StringUtil.quote((String)theEntityValues.get(aField))).append("'");
163                                         }
164                                 }
165                         }
166                 }
167                 StringBuffer sql = new StringBuffer("update ").append(theTable).append(" set ").append(fv);
168                 // exceptions
169                 if (metadataFields.contains("webdb_lastchange")) {
170                         sql.append(",webdb_lastchange=NOW()");
171                 }
172                 if (streamedInput != null) {
173                         for (int i = 0; i < streamedInput.size(); i++) {
174                                 sql.append(",").append(streamedInput.get(i)).append("=?");
175                         }
176                 }
177                 sql.append(" where id=").append(id);
178                 theLog.printInfo("UPDATE: " + sql);
179                 // execute sql
180                 try {
181                         con = getPooledCon();
182                         con.setAutoCommit(false);
183                         pstmt = con.prepareStatement(sql.toString());
184                         if (streamedInput != null) {
185                                 for (int i = 0; i < streamedInput.size(); i++) {
186                                         String inputString = (String)theEntityValues.get(streamedInput.get(i));
187                                         pstmt.setBytes(i + 1, inputString.getBytes());
188                                 }
189                         }
190                         pstmt.executeUpdate();
191                 } catch (SQLException sqe) {
192                         throwSQLException(sqe, "update");
193                 } finally {
194                         try {
195                                 con.setAutoCommit(true);
196                         } catch (Exception e) {
197                                 ;
198                         }
199                         freeConnection(con, pstmt);
200                 }
201         }
202
203 }
204
205
206