/*
 * Licensed to Islandora Foundation under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * The Islandora Foundation licenses this file to you under the MIT License.
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/MIT
 *
 * 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 ca.islandora.indexing.triplestore;

import org.apache.camel.LoggingLevel;
import org.apache.camel.Predicate;
import org.apache.camel.builder.PredicateBuilder;
import org.apache.camel.builder.RouteBuilder;
import org.fcrepo.camel.FcrepoHeaders;
import org.fcrepo.camel.processor.SparqlUpdateProcessor;
import org.fcrepo.camel.processor.SparqlDeleteProcessor;

public class TriplestoreIndexer extends RouteBuilder {

    @Override
    public void configure() {

        Predicate isTriples = header("Content-Type").isEqualTo("application/n-triples");
        Predicate hasBaseUrl = header(FcrepoHeaders.FCREPO_BASE_URL).isNotNull();
        Predicate hasIdentifier = header(FcrepoHeaders.FCREPO_IDENTIFIER).isNotNull();
        Predicate hasFcrepoCamelHeaders = PredicateBuilder.and(hasBaseUrl, hasIdentifier);
        Predicate hasAction = PredicateBuilder.or(header("action").isEqualTo("delete"), header("action").isEqualTo("upsert"));
        Predicate isValid = PredicateBuilder.and(isTriples, hasFcrepoCamelHeaders, hasAction);

        onException(Exception.class)
            .maximumRedeliveries("{{error.maxRedeliveries}}")
            .log(LoggingLevel.ERROR, "Error Indexing in Triplestore: ${routeId}");

        from("{{input.stream}}")
            .routeId("IslandoraTriplestoreIndexerRouter")
            .filter(isValid)
                .choice()
                    .when(header("action").isEqualTo("delete"))
                        .to("direct:triplestoreDelete")
                    .otherwise()
                        .to("direct:triplestoreUpsert");

        from("direct:triplestoreUpsert")
            .routeId("islandoraTripelstoreIndexerUpsert")
            .process(new SparqlUpdateProcessor())
            .to("{{triplestore.baseUrl}}");

        from("direct:triplestoreDelete")
            .routeId("islandoraTripelstoreIndexerDelete")
            .process(new SparqlDeleteProcessor())
            .to("{{triplestore.baseUrl}}");

    }
}

@@ -802,32 +802,39 @@
 estore;%0A%0Aimport 
+static 
 org.apache.camel
@@ -834,28 +834,44 @@
 e.camel.
-LoggingLevel
+builder.PredicateBuilder.and
 ;%0Aimport
@@ -863,32 +863,39 @@
 der.and;%0Aimport 
+static 
 org.apache.camel
@@ -895,16 +895,24 @@
 e.camel.
+builder.
 Predicat
@@ -912,16 +912,26 @@
 redicate
+Builder.or
 ;%0Aimport
@@ -923,32 +923,39 @@
 lder.or;%0Aimport 
+static 
 org.apache.camel
@@ -959,32 +959,188 @@
 mel.
-builder.PredicateBuilder
+LoggingLevel.ERROR;%0Aimport static org.fcrepo.camel.FcrepoHeaders.FCREPO_BASE_URL;%0Aimport static org.fcrepo.camel.FcrepoHeaders.FCREPO_IDENTIFIER;%0A%0Aimport org.apache.camel.Predicate
 ;%0Aim
@@ -1187,47 +1187,8 @@
 er;%0A
-import org.fcrepo.camel.FcrepoHeaders;%0A
 impo
@@ -1298,16 +1298,42 @@
 essor;%0A%0A
+/**%0A * @author dhlamb%0A */%0A
 public c
@@ -1425,32 +1425,38 @@
 re() %7B%0A%0A        
+final 
 Predicate isTrip
@@ -1520,32 +1520,38 @@
 ples%22);%0A        
+final 
 Predicate hasBas
@@ -1560,38 +1560,24 @@
 rl = header(
-FcrepoHeaders.
 FCREPO_BASE_
@@ -1594,32 +1594,38 @@
 Null();%0A        
+final 
 Predicate hasIde
@@ -1641,30 +1641,16 @@
  header(
-FcrepoHeaders.
 FCREPO_I
@@ -1673,32 +1673,38 @@
 Null();%0A        
+final 
 Predicate hasFcr
@@ -1721,33 +1721,16 @@
 aders = 
-PredicateBuilder.
 and(hasB
@@ -1753,32 +1753,38 @@
 ifier);%0A        
+final 
 Predicate hasAct
@@ -1789,33 +1789,16 @@
 ction = 
-PredicateBuilder.
 or(heade
@@ -1877,16 +1877,22 @@
         
+final 
 Predicat
@@ -1903,33 +1903,16 @@
 Valid = 
-PredicateBuilder.
 and(isTr
@@ -2070,29 +2070,16 @@
    .log(
-LoggingLevel.
 ERROR, %22

fix checkstyle warnings (#18)