/*
   Copyright (c) 2010 Zepheira LLC, Some Rights Reserved

   Licensed 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.callimachusproject.behaviours;

import static org.openrdf.query.QueryLanguage.SPARQL;

import java.io.InputStream;

import org.callimachusproject.concepts.Template;
import org.callimachusproject.helpers.GraphPatternBuilder;
import org.callimachusproject.helpers.SubjectTracker;
import org.openrdf.http.object.exceptions.BadRequest;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.object.ObjectConnection;
import org.openrdf.repository.object.RDFObject;
import org.openrdf.rio.RDFHandler;
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.helpers.RDFHandlerBase;
import org.openrdf.rio.rdfxml.RDFXMLParser;

public abstract class DeleteSupport implements Template {

	private static class Remover extends RDFHandlerBase {
		private final ObjectConnection con;

		private Remover(ObjectConnection con) {
			this.con = con;
		}

		public void handleStatement(Statement st) throws RDFHandlerException {
			try {
				con.remove(st.getSubject(), st.getPredicate(), st.getObject());
			} catch (RepositoryException e) {
				throw new RDFHandlerException(e);
			}
		}
	}

	public void calliDeleteResource(RDFObject target, InputStream in)
			throws Exception {
		try {
			RDFXMLParser parser = new RDFXMLParser();
			ObjectConnection con = target.getObjectConnection();
			remove((URI) target.getResource(), in, parser, con);
		} catch (RDFHandlerException e) {
			throw new BadRequest(e);
		} finally {
			in.close();
		}
	}

	private void remove(URI resource, InputStream in,
			RDFXMLParser parser, ObjectConnection con) throws Exception {
		SubjectTracker remover = createSubjectTracker(resource, new Remover(con));
		GraphPatternBuilder pattern = new GraphPatternBuilder();
		parser.setRDFHandler(pattern);
		parser.parse(in, resource.stringValue());
		if (!pattern.isEmpty()) {
			String sparql = pattern.toSPARQLQuery();
			con.prepareGraphQuery(SPARQL, sparql).evaluate(remover);
			if (remover.isEmpty())
				throw new BadRequest("Content has Changed");
			if (!remover.isSubject(resource))
				throw new BadRequest("Wrong Subject");
		}
	}

	private SubjectTracker createSubjectTracker(URI resource,
			RDFHandler delegate) throws Exception {
		SubjectTracker tracker = new SubjectTracker(delegate);
		String about = resource.stringValue();
		tracker.accept(openBoundedPatterns("delete", about));
		return tracker;
	}
}

@@ -896,24 +896,75 @@
 ectTracker;%0A
+import org.callimachusproject.traits.SoundexTrait;%0A
 import org.o
@@ -1005,16 +1005,16 @@
 equest;%0A
-
 import o
@@ -1071,16 +1071,58 @@
 el.URI;%0A
+import org.openrdf.model.vocabulary.RDFS;%0A
 import o
@@ -1589,16 +1589,39 @@
 ion con;
+%0A%09%09private URI soundex;
 %0A%0A%09%09priv
@@ -1675,16 +1675,84 @@
  = con;%0A
+%09%09%09soundex = con.getValueFactory().createURI(SoundexTrait.SOUNDEX);%0A
 %09%09%7D%0A%0A%09%09p
@@ -1830,16 +1830,16 @@
 %09%09try %7B%0A
-
 %09%09%09%09con.
@@ -1898,16 +1898,119 @@
 ect());%0A
+%09%09%09%09if (RDFS.LABEL.equals(st.getPredicate())) %7B%0A%09%09%09%09%09con.remove(st.getSubject(), soundex, null);%0A%09%09%09%09%7D%0A
 %09%09%09%7D cat

Remove all soundex tags on delete