1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package olr.statementpool;
20
21 import java.sql.Connection;
22 import java.sql.ResultSet;
23
24 import olr.om.DBUtils;
25 import olr.om.ModelStatement;
26 import olr.om.ModelStatementPeer;
27 import olr.om.OmContext;
28 import olr.om.RdfLiteralPeer;
29 import olr.om.RdfNamespacePeer;
30 import olr.om.RdfResourcePeer;
31 import olr.om.RdfStatement;
32 import olr.om.RdfStatementPeer;
33 import olr.om.RdfUser;
34 import olr.rdf.Attribute;
35 import olr.rdf.Tools;
36
37 import org.apache.log4j.Logger;
38 import org.apache.torque.Torque;
39 import org.apache.torque.util.Transaction;
40
41 public class StatementFactory
42 {
43
44 private static final String ALL_STATEMENTS = "SELECT DISTINCT model.id AS model_id, model.name AS model_name, rdf_statement.id AS stmt_id, sub_ns.id AS sub_ns_id, sub_ns.ns_name AS sub_ns_name, subject.id AS sub_name_id, subject.ro_name AS sub_name, pre_ns.id AS pre_ns_id, pre_ns.ns_name AS pre_ns_name, predicate.id AS pre_name_id, predicate.ro_name AS pre_name, rdf_statement.res, obj_ns.id AS obj_ns_id, obj_ns.ns_name AS obj_ns_name, obj_res.id AS obj_res_id, obj_res.ro_name AS obj_res_name, obj_lit.id AS obj_lit_id, obj_lit.literal AS obj_lit_name, rdf_statement.usr, rdf_statement.ispublic FROM model, model_statement, rdf_statement, rdf_resource subject, rdf_namespace sub_ns, rdf_resource predicate, rdf_namespace pre_ns, rdf_resource obj_res, rdf_namespace obj_ns, rdf_literal obj_lit WHERE ((((((((model.id=model_statement.model_id AND model_statement.statement_id = rdf_statement.id) AND (rdf_statement.subject = subject.id)) AND (rdf_statement.predicate = predicate.id)) AND (rdf_statement.obj_resource = obj_res.id)) AND (rdf_statement.obj_literal = obj_lit.id)) AND (subject.ns = sub_ns.id)) AND (predicate.ns = pre_ns.id)) AND (obj_res.ns = obj_ns.id)) ORDER BY model.id, model.name, rdf_statement.id, sub_ns.id, sub_ns.ns_name, subject.id, subject.ro_name, pre_ns.id, pre_ns.ns_name, predicate.id, predicate.ro_name, rdf_statement.res, obj_ns.id, obj_ns.ns_name, obj_res.id, obj_res.ro_name, obj_lit.id, obj_lit.literal, rdf_statement.usr, rdf_statement.ispublic";
45
46
47 public static int loadStatements(DbStatementPool statementPool)
48 {
49 Connection dbConn = null;
50 int count = 0;
51 try
52 {
53 dbConn = DBUtils.getConnection();
54
55
56
57
58 ResultSet rs;
59 String sql = new StringBuffer("SELECT DISTINCT rdf_statement.id AS STMT_ID, sub_ns.ns_name AS SUB_NS_NAME, subject.ro_name AS SUB_NAME, pre_ns.ns_name AS PRE_NS_NAME, res, predicate.ro_name AS PRE_NAME, obj_ns.ns_name AS OBJ_NS_NAME, obj_res.ro_name AS OBJ_RES_NAME, obj_lit.literal AS OBJ_LIT_NAME, rdf_statement.ispublic FROM model, model_statement, rdf_statement, rdf_resource subject, rdf_namespace sub_ns, rdf_resource predicate, rdf_namespace pre_ns, rdf_resource obj_res, rdf_namespace obj_ns, rdf_literal obj_lit WHERE ((((((((model.id=model_statement.model_id AND model_statement.statement_id = rdf_statement.id) AND (rdf_statement.subject = subject.id)) AND (rdf_statement.predicate = predicate.id)) AND (rdf_statement.obj_resource = obj_res.id)) AND (rdf_statement.obj_literal = obj_lit.id)) AND (subject.ns = sub_ns.id)) AND (predicate.ns = pre_ns.id)) AND (obj_res.ns = obj_ns.id))")
60 .append(" AND model_id=").append(statementPool.getModel().getId())
61 .append(" AND model_statement.usr=").append(OmContext.getUser().getId())
62
63 .append(" ORDER BY rdf_statement.id, sub_ns.ns_name, subject.ro_name, pre_ns.ns_name, predicate.ro_name, obj_ns.ns_name, obj_res.ro_name, rdf_statement.ispublic")
64 .toString();
65
66 for (rs = dbConn.createStatement().executeQuery(sql); rs.next();)
67 try
68 {
69 int statementID = rs.getInt("STMT_ID");
70 String subject = String
71 .valueOf(rs.getString("SUB_NS_NAME"))
72 + String.valueOf(rs.getString("SUB_NAME"));
73 String predicate = String.valueOf(rs
74 .getString("PRE_NS_NAME"))
75 + String.valueOf(rs.getString("PRE_NAME"));
76 String object = rs.getString("RES").equalsIgnoreCase("Y") ? String
77 .valueOf(rs.getString("OBJ_NS_NAME"))
78 + String.valueOf(rs.getString("OBJ_RES_NAME"))
79 : rs.getString("OBJ_LIT_NAME");
80 boolean isPublic = rs.getString("ISPUBLIC")
81 .equalsIgnoreCase("Y");
82 DBStatement stmt = new DBStatement(subject, predicate,
83 object, statementID);
84 stmt.setPublic(isPublic);
85 statementPool.addStatement(stmt);
86 count++;
87 }
88 catch (Exception e)
89 {
90 Logger.getLogger(StatementFactory.class).error(e);
91 }
92
93 rs.close();
94 }
95 catch (Exception e)
96 {
97 e.printStackTrace();
98 }
99 DBUtils.releaseConnection(dbConn);
100 return count;
101 }
102
103 public static int addStatement(String subject, Attribute attribute,
104 int modelID)
105 throws Exception
106 {
107 Connection connection = null;
108 try
109 {
110 connection = Transaction.begin(Torque.getDefaultDB());
111 OmContext.setConnection(connection);
112
113 int rc = 0;
114
115 if (attribute instanceof ExtStatement)
116 rc = addStatement(subject, attribute, modelID, OmContext.getUser(),
117 ((ExtStatement) attribute).isPublic(), connection);
118 else
119 rc= addStatement(subject, attribute, modelID, OmContext.getUser(), true, connection);
120
121 Transaction.commit(connection);
122 return rc;
123 }
124 catch(Exception e)
125 {
126 if(connection != null) Transaction.rollback(connection);
127 throw e;
128 }
129 }
130
131 public static int addStatement(String subject, Attribute attribute,
132 int modelID, RdfUser user, boolean isPublic, Connection connection)
133 throws Exception
134 {
135 int newStatementID = 0;
136
137 try
138 {
139 String predicate = attribute.getPredicate();
140 String object = attribute.getObject();
141 int subjectID = addResource(addNamespace(Tools.getNS(subject)),
142 Tools.getName(subject));
143 int predicateID = addResource(addNamespace(Tools
144 .getNS(predicate)), Tools.getName(predicate));
145 int objectID = 0;
146 int literalID = 0;
147 if (Tools.isURI(object))
148 objectID = addResource(addNamespace(Tools.getNS(object)),
149 Tools.getName(object));
150 else
151 literalID = addLiteral(object);
152
153 final RdfStatement statement = new RdfStatement(subjectID, predicateID);
154 if(objectID == 0)
155 {
156 statement.setRes("N");
157 statement.setObjLiteral(literalID);
158 }
159 else
160 {
161 statement.setRes("Y");
162 statement.setObjResource(objectID);
163 }
164 statement.setIspublic(isPublic ? "Y" : "N");
165 statement.save(connection);
166
167 if(modelID > 0)
168 {
169 final ModelStatement modelStatement = new ModelStatement(modelID, statement);
170 modelStatement.save(connection);
171 }
172
173 return statement.getId();
174 }
175 catch (Exception e)
176 {
177 Logger.getLogger(StatementFactory.class).error(e);
178 throw e;
179 }
180 }
181
182 public static boolean updateStatement(DBStatement statement)
183 {
184 Connection dbConn = null;
185 boolean rc = false;
186 try
187 {
188 dbConn = DBUtils.getConnection();
189 }
190 catch (Exception e)
191 {
192 Logger.getLogger(StatementFactory.class).error(e);
193 return false;
194 }
195
196 try
197 {
198 String sql = new StringBuffer("SELECT DISTINCT sub_ns.id AS sub_ns_id, subject.id AS sub_name_id, pre_ns.id AS pre_ns_id, predicate.id AS pre_name_id, rdf_statement.res, obj_ns.id AS obj_ns_id, obj_res.id AS obj_res_id, obj_lit.id AS obj_lit_id FROM model, model_statement, rdf_statement, rdf_resource subject, rdf_namespace sub_ns, rdf_resource predicate, rdf_namespace pre_ns, rdf_resource obj_res, rdf_namespace obj_ns, rdf_literal obj_lit WHERE ((((((((model.id=model_statement.model_id AND model_statement.statement_id = rdf_statement.id) AND (rdf_statement.subject = subject.id)) AND (rdf_statement.predicate = predicate.id)) AND (rdf_statement.obj_resource = obj_res.id)) AND (rdf_statement.obj_literal = obj_lit.id)) AND (subject.ns = sub_ns.id)) AND (predicate.ns = pre_ns.id)) AND (obj_res.ns = obj_ns.id))")
199 .append(" AND rdf_statement.id=").append(statement.getDBstatementID())
200 .append(" ORDER BY sub_ns.id, subject.id, pre_ns.id,predicate.id, rdf_statement.res, obj_ns.id, obj_res.id, obj_lit.id")
201 .toString();
202 ResultSet rs = dbConn.createStatement().executeQuery(sql);
203 if (rs.next())
204 {
205 int SUB_NS_ID = rs.getInt("SUB_NS_ID");
206 int SUB_NAME_ID = rs.getInt("SUB_NAME_ID");
207 int PRE_NS_ID = rs.getInt("PRE_NS_ID");
208 int PRE_NAME_ID = rs.getInt("PRE_NAME_ID");
209 int OBJ_NS_ID = rs.getInt("OBJ_NS_ID");
210 int OBJ_RES_ID = rs.getInt("OBJ_RES_ID");
211 int OBJ_LIT_ID = rs.getInt("OBJ_LIT_ID");
212 String OBJ_RES = rs.getString("RES");
213
214 rs.close();
215 sql = "UPDATE rdf_statement SET";
216 String object = statement.getObject();
217 if (Tools.isURI(object))
218 {
219 String ns = Tools.getNS(object);
220 String name = Tools.getName(object);
221 sql = String.valueOf(sql)
222 + String.valueOf(String
223 .valueOf(String.valueOf((new StringBuffer(
224 " OBJ_RESOURCE=")).append(
225 addResource(addNamespace(ns),
226 name)).append(","))));
227 sql = String.valueOf(String.valueOf(sql)).concat(
228 " OBJ_LITERAL=0,");
229 sql = String.valueOf(String.valueOf(sql)).concat(
230 " RES='Y',");
231 }
232 else
233 {
234 sql = String.valueOf(String.valueOf(sql)).concat(
235 " OBJ_RESOURCE=0,");
236 sql = String.valueOf(sql)
237 + String
238 .valueOf(String.valueOf(String
239 .valueOf((new StringBuffer(
240 " OBJ_LITERAL=")).append(
241 addLiteral(object))
242 .append(","))));
243 sql = String.valueOf(String.valueOf(sql)).concat(
244 " RES='N',");
245 }
246 sql = String.valueOf(sql)
247 + String.valueOf(String.valueOf(String
248 .valueOf((new StringBuffer(" USR=")).append(
249 OmContext.getUser().getId()).append(","))));
250 sql = String.valueOf(sql)
251 + String.valueOf(String.valueOf(String
252 .valueOf((new StringBuffer(" ISPUBLIC='"))
253 .append(
254 statement.isPublic() ? 'Y'
255 : 'N').append("'"))));
256 sql = String.valueOf(sql)
257 + String.valueOf(" WHERE ID=".concat(String
258 .valueOf(String.valueOf(statement
259 .getDBstatementID()))));
260 if (dbConn.createStatement().executeUpdate(sql) > 0) rc = true;
261
262 if(OBJ_LIT_ID == 0)
263 RdfResourcePeer.delete(OBJ_RES_ID);
264 else
265 RdfLiteralPeer.delete(OBJ_LIT_ID);
266 }
267 else
268 {
269 rs.close();
270 }
271 }
272 catch (Exception e)
273 {
274 e.printStackTrace();
275 }
276 DBUtils.releaseConnection(dbConn);
277 return rc;
278 }
279
280 public static boolean removeStatement(DBStatement statement, int modelID)
281 {
282 return removeStatement(statement.getDBstatementID(), modelID);
283 }
284
285 public static boolean removeStatement(DBStatement statement, int modelID, boolean ofThisUser)
286 {
287 return removeStatement(statement.getDBstatementID(), modelID, ofThisUser);
288 }
289
290 private static boolean removeStatement(int statementID, int modelID)
291 {
292 return removeStatement(statementID, modelID, false);
293 }
294
295 private static boolean removeStatement(int statementID, int modelID, boolean ofThisUser)
296 {
297 Connection connection = null;
298 boolean rc = true;
299 try
300 {
301 connection = DBUtils.getConnection();
302 }
303 catch (Exception e)
304 {
305 e.printStackTrace();
306 boolean flag = false;
307 return flag;
308 }
309
310 try
311 {
312 ModelStatementPeer.delete(modelID, statementID);
313 RdfStatementPeer.delete(statementID);
314 }
315 catch (Exception e)
316 {
317 Logger.getLogger(StatementFactory.class).error(e);
318 rc = false;
319 }
320 DBUtils.releaseConnection(connection);
321
322 return rc;
323 }
324
325 private static int addLiteral(String value) throws Exception
326 {
327 if (value.length() == 0) return 0;
328 return RdfLiteralPeer.insert(value).getId();
329 }
330
331 private static int addResource(int nsID, String name)
332 throws Exception
333 {
334 if (nsID == 0 || name.length() == 0) return 0;
335 return RdfResourcePeer.selectElseInsert(nsID, name).getId();
336 }
337
338 private static int addNamespace(String nsName)
339 throws Exception
340 {
341 return RdfNamespacePeer.selectElseInsert(nsName).getId();
342 }
343 }