1 package mircoders.storage;
7 import freemarker.template.*;
14 * <b>Diese Klasse implementiert die Datenbankverbindung zur MetaObjekt-Tabelle
18 public class DatabaseLinksImcs extends Database
19 implements StorageObject {
20 private static DatabaseLinksImcs instance;
23 * put your documentation comment here
25 * @exception StorageObjectException
27 public static DatabaseLinksImcs getInstance () throws StorageObjectException {
28 if (instance == null) {
29 instance = new DatabaseLinksImcs();
30 instance.myselfDatabase = instance;
36 * put your documentation comment here
38 private DatabaseLinksImcs () throws StorageObjectException
41 ////this.cache = new HashMap();
42 this.hasTimestamp = false;
43 this.theTable = "links_imcs";
45 this.theEntityClass = Class.forName("mircoders.entity.EntityLinksImcs");
46 } catch (Exception e) {
47 throw new StorageObjectException(e.toString());
51 /** @todo toooo much copy/paste in this class //rk */
53 public String insert (Entity theEntity) throws StorageObjectException {
54 String returnId = "0";
55 Connection con = null;
56 PreparedStatement pstmt = null;
58 invalidatePopupCache();
60 HashMap theEntityValues = theEntity.getValues();
61 ArrayList streamedInput = theEntity.streamedInput();
62 StringBuffer f = new StringBuffer();
63 StringBuffer v = new StringBuffer();
64 String aField, aValue;
65 boolean firstField = true;
67 for (int i = 0; i < getFields().size(); i++) {
68 aField = (String)getFields().get(i);
69 if (!aField.equals(thePKeyName)) {
72 if (aField.equals("webdb_create")) {
76 if (streamedInput != null && streamedInput.contains(aField)) {
80 if (theEntityValues.containsKey(aField)) {
81 if (aField.equals("to_parent_id")) {
82 aValue = StringUtil.quote((String)theEntityValues.get(aField));
84 aValue = "'" + StringUtil.quote((String)theEntityValues.get(aField)) + "'";
89 // wenn Wert gegeben, dann einbauen
91 if (firstField == false) {
104 StringBuffer sqlBuf = new StringBuffer("insert into ").append(theTable).append("(").append(f).append(") values (").append(v).append(")");
105 String sql = sqlBuf.toString();
106 theLog.printInfo("INSERT: " + sql);
107 con = getPooledCon();
108 con.setAutoCommit(false);
109 pstmt = con.prepareStatement(sql);
110 if (streamedInput != null) {
111 for (int i = 0; i < streamedInput.size(); i++) {
112 String inputString = (String)theEntityValues.get(streamedInput.get(i));
113 pstmt.setBytes(i + 1, inputString.getBytes());
117 pstmt = con.prepareStatement(theAdaptor.getLastInsertSQL((Database)myselfDatabase));
118 ResultSet rs = pstmt.executeQuery();
120 returnId = rs.getString(1);
121 theEntity.setId(returnId);
122 } catch (SQLException sqe) {
123 throwSQLException(sqe, "insert");
126 con.setAutoCommit(true);
127 } catch (Exception e) {
130 freeConnection(con, pstmt);
135 public void update (Entity theEntity) throws StorageObjectException {
136 Connection con = null;
137 PreparedStatement pstmt = null;
138 ArrayList streamedInput = theEntity.streamedInput();
139 HashMap theEntityValues = theEntity.getValues();
140 String id = theEntity.getId();
142 StringBuffer fv = new StringBuffer();
143 boolean firstField = true;
145 invalidatePopupCache();
146 // build sql statement
147 for (int i = 0; i < getFields().size(); i++) {
148 aField = (String)metadataFields.get(i);
150 if (!(aField.equals(thePKeyName) || aField.equals("webdb_create") ||
151 aField.equals("webdb_lastchange") || (streamedInput != null && streamedInput.contains(aField)))) {
152 if (theEntityValues.containsKey(aField)) {
153 if (firstField == false) {
159 if (aField.equals("to_parent_id")) {
160 fv.append(aField).append("=").append(StringUtil.quote((String)theEntityValues.get(aField)));
162 fv.append(aField).append("='").append(StringUtil.quote((String)theEntityValues.get(aField))).append("'");
167 StringBuffer sql = new StringBuffer("update ").append(theTable).append(" set ").append(fv);
169 if (metadataFields.contains("webdb_lastchange")) {
170 sql.append(",webdb_lastchange=NOW()");
172 if (streamedInput != null) {
173 for (int i = 0; i < streamedInput.size(); i++) {
174 sql.append(",").append(streamedInput.get(i)).append("=?");
177 sql.append(" where id=").append(id);
178 theLog.printInfo("UPDATE: " + sql);
181 con = getPooledCon();
182 con.setAutoCommit(false);
183 pstmt = con.prepareStatement(sql.toString());
184 if (streamedInput != null) {
185 for (int i = 0; i < streamedInput.size(); i++) {
186 String inputString = (String)theEntityValues.get(streamedInput.get(i));
187 pstmt.setBytes(i + 1, inputString.getBytes());
190 pstmt.executeUpdate();
191 } catch (SQLException sqe) {
192 throwSQLException(sqe, "update");
195 con.setAutoCommit(true);
196 } catch (Exception e) {
199 freeConnection(con, pstmt);