// BridgeDb,
// An abstraction layer for identifier mapping services, both local and online.
//
// Copyright 2006-2009  BridgeDb developers
// Copyright 2012-2013  Christian Y. A. Brenninkmeijer
// Copyright 2012-2013  OpenPhacts
//
// 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.bridgedb.uri.loader;

import org.apache.log4j.Logger;
import org.bridgedb.sql.justification.OpsJustificationMaker;
import org.bridgedb.uri.tools.RegexUriPattern;
import org.bridgedb.uri.tools.UriListener;
import org.bridgedb.utils.BridgeDBException;
import org.bridgedb.utils.Reporter;
import org.openrdf.model.Resource;
import org.openrdf.model.Statement;
import org.openrdf.model.URI;
import org.openrdf.model.Value;
import org.openrdf.rio.RDFHandlerException;

/**
 * Reads and RDF linkset file and passes the information on to a RdfLoader.
 *
 * Sperates the data into the different types supported by RDFLoader abd calls the appropriate methods
 *
 * What actually happens then is RDFLoader specific.
 *
 * @author Christian
 */
public class LinksetHandler extends LinkHandler{
    
    boolean processingFirstStatement = true;
    private final String justification;
    protected final URI mappingSource;

    private int noneLinkStatements;
    static final Logger logger = Logger.getLogger(LinksetHandler.class);
    
    public LinksetHandler(UriListener uriListener, URI linkPredicate, String justification, URI mappingSource){
        super(uriListener, linkPredicate);
        this.justification = justification;
        this.mappingSource = mappingSource;
    }
    
    /**
     * @deprecated 
     * @param uriListener
     * @param linkPredicate
     * @param justification
     * @param mappingSource
     * @param ignore 
     */
    public LinksetHandler(UriListener uriListener, URI linkPredicate, String justification, URI mappingSource, boolean ignore){
        this(uriListener, linkPredicate, justification, mappingSource);
    }
        
    @Override
    public void handleStatement(Statement st) throws RDFHandlerException {
        if (processingFirstStatement) {
            processFirstStatement(st);
        } else {
            super.handleStatement(st);
        }
    }
       
    /**
     * Process an RDF statement that forms part of the VoID header for the 
     * linkset file.
     * 
     * Once the header processor detects that it is starting to process links
     * it sets a flag, inserts the VoID header information into the database,
     * and then goes into a link insert only mode.
     * 
     * @param st an RDF statement
     * @throws RDFHandlerException
     */
    private void processFirstStatement(Statement st) throws RDFHandlerException{
        final URI predicate = st.getPredicate();
        if (predicate.equals(linkPredicate)) {
            Resource subject = st.getSubject();
            Value object = st.getObject();
            try {
                processingFirstStatement = false;
                if (!(subject instanceof URI)){
                    Reporter.error("None URI subject in " + st);
                    return;
                }
                if (!(object instanceof URI)){
                    Reporter.error("None URI object in " + st);
                    return;
                }
                RegexUriPattern sourcePattern = uriListener.toUriPattern(subject.stringValue());
                if (sourcePattern == null){
                     Reporter.error("Unable to get a pattern for subject in " + st);
                     return;
                }
                RegexUriPattern targetPattern = uriListener.toUriPattern(object.stringValue());
                if (targetPattern == null){
                    Reporter.error("Unable to get a pattern for " + object.stringValue());
                    return;
                }
                String backwardJustification = OpsJustificationMaker.getInstance().getInverse(justification);
                if (backwardJustification == null){
                    mappingSet = uriListener.registerMappingSet(sourcePattern, linkPredicate.stringValue(), 
                            justification, targetPattern, mappingSource, false);
                    this.setSymetric(false);
                } else {
                    mappingSet = uriListener.registerMappingSet(sourcePattern, linkPredicate.stringValue(), 
                            justification, backwardJustification, targetPattern, mappingSource);
                }
            } catch (BridgeDBException ex) {
                Reporter.error("Error handling: " + st, ex);
            }
            super.handleStatement(st);
        } else {
            this.noneLinkStatements++;
        }
    }

    @Override
    public void endRDF() throws RDFHandlerException{
        super.endRDF();
        if (this.processingFirstStatement){
            throw new RDFHandlerException("No Valid Statements found with predicate: " + linkPredicate);
        }
    }

 }

@@ -1661,24 +1661,73 @@
 tification;%0A
+    private final String backwardJustification;%0A%0A
     protecte
@@ -2111,16 +2111,111 @@
 Source;%0A
+        backwardJustification = OpsJustificationMaker.getInstance().getInverse(justification);%0A
     %7D%0A  
@@ -4475,110 +4475,430 @@
     
-String backwardJustification = OpsJustificationMaker.getInstance().getInverse(justification);%0A        
+registerMappingSet(sourcePattern, targetPattern);%0A            %7D catch (BridgeDBException ex) %7B%0A                Reporter.error(%22Error handling: %22 + st, ex);%0A            %7D%0A            super.handleStatement(st);%0A        %7D else %7B%0A            this.noneLinkStatements++;%0A        %7D%0A    %7D%0A%0A    protected void registerMappingSet(RegexUriPattern sourcePattern, RegexUriPattern targetPattern ) %0A            throws BridgeDBException%7B%0A
     
@@ -4945,32 +4945,24 @@
             
-    
-    
 mappingSet =
@@ -5054,32 +5054,24 @@
             
-       
- 
 justificatio
@@ -5119,32 +5119,24 @@
             
-        
 this.setSyme
@@ -5156,24 +5156,16 @@
         
- 
-       
 %7D else %7B
@@ -5161,32 +5161,24 @@
    %7D else %7B%0A
-        
             
@@ -5282,32 +5282,24 @@
             
-        
 justificatio
@@ -5359,241 +5359,8 @@
 e);%0A
-                %7D%0A            %7D catch (BridgeDBException ex) %7B%0A                Reporter.error(%22Error handling: %22 + st, ex);%0A            %7D%0A            super.handleStatement(st);%0A        %7D else %7B%0A            this.noneLinkStatements++;%0A
     

Split out registerMappingSet so it can be over written by IMS