2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
32 package mircoders.storage;
38 import freemarker.template.*;
45 * <b>Diese Klasse implementiert die Datenbankverbindung zur MetaObjekt-Tabelle
49 public class DatabaseLinksImcs extends Database
50 implements StorageObject {
51 private static DatabaseLinksImcs instance;
54 * put your documentation comment here
56 * @exception StorageObjectException
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;
71 * put your documentation comment here
73 private DatabaseLinksImcs () throws StorageObjectException
76 ////this.cache = new HashMap();
77 this.hasTimestamp = false;
78 this.theTable = "links_imcs";
80 this.theEntityClass = Class.forName("mircoders.entity.EntityLinksImcs");
81 } catch (Exception e) {
82 throw new StorageObjectException(e.toString());
86 /** @todo toooo much copy/paste in this class //rk */
88 public String insert (Entity theEntity) throws StorageObjectException {
89 String returnId = "0";
90 Connection con = null;
91 PreparedStatement pstmt = null;
93 invalidatePopupCache();
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;
102 for (int i = 0; i < getFields().size(); i++) {
103 aField = (String)getFields().get(i);
104 if (!aField.equals(thePKeyName)) {
107 if (aField.equals("webdb_create")) {
111 if (streamedInput != null && streamedInput.contains(aField)) {
115 if (theEntityValues.containsKey(aField)) {
116 if (aField.equals("to_parent_id")) {
117 aValue = StringUtil.quote((String)theEntityValues.get(aField));
119 aValue = "'" + StringUtil.quote((String)theEntityValues.get(aField)) + "'";
124 // wenn Wert gegeben, dann einbauen
125 if (aValue != null) {
126 if (firstField == false) {
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());
152 pstmt = con.prepareStatement(theAdaptor.getLastInsertSQL((Database)myselfDatabase));
153 ResultSet rs = pstmt.executeQuery();
155 returnId = rs.getString(1);
156 theEntity.setId(returnId);
157 } catch (SQLException sqe) {
158 throwSQLException(sqe, "insert");
161 con.setAutoCommit(true);
162 } catch (Exception e) {
165 freeConnection(con, pstmt);
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();
177 StringBuffer fv = new StringBuffer();
178 boolean firstField = true;
180 invalidatePopupCache();
181 // build sql statement
182 for (int i = 0; i < getFields().size(); i++) {
183 aField = (String)metadataFields.get(i);
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) {
194 if (aField.equals("to_parent_id")) {
195 fv.append(aField).append("=").append(StringUtil.quote((String)theEntityValues.get(aField)));
197 fv.append(aField).append("='").append(StringUtil.quote((String)theEntityValues.get(aField))).append("'");
202 StringBuffer sql = new StringBuffer("update ").append(theTable).append(" set ").append(fv);
204 if (metadataFields.contains("webdb_lastchange")) {
205 sql.append(",webdb_lastchange=NOW()");
207 if (streamedInput != null) {
208 for (int i = 0; i < streamedInput.size(); i++) {
209 sql.append(",").append(streamedInput.get(i)).append("=?");
212 sql.append(" where id=").append(id);
213 theLog.printInfo("UPDATE: " + sql);
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());
225 pstmt.executeUpdate();
226 } catch (SQLException sqe) {
227 throwSQLException(sqe, "update");
230 con.setAutoCommit(true);
231 } catch (Exception e) {
234 freeConnection(con, pstmt);