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 // the following *has* to be sychronized cause this static method
28 // could get preemted and we could end up with 2 instances of DatabaseFoo..
29 // see the "Singletons with needles and thread" article at JavaWorld -mh
30 public synchronized static DatabaseLinksImcs getInstance ()
31 throws StorageObjectException {
32 if (instance == null) {
33 instance = new DatabaseLinksImcs();
34 instance.myselfDatabase = instance;
40 * put your documentation comment here
42 private DatabaseLinksImcs () throws StorageObjectException
45 ////this.cache = new HashMap();
46 this.hasTimestamp = false;
47 this.theTable = "links_imcs";
49 this.theEntityClass = Class.forName("mircoders.entity.EntityLinksImcs");
50 } catch (Exception e) {
51 throw new StorageObjectException(e.toString());
55 /** @todo toooo much copy/paste in this class //rk */
57 public String insert (Entity theEntity) throws StorageObjectException {
58 String returnId = "0";
59 Connection con = null;
60 PreparedStatement pstmt = null;
62 invalidatePopupCache();
64 HashMap theEntityValues = theEntity.getValues();
65 ArrayList streamedInput = theEntity.streamedInput();
66 StringBuffer f = new StringBuffer();
67 StringBuffer v = new StringBuffer();
68 String aField, aValue;
69 boolean firstField = true;
71 for (int i = 0; i < getFields().size(); i++) {
72 aField = (String)getFields().get(i);
73 if (!aField.equals(thePKeyName)) {
76 if (aField.equals("webdb_create")) {
80 if (streamedInput != null && streamedInput.contains(aField)) {
84 if (theEntityValues.containsKey(aField)) {
85 if (aField.equals("to_parent_id")) {
86 aValue = StringUtil.quote((String)theEntityValues.get(aField));
88 aValue = "'" + StringUtil.quote((String)theEntityValues.get(aField)) + "'";
93 // wenn Wert gegeben, dann einbauen
95 if (firstField == false) {
108 StringBuffer sqlBuf = new StringBuffer("insert into ").append(theTable).append("(").append(f).append(") values (").append(v).append(")");
109 String sql = sqlBuf.toString();
110 theLog.printInfo("INSERT: " + sql);
111 con = getPooledCon();
112 con.setAutoCommit(false);
113 pstmt = con.prepareStatement(sql);
114 if (streamedInput != null) {
115 for (int i = 0; i < streamedInput.size(); i++) {
116 String inputString = (String)theEntityValues.get(streamedInput.get(i));
117 pstmt.setBytes(i + 1, inputString.getBytes());
121 pstmt = con.prepareStatement(theAdaptor.getLastInsertSQL((Database)myselfDatabase));
122 ResultSet rs = pstmt.executeQuery();
124 returnId = rs.getString(1);
125 theEntity.setId(returnId);
126 } catch (SQLException sqe) {
127 throwSQLException(sqe, "insert");
130 con.setAutoCommit(true);
131 } catch (Exception e) {
134 freeConnection(con, pstmt);
139 public void update (Entity theEntity) throws StorageObjectException {
140 Connection con = null;
141 PreparedStatement pstmt = null;
142 ArrayList streamedInput = theEntity.streamedInput();
143 HashMap theEntityValues = theEntity.getValues();
144 String id = theEntity.getId();
146 StringBuffer fv = new StringBuffer();
147 boolean firstField = true;
149 invalidatePopupCache();
150 // build sql statement
151 for (int i = 0; i < getFields().size(); i++) {
152 aField = (String)metadataFields.get(i);
154 if (!(aField.equals(thePKeyName) || aField.equals("webdb_create") ||
155 aField.equals("webdb_lastchange") || (streamedInput != null && streamedInput.contains(aField)))) {
156 if (theEntityValues.containsKey(aField)) {
157 if (firstField == false) {
163 if (aField.equals("to_parent_id")) {
164 fv.append(aField).append("=").append(StringUtil.quote((String)theEntityValues.get(aField)));
166 fv.append(aField).append("='").append(StringUtil.quote((String)theEntityValues.get(aField))).append("'");
171 StringBuffer sql = new StringBuffer("update ").append(theTable).append(" set ").append(fv);
173 if (metadataFields.contains("webdb_lastchange")) {
174 sql.append(",webdb_lastchange=NOW()");
176 if (streamedInput != null) {
177 for (int i = 0; i < streamedInput.size(); i++) {
178 sql.append(",").append(streamedInput.get(i)).append("=?");
181 sql.append(" where id=").append(id);
182 theLog.printInfo("UPDATE: " + sql);
185 con = getPooledCon();
186 con.setAutoCommit(false);
187 pstmt = con.prepareStatement(sql.toString());
188 if (streamedInput != null) {
189 for (int i = 0; i < streamedInput.size(); i++) {
190 String inputString = (String)theEntityValues.get(streamedInput.get(i));
191 pstmt.setBytes(i + 1, inputString.getBytes());
194 pstmt.executeUpdate();
195 } catch (SQLException sqe) {
196 throwSQLException(sqe, "update");
199 con.setAutoCommit(true);
200 } catch (Exception e) {
203 freeConnection(con, pstmt);