1 package olr.om;
2
3 import java.util.Date;
4 import java.util.List;
5
6 import olr.rdf.Definitions;
7 import olr.rdf.OLR3Definitions;
8
9 import org.apache.torque.util.Criteria;
10
11 /***
12 * The skeleton for this class was autogenerated by Torque on:
13 *
14 * [Wed Jun 16 21:12:50 CEST 2004]
15 *
16 * You should add additional methods to this class to meet the
17 * application requirements. This class will only be generated as
18 * long as it does not already exist in the output directory.
19 */
20 public class RdfResourcePeer
21 extends olr.om.BaseRdfResourcePeer
22 {
23 public static void delete(int id) throws Exception
24 {
25 final int[] ids = {id};
26 delete(ids);
27 }
28
29 static void delete(int[] ids) throws Exception
30 {
31 if (ids.length == 0) return;
32
33 final StringBuffer sql = new StringBuffer("DELETE FROM ").append(RdfResourcePeer.TABLE_NAME)
34 .append(" WHERE ID IN ").append(DBUtils.idsToInList(ids))
35 .append(" AND ").append(getReferencedResourcesSubQuery(RdfStatementPeer.SUBJECT))
36 .append(" AND ").append(getReferencedResourcesSubQuery(RdfStatementPeer.PREDICATE))
37 .append(" AND ").append(getReferencedResourcesSubQuery(RdfStatementPeer.OBJ_RESOURCE));
38
39 final int[] namespaceIds = getNamespaceIdsForDelete(ids);
40 RdfResourcePeer.executeStatement(sql.toString(), OmContext.getConnection());
41 RdfNamespacePeer.delete(namespaceIds);
42 }
43
44 private static String getReferencedResourcesSubQuery(String resourceRole)
45 {
46 final StringBuffer query = new StringBuffer(RdfResourcePeer.ID).append(" NOT IN (")
47 .append("SELECT DISTINCT ").append(resourceRole)
48 .append(" FROM ").append(RdfStatementPeer.TABLE_NAME).append(")");
49
50 return query.toString();
51 }
52
53 private static int[] getNamespaceIdsForDelete(int[] resourceIds)
54 throws Exception
55 {
56 final Criteria criteria = new Criteria();
57 criteria.setDistinct();
58 criteria.addJoin(RdfNamespacePeer.ID, RdfResourcePeer.NS);
59 criteria.addIn(RdfResourcePeer.ID, resourceIds);
60
61 final List namespaces = RdfNamespacePeer.doSelect(criteria, OmContext.getConnection());
62 final int[] ids = new int[namespaces.size()];
63
64 for(int i=0; i<namespaces.size(); i++)
65 ids[i] = ((RdfNamespace)namespaces.get(i)).getId();
66
67 return ids;
68 }
69
70 static RdfResource selectElseInsert(RdfNamespace namespace, String name)
71 throws Exception
72 {
73 final Criteria criteria = new Criteria();
74 criteria.add(RdfResourcePeer.NS, namespace.getId());
75 criteria.add(RdfResourcePeer.RO_NAME, name);
76 final List resources = RdfResourcePeer.doSelect(criteria, OmContext.getConnection());
77
78 assert (resources.size() <= 1);
79
80 if(resources.size() == 1)
81 return (RdfResource)resources.get(0);
82
83 final RdfResource resource = new RdfResource(namespace, name);
84 resource.setModified(new Date());
85 resource.save(OmContext.getConnection());
86 return resource;
87 }
88
89 public static RdfResource selectElseInsert(int namespace, String name)
90 throws Exception
91 {
92 return selectElseInsert(new RdfNamespace(namespace), name);
93 }
94
95 static RdfResource selectElseInsert(String namespace, String name)
96 throws Exception
97 {
98 return selectElseInsert(
99 RdfNamespacePeer.selectElseInsert(namespace),
100 name);
101 }
102
103 static RdfResource createModelResource(Model model)
104 throws Exception
105 {
106 return selectElseInsert(Definitions.getHomeNamespace(), OLR3Definitions.OLR3_MODEL_NAME + model.getId());
107 }
108
109 static RdfResource getLabelResource() throws Exception
110 {
111 return selectElseInsert(Definitions.RDFS_NS, Definitions.RDF_LABEL_NAME);
112 }
113
114 static RdfResource getCourseResource() throws Exception
115 {
116 return selectElseInsert(OLR3Definitions.OLR3_NS, OLR3Definitions.OLR3_COURSE_NAME);
117 }
118
119 static RdfResource getHasCourseResource() throws Exception
120 {
121 return selectElseInsert(OLR3Definitions.OLR3_NS, OLR3Definitions.OLR3_HAS_COURSE_NAME);
122 }
123
124 static RdfResource getModelResource() throws Exception
125 {
126 return selectElseInsert(OLR3Definitions.OLR3_NS, OLR3Definitions.OLR3_MODEL_NAME);
127 }
128
129 static RdfResource getModelRootResource() throws Exception
130 {
131 return selectElseInsert(OLR3Definitions.OLR3_NS, OLR3Definitions.OLR3_ROOT_NAME);
132 }
133
134 static RdfResource getTypeResource() throws Exception
135 {
136 return selectElseInsert(Definitions.RDF_NS, Definitions.RDF_TYPE_NAME);
137 }
138 }