0499acbab368f1ffa7e963dcea528c5eb2400c71
[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.List;\r
39 import java.util.Map;\r
40 \r
41 import mir.entity.Entity;\r
42 import mir.log.LoggerWrapper;\r
43 import mir.storage.Database;\r
44 import mir.storage.StorageObject;\r
45 import mir.storage.StorageObjectFailure;\r
46 import mir.util.JDBCStringRoutines;\r
47 \r
48 \r
49 /**\r
50  * <b>Diese Klasse implementiert die Datenbankverbindung zur MetaObjekt-Tabelle\r
51  *\r
52  *\r
53  */\r
54 public class DatabaseLinksImcs extends Database implements StorageObject {\r
55 \r
56   private static DatabaseLinksImcs instance;\r
57 \r
58   public static DatabaseLinksImcs getInstance() {\r
59     if (instance == null) {\r
60       synchronized (DatabaseLinksImcs.class) {\r
61         if (instance == null) {\r
62           DatabaseLinksImcs newInstance;\r
63 \r
64           newInstance = new DatabaseLinksImcs();\r
65           newInstance.myselfDatabase = newInstance;\r
66           instance = newInstance;\r
67         }\r
68       }\r
69     }\r
70 \r
71     return instance;\r
72   }\r
73 \r
74   private DatabaseLinksImcs() {\r
75     super();\r
76 \r
77     logger = new LoggerWrapper("ServletModule.Database.LinksImcs");\r
78     hasTimestamp = false;\r
79     theTable = "links_imcs";\r
80 \r
81     theEntityClass = mircoders.entity.EntityLinksImcs.class;\r
82   }\r
83 \r
84   /** @todo toooo much copy/paste in this class //rk  */\r
85 \r
86   public String insert(Entity theEntity) throws StorageObjectFailure {\r
87     String returnId = "0";\r
88     Connection con = null;\r
89     PreparedStatement pstmt = null;\r
90     //cache\r
91     invalidatePopupCache();\r
92     try {\r
93       Map theEntityValues = theEntity.getValues();\r
94       List streamedInput = theEntity.streamedInput();\r
95       StringBuffer f = new StringBuffer();\r
96       StringBuffer v = new StringBuffer();\r
97       String aField, aValue;\r
98       boolean firstField = true;\r
99       // make sql-string\r
100       for (int i = 0; i < getFields().size(); i++) {\r
101         aField = (String) getFields().get(i);\r
102         if (!aField.equals(thePKeyName)) {\r
103           aValue = null;\r
104           // sonderfaelle\r
105           if (aField.equals("webdb_create")) {\r
106             aValue = "NOW()";\r
107           }\r
108           else {\r
109             if (streamedInput != null && streamedInput.contains(aField)) {\r
110               aValue = "?";\r
111             }\r
112             else {\r
113               if (theEntityValues.containsKey(aField)) {\r
114                 if (aField.equals("to_parent_id")) {\r
115                   aValue = JDBCStringRoutines.escapeStringLiteral((String) theEntityValues.get(aField));\r
116                 }\r
117                 else {\r
118                   aValue = "'" + JDBCStringRoutines.escapeStringLiteral((String) theEntityValues.get(aField)) +  "'";\r
119                 }\r
120               }\r
121             }\r
122           }\r
123           // wenn Wert gegeben, dann einbauen\r
124           if (aValue != null) {\r
125             if (firstField == false) {\r
126               f.append(",");\r
127               v.append(",");\r
128             }\r
129             else {\r
130               firstField = false;\r
131             }\r
132             f.append(aField);\r
133             v.append(aValue);\r
134           }\r
135         }\r
136       } // end for\r
137       // insert into db\r
138       StringBuffer sqlBuf =\r
139           new StringBuffer("insert into ").append(theTable).append("(").append(f).append(") values (").append(v).append(")");\r
140       String sql = sqlBuf.toString();\r
141 \r
142       logger.info("INSERT: " + sql);\r
143 \r
144       con = getPooledCon();\r
145       con.setAutoCommit(false);\r
146       pstmt = con.prepareStatement(sql);\r
147       if (streamedInput != null) {\r
148         for (int i = 0; i < streamedInput.size(); i++) {\r
149           String inputString = (String) theEntityValues.get(streamedInput.get(i));\r
150           pstmt.setBytes(i + 1, inputString.getBytes());\r
151         }\r
152       }\r
153       pstmt.execute();\r
154       pstmt = con.prepareStatement(theAdaptor.getLastInsertSQL( (Database)\r
155           myselfDatabase));\r
156       ResultSet rs = pstmt.executeQuery();\r
157       rs.next();\r
158       returnId = rs.getString(1);\r
159       theEntity.setId(returnId);\r
160     }\r
161     catch (SQLException sqe) {\r
162       throwSQLException(sqe, "insert");\r
163     }\r
164     finally {\r
165       try {\r
166         con.setAutoCommit(true);\r
167       }\r
168       catch (Exception e) {\r
169         ;\r
170       }\r
171       freeConnection(con, pstmt);\r
172     }\r
173     return returnId;\r
174   }\r
175 \r
176   public void update(Entity theEntity) throws StorageObjectFailure {\r
177     Connection con = null;\r
178     PreparedStatement pstmt = null;\r
179     List streamedInput = theEntity.streamedInput();\r
180     Map theEntityValues = theEntity.getValues();\r
181     String id = theEntity.getId();\r
182     String aField;\r
183     StringBuffer fv = new StringBuffer();\r
184     boolean firstField = true;\r
185     //cache\r
186     invalidatePopupCache();\r
187     // build sql statement\r
188     for (int i = 0; i < getFields().size(); i++) {\r
189       aField = (String) metadataFields.get(i);\r
190       // only normal cases\r
191       if (! (aField.equals(thePKeyName) || aField.equals("webdb_create") ||\r
192              aField.equals("webdb_lastchange") ||\r
193              (streamedInput != null && streamedInput.contains(aField)))) {\r
194         if (theEntityValues.containsKey(aField)) {\r
195           if (firstField == false) {\r
196             fv.append(", ");\r
197           }\r
198           else {\r
199             firstField = false;\r
200           }\r
201           if (aField.equals("to_parent_id")) {\r
202             fv.append(aField).append("=").append(JDBCStringRoutines.escapeStringLiteral((String)theEntityValues.get(aField)));\r
203           }\r
204           else {\r
205             fv.append(aField).append("='").append(JDBCStringRoutines.escapeStringLiteral((String)theEntityValues.get(aField))).append("'");\r
206           }\r
207         }\r
208       }\r
209     }\r
210     StringBuffer sql = new StringBuffer("update ").append(theTable).append(\r
211         " set ").append(fv);\r
212     // exceptions\r
213     if (metadataFields.contains("webdb_lastchange")) {\r
214       sql.append(",webdb_lastchange=NOW()");\r
215     }\r
216     if (streamedInput != null) {\r
217       for (int i = 0; i < streamedInput.size(); i++) {\r
218         sql.append(",").append(streamedInput.get(i)).append("=?");\r
219       }\r
220     }\r
221     sql.append(" where id=").append(id);\r
222     logger.info("UPDATE: " + sql);\r
223     // execute sql\r
224     try {\r
225       con = getPooledCon();\r
226       con.setAutoCommit(false);\r
227       pstmt = con.prepareStatement(sql.toString());\r
228       if (streamedInput != null) {\r
229         for (int i = 0; i < streamedInput.size(); i++) {\r
230           String inputString = (String) theEntityValues.get(streamedInput.get(i));\r
231           pstmt.setBytes(i + 1, inputString.getBytes());\r
232         }\r
233       }\r
234       pstmt.executeUpdate();\r
235     }\r
236     catch (SQLException sqe) {\r
237       throwSQLException(sqe, "update");\r
238     }\r
239     finally {\r
240       try {\r
241         con.setAutoCommit(true);\r
242       }\r
243       catch (Exception e) {\r
244         ;\r
245       }\r
246       freeConnection(con, pstmt);\r
247     }\r
248   }\r
249 \r
250 }