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 public String insert (Entity theEntity) throws StorageObjectException {
52 String returnId = "0";
53 Connection con = null;
54 PreparedStatement pstmt = null;
56 invalidatePopupCache();
58 HashMap theEntityValues = theEntity.getValues();
59 ArrayList streamedInput = theEntity.streamedInput();
60 StringBuffer f = new StringBuffer();
61 StringBuffer v = new StringBuffer();
62 String aField, aValue;
63 boolean firstField = true;
65 for (int i = 0; i < getFields().size(); i++) {
66 aField = (String)getFields().get(i);
67 if (!aField.equals(thePKeyName)) {
70 if (aField.equals("webdb_create")) {
74 if (streamedInput != null && streamedInput.contains(aField)) {
78 if (theEntityValues.containsKey(aField)) {
79 if (aField.equals("to_parent_id")) {
80 aValue = StringUtil.quote((String)theEntityValues.get(aField));
82 aValue = "'" + StringUtil.quote((String)theEntityValues.get(aField)) + "'";
87 // wenn Wert gegeben, dann einbauen
89 if (firstField == false) {
102 StringBuffer sqlBuf = new StringBuffer("insert into ").append(theTable).append("(").append(f).append(") values (").append(v).append(")");
103 String sql = sqlBuf.toString();
104 theLog.printInfo("INSERT: " + sql);
105 con = getPooledCon();
106 con.setAutoCommit(false);
107 pstmt = con.prepareStatement(sql);
108 if (streamedInput != null) {
109 for (int i = 0; i < streamedInput.size(); i++) {
110 String inputString = (String)theEntityValues.get(streamedInput.get(i));
111 pstmt.setBytes(i + 1, inputString.getBytes());
115 pstmt = con.prepareStatement(theAdaptor.getLastInsertSQL((Database)myselfDatabase));
116 ResultSet rs = pstmt.executeQuery();
118 returnId = rs.getString(1);
119 theEntity.setId(returnId);
120 } catch (SQLException sqe) {
121 throwSQLException(sqe, "insert");
124 con.setAutoCommit(true);
125 } catch (Exception e) {
128 freeConnection(con, pstmt);
133 public void update (Entity theEntity) throws StorageObjectException {
134 Connection con = null;
135 PreparedStatement pstmt = null;
136 ArrayList streamedInput = theEntity.streamedInput();
137 HashMap theEntityValues = theEntity.getValues();
138 String id = theEntity.getId();
140 StringBuffer fv = new StringBuffer();
141 boolean firstField = true;
143 invalidatePopupCache();
144 // build sql statement
145 for (int i = 0; i < getFields().size(); i++) {
146 aField = (String)metadataFields.get(i);
148 if (!(aField.equals(thePKeyName) || aField.equals("webdb_create") ||
149 aField.equals("webdb_lastchange") || (streamedInput != null && streamedInput.contains(aField)))) {
150 if (theEntityValues.containsKey(aField)) {
151 if (firstField == false) {
157 if (aField.equals("to_parent_id")) {
158 fv.append(aField).append("=").append(StringUtil.quote((String)theEntityValues.get(aField)));
160 fv.append(aField).append("='").append(StringUtil.quote((String)theEntityValues.get(aField))).append("'");
165 StringBuffer sql = new StringBuffer("update ").append(theTable).append(" set ").append(fv);
167 if (metadataFields.contains("webdb_lastchange")) {
168 sql.append(",webdb_lastchange=NOW()");
170 if (streamedInput != null) {
171 for (int i = 0; i < streamedInput.size(); i++) {
172 sql.append(",").append(streamedInput.get(i)).append("=?");
175 sql.append(" where id=").append(id);
176 theLog.printInfo("UPDATE: " + sql);
179 con = getPooledCon();
180 con.setAutoCommit(false);
181 pstmt = con.prepareStatement(sql.toString());
182 if (streamedInput != null) {
183 for (int i = 0; i < streamedInput.size(); i++) {
184 String inputString = (String)theEntityValues.get(streamedInput.get(i));
185 pstmt.setBytes(i + 1, inputString.getBytes());
188 pstmt.executeUpdate();
189 } catch (SQLException sqe) {
190 throwSQLException(sqe, "update");
193 con.setAutoCommit(true);
194 } catch (Exception e) {
197 freeConnection(con, pstmt);
202 * put your documentation comment here
205 public SimpleHash getHashData () {
206 return getHashData();
209 public SimpleList getPopupData () throws StorageObjectException {
210 return getPopupData();