You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stanbol.apache.org by su...@apache.org on 2011/08/06 17:51:57 UTC
svn commit: r1154531 - in /incubator/stanbol/trunk/cmsadapter:
core/src/main/java/org/apache/stanbol/cmsadapter/core/mapping/
jcr/src/main/java/org/apache/stanbol/cmsadapter/jcr/repository/ parent/
Author: suat
Date: Sat Aug 6 15:51:57 2011
New Revision: 1154531
URL: http://svn.apache.org/viewvc?rev=1154531&view=rev
Log:
STANBOL 306:
-Added storage component for JCR repositories that reads CMS vocabulary annotated RDF and updates the repository accordingly
-It creates new nodes or updates existing ones based on the RDFBridge configurations.
Added:
incubator/stanbol/trunk/cmsadapter/core/src/main/java/org/apache/stanbol/cmsadapter/core/mapping/RDFBridgeHelper.java
incubator/stanbol/trunk/cmsadapter/jcr/src/main/java/org/apache/stanbol/cmsadapter/jcr/repository/JCRRepositoryAccessImpl.java
- copied, changed from r1153911, incubator/stanbol/trunk/cmsadapter/jcr/src/main/java/org/apache/stanbol/cmsadapter/jcr/repository/JCRRepositoryAccess.java
Modified:
incubator/stanbol/trunk/cmsadapter/core/src/main/java/org/apache/stanbol/cmsadapter/core/mapping/CMSVocabularyAnnotator.java
incubator/stanbol/trunk/cmsadapter/core/src/main/java/org/apache/stanbol/cmsadapter/core/mapping/RDFBridgeManager.java
incubator/stanbol/trunk/cmsadapter/jcr/src/main/java/org/apache/stanbol/cmsadapter/jcr/repository/JCRRDFMapper.java
incubator/stanbol/trunk/cmsadapter/jcr/src/main/java/org/apache/stanbol/cmsadapter/jcr/repository/JCRRepositoryAccess.java
incubator/stanbol/trunk/cmsadapter/parent/ (props changed)
Modified: incubator/stanbol/trunk/cmsadapter/core/src/main/java/org/apache/stanbol/cmsadapter/core/mapping/CMSVocabularyAnnotator.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/cmsadapter/core/src/main/java/org/apache/stanbol/cmsadapter/core/mapping/CMSVocabularyAnnotator.java?rev=1154531&r1=1154530&r2=1154531&view=diff
==============================================================================
--- incubator/stanbol/trunk/cmsadapter/core/src/main/java/org/apache/stanbol/cmsadapter/core/mapping/CMSVocabularyAnnotator.java (original)
+++ incubator/stanbol/trunk/cmsadapter/core/src/main/java/org/apache/stanbol/cmsadapter/core/mapping/CMSVocabularyAnnotator.java Sat Aug 6 15:51:57 2011
@@ -16,9 +16,6 @@
*/
package org.apache.stanbol.cmsadapter.core.mapping;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@@ -30,28 +27,44 @@ import org.apache.clerezza.rdf.core.MGra
import org.apache.clerezza.rdf.core.NonLiteral;
import org.apache.clerezza.rdf.core.Resource;
import org.apache.clerezza.rdf.core.Triple;
-import org.apache.clerezza.rdf.core.TripleCollection;
import org.apache.clerezza.rdf.core.UriRef;
-import org.apache.clerezza.rdf.core.impl.SimpleMGraph;
import org.apache.clerezza.rdf.core.impl.TripleImpl;
-import org.apache.clerezza.rdf.jena.facade.JenaGraph;
import org.apache.stanbol.cmsadapter.servicesapi.helper.CMSAdapterVocabulary;
import org.apache.stanbol.cmsadapter.servicesapi.mapping.RDFBridge;
-import org.apache.stanbol.entityhub.servicesapi.defaults.NamespaceEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import com.hp.hpl.jena.ontology.OntModel;
-import com.hp.hpl.jena.ontology.OntModelSpec;
-import com.hp.hpl.jena.rdf.model.Model;
-import com.hp.hpl.jena.rdf.model.ModelFactory;
-import com.hp.hpl.jena.rdf.model.RDFWriter;
-
+/**
+ * This class takes an RDF in {@link MGraph} and annotates it according to specified {@link RDFBridge}s.
+ *
+ * @author suat
+ *
+ */
public class CMSVocabularyAnnotator {
private static final Logger log = LoggerFactory.getLogger(CMSAdapterVocabulary.class);
- private static final UriRef RDF_TYPE = new UriRef(NamespaceEnum.rdf + "Type");
-
+ /**
+ * It is the single method of this class that add CMS vocabulary annotations to the {@link MGraph}
+ * specified.<br>
+ * <br>
+ * It first select target resources by using the configurations obtained from
+ * {@link RDFBridge#getTargetPropertyResources()} and {@link RDFBridge#getTargetResourceValue()}.<br>
+ * <br>
+ * In the next step, parent/child relations are set according to configuration values obtained from
+ * {@link RDFBridge#getChildrenResources()}. In case of multiple children having same name, an integer
+ * value added to the end of the name incrementally e.g <b>name</b>,<b>name1</b>,<b>name2</b>,...<br>
+ * <br>
+ * Then property annotations are added to according configuration values obtained from
+ * {@link RDFBridge#getTargetPropertyResources()}. Name of a property is kept for each bridge to to make
+ * possible giving different names for the same property in different bridges.<br>
+ * <br>
+ * In the last step parent annotations are added according hierarchy that was formed previously.
+ *
+ * @param rdfBridges
+ * {@link RDFBridge} instances keeping configurations to annotate raw RDF data
+ * @param graph
+ * {@link MGraph} keeping the raw RDF data
+ */
public void addAnnotationsToGraph(List<RDFBridge> rdfBridges, MGraph graph) {
LiteralFactory literalFactory = LiteralFactory.getInstance();
Map<UriRef,Object> children;
@@ -73,7 +86,8 @@ public class CMSVocabularyAnnotator {
// There should be a valid name for CMS Object
if (!name.contentEquals("")) {
- graph.add(new TripleImpl(subject, RDF_TYPE, CMSAdapterVocabulary.CMS_OBJECT));
+ graph.add(new TripleImpl(subject, RDFBridgeHelper.RDF_TYPE,
+ CMSAdapterVocabulary.CMS_OBJECT));
// if this object has already has name and path annotations, it means that it's already
// processed as child of another object. So, don't put new name and path annotations
@@ -88,15 +102,15 @@ public class CMSVocabularyAnnotator {
Map<String,Integer> childNames = new HashMap<String,Integer>();
while (childrenIt.hasNext()) {
Triple child = childrenIt.next();
- NonLiteral childSubject = new UriRef(replaceEndCharacters(child.getObject()
- .toString()));
+ NonLiteral childSubject = new UriRef(RDFBridgeHelper.removeEndCharacters(child
+ .getObject().toString()));
String childName = getNameOfProperty(childSubject, children.get(childPropURI),
graph);
if (!childName.contentEquals("")) {
- removeExistingTriple(childSubject, CMSAdapterVocabulary.CMS_OBJECT_NAME,
- graph);
- graph.add(new TripleImpl(childSubject, RDF_TYPE,
+ RDFBridgeHelper.removeExistingTriple(childSubject,
+ CMSAdapterVocabulary.CMS_OBJECT_NAME, graph);
+ graph.add(new TripleImpl(childSubject, RDFBridgeHelper.RDF_TYPE,
CMSAdapterVocabulary.CMS_OBJECT));
graph.add(new TripleImpl(childSubject,
CMSAdapterVocabulary.CMS_OBJECT_PARENT_REF, subject));
@@ -141,26 +155,11 @@ public class CMSVocabularyAnnotator {
*/
annotatePaths(targetRootPath, graph);
}
-
- // remove code
- try {
- saveOntology(graph, ontologyURI, true);
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
}
private static void annotatePaths(String targetRootPath, MGraph graph) {
// first detect root objects
- List<NonLiteral> roots = new ArrayList<NonLiteral>();
- Iterator<Triple> it = graph.filter(null, RDF_TYPE, CMSAdapterVocabulary.CMS_OBJECT);
- while (it.hasNext()) {
- Triple t = it.next();
- if (isRoot(t, graph)) {
- roots.add(t.getSubject());
- }
- }
+ List<NonLiteral> roots = RDFBridgeHelper.getRootObjetsOfGraph(graph);
// assign paths to children recursively
LiteralFactory literalFactory = LiteralFactory.getInstance();
@@ -194,15 +193,6 @@ public class CMSVocabularyAnnotator {
}
}
- private static boolean isRoot(Triple cmsObjectTriple, MGraph graph) {
- NonLiteral subject = cmsObjectTriple.getSubject();
- if (graph.filter(subject, CMSAdapterVocabulary.CMS_OBJECT_PARENT_REF, null).hasNext()) {
- return false;
- } else {
- return true;
- }
- }
-
private static String formRootPath(String targetRootPath, String objectName) {
if (!targetRootPath.endsWith("/")) {
targetRootPath += "/";
@@ -232,10 +222,6 @@ public class CMSVocabularyAnnotator {
return candidateName;
}
- private static String replaceEndCharacters(String resource) {
- return resource.replace("<", "").replace(">", "");
- }
-
private static String getResourceStringValue(NonLiteral subject, UriRef nameProp, MGraph graph) {
Iterator<Triple> it = graph.filter(subject, nameProp, null);
if (it.hasNext()) {
@@ -252,94 +238,4 @@ public class CMSVocabularyAnnotator {
return "";
}
}
-
- private static void removeExistingTriple(NonLiteral subject, UriRef predicate, MGraph mGraph) {
- Iterator<Triple> it = mGraph.filter(subject, predicate, null);
- if (it.hasNext()) {
- mGraph.remove(it.next());
- }
- }
-
- // /////////////////////////////////////
-
- private static String ontologyURI = "http://deneme#";
-
- public static void main(String[] args) throws FileNotFoundException {
- CMSVocabularyAnnotator cmsVocabularyAnnotator = new CMSVocabularyAnnotator();
- cmsVocabularyAnnotator.fillCMSObjects();
- }
-
- private MGraph fillCMSObjects() throws FileNotFoundException {
-
- MGraph mGraph = new SimpleMGraph();
- UriRef cmsObject1 = new UriRef(ontologyURI + "Concept1");
- UriRef cmsObject2 = new UriRef(ontologyURI + "Concept2");
- UriRef cmsObject3 = new UriRef(ontologyURI + "Concept3");
- UriRef populatedPlace2 = new UriRef(ontologyURI + "PopulatedPlace2");
- UriRef populatedPlace3 = new UriRef(ontologyURI + "PopulatedPlace3");
- UriRef city1 = new UriRef(ontologyURI + "City1");
- UriRef city2 = new UriRef(ontologyURI + "City2");
-
- // types
- addProperty(cmsObject1, NamespaceEnum.rdf + "Type", new UriRef(NamespaceEnum.skos + "Concept"), null,
- mGraph);
- addProperty(cmsObject2, NamespaceEnum.rdf + "Type", new UriRef(NamespaceEnum.skos + "Concept"), null,
- mGraph);
- addProperty(cmsObject3, NamespaceEnum.rdf + "Type", new UriRef(NamespaceEnum.skos + "Concept"), null,
- mGraph);
- addProperty(populatedPlace2, NamespaceEnum.rdf + "Type", new UriRef(NamespaceEnum.dbpediaOnt
- + "PopulatedPlace"), null, mGraph);
- addProperty(populatedPlace3, NamespaceEnum.rdf + "Type", new UriRef(NamespaceEnum.dbpediaOnt
- + "PopulatedPlace"), null, mGraph);
-
- // labels
- addProperty(cmsObject1, NamespaceEnum.rdfs + "label", null, "CMSObject1", mGraph);
- addProperty(cmsObject2, NamespaceEnum.rdfs + "label", null, "CMSObject2", mGraph);
- addProperty(cmsObject3, NamespaceEnum.rdfs + "label", null, "CMSObject3", mGraph);
-
- // children
- addProperty(cmsObject1, NamespaceEnum.skos + "narrower", cmsObject2, null, mGraph);
- addProperty(cmsObject3, NamespaceEnum.skos + "narrower", cmsObject3, null, mGraph);
- addProperty(cmsObject1, NamespaceEnum.dbpediaProp + "city", city1, null, mGraph);
- addProperty(cmsObject2, NamespaceEnum.dbpediaProp + "city", city2, null, mGraph);
-
- // prop
- addProperty(cmsObject2, NamespaceEnum.dbpediaProp + "place", populatedPlace2, null, mGraph);
- addProperty(cmsObject3, NamespaceEnum.dbpediaProp + "place", populatedPlace3, null, mGraph);
- addProperty(cmsObject1, NamespaceEnum.skos + "definition", null, "CMSObject1Def", mGraph);
- addProperty(cmsObject2, NamespaceEnum.skos + "definition", null, "CMSObject2Def", mGraph);
- addProperty(cmsObject3, NamespaceEnum.skos + "definition", null, "CMSObject3Def", mGraph);
-
- saveOntology(mGraph, ontologyURI, false);
-
- return mGraph;
- }
-
- private void addProperty(UriRef subject, String predicate, UriRef object, Object litObject, MGraph mGraph) {
- UriRef prop = new UriRef(predicate);
- if (object != null) {
- mGraph.add(new TripleImpl(subject, prop, object));
- } else if (litObject != null) {
- mGraph.add(new TripleImpl(subject, prop, LiteralFactory.getInstance().createTypedLiteral(
- litObject)));
- }
- }
-
- private static void saveOntology(TripleCollection tc, String ontologyURI, boolean output) throws FileNotFoundException {
- JenaGraph jenaGraph = new JenaGraph(tc);
- Model model = ModelFactory.createModelForGraph(jenaGraph);
- OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM, model);
-
- FileOutputStream fos;
- if (output) {
- fos = new FileOutputStream("/home/srdc/Desktop/cmsAdapterTest/rdfmap/Outmgraph");
- } else {
- fos = new FileOutputStream("/home/srdc/Desktop/cmsAdapterTest/rdfmap/mgraph");
- }
- RDFWriter rdfWriter = ontModel.getWriter("RDF/XML");
- rdfWriter.setProperty("xmlbase", ontologyURI);
- rdfWriter.write(ontModel, fos, ontologyURI);
- }
-
- // /////////////////////////////////////
}
Added: incubator/stanbol/trunk/cmsadapter/core/src/main/java/org/apache/stanbol/cmsadapter/core/mapping/RDFBridgeHelper.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/cmsadapter/core/src/main/java/org/apache/stanbol/cmsadapter/core/mapping/RDFBridgeHelper.java?rev=1154531&view=auto
==============================================================================
--- incubator/stanbol/trunk/cmsadapter/core/src/main/java/org/apache/stanbol/cmsadapter/core/mapping/RDFBridgeHelper.java (added)
+++ incubator/stanbol/trunk/cmsadapter/core/src/main/java/org/apache/stanbol/cmsadapter/core/mapping/RDFBridgeHelper.java Sat Aug 6 15:51:57 2011
@@ -0,0 +1,211 @@
+package org.apache.stanbol.cmsadapter.core.mapping;
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
+import javax.jcr.PropertyType;
+
+import org.apache.clerezza.rdf.core.Literal;
+import org.apache.clerezza.rdf.core.MGraph;
+import org.apache.clerezza.rdf.core.NonLiteral;
+import org.apache.clerezza.rdf.core.Resource;
+import org.apache.clerezza.rdf.core.Triple;
+import org.apache.clerezza.rdf.core.TypedLiteral;
+import org.apache.clerezza.rdf.core.UriRef;
+import org.apache.stanbol.cmsadapter.servicesapi.helper.CMSAdapterVocabulary;
+import org.apache.stanbol.entityhub.servicesapi.defaults.NamespaceEnum;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Provides utility classes that are used parsing the RDF data
+ *
+ * @author suat
+ *
+ */
+public class RDFBridgeHelper {
+ private static final Logger log = LoggerFactory.getLogger(RDFBridgeHelper.class);
+
+ public static final UriRef RDF_TYPE = new UriRef(NamespaceEnum.rdf + "Type");
+
+ private static final UriRef base64Uri = dataTypeURI("base64Binary");
+ private static final UriRef dateTimeUri = dataTypeURI("dateTime");
+ private static final UriRef booleanUri = dataTypeURI("boolean");
+ private static final UriRef stringUri = dataTypeURI("string");
+ private static final UriRef xsdInteger = dataTypeURI("integer");
+ private static final UriRef xsdInt = dataTypeURI("int");
+ private static final UriRef xsdShort = dataTypeURI("short");
+ private static final UriRef xsdLong = dataTypeURI("long");
+ private static final UriRef xsdDouble = dataTypeURI("double");
+ private static final UriRef xsdAnyURI = dataTypeURI("anyURI");
+
+ /**
+ * Gets a list of {@link NonLiteral} which indicates URIs of resources representing the root objects in
+ * the graph e.g the object that do not have {@code CMSAdapterVocabulary#CMS_OBJECT_PARENT_REF} property
+ *
+ * @param annotatedGraph
+ * @return
+ */
+ public static List<NonLiteral> getRootObjetsOfGraph(MGraph annotatedGraph) {
+ List<NonLiteral> roots = new ArrayList<NonLiteral>();
+ Iterator<Triple> it = annotatedGraph.filter(null, RDF_TYPE, CMSAdapterVocabulary.CMS_OBJECT);
+ while (it.hasNext()) {
+ Triple t = it.next();
+ if (isRoot(t, annotatedGraph)) {
+ roots.add(t.getSubject());
+ }
+ }
+ return roots;
+ }
+
+ private static boolean isRoot(Triple cmsObjectTriple, MGraph graph) {
+ NonLiteral subject = cmsObjectTriple.getSubject();
+ if (graph.filter(subject, CMSAdapterVocabulary.CMS_OBJECT_PARENT_REF, null).hasNext()) {
+ return false;
+ } else {
+ return true;
+ }
+ }
+
+ /**
+ * Gets {@link Resource} of {@code Triple} which is specified the <code>subject</code> and
+ * <code>propName</code> parameters
+ *
+ * @param subject
+ * @param propName
+ * @param graph
+ * @return specified resource if it exists, otherwise it returns <code>null</code>
+ */
+ public static Resource getResource(NonLiteral subject, UriRef propName, MGraph graph) {
+ Iterator<Triple> it = graph.filter(subject, propName, null);
+ if (it.hasNext()) {
+ return it.next().getObject();
+ } else {
+ log.warn("No triple for subject: {} and property: {}", subject, propName);
+ return null;
+ }
+ }
+
+ /**
+ * Gets lexical form of {@code Triple} which is specified the <code>subject</code> and
+ * <code>propName</code> parameters if the target resource is an instance of {@link Literal}.
+ *
+ * @param subject
+ * @param propName
+ * @param graph
+ * @return lexical value of specified resource it exists and an instance of {@link Literal}, otherwise it
+ * returns empty string
+ */
+ public static String getResourceStringValue(NonLiteral subject, UriRef propName, MGraph graph) {
+ Resource r = getResource(subject, propName, graph);
+ if (r != null) {
+ if (r instanceof Literal) {
+ return ((Literal) r).getLexicalForm();
+ } else {
+ log.warn("Resource value is not a Literal for subject: {} and property: {}", r);
+ return "";
+ }
+ } else {
+ return "";
+ }
+ }
+
+ /**
+ * Gets {@link UriRef} from the {@link Resource} of {@link Triple} which is specified the
+ * <code>subject</code> and <code>propName</code> parameters if the target resource is an instance of
+ * {@link UriRef}.
+ *
+ * @param subject
+ * @param propName
+ * @param graph
+ * @return {@link UriRef} of resource if it exists and is instance of {@link UriRef}
+ */
+ public static UriRef getResourceURIValue(NonLiteral subject, UriRef propName, MGraph graph) {
+ Resource r = getResource(subject, propName, graph);
+ if (r != null) {
+ if (r instanceof UriRef) {
+ return new UriRef(removeEndCharacters(r.toString()));
+ } else {
+ log.warn("Resource value is not a UriRef for subject: {} and property: {}", r);
+ return null;
+ }
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Remove first {@link Triple} specified with <code>subject</code> and <code>predicate</code> parameters
+ * from the specified {@link MGraph}
+ *
+ * @param subject
+ * {@link NonLiteral} subject of triple to be deleted
+ * @param predicate
+ * {@link UriRef} predicate of triple to be deleted
+ * @param mGraph
+ * {@link MGraph} from where the triple to be deleted
+ */
+ public static void removeExistingTriple(NonLiteral subject, UriRef predicate, MGraph mGraph) {
+ Iterator<Triple> it = mGraph.filter(subject, predicate, null);
+ if (it.hasNext()) {
+ mGraph.remove(it.next());
+ }
+ }
+
+ /**
+ * Removes <b><</b> and <b>></b> characters from start and end of the string respectively
+ *
+ * @param resource
+ * @return
+ */
+ public static String removeEndCharacters(String resource) {
+ return resource.replace("<", "").replace(">", "");
+ }
+
+ /**
+ * Return related {@link PropertyType} according to data type of a {@link Resource} if it is an instance
+ * of {@link TypedLiteral} ot {@link UriRef}, otherwise it return {@code PropertyType#STRING} as default
+ * type.
+ *
+ * @param r
+ * @link {@link Resource} instance of which property type is demanded
+ * @return related {@link PropertyType}
+ */
+ public static int getPropertyType(Resource r) {
+ if (r instanceof TypedLiteral) {
+ UriRef type = ((TypedLiteral) r).getDataType();
+ if (type.equals(stringUri)) {
+ return PropertyType.STRING;
+ } else if (type.equals(base64Uri)) {
+ return PropertyType.BINARY;
+ } else if (type.equals(booleanUri)) {
+ return PropertyType.BOOLEAN;
+ } else if (type.equals(dateTimeUri)) {
+ return PropertyType.DATE;
+ } else if (type.equals(xsdAnyURI)) {
+ return PropertyType.URI;
+ } else if (type.equals(xsdDouble)) {
+ return PropertyType.DOUBLE;
+ } else if (type.equals(xsdInt)) {
+ return PropertyType.DECIMAL;
+ } else if (type.equals(xsdInteger)) {
+ return PropertyType.DECIMAL;
+ } else if (type.equals(xsdLong)) {
+ return PropertyType.LONG;
+ } else if (type.equals(xsdShort)) {
+ return PropertyType.DECIMAL;
+ } else {
+ return PropertyType.STRING;
+ }
+ } else if (r instanceof UriRef) {
+ return PropertyType.URI;
+ } else {
+ return PropertyType.STRING;
+ }
+ }
+
+ private static UriRef dataTypeURI(String type) {
+ return new UriRef(NamespaceEnum.xsd + type);
+ }
+}
Modified: incubator/stanbol/trunk/cmsadapter/core/src/main/java/org/apache/stanbol/cmsadapter/core/mapping/RDFBridgeManager.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/cmsadapter/core/src/main/java/org/apache/stanbol/cmsadapter/core/mapping/RDFBridgeManager.java?rev=1154531&r1=1154530&r2=1154531&view=diff
==============================================================================
--- incubator/stanbol/trunk/cmsadapter/core/src/main/java/org/apache/stanbol/cmsadapter/core/mapping/RDFBridgeManager.java (original)
+++ incubator/stanbol/trunk/cmsadapter/core/src/main/java/org/apache/stanbol/cmsadapter/core/mapping/RDFBridgeManager.java Sat Aug 6 15:51:57 2011
@@ -49,16 +49,21 @@ public class RDFBridgeManager {
private static final Logger log = LoggerFactory.getLogger(RDFBridgeManager.class);
- @Reference(cardinality = ReferenceCardinality.MANDATORY_MULTIPLE, referenceInterface = RDFBridge.class, policy = ReferencePolicy.DYNAMIC, bind = "bindRDFBridge", unbind = "unbindRDFBridge", strategy = ReferenceStrategy.EVENT)
+ @Reference(cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE, referenceInterface = RDFBridge.class, policy = ReferencePolicy.DYNAMIC, bind = "bindRDFBridge", unbind = "unbindRDFBridge", strategy = ReferenceStrategy.EVENT)
List<RDFBridge> rdfBridges = new CopyOnWriteArrayList<RDFBridge>();
- @Reference(cardinality = ReferenceCardinality.MANDATORY_MULTIPLE, referenceInterface = RDFMapper.class, policy = ReferencePolicy.DYNAMIC, bind = "bindRDFMapper", unbind = "unbindRDFMapper", strategy = ReferenceStrategy.EVENT)
+ @Reference(cardinality = ReferenceCardinality.OPTIONAL_MULTIPLE, referenceInterface = RDFMapper.class, policy = ReferencePolicy.DYNAMIC, bind = "bindRDFMapper", unbind = "unbindRDFMapper", strategy = ReferenceStrategy.EVENT)
List<RDFMapper> rdfMappers = new CopyOnWriteArrayList<RDFMapper>();
@Reference
RepositoryAccessManager accessManager;
public void storeRDFToRepository(ConnectionInfo connectionInfo, MGraph rawRDFData) throws RepositoryAccessException {
+ if(rdfBridges.size() == 0) {
+ log.info("There is no RDF Bridge to execute");
+ return;
+ }
+
// According to connection type get RDF mapper, repository accessor, session
RDFMapper mapper = getRDFMapper(connectionInfo);
RepositoryAccess repositoryAccess = accessManager.getRepositoryAccessor(connectionInfo);
Modified: incubator/stanbol/trunk/cmsadapter/jcr/src/main/java/org/apache/stanbol/cmsadapter/jcr/repository/JCRRDFMapper.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/cmsadapter/jcr/src/main/java/org/apache/stanbol/cmsadapter/jcr/repository/JCRRDFMapper.java?rev=1154531&r1=1154530&r2=1154531&view=diff
==============================================================================
--- incubator/stanbol/trunk/cmsadapter/jcr/src/main/java/org/apache/stanbol/cmsadapter/jcr/repository/JCRRDFMapper.java (original)
+++ incubator/stanbol/trunk/cmsadapter/jcr/src/main/java/org/apache/stanbol/cmsadapter/jcr/repository/JCRRDFMapper.java Sat Aug 6 15:51:57 2011
@@ -1,25 +1,187 @@
package org.apache.stanbol.cmsadapter.jcr.repository;
import java.util.Iterator;
+import java.util.List;
+import javax.jcr.Node;
+import javax.jcr.RepositoryException;
+import javax.jcr.Session;
+import javax.jcr.Value;
+
+import org.apache.clerezza.rdf.core.Literal;
import org.apache.clerezza.rdf.core.MGraph;
+import org.apache.clerezza.rdf.core.NonLiteral;
+import org.apache.clerezza.rdf.core.Resource;
import org.apache.clerezza.rdf.core.Triple;
import org.apache.clerezza.rdf.core.UriRef;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Service;
+import org.apache.stanbol.cmsadapter.core.mapping.RDFBridgeHelper;
import org.apache.stanbol.cmsadapter.servicesapi.helper.CMSAdapterVocabulary;
import org.apache.stanbol.cmsadapter.servicesapi.mapping.RDFMapper;
-import org.apache.stanbol.entityhub.servicesapi.defaults.NamespaceEnum;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+/**
+ * Implementation of {@link RDFMapper} for JCR repositories.
+ *
+ * @author suat
+ *
+ */
@Component(immediate = true)
@Service
public class JCRRDFMapper implements RDFMapper {
+ private static final Logger log = LoggerFactory.getLogger(JCRRDFMapper.class);
+
+ @Override
+ public void storeRDFinRepository(Object session, MGraph annotatedGraph) {
+ List<NonLiteral> rootObjects = RDFBridgeHelper.getRootObjetsOfGraph(annotatedGraph);
+ for (NonLiteral root : rootObjects) {
+ String nodePath = RDFBridgeHelper.getResourceStringValue(root,
+ CMSAdapterVocabulary.CMS_OBJECT_PATH, annotatedGraph);
+ String nodeName = RDFBridgeHelper.getResourceStringValue(root,
+ CMSAdapterVocabulary.CMS_OBJECT_NAME, annotatedGraph);
+ Node parent = checkCreateParentNodes(nodePath, (Session) session);
+ createNode(parent, root, nodeName, annotatedGraph, (Session) session);
+ }
+ try {
+ ((Session) session).save();
+ } catch (RepositoryException e) {
+ log.warn("Failed to save JCR session", e);
+ }
+ }
+
+ /**
+ * Recursively creates the node itself and its children. If processed node already exists, it is updated
+ * with the new properties, a new node is created in the repository.
+ *
+ * @param parent
+ * parent {@link Node} of the the node to be created
+ * @param nodeSubject
+ * URI of the node to be created
+ * @param nodeName
+ * name of the node to be created
+ * @param graph
+ * annotated {@link MGraph}
+ * @param session
+ * {@link Session} object to access repository
+ */
+ private void createNode(Node parent,
+ NonLiteral nodeSubject,
+ String nodeName,
+ MGraph graph,
+ Session session) {
+ Node n = null;
+ String parentPath = "";
+ try {
+ parentPath = parent.getPath();
+ if (parent.hasNode(nodeName)) {
+ n = parent.getNode(nodeName);
+ } else {
+ n = parent.addNode(nodeName);
+ }
+ } catch (RepositoryException e) {
+ log.warn("Failed to create node {} for parent {}. ", new Object[] {parentPath, nodeName}, e);
+ }
+
+ // create properties
+ createNodeProperties(n, nodeSubject, graph);
+
+ // create children
+ Iterator<Triple> it = graph.filter(null, CMSAdapterVocabulary.CMS_OBJECT_PARENT_REF, nodeSubject);
+ while (it.hasNext()) {
+ NonLiteral childSubject = it.next().getSubject();
+ String childName = RDFBridgeHelper.getResourceStringValue(childSubject,
+ CMSAdapterVocabulary.CMS_OBJECT_NAME, graph);
+ createNode(n, childSubject, childName, graph, session);
+ }
+ }
+
+ /**
+ * This function creates specified properties for the node given as parameter. If a property already
+ * exists, it first is nullified then new value is assigned. Properties are accessed through
+ * {@code CMSAdapterVocabulary#CMS_OBJECT_HAS_PROPERTY} property.
+ *
+ * @param n
+ * {@link Node} of which properties will be updated/created
+ * @param subject
+ * corresponding URI of the node
+ * @param graph
+ * annotated {@link MGraph}
+ */
+ private void createNodeProperties(Node n, NonLiteral subject, MGraph graph) {
+ Iterator<Triple> it = graph.filter(subject, CMSAdapterVocabulary.CMS_OBJECT_HAS_PROPERTY, null);
+ while (it.hasNext()) {
+ UriRef tempPropURI = new UriRef(RDFBridgeHelper.removeEndCharacters(it.next().getObject()
+ .toString()));
+ String propName = RDFBridgeHelper.getResourceStringValue(tempPropURI,
+ CMSAdapterVocabulary.CMS_OBJECT_PROPERTY_NAME, graph);
+ UriRef propURI = RDFBridgeHelper.getResourceURIValue(tempPropURI,
+ CMSAdapterVocabulary.CMS_OBJECT_PROPERTY_URI, graph);
+ Resource resource = RDFBridgeHelper.getResource(subject, propURI, graph);
+
+ if (resource == null) {
+ continue;
+ }
+
+ String propValue = "";
+ if (resource instanceof Literal) {
+ propValue = RDFBridgeHelper.getResourceStringValue(subject, propURI, graph);
+ } else if (resource instanceof UriRef) {
+ propValue = RDFBridgeHelper.removeEndCharacters(resource.toString());
+ } else {
+ propValue = resource.toString();
+ }
+
+ int propType = RDFBridgeHelper.getPropertyType(resource);
+ try {
+ n.setProperty(propName, (Value) null);
+ n.setProperty(propName, propValue, propType);
+ log.debug("{} property of updated/created with {}", propName, propValue);
+ } catch (Exception e) {
+ log.warn("Failed to update property: {} for node {}: ", propName, e);
+ }
+ }
+ }
+
+ /**
+ * Takes path of a root object in the annotated RDF and tries to check parent nodes. If parent nodes do
+ * not exist, they are created.
+ *
+ * @param nodePath
+ * path of a root object
+ * @param session
+ * session to access repository
+ * @return the first level parent {@link Node} of the node specified with <code>nodePath</code> if there
+ * is not any exception, otherwise returns <code>null</code>.
+ */
+ private Node checkCreateParentNodes(String nodePath, Session session) {
+ Node n;
+ String currentPath;
+ try {
+ n = session.getRootNode();
+ currentPath = n.getPath();
+ } catch (RepositoryException e) {
+ log.warn("Failed to get root node while trying to get Node for path: {}", nodePath, e);
+ return null;
+ }
- @Override
- public void storeRDFinRepository(Object session, MGraph annotatedGraph) {
- Iterator<Triple> cmsObjectIt = annotatedGraph.filter(null, new UriRef(NamespaceEnum.rdf + "Type"),
- CMSAdapterVocabulary.CMS_OBJECT);
-
- }
+ String[] pathSections = nodePath.split("/");
+ for (int i = 1; i < pathSections.length - 1; i++) {
+ try {
+ if (!n.hasNode(pathSections[i])) {
+ n = n.addNode(pathSections[i]);
+ } else {
+ n = n.getNode(pathSections[i]);
+ }
+ currentPath = n.getPath();
+ } catch (RepositoryException e) {
+ log.warn("Failed to get child node for name: {} of node: {}", new Object[] {pathSections[i],
+ currentPath}, e);
+ return null;
+ }
+ }
+ return n;
+ }
}
Modified: incubator/stanbol/trunk/cmsadapter/jcr/src/main/java/org/apache/stanbol/cmsadapter/jcr/repository/JCRRepositoryAccess.java
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/cmsadapter/jcr/src/main/java/org/apache/stanbol/cmsadapter/jcr/repository/JCRRepositoryAccess.java?rev=1154531&r1=1154530&r2=1154531&view=diff
==============================================================================
--- incubator/stanbol/trunk/cmsadapter/jcr/src/main/java/org/apache/stanbol/cmsadapter/jcr/repository/JCRRepositoryAccess.java (original)
+++ incubator/stanbol/trunk/cmsadapter/jcr/src/main/java/org/apache/stanbol/cmsadapter/jcr/repository/JCRRepositoryAccess.java Sat Aug 6 15:51:57 2011
@@ -1,442 +1,17 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
package org.apache.stanbol.cmsadapter.jcr.repository;
-import java.net.MalformedURLException;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-
-import javax.jcr.LoginException;
-import javax.jcr.NamespaceException;
import javax.jcr.Node;
-import javax.jcr.NodeIterator;
-import javax.jcr.Repository;
-import javax.jcr.RepositoryException;
import javax.jcr.Session;
-import javax.jcr.SimpleCredentials;
-import javax.jcr.nodetype.NodeType;
-import javax.jcr.query.InvalidQueryException;
-import javax.jcr.query.Query;
-import javax.jcr.query.QueryManager;
-import javax.jcr.query.QueryResult;
-import org.apache.felix.scr.annotations.Component;
-import org.apache.felix.scr.annotations.Service;
-import org.apache.jackrabbit.rmi.repository.RMIRemoteRepository;
-import org.apache.jackrabbit.rmi.repository.URLRemoteRepository;
-import org.apache.stanbol.cmsadapter.servicesapi.model.web.CMSObject;
-import org.apache.stanbol.cmsadapter.servicesapi.model.web.ConnectionInfo;
-import org.apache.stanbol.cmsadapter.servicesapi.model.web.ObjectTypeDefinition;
-import org.apache.stanbol.cmsadapter.servicesapi.model.web.Property;
-import org.apache.stanbol.cmsadapter.servicesapi.model.web.PropertyDefinition;
import org.apache.stanbol.cmsadapter.servicesapi.repository.RepositoryAccess;
import org.apache.stanbol.cmsadapter.servicesapi.repository.RepositoryAccessException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-//TODO Implement session management. Currently for each operation a new Session is retrieved.
-@Component(immediate = true)
-@Service
-public class JCRRepositoryAccess implements RepositoryAccess {
-
- private static final Logger log = LoggerFactory.getLogger(JCRRepositoryAccess.class);
-
- /**
- * Tries to get a {@link Session} first through a {@link RMIRemoteRepository}. If the attempt is
- * unsuccessful it tries {@link URLRemoteRepository}. If the second attempt is also unsuccessful, throws a
- * {@link RepositoryAccessException}, otherwise returns a {@link Session} object.
- *
- * @param connectionInfo
- * @return {@link Session} if it was able to get one
- * @throws RepositoryAccessException
- */
- @Override
- public Session getSession(ConnectionInfo connectionInfo) throws RepositoryAccessException {
-
- Session session = null;
-
- ClassLoader cl = Thread.currentThread().getContextClassLoader();
- try {
- Thread.currentThread().setContextClassLoader(JCRRepositoryAccess.class.getClassLoader());
- session = getSessionByRMI(connectionInfo);
- if (session == null) {
- session = getSessionByURL(connectionInfo);
- }
- if (session == null) {
- throw new RepositoryAccessException("Failed to get JCR Session");
- }
- } finally {
- Thread.currentThread().setContextClassLoader(cl);
- }
-
- return session;
- }
-
- private Session getSessionByRMI(ConnectionInfo connectionInfo) {
- Repository repository = new RMIRemoteRepository(connectionInfo.getRepositoryURL());
- Session session = null;
- try {
- String workspaceName = connectionInfo.getWorkspaceName();
- String username = connectionInfo.getUsername();
- String password = connectionInfo.getPassword();
-
- if (workspaceName == null || workspaceName.equals("") || workspaceName.equals("default")) {
-
- session = repository.login(new SimpleCredentials(username, password.toCharArray()));
- } else {
- session = repository.login(new SimpleCredentials(username, password.toCharArray()),
- workspaceName);
- }
-
- } catch (LoginException e) {
- log.debug("Failed to get JCR session by RMIRemoteRepository");
- log.debug("Error message: " + e.getMessage());
- } catch (RepositoryException e) {
- log.debug("Failed to get JCR session by RMIRemoteRepository");
- log.debug("Error message: " + e.getMessage());
- }
- return session;
- }
-
- private Session getSessionByURL(ConnectionInfo connectionInfo) {
- Session session = null;
- try {
- Repository repository = new URLRemoteRepository(connectionInfo.getRepositoryURL());
- String workspaceName = connectionInfo.getWorkspaceName();
- String username = connectionInfo.getUsername();
- String password = connectionInfo.getPassword();
-
- if (workspaceName == null || workspaceName.equals("") || workspaceName.equals("default")) {
-
- session = repository.login(new SimpleCredentials(username, password.toCharArray()));
- } else {
- session = repository.login(new SimpleCredentials(username, password.toCharArray()),
- workspaceName);
- }
-
- } catch (LoginException e) {
- log.debug("Failed to get JCR session by URLRemoteRepository");
- log.debug("Error message: " + e.getMessage());
- } catch (RepositoryException e) {
- log.debug("Failed to get JCR session by URLRemoteRepository");
- log.debug("Error message: " + e.getMessage());
- } catch (MalformedURLException e) {
- log.debug("Failed to get JCR session by URLRemoteRepository");
- log.debug("Error message: " + e.getMessage());
- }
- return session;
- }
-
- @Override
- public List<CMSObject> getNodeByPath(String path, ConnectionInfo connectionInfo) throws RepositoryAccessException {
- List<JCRQueryRepresentation> queries = QueryHelper.getJCRItemByPathQuery(path);
- return processQuery(connectionInfo, queries, Integer.MAX_VALUE);
-
- }
-
- @Override
- public List<CMSObject> getNodeById(String id, ConnectionInfo connectionInfo) throws RepositoryAccessException {
- JCRQueryRepresentation query = QueryHelper.getJCRItemByIDQuery(id);
- return processQuery(connectionInfo, Arrays.asList(new JCRQueryRepresentation[] {query}),
- Integer.MAX_VALUE);
- }
-
- @Override
- public boolean canRetrieve(ConnectionInfo connectionInfo) {
- return connectionInfo.getConnectionType().equals("JCR");
- }
-
- private QueryResult executeQuery(JCRQueryRepresentation queryRep, ConnectionInfo connectionInfo) throws RepositoryAccessException {
-
- Session session = getSession(connectionInfo);
- return executeQuery(queryRep, session);
- }
-
- private QueryResult executeQuery(JCRQueryRepresentation queryRep, Session session) throws RepositoryAccessException {
- try {
-
- QueryManager qm;
- qm = session.getWorkspace().getQueryManager();
- Query query = qm.createQuery(queryRep.getQueryString(), queryRep.getQueryType());
- QueryResult queryResult = query.execute();
- return queryResult;
-
- } catch (InvalidQueryException e1) {
- throw new RepositoryAccessException("Invalid Query", e1);
- } catch (RepositoryException e) {
- throw new RepositoryAccessException("Error at query execution", e);
- }
- }
-
- private List<CMSObject> processQuery(ConnectionInfo connInfo,
- List<JCRQueryRepresentation> queryReps,
- int max) throws RepositoryAccessException {
- List<CMSObject> results = new ArrayList<CMSObject>();
- for (JCRQueryRepresentation queryRep : queryReps) {
- QueryResult queryResult = executeQuery(queryRep, connInfo);
- processResult(results, queryResult, max);
- }
- return results;
-
- }
-
- private List<CMSObject> processQuery(Session session, List<JCRQueryRepresentation> queryReps, int max) throws RepositoryAccessException {
- List<CMSObject> results = new ArrayList<CMSObject>();
- for (JCRQueryRepresentation queryRep : queryReps) {
- QueryResult queryResult = executeQuery(queryRep, session);
- processResult(results, queryResult, max);
- }
- return results;
- }
-
- private void processResult(List<CMSObject> cmsObjects, QueryResult queryResult, int max) throws RepositoryAccessException {
- try {
- NodeIterator nodes = queryResult.getNodes();
- while (nodes.hasNext() && max > 0) {
- Node current = nodes.nextNode();
- try {
- cmsObjects.add(JCRModelMapper.getCMSObject(current));
- } catch (RepositoryException e) {
- log.info("Can not create ObjectType for node {}", current.getPath());
- }
- max--;
- }
-
- } catch (RepositoryException e) {
- throw new RepositoryAccessException("Error accesing result nodes", e);
- }
- }
-
- @Override
- public List<CMSObject> getNodeByPath(String path, Object session) throws RepositoryAccessException {
- List<JCRQueryRepresentation> queries = QueryHelper.getJCRItemByPathQuery(path);
- return processQuery((Session) session, queries, Integer.MAX_VALUE);
- }
-
- @Override
- public List<CMSObject> getNodeById(String id, Object session) throws RepositoryAccessException {
- JCRQueryRepresentation query = QueryHelper.getJCRItemByIDQuery(id);
- return processQuery((Session) session, Arrays.asList(new JCRQueryRepresentation[] {query}),
- Integer.MAX_VALUE);
- }
-
- @Override
- public boolean canRetrieve(Object session) {
- return session instanceof Session;
- }
-
- @Override
- public List<CMSObject> getNodeByName(String name, Object session) throws RepositoryAccessException {
- JCRQueryRepresentation query = QueryHelper.getJCRItemByNameQuery(name);
- return processQuery((Session) session, Arrays.asList(new JCRQueryRepresentation[] {query}),
- Integer.MAX_VALUE);
- }
-
- @Override
- public List<CMSObject> getNodeByName(String name, ConnectionInfo connectionInfo) throws RepositoryAccessException {
- JCRQueryRepresentation query = QueryHelper.getJCRItemByNameQuery(name);
- return processQuery(connectionInfo, Arrays.asList(new JCRQueryRepresentation[] {query}),
- Integer.MAX_VALUE);
- }
-
- @Override
- public CMSObject getFirstNodeByPath(String path, ConnectionInfo connectionInfo) throws RepositoryAccessException {
- JCRQueryRepresentation query = QueryHelper.getJCRItemByPathQuery(path).get(0);
- return selectFirst(processQuery(connectionInfo, Arrays.asList(new JCRQueryRepresentation[] {query}),
- 1));
- }
-
- @Override
- public CMSObject getFirstNodeById(String id, ConnectionInfo connectionInfo) throws RepositoryAccessException {
- JCRQueryRepresentation query = QueryHelper.getJCRItemByIDQuery(id);
- return selectFirst(processQuery(connectionInfo, Arrays.asList(new JCRQueryRepresentation[] {query}),
- 1));
- }
-
- @Override
- public CMSObject getFirstNodeByPath(String path, Object session) throws RepositoryAccessException {
- JCRQueryRepresentation query = QueryHelper.getJCRItemByPathQuery(path).get(0);
- return selectFirst(processQuery((Session) session,
- Arrays.asList(new JCRQueryRepresentation[] {query}), 1));
- }
-
- @Override
- public CMSObject getFirstNodeById(String id, Object session) throws RepositoryAccessException {
- JCRQueryRepresentation query = QueryHelper.getJCRItemByIDQuery(id);
- return selectFirst(processQuery((Session) session,
- Arrays.asList(new JCRQueryRepresentation[] {query}), 1));
- }
-
- @Override
- public CMSObject getFirstNodeByName(String name, Object session) throws RepositoryAccessException {
- JCRQueryRepresentation query = QueryHelper.getJCRItemByNameQuery(name);
- return selectFirst(processQuery((Session) session,
- Arrays.asList(new JCRQueryRepresentation[] {query}), 1));
- }
-
- @Override
- public CMSObject getFirstNodeByName(String name, ConnectionInfo connectionInfo) throws RepositoryAccessException {
- JCRQueryRepresentation query = QueryHelper.getJCRItemByNameQuery(name);
- return selectFirst(processQuery(connectionInfo, Arrays.asList(new JCRQueryRepresentation[] {query}),
- 1));
-
- }
-
- private CMSObject selectFirst(List<CMSObject> nodes) {
- if (nodes.size() != 1) {
- return null;
- } else {
- return nodes.get(0);
- }
- }
-
- @Override
- public List<CMSObject> getChildren(CMSObject node, Object session) throws RepositoryAccessException {
- try {
- Node jcrNode = ((Session) session).getNodeByIdentifier(node.getUniqueRef());
- JCRModelMapper.fillCMSObjectChildren(node, jcrNode);
- return node.getChildren();
- } catch (RepositoryException e) {
- throw new RepositoryAccessException(e.getMessage(), e);
- }
- }
-
- @Override
- public ObjectTypeDefinition getObjectTypeDefinition(String typeRef, Object session) throws RepositoryAccessException {
- try {
- NodeType nodeType = ((Session) session).getWorkspace().getNodeTypeManager().getNodeType(typeRef);
- return JCRModelMapper.getObjectTypeDefinition(nodeType);
- } catch (RepositoryException e) {
- throw new RepositoryAccessException(e.getMessage(), e);
- }
-
- }
-
- @Override
- public List<Property> getProperties(CMSObject node, Object session) throws RepositoryAccessException {
- try {
- Node jcrNode = ((Session) session).getNodeByIdentifier(node.getUniqueRef());
- JCRModelMapper.fillProperties(node, jcrNode);
- return node.getProperty();
- } catch (RepositoryException e) {
- throw new RepositoryAccessException(e.getMessage(), e);
- }
-
- }
-
- @Override
- public List<PropertyDefinition> getPropertyDefinitions(ObjectTypeDefinition instance, Object session) throws RepositoryAccessException {
- try {
- NodeType nodeType = ((Session) session).getWorkspace().getNodeTypeManager()
- .getNodeType(instance.getUniqueRef());
- JCRModelMapper.fillPropertyDefinitions(instance, nodeType);
- return instance.getPropertyDefinition();
- } catch (RepositoryException e) {
- throw new RepositoryAccessException(e.getMessage(), e);
- }
- }
-
- @Override
- public List<ObjectTypeDefinition> getParentTypeDefinitions(ObjectTypeDefinition instance, Object session) throws RepositoryAccessException {
- try {
- List<ObjectTypeDefinition> parentsResolved = new ArrayList<ObjectTypeDefinition>(instance
- .getParentRef().size());
- for (String parentRef : instance.getParentRef()) {
- NodeType nodeType = ((Session) session).getWorkspace().getNodeTypeManager()
- .getNodeType(parentRef);
- parentsResolved.add(JCRModelMapper.getObjectTypeDefinition(nodeType));
- }
-
- return parentsResolved;
- } catch (RepositoryException e) {
- throw new RepositoryAccessException(e.getMessage(), e);
- }
- }
-
- @Override
- public List<ObjectTypeDefinition> getChildObjectTypeDefinitions(ObjectTypeDefinition instance,
- Object session) throws RepositoryAccessException {
- try {
- NodeType nodeType = ((Session) session).getWorkspace().getNodeTypeManager()
- .getNodeType(instance.getUniqueRef());
-
- JCRModelMapper.fillChildObjectDefinitions(instance, nodeType);
- return instance.getObjectTypeDefinition();
- } catch (RepositoryException e) {
- throw new RepositoryAccessException(e.getMessage(), e);
- }
-
- }
-
- @Override
- public CMSObject getContainerObject(Property instance, Object session) throws RepositoryAccessException {
- try {
- Node jcrNode = ((Session) session).getNodeByIdentifier(instance.getContainerObjectRef());
- return JCRModelMapper.getCMSObject(jcrNode);
- } catch (RepositoryException e) {
- throw new RepositoryAccessException(e.getMessage(), e);
- }
- }
-
- @Override
- public PropertyDefinition getPropertyDefinition(Property instance, Object session) throws RepositoryAccessException {
- try {
- NodeType sourceObjectType = ((Session) session).getWorkspace().getNodeTypeManager()
- .getNodeType(instance.getSourceObjectTypeRef());
- JCRModelMapper.fillPropertyDefinition(instance, sourceObjectType);
-
- return instance.getPropertyDefinition();
- } catch (RepositoryException e) {
- throw new RepositoryAccessException(e.getMessage(), e);
- }
- }
-
- @Override
- public String getNamespaceURI(String prefix, Object session) throws RepositoryAccessException {
- try {
- return ((Session) session).getNamespaceURI(prefix);
- } catch (NamespaceException e) {
- return null;
- } catch (RepositoryException e) {
- throw new RepositoryAccessException(e.getMessage(), e);
- }
- }
-
- @Override
- public CMSObject getParentByNode(CMSObject instance, Object session) throws RepositoryAccessException {
- Node parent;
- try {
- if (instance.getParentRef() == null) {
- Node jcrNode = ((Session) session).getNodeByIdentifier(instance.getUniqueRef());
- try {
- parent = jcrNode.getParent();
- instance.setParentRef(parent.getIdentifier());
- } catch (Exception e) {
- // No parent
- parent = null;
- }
- } else {
- parent = ((Session) session).getNodeByIdentifier(instance.getParentRef());
- }
- return JCRModelMapper.getCMSObject(parent);
- } catch (RepositoryException e) {
- throw new RepositoryAccessException("Error at accessing parent", e);
- }
- }
+/**
+ * This interface provides JCR specific repository access functionalities on top of {@link RepositoryAccess}
+ *
+ * @author suat
+ *
+ */
+public interface JCRRepositoryAccess extends RepositoryAccess {
+ Node getNodeByPath(String path, Session session) throws RepositoryAccessException;
}
Copied: incubator/stanbol/trunk/cmsadapter/jcr/src/main/java/org/apache/stanbol/cmsadapter/jcr/repository/JCRRepositoryAccessImpl.java (from r1153911, incubator/stanbol/trunk/cmsadapter/jcr/src/main/java/org/apache/stanbol/cmsadapter/jcr/repository/JCRRepositoryAccess.java)
URL: http://svn.apache.org/viewvc/incubator/stanbol/trunk/cmsadapter/jcr/src/main/java/org/apache/stanbol/cmsadapter/jcr/repository/JCRRepositoryAccessImpl.java?p2=incubator/stanbol/trunk/cmsadapter/jcr/src/main/java/org/apache/stanbol/cmsadapter/jcr/repository/JCRRepositoryAccessImpl.java&p1=incubator/stanbol/trunk/cmsadapter/jcr/src/main/java/org/apache/stanbol/cmsadapter/jcr/repository/JCRRepositoryAccess.java&r1=1153911&r2=1154531&rev=1154531&view=diff
==============================================================================
--- incubator/stanbol/trunk/cmsadapter/jcr/src/main/java/org/apache/stanbol/cmsadapter/jcr/repository/JCRRepositoryAccess.java (original)
+++ incubator/stanbol/trunk/cmsadapter/jcr/src/main/java/org/apache/stanbol/cmsadapter/jcr/repository/JCRRepositoryAccessImpl.java Sat Aug 6 15:51:57 2011
@@ -44,7 +44,6 @@ import org.apache.stanbol.cmsadapter.ser
import org.apache.stanbol.cmsadapter.servicesapi.model.web.ObjectTypeDefinition;
import org.apache.stanbol.cmsadapter.servicesapi.model.web.Property;
import org.apache.stanbol.cmsadapter.servicesapi.model.web.PropertyDefinition;
-import org.apache.stanbol.cmsadapter.servicesapi.repository.RepositoryAccess;
import org.apache.stanbol.cmsadapter.servicesapi.repository.RepositoryAccessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -52,9 +51,9 @@ import org.slf4j.LoggerFactory;
//TODO Implement session management. Currently for each operation a new Session is retrieved.
@Component(immediate = true)
@Service
-public class JCRRepositoryAccess implements RepositoryAccess {
+public class JCRRepositoryAccessImpl implements JCRRepositoryAccess {
- private static final Logger log = LoggerFactory.getLogger(JCRRepositoryAccess.class);
+ private static final Logger log = LoggerFactory.getLogger(JCRRepositoryAccessImpl.class);
/**
* Tries to get a {@link Session} first through a {@link RMIRemoteRepository}. If the attempt is
@@ -72,7 +71,7 @@ public class JCRRepositoryAccess impleme
ClassLoader cl = Thread.currentThread().getContextClassLoader();
try {
- Thread.currentThread().setContextClassLoader(JCRRepositoryAccess.class.getClassLoader());
+ Thread.currentThread().setContextClassLoader(JCRRepositoryAccessImpl.class.getClassLoader());
session = getSessionByRMI(connectionInfo);
if (session == null) {
session = getSessionByURL(connectionInfo);
@@ -220,7 +219,6 @@ public class JCRRepositoryAccess impleme
} catch (RepositoryException e) {
throw new RepositoryAccessException("Error accesing result nodes", e);
}
-
}
@Override
@@ -439,4 +437,21 @@ public class JCRRepositoryAccess impleme
throw new RepositoryAccessException("Error at accessing parent", e);
}
}
+
+ @Override
+ public Node getNodeByPath(String path, Session session) throws RepositoryAccessException {
+ JCRQueryRepresentation query = QueryHelper.getJCRItemByPathQuery(path).get(0);
+ QueryResult queryResult = executeQuery(query, session);
+ NodeIterator nodes;
+ try {
+ nodes = queryResult.getNodes();
+ if (nodes.hasNext()) {
+ return nodes.nextNode();
+ } else {
+ return null;
+ }
+ } catch (RepositoryException e) {
+ throw new RepositoryAccessException("Failed to execute query results.", e);
+ }
+ }
}
Propchange: incubator/stanbol/trunk/cmsadapter/parent/
------------------------------------------------------------------------------
--- svn:ignore (original)
+++ svn:ignore Sat Aug 6 15:51:57 2011
@@ -1,2 +1,3 @@
.settings
.project
+target