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