66182515f7645fcc07bd5a0a236b37211cf264f8
[mir.git] / source / mircoders / storage / DatabaseLinksImcs.java
1 package  mircoders.storage;
2
3 import  java.lang.*;
4 import  java.sql.*;
5 import  java.io.*;
6 import  java.util.*;
7 import  freemarker.template.*;
8 import  mir.storage.*;
9 import  mir.entity.*;
10 import  mir.misc.*;
11
12
13 /**
14  * <b>Diese Klasse implementiert die Datenbankverbindung zur MetaObjekt-Tabelle
15  *
16  *
17  */
18 public class DatabaseLinksImcs extends Database
19     implements StorageObject {
20   private static DatabaseLinksImcs instance;
21
22   /**
23    * put your documentation comment here
24    * @return
25    * @exception StorageObjectException
26    */
27   public static DatabaseLinksImcs getInstance () throws StorageObjectException {
28     if (instance == null) {
29       instance = new DatabaseLinksImcs();
30       instance.myselfDatabase = instance;
31     }
32     return  instance;
33   }
34
35   /**
36    * put your documentation comment here
37    */
38   private DatabaseLinksImcs () throws StorageObjectException
39   {
40     super();
41     //this.cache = new HashMap();
42     this.hasTimestamp = false;
43     this.theTable = "links_imcs";
44     try {
45       this.theEntityClass = Class.forName("mircoders.entity.EntityLinksImcs");
46     } catch (Exception e) {
47       throw  new StorageObjectException(e.toString());
48     }
49   }
50
51   public String insert (Entity theEntity) throws StorageObjectException {
52                 String returnId = "0";
53                 Connection con = null;
54                 PreparedStatement pstmt = null;
55                 //cache
56                 invalidatePopupCache();
57                 try {
58                         ArrayList streamedInput = theEntity.streamedInput();
59                         StringBuffer f = new StringBuffer();
60                         StringBuffer v = new StringBuffer();
61                         String aField, aValue;
62                         boolean firstField = true;
63                         // make sql-string
64                         for (int i = 0; i < getFields().size(); i++) {
65                                 aField = (String)getFields().get(i);
66                                 if (!aField.equals(thePKeyName)) {
67                                         aValue = null;
68                                         // sonderfaelle
69                                         if (aField.equals("webdb_create")) {
70                                                 aValue = "NOW()";
71                                         }
72                                         else {
73                                                 if (streamedInput != null && streamedInput.contains(aField)) {
74                                                         aValue = "?";
75                                                 }
76                                                 else {
77                                                         if (theEntity.hasValueForField(aField)) {
78                 if (aField.equals("to_parent_id")) {
79                   aValue = StringUtil.quote((String)theEntity.getValue(aField));
80                 } else {
81                                                                   aValue = "'" + StringUtil.quote((String)theEntity.getValue(aField)) + "'";
82                 }
83                                                         }
84                                                 }
85                                         }
86                                         // wenn Wert gegeben, dann einbauen
87                                         if (aValue != null) {
88                                                 if (firstField == false) {
89                                                         f.append(",");
90                                                         v.append(",");
91                                                 }
92                                                 else {
93                                                         firstField = false;
94                                                 }
95                                                 f.append(aField);
96                                                 v.append(aValue);
97                                         }
98                                 }
99                         }         // end for
100                         // insert into db
101                         StringBuffer sqlBuf = new StringBuffer("insert into ").append(theTable).append("(").append(f).append(") values (").append(v).append(")");
102                         String sql = sqlBuf.toString();
103                         theLog.printInfo("INSERT: " + sql);
104                         con = getPooledCon();
105                         con.setAutoCommit(false);
106                         pstmt = con.prepareStatement(sql);
107                         if (streamedInput != null) {
108                                 for (int i = 0; i < streamedInput.size(); i++) {
109                                         String inputString = (String)theEntity.getValue((String)streamedInput.get(i));
110                                         pstmt.setBytes(i + 1, inputString.getBytes());
111                                 }
112                         }
113                         pstmt.execute();
114                         pstmt = con.prepareStatement(theAdaptor.getLastInsertSQL((Database)myselfDatabase));
115                         ResultSet rs = pstmt.executeQuery();
116                         rs.next();
117                         returnId = rs.getString(1);
118                         theEntity.setId(returnId);
119                 } catch (SQLException sqe) {
120                         throwSQLException(sqe, "insert");
121                 } finally {
122                         try {
123                                 con.setAutoCommit(true);
124                         } catch (Exception e) {
125                                 ;
126                         }
127                         freeConnection(con, pstmt);
128                 }
129                 return  returnId;
130         }
131
132   public void update (Entity theEntity) throws StorageObjectException {
133     Connection con = null;
134     PreparedStatement pstmt = null;
135     ArrayList streamedInput = theEntity.streamedInput();
136     String id = theEntity.getId();
137     String aField;
138     StringBuffer fv = new StringBuffer();
139     boolean firstField = true;
140     //cache
141     invalidatePopupCache();
142     // build sql statement
143     for (int i = 0; i < getFields().size(); i++) {
144       aField = (String)metadataFields.get(i);
145       // only normal cases
146       if (!(aField.equals(thePKeyName) || aField.equals("webdb_create") ||
147           aField.equals("webdb_lastchange") || (streamedInput != null && streamedInput.contains(aField)))) {
148         if (theEntity.hasValueForField(aField)) {
149           if (firstField == false) {
150             fv.append(", ");
151           }
152           else {
153             firstField = false;
154           }
155           if (aField.equals("to_parent_id")) {
156             fv.append(aField).append("=").append(StringUtil.quote(theEntity.getValue(aField)));
157           } else {
158             fv.append(aField).append("='").append(StringUtil.quote((String)theEntity.getValue(aField))).append("'");
159           }
160         }
161       }
162     }
163     StringBuffer sql = new StringBuffer("update ").append(theTable).append(" set ").append(fv);
164     // exceptions
165     if (metadataFields.contains("webdb_lastchange")) {
166       sql.append(",webdb_lastchange=NOW()");
167     }
168     if (streamedInput != null) {
169       for (int i = 0; i < streamedInput.size(); i++) {
170         sql.append(",").append(streamedInput.get(i)).append("=?");
171       }
172     }
173     sql.append(" where id=").append(id);
174     theLog.printInfo("UPDATE: " + sql);
175     // execute sql
176     try {
177       con = getPooledCon();
178       con.setAutoCommit(false);
179       pstmt = con.prepareStatement(sql.toString());
180       if (streamedInput != null) {
181         for (int i = 0; i < streamedInput.size(); i++) {
182           String inputString = (String)theEntity.getValue((String)streamedInput.get(i));
183           pstmt.setBytes(i + 1, inputString.getBytes());
184         }
185       }
186       pstmt.executeUpdate();
187     } catch (SQLException sqe) {
188       throwSQLException(sqe, "update");
189     } finally {
190       try {
191         con.setAutoCommit(true);
192       } catch (Exception e) {
193         ;
194       }
195       freeConnection(con, pstmt);
196     }
197   }
198
199   /**
200    * put your documentation comment here
201    * @return
202    */
203   public SimpleHash getHashData () {
204     return  getHashData();
205   }
206
207   public SimpleList getPopupData () {
208     return  getPopupData();
209   }
210 }
211
212
213