/**
 * 
 */
package fi.seco.rdfobject.jena;

import com.hp.hpl.jena.datatypes.TypeMapper;
import com.hp.hpl.jena.graph.Node;
import com.hp.hpl.jena.graph.NodeFactory;
import com.hp.hpl.jena.graph.Triple;
import com.hp.hpl.jena.rdf.model.AnonId;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.rdf.model.ResourceFactory;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.sparql.core.Quad;

import fi.seco.rdfobject.BNodeResourceRDFObject;
import fi.seco.rdfobject.IQuad;
import fi.seco.rdfobject.IRDFObject;
import fi.seco.rdfobject.ITriple;
import fi.seco.rdfobject.LiteralRDFObject;
import fi.seco.rdfobject.URIResourceRDFObject;
import fi.seco.util.LocaleUtil;

/**
 * @author jiemakel
 * 
 */
public class JenaRDFObjectUtil {

	private static final Model m = ModelFactory.createDefaultModel();

	public static RDFNode getRDFNodeForRDFObject(IRDFObject o) {
		if (o.isLiteral()) {
			if (o.getLang() == null && o.getDataType() == null)
				return ResourceFactory.createPlainLiteral(o.getText());
			else if (o.getLang() != null)
				return m.createLiteral(o.getText(), o.getLang().toString());
			else return ResourceFactory.createTypedLiteral(o.getText(), TypeMapper.getInstance().getSafeTypeByName(o.getDataType()));
		} else if (o.isBlankNode())
			return m.createResource(new AnonId(o.getURI()));
		else return ResourceFactory.createResource(o.getURI());
	}

	public static Node getNodeForRDFObject(IRDFObject id) {
		if (id.isResource()) {
			String uri;
			uri = id.getURI();
			if (id.isBlankNode())
				return NodeFactory.createAnon(new AnonId(uri));
			else return NodeFactory.createURI(uri);
		} else if (id.getLang() == null && id.getDataType() == null)
			return NodeFactory.createLiteral(id.getText(), null, null);
		else if (id.getLang() != null)
			return NodeFactory.createLiteral(id.getText(), id.getLang().toString(), null);
		else return NodeFactory.createLiteral(id.getText(), null, TypeMapper.getInstance().getSafeTypeByName(id.getDataType()));

	}

	public static Triple getJenaTripleForTriple(ITriple t) {
		return new Triple(getNodeForRDFObject(t.getSubject()), getNodeForRDFObject(t.getProperty()), getNodeForRDFObject(t.getObject()));
	}

	public static Statement getJenaStatementForTriple(ITriple t) {
		RDFNode subject = getRDFNodeForRDFObject(t.getSubject());
		RDFNode object = getRDFNodeForRDFObject(t.getObject());
		if (!subject.isResource())
			throw new IllegalArgumentException("Cannot make Jena statement where subject is not a resource: " + t);
		if (!t.getProperty().isURI())
			throw new IllegalArgumentException("Cannot make Jena statement where property is not a URI resource: " + t);
		return ResourceFactory.createStatement(subject.asResource(), ResourceFactory.createProperty(t.getProperty().getURI()), object);
	}

	public static Quad getJenaQuadForQuad(IQuad t) {
		return new Quad(getNodeForRDFObject(t.getSubject()), getNodeForRDFObject(t.getProperty()), getNodeForRDFObject(t.getObject()), getNodeForRDFObject(t.getGraph()));
	}

	public static IRDFObject getRDFObjectForNode(Node object) {
		if (object.isLiteral())
			return new LiteralRDFObject(object.getLiteralLexicalForm(), !"".equals(object.getLiteralLanguage()) ? LocaleUtil.parseLocaleString(object.getLiteralLanguage()) : null, object.getLiteralDatatypeURI());
		else if (object.isBlank())
			return new BNodeResourceRDFObject(object.getBlankNodeLabel());
		else return new URIResourceRDFObject(object.getURI());
	}
}

@@ -238,16 +238,58 @@
 AnonId;%0A
+import com.hp.hpl.jena.rdf.model.Literal;%0A
 import c
@@ -3584,10 +3584,1164 @@
 ());%0A%09%7D%0A
+%0A%09public static IRDFObject getRDFObjectForRDFNode(RDFNode object) %7B%0A%09%09if (object.isLiteral()) %7B%0A%09%09%09Literal l = object.asLiteral();%0A%09%09%09return new LiteralRDFObject(l.getLexicalForm(), !%22%22.equals(l.getLanguage()) ? LocaleUtil.parseLocaleString(l.getLanguage()) : null, l.getDatatypeURI());%0A%09%09%7D else if (object.isAnon())%0A%09%09%09return new BNodeResourceRDFObject(object.asResource().getId().getLabelString());%0A%09%09else return new URIResourceRDFObject(object.asResource().getURI());%0A%09%7D%0A%0A%09public static ITriple getTripleForJenaTriple(Triple t) %7B%0A%09%09return new fi.seco.rdfobject.Triple(getRDFObjectForNode(t.getSubject()), getRDFObjectForNode(t.getPredicate()), getRDFObjectForNode(t.getObject()));%0A%09%7D%0A%0A%09public static ITriple getTripleForJenaStatement(Statement s) %7B%0A%09%09return new fi.seco.rdfobject.Triple(getRDFObjectForRDFNode(s.getSubject()), getRDFObjectForRDFNode(s.getPredicate()), getRDFObjectForRDFNode(s.getObject()));%0A%09%7D%0A%0A%09public static IQuad getQuadForJenaQuad(Quad q) %7B%0A%09%09return new fi.seco.rdfobject.Quad(getRDFObjectForNode(q.getSubject()), getRDFObjectForNode(q.getPredicate()), getRDFObjectForNode(q.getObject()), getRDFObjectForNode(q.getGraph()));%0A%09%7D%0A%0A
 %7D%0A

add convenience methods from Jena to RDFObject