|
1 |
| package org.objectledge.parameters.db; |
|
2 |
| |
|
3 |
| import java.sql.Connection; |
|
4 |
| import java.sql.ResultSet; |
|
5 |
| import java.sql.SQLException; |
|
6 |
| import java.sql.Statement; |
|
7 |
| import java.util.HashMap; |
|
8 |
| import java.util.Map; |
|
9 |
| |
|
10 |
| import org.jcontainer.dna.Logger; |
|
11 |
| import org.objectledge.database.Database; |
|
12 |
| import org.objectledge.database.DatabaseUtils; |
|
13 |
| import org.objectledge.parameters.DefaultParameters; |
|
14 |
| import org.objectledge.parameters.Parameters; |
|
15 |
| |
|
16 |
| |
|
17 |
| |
|
18 |
| |
|
19 |
| |
|
20 |
| |
|
21 |
| |
|
22 |
| public class DefaultDBParametersManager implements DBParametersManager |
|
23 |
| { |
|
24 |
| |
|
25 |
| private Logger logger; |
|
26 |
| |
|
27 |
| |
|
28 |
| private Database database; |
|
29 |
| |
|
30 |
| |
|
31 |
| private Map<Long, Parameters> localCache; |
|
32 |
| |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
2346
| public DefaultDBParametersManager(Database database, Logger logger)
|
|
40 |
| { |
|
41 |
2346
| this.logger = logger;
|
|
42 |
2346
| this.database = database;
|
|
43 |
2346
| localCache = new HashMap<Long, Parameters>();
|
|
44 |
| } |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
2300
| public Parameters createContainer()
|
|
53 |
| throws DBParametersException |
|
54 |
| { |
|
55 |
| |
|
56 |
2300
| long id = -1;
|
|
57 |
2300
| Connection conn = null;
|
|
58 |
2300
| try
|
|
59 |
| { |
|
60 |
2300
| conn = database.getConnection();
|
|
61 |
2300
| Statement statement = conn.createStatement();
|
|
62 |
2300
| id = database.getNextId(TABLE_NAME);
|
|
63 |
2300
| statement.execute("INSERT INTO "+TABLE_NAME+" values ("+id+",'','')");
|
|
64 |
| } |
|
65 |
| |
|
66 |
| catch(SQLException e) |
|
67 |
| { |
|
68 |
| throw new DBParametersException("Failed to insert the empty parameters", e); |
|
69 |
| } |
|
70 |
| |
|
71 |
| finally |
|
72 |
| { |
|
73 |
2300
| DatabaseUtils.close(conn);
|
|
74 |
| } |
|
75 |
2300
| DBParameters parameters = new DBParameters(null, id, database, logger);
|
|
76 |
2300
| Long key = new Long(id);
|
|
77 |
2300
| localCache.put(key, parameters);
|
|
78 |
2300
| return parameters;
|
|
79 |
| } |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
138
| public Parameters getParameters(long id) throws DBParametersException
|
|
89 |
| { |
|
90 |
138
| Parameters parameters;
|
|
91 |
138
| Long key = new Long(id);
|
|
92 |
138
| if (localCache.containsKey(key))
|
|
93 |
| { |
|
94 |
46
| return (Parameters)localCache.get(key);
|
|
95 |
| } |
|
96 |
92
| parameters = new DefaultParameters();
|
|
97 |
92
| Connection conn = null;
|
|
98 |
92
| try
|
|
99 |
| { |
|
100 |
92
| conn = database.getConnection( );
|
|
101 |
92
| Statement statement = conn.createStatement();
|
|
102 |
92
| ResultSet result = statement.executeQuery("SELECT * from " + TABLE_NAME +
|
|
103 |
| " where parameters_id = " + id); |
|
104 |
92
| boolean exist = false;
|
|
105 |
92
| while (result.next())
|
|
106 |
| { |
|
107 |
9384
| exist = true;
|
|
108 |
9384
| if (!result.getString("name").equals(""))
|
|
109 |
| { |
|
110 |
7038
| parameters.add(DatabaseUtils.unescapeSqlString(result.getString("name")),
|
|
111 |
| DatabaseUtils.unescapeSqlString(result.getString("value"))); |
|
112 |
| } |
|
113 |
| } |
|
114 |
92
| if (!exist)
|
|
115 |
| { |
|
116 |
46
| throw new DBParametersException("DBParameters with id = " + id + " does not exist");
|
|
117 |
| } |
|
118 |
46
| parameters = new DBParameters(parameters, id, database, logger);
|
|
119 |
46
| localCache.put(key, parameters);
|
|
120 |
46
| return parameters;
|
|
121 |
| } |
|
122 |
| |
|
123 |
| catch (SQLException e) |
|
124 |
| { |
|
125 |
| throw new DBParametersException("Failed to retrieve object", e); |
|
126 |
| } |
|
127 |
| |
|
128 |
| finally |
|
129 |
| { |
|
130 |
92
| DatabaseUtils.close(conn);
|
|
131 |
| } |
|
132 |
| } |
|
133 |
| |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
46
| public void deleteParameters(long id)
|
|
141 |
| throws DBParametersException |
|
142 |
| { |
|
143 |
46
| Long key = new Long(id);
|
|
144 |
46
| Connection conn = null;
|
|
145 |
46
| try
|
|
146 |
| { |
|
147 |
46
| conn = database.getConnection();
|
|
148 |
46
| Statement statement = conn.createStatement();
|
|
149 |
46
| statement.execute("DELETE FROM "+TABLE_NAME+" where parameters_id = "+id);
|
|
150 |
46
| localCache.remove(key);
|
|
151 |
| } |
|
152 |
| |
|
153 |
| catch(SQLException e) |
|
154 |
| { |
|
155 |
| throw new DBParametersException("Failed to delete parameters", e); |
|
156 |
| } |
|
157 |
| |
|
158 |
| finally |
|
159 |
| { |
|
160 |
46
| DatabaseUtils.close(conn);
|
|
161 |
| } |
|
162 |
| } |
|
163 |
| |
|
164 |
| |
|
165 |
| |
|
166 |
| |
|
167 |
0
| public void preloadContainers()
|
|
168 |
| throws DBParametersException |
|
169 |
| { |
|
170 |
0
| Connection conn = null;
|
|
171 |
0
| try
|
|
172 |
| { |
|
173 |
0
| conn = database.getConnection();
|
|
174 |
0
| Statement statement = conn.createStatement();
|
|
175 |
0
| ResultSet result = statement.executeQuery("SELECT parameters_id, name, value FROM "+
|
|
176 |
| TABLE_NAME+" ORDER BY parameters_id"); |
|
177 |
0
| if(result.next())
|
|
178 |
| { |
|
179 |
0
| long lastId;
|
|
180 |
0
| Parameters temp = new DefaultParameters();
|
|
181 |
0
| do
|
|
182 |
| { |
|
183 |
0
| do
|
|
184 |
| { |
|
185 |
0
| lastId = result.getLong(1);
|
|
186 |
0
| if(result.getString(2).length() > 0)
|
|
187 |
| { |
|
188 |
0
| temp.add(DatabaseUtils.unescapeSqlString(result.getString(2)),
|
|
189 |
| DatabaseUtils.unescapeSqlString(result.getString(3))); |
|
190 |
| } |
|
191 |
| } |
|
192 |
0
| while(result.next() && result.getLong(1) == lastId);
|
|
193 |
0
| Parameters pc = new DBParameters(temp, lastId, database, logger);
|
|
194 |
0
| temp.remove();
|
|
195 |
0
| localCache.put(new Long(lastId), pc);
|
|
196 |
| } |
|
197 |
0
| while(!result.isAfterLast());
|
|
198 |
| } |
|
199 |
| } |
|
200 |
| catch(SQLException e) |
|
201 |
| { |
|
202 |
0
| throw new DBParametersException("failed to preload parameters", e);
|
|
203 |
| } |
|
204 |
| finally |
|
205 |
| { |
|
206 |
0
| DatabaseUtils.close(conn);
|
|
207 |
| } |
|
208 |
| } |
|
209 |
| } |