data. ... got lost
[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         // the following *has* to be sychronized cause this static method
28         // could get preemted and we could end up with 2 instances of DatabaseFoo..
29         // see the "Singletons with needles and thread" article at JavaWorld -mh
30         public synchronized static DatabaseLinksImcs getInstance () 
31           throws StorageObjectException {
32                 if (instance == null) {
33                         instance = new DatabaseLinksImcs();
34                         instance.myselfDatabase = instance;
35                 }
36                 return  instance;
37         }
38
39         /**
40          * put your documentation comment here
41          */
42         private DatabaseLinksImcs () throws StorageObjectException
43         {
44                 super();
45                 ////this.cache = new HashMap();
46                 this.hasTimestamp = false;
47                 this.theTable = "links_imcs";
48                 try {
49                         this.theEntityClass = Class.forName("mircoders.entity.EntityLinksImcs");
50                 } catch (Exception e) {
51                         throw  new StorageObjectException(e.toString());
52                 }
53         }
54
55         /** @todo toooo much copy/paste in this class //rk  */
56
57         public String insert (Entity theEntity) throws StorageObjectException {
58                 String returnId = "0";
59                 Connection con = null;
60                 PreparedStatement pstmt = null;
61                 //cache
62                 invalidatePopupCache();
63                 try {
64                         HashMap theEntityValues = theEntity.getValues();
65                         ArrayList streamedInput = theEntity.streamedInput();
66                         StringBuffer f = new StringBuffer();
67                         StringBuffer v = new StringBuffer();
68                         String aField, aValue;
69                         boolean firstField = true;
70                         // make sql-string
71                         for (int i = 0; i < getFields().size(); i++) {
72                                 aField = (String)getFields().get(i);
73                                 if (!aField.equals(thePKeyName)) {
74                                         aValue = null;
75                                         // sonderfaelle
76                                         if (aField.equals("webdb_create")) {
77                                                 aValue = "NOW()";
78                                         }
79                                         else {
80                                                 if (streamedInput != null && streamedInput.contains(aField)) {
81                                                         aValue = "?";
82                                                 }
83                                                 else {
84                                                         if (theEntityValues.containsKey(aField)) {
85                                                                 if (aField.equals("to_parent_id")) {
86                                                                         aValue = StringUtil.quote((String)theEntityValues.get(aField));
87                                                                 } else {
88                                                                         aValue = "'" + StringUtil.quote((String)theEntityValues.get(aField)) + "'";
89                                                                 }
90                                                         }
91                                                 }
92                                         }
93                                         // wenn Wert gegeben, dann einbauen
94                                         if (aValue != null) {
95                                                 if (firstField == false) {
96                                                         f.append(",");
97                                                         v.append(",");
98                                                 }
99                                                 else {
100                                                         firstField = false;
101                                                 }
102                                                 f.append(aField);
103                                                 v.append(aValue);
104                                         }
105                                 }
106                         }         // end for
107                         // insert into db
108                         StringBuffer sqlBuf = new StringBuffer("insert into ").append(theTable).append("(").append(f).append(") values (").append(v).append(")");
109                         String sql = sqlBuf.toString();
110                         theLog.printInfo("INSERT: " + sql);
111                         con = getPooledCon();
112                         con.setAutoCommit(false);
113                         pstmt = con.prepareStatement(sql);
114                         if (streamedInput != null) {
115                                 for (int i = 0; i < streamedInput.size(); i++) {
116                                         String inputString = (String)theEntityValues.get(streamedInput.get(i));
117                                         pstmt.setBytes(i + 1, inputString.getBytes());
118                                 }
119                         }
120                         pstmt.execute();
121                         pstmt = con.prepareStatement(theAdaptor.getLastInsertSQL((Database)myselfDatabase));
122                         ResultSet rs = pstmt.executeQuery();
123                         rs.next();
124                         returnId = rs.getString(1);
125                         theEntity.setId(returnId);
126                 } catch (SQLException sqe) {
127                         throwSQLException(sqe, "insert");
128                 } finally {
129                         try {
130                                 con.setAutoCommit(true);
131                         } catch (Exception e) {
132                                 ;
133                         }
134                         freeConnection(con, pstmt);
135                 }
136                 return  returnId;
137         }
138
139         public void update (Entity theEntity) throws StorageObjectException {
140                 Connection con = null;
141                 PreparedStatement pstmt = null;
142                 ArrayList streamedInput = theEntity.streamedInput();
143                 HashMap theEntityValues = theEntity.getValues();
144                 String id = theEntity.getId();
145                 String aField;
146                 StringBuffer fv = new StringBuffer();
147                 boolean firstField = true;
148                 //cache
149                 invalidatePopupCache();
150                 // build sql statement
151                 for (int i = 0; i < getFields().size(); i++) {
152                         aField = (String)metadataFields.get(i);
153                         // only normal cases
154                         if (!(aField.equals(thePKeyName) || aField.equals("webdb_create") ||
155                                         aField.equals("webdb_lastchange") || (streamedInput != null && streamedInput.contains(aField)))) {
156                                 if (theEntityValues.containsKey(aField)) {
157                                         if (firstField == false) {
158                                                 fv.append(", ");
159                                         }
160                                         else {
161                                                 firstField = false;
162                                         }
163                                         if (aField.equals("to_parent_id")) {
164                                                 fv.append(aField).append("=").append(StringUtil.quote((String)theEntityValues.get(aField)));
165                                         } else {
166                                                 fv.append(aField).append("='").append(StringUtil.quote((String)theEntityValues.get(aField))).append("'");
167                                         }
168                                 }
169                         }
170                 }
171                 StringBuffer sql = new StringBuffer("update ").append(theTable).append(" set ").append(fv);
172                 // exceptions
173                 if (metadataFields.contains("webdb_lastchange")) {
174                         sql.append(",webdb_lastchange=NOW()");
175                 }
176                 if (streamedInput != null) {
177                         for (int i = 0; i < streamedInput.size(); i++) {
178                                 sql.append(",").append(streamedInput.get(i)).append("=?");
179                         }
180                 }
181                 sql.append(" where id=").append(id);
182                 theLog.printInfo("UPDATE: " + sql);
183                 // execute sql
184                 try {
185                         con = getPooledCon();
186                         con.setAutoCommit(false);
187                         pstmt = con.prepareStatement(sql.toString());
188                         if (streamedInput != null) {
189                                 for (int i = 0; i < streamedInput.size(); i++) {
190                                         String inputString = (String)theEntityValues.get(streamedInput.get(i));
191                                         pstmt.setBytes(i + 1, inputString.getBytes());
192                                 }
193                         }
194                         pstmt.executeUpdate();
195                 } catch (SQLException sqe) {
196                         throwSQLException(sqe, "update");
197                 } finally {
198                         try {
199                                 con.setAutoCommit(true);
200                         } catch (Exception e) {
201                                 ;
202                         }
203                         freeConnection(con, pstmt);
204                 }
205         }
206
207 }
208
209
210