7290782e14819bc88fac1bc21c723872e8d6be57
[mir.git] / source / mircoders / storage / DatabaseLinksImcs.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31
32 package  mircoders.storage;
33
34 import  java.lang.*;
35 import  java.sql.*;
36 import  java.io.*;
37 import  java.util.*;
38 import  freemarker.template.*;
39 import  mir.storage.*;
40 import  mir.entity.*;
41 import  mir.misc.*;
42
43
44 /**
45  * <b>Diese Klasse implementiert die Datenbankverbindung zur MetaObjekt-Tabelle
46  *
47  *
48  */
49 public class DatabaseLinksImcs extends Database
50                 implements StorageObject {
51         private static DatabaseLinksImcs instance;
52
53         /**
54          * put your documentation comment here
55          * @return
56          * @exception StorageObjectException
57          */
58         // the following *has* to be sychronized cause this static method
59         // could get preemted and we could end up with 2 instances of DatabaseFoo..
60         // see the "Singletons with needles and thread" article at JavaWorld -mh
61         public synchronized static DatabaseLinksImcs getInstance () 
62           throws StorageObjectException {
63                 if (instance == null) {
64                         instance = new DatabaseLinksImcs();
65                         instance.myselfDatabase = instance;
66                 }
67                 return  instance;
68         }
69
70         /**
71          * put your documentation comment here
72          */
73         private DatabaseLinksImcs () throws StorageObjectException
74         {
75                 super();
76                 ////this.cache = new HashMap();
77                 this.hasTimestamp = false;
78                 this.theTable = "links_imcs";
79                 try {
80                         this.theEntityClass = Class.forName("mircoders.entity.EntityLinksImcs");
81                 } catch (Exception e) {
82                         throw  new StorageObjectException(e.toString());
83                 }
84         }
85
86         /** @todo toooo much copy/paste in this class //rk  */
87
88         public String insert (Entity theEntity) throws StorageObjectException {
89                 String returnId = "0";
90                 Connection con = null;
91                 PreparedStatement pstmt = null;
92                 //cache
93                 invalidatePopupCache();
94                 try {
95                         HashMap theEntityValues = theEntity.getValues();
96                         ArrayList streamedInput = theEntity.streamedInput();
97                         StringBuffer f = new StringBuffer();
98                         StringBuffer v = new StringBuffer();
99                         String aField, aValue;
100                         boolean firstField = true;
101                         // make sql-string
102                         for (int i = 0; i < getFields().size(); i++) {
103                                 aField = (String)getFields().get(i);
104                                 if (!aField.equals(thePKeyName)) {
105                                         aValue = null;
106                                         // sonderfaelle
107                                         if (aField.equals("webdb_create")) {
108                                                 aValue = "NOW()";
109                                         }
110                                         else {
111                                                 if (streamedInput != null && streamedInput.contains(aField)) {
112                                                         aValue = "?";
113                                                 }
114                                                 else {
115                                                         if (theEntityValues.containsKey(aField)) {
116                                                                 if (aField.equals("to_parent_id")) {
117                                                                         aValue = StringUtil.quote((String)theEntityValues.get(aField));
118                                                                 } else {
119                                                                         aValue = "'" + StringUtil.quote((String)theEntityValues.get(aField)) + "'";
120                                                                 }
121                                                         }
122                                                 }
123                                         }
124                                         // wenn Wert gegeben, dann einbauen
125                                         if (aValue != null) {
126                                                 if (firstField == false) {
127                                                         f.append(",");
128                                                         v.append(",");
129                                                 }
130                                                 else {
131                                                         firstField = false;
132                                                 }
133                                                 f.append(aField);
134                                                 v.append(aValue);
135                                         }
136                                 }
137                         }         // end for
138                         // insert into db
139                         StringBuffer sqlBuf = new StringBuffer("insert into ").append(theTable).append("(").append(f).append(") values (").append(v).append(")");
140                         String sql = sqlBuf.toString();
141                         theLog.printInfo("INSERT: " + sql);
142                         con = getPooledCon();
143                         con.setAutoCommit(false);
144                         pstmt = con.prepareStatement(sql);
145                         if (streamedInput != null) {
146                                 for (int i = 0; i < streamedInput.size(); i++) {
147                                         String inputString = (String)theEntityValues.get(streamedInput.get(i));
148                                         pstmt.setBytes(i + 1, inputString.getBytes());
149                                 }
150                         }
151                         pstmt.execute();
152                         pstmt = con.prepareStatement(theAdaptor.getLastInsertSQL((Database)myselfDatabase));
153                         ResultSet rs = pstmt.executeQuery();
154                         rs.next();
155                         returnId = rs.getString(1);
156                         theEntity.setId(returnId);
157                 } catch (SQLException sqe) {
158                         throwSQLException(sqe, "insert");
159                 } finally {
160                         try {
161                                 con.setAutoCommit(true);
162                         } catch (Exception e) {
163                                 ;
164                         }
165                         freeConnection(con, pstmt);
166                 }
167                 return  returnId;
168         }
169
170         public void update (Entity theEntity) throws StorageObjectException {
171                 Connection con = null;
172                 PreparedStatement pstmt = null;
173                 ArrayList streamedInput = theEntity.streamedInput();
174                 HashMap theEntityValues = theEntity.getValues();
175                 String id = theEntity.getId();
176                 String aField;
177                 StringBuffer fv = new StringBuffer();
178                 boolean firstField = true;
179                 //cache
180                 invalidatePopupCache();
181                 // build sql statement
182                 for (int i = 0; i < getFields().size(); i++) {
183                         aField = (String)metadataFields.get(i);
184                         // only normal cases
185                         if (!(aField.equals(thePKeyName) || aField.equals("webdb_create") ||
186                                         aField.equals("webdb_lastchange") || (streamedInput != null && streamedInput.contains(aField)))) {
187                                 if (theEntityValues.containsKey(aField)) {
188                                         if (firstField == false) {
189                                                 fv.append(", ");
190                                         }
191                                         else {
192                                                 firstField = false;
193                                         }
194                                         if (aField.equals("to_parent_id")) {
195                                                 fv.append(aField).append("=").append(StringUtil.quote((String)theEntityValues.get(aField)));
196                                         } else {
197                                                 fv.append(aField).append("='").append(StringUtil.quote((String)theEntityValues.get(aField))).append("'");
198                                         }
199                                 }
200                         }
201                 }
202                 StringBuffer sql = new StringBuffer("update ").append(theTable).append(" set ").append(fv);
203                 // exceptions
204                 if (metadataFields.contains("webdb_lastchange")) {
205                         sql.append(",webdb_lastchange=NOW()");
206                 }
207                 if (streamedInput != null) {
208                         for (int i = 0; i < streamedInput.size(); i++) {
209                                 sql.append(",").append(streamedInput.get(i)).append("=?");
210                         }
211                 }
212                 sql.append(" where id=").append(id);
213                 theLog.printInfo("UPDATE: " + sql);
214                 // execute sql
215                 try {
216                         con = getPooledCon();
217                         con.setAutoCommit(false);
218                         pstmt = con.prepareStatement(sql.toString());
219                         if (streamedInput != null) {
220                                 for (int i = 0; i < streamedInput.size(); i++) {
221                                         String inputString = (String)theEntityValues.get(streamedInput.get(i));
222                                         pstmt.setBytes(i + 1, inputString.getBytes());
223                                 }
224                         }
225                         pstmt.executeUpdate();
226                 } catch (SQLException sqe) {
227                         throwSQLException(sqe, "update");
228                 } finally {
229                         try {
230                                 con.setAutoCommit(true);
231                         } catch (Exception e) {
232                                 ;
233                         }
234                         freeConnection(con, pstmt);
235                 }
236         }
237
238 }
239
240
241