1 package olr.om;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.apache.torque.util.Criteria;
7
8 /***
9 * The skeleton for this class was autogenerated by Torque on:
10 *
11 * [Wed Jun 16 21:12:50 CEST 2004]
12 *
13 * You should add additional methods to this class to meet the
14 * application requirements. This class will only be generated as
15 * long as it does not already exist in the output directory.
16 */
17 public class RdfStatementPeer
18 extends olr.om.BaseRdfStatementPeer
19 {
20 public static void delete(int id) throws Exception
21 {
22 final int[] ids = {id};
23 delete(ids);
24 }
25
26 /***
27 * Deletes statements and its related resources (subject,object,predicate),
28 * as long as the resources are not referenced by other objects (statements)
29 * @param ids The statements to delete
30 * @param connection Use this SQL connection
31 * @throws Exception Delegating all exceptions.
32 */
33 static void delete(int[] ids)
34 throws Exception
35 {
36 if (ids.length == 0) return;
37
38 final int[] resourceIds = getResourceIdsForDelete(ids);
39 final int[] literalIds = getLiteralIdsForDelete(ids);
40 final Criteria criteria = new Criteria();
41 criteria.addIn(RdfStatementPeer.ID, ids);
42 doDelete(criteria, OmContext.getConnection());
43 RdfResourcePeer.delete(resourceIds);
44 RdfLiteralPeer.delete(literalIds);
45 }
46
47 /***
48 * @param statementIds
49 * @param connection
50 * @return
51 * @throws Exception
52 */
53 private static int[] getResourceIdsForDelete(int[] statementIds)
54 throws Exception
55 {
56 final List resources = new ArrayList(10000);
57
58 resources.addAll(getStatementsResourceIds(statementIds, RdfStatementPeer.SUBJECT));
59 resources.addAll(getStatementsResourceIds(statementIds, RdfStatementPeer.OBJ_RESOURCE));
60 resources.addAll(getStatementsResourceIds(statementIds, RdfStatementPeer.PREDICATE));
61
62 final int[] ids = new int[resources.size()];
63
64 for(int i=0; i<resources.size(); i++)
65 ids[i] = ((RdfResource)resources.get(i)).getId();
66
67 return ids;
68 }
69
70 /***
71 *
72 * @param statementIds
73 * @param connection
74 * @param resourceRole
75 * @return
76 * @throws Exception
77 */
78 private static final List getStatementsResourceIds(int[] statementIds, String resourceRole)
79 throws Exception
80 {
81 final Criteria criteria = new Criteria();
82
83 criteria.setDistinct();
84 criteria.addJoin(RdfResourcePeer.ID, resourceRole);
85 criteria.addIn(RdfStatementPeer.ID, statementIds);
86
87 return RdfResourcePeer.doSelect(criteria, OmContext.getConnection());
88 }
89
90 /***
91 *
92 * @param statementIds
93 * @param connection
94 * @return All literal ids which are referenced by special statements (statementIds)
95 * @throws Exception All exceptions are delegated
96 */
97 private static int[] getLiteralIdsForDelete(int[] statementIds)
98 throws Exception
99 {
100 final StringBuffer subQuery = new StringBuffer(RdfLiteralPeer.ID).append(" NOT IN (SELECT DISTINCT ").append(RdfStatementPeer.OBJ_LITERAL)
101 .append(" FROM ").append(RdfStatementPeer.TABLE_NAME)
102 .append(" WHERE ").append(RdfStatementPeer.ID).append(" NOT IN ").append(DBUtils.idsToInList(statementIds))
103 .append(")");
104
105 final Criteria criteria = new Criteria();
106 criteria.setDistinct();
107 criteria.addJoin(RdfLiteralPeer.ID, RdfStatementPeer.OBJ_LITERAL);
108 criteria.addIn(RdfStatementPeer.ID, statementIds);
109 criteria.add(RdfLiteralPeer.ID, ((Object)subQuery.toString()), Criteria.CUSTOM);
110
111 final List literals = RdfLiteralPeer.doSelect(criteria, OmContext.getConnection());
112 final int[] ids = new int[literals.size()];
113
114 for(int i=0; i<literals.size(); i++)
115 ids[i] = ((RdfLiteral)literals.get(i)).getId();
116
117 return ids;
118 }
119 }