/**
 * 
 */
package com.github.podd.impl.purl;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import org.openrdf.model.URI;

import com.github.podd.api.PoddProcessorStage;
import com.github.podd.api.purl.PoddPurlProcessor;
import com.github.podd.api.purl.PoddPurlProcessorFactory;

/**
 * A Simple Processor Factory that creates <code>SimpleUUIDPurlProcessor</code> instances.
 * 
 * 
 * @author kutila
 * 
 */
public class SimpleUUIDPurlProcessorFactory implements PoddPurlProcessorFactory
{
    private String prefix;
    
    private String[] temporaryUriArray;
    
    /* The fixed set of stages supported by this Factory */
    private static final Set<PoddProcessorStage> stages = new HashSet<PoddProcessorStage>(
            Arrays.asList(new PoddProcessorStage[] { PoddProcessorStage.RDF_PARSING }));
    
    @Override
    public String getSPARQLConstructBGP()
    {
        return "?subject ?predicate ?object";
    }
    
    @Override
    public String getSPARQLConstructWhere()
    {
        final StringBuilder builder = new StringBuilder();

        // match all triples
        builder.append(" ?subject ?predicate ?object ");
        
        // get a focused SPARQL by checking for the temporary URI patterns
        if(this.temporaryUriArray != null && this.temporaryUriArray.length > 0)
        {
            builder.append("FILTER ( ");
            int startLength = builder.length();
            
            for (String tempUri : temporaryUriArray)
            {
                if (builder.length() > startLength)
                {
                    builder.append(" || ");
                }
                builder.append(" regex(?subject, ");
                builder.append("\"^");
                builder.append(tempUri);
                builder.append("\")"); 
                builder.append(" || ");
                builder.append(" regex(?predicate, ");
                builder.append("\"^");
                builder.append(tempUri);
                builder.append("\")"); 
            }
            builder.append(" ) ");
            
        }
        return builder.toString();
    }
    
    @Override
    public String getSPARQLConstructWhere(final URI subject)
    {
        // TODO
        if(subject == null)
        {
            return "?s ?p ?o";
        }
        final StringBuilder builder = new StringBuilder();
        builder.append("<");
        builder.append(subject.stringValue());
        builder.append("> ?p ?o");
        return builder.toString();
    }
    
    @Override
    public String getSPARQLGroupBy()
    {
        // an empty GROUP BY clause
        return "";
    }
    
    @Override
    public boolean canHandleStage(final PoddProcessorStage stage)
    {
        if(stage == null)
        {
            throw new NullPointerException("Cannot handle NULL stage");
        }
        return SimpleUUIDPurlProcessorFactory.stages.contains(stage);
    }
    
    @Override
    public String getKey()
    {
        return this.getClass().getName();
    }
    
    @Override
    public PoddPurlProcessor getProcessor()
    {
        SimpleUUIDPurlProcessor processor = new SimpleUUIDPurlProcessor();
        if(this.prefix != null)
        {
            processor = new SimpleUUIDPurlProcessor(this.prefix);
        }
        
        if(this.temporaryUriArray == null || this.temporaryUriArray.length == 0)
        {
            // NOTE: Could throw a custom exception, possibly extending a PoddRuntimeException ?
            throw new RuntimeException("Not enough data to create SimplePoddPurlProcessor (temporary URIs)");
        }
        for(final String tempUri : this.temporaryUriArray)
        {
            processor.addTemporaryUriHandler(tempUri);
        }
        
        return processor;
    }
    
    @Override
    public Set<PoddProcessorStage> getStages()
    {
        return SimpleUUIDPurlProcessorFactory.stages;
    }
    
    /**
     * 
     * @param prefix
     *            The Prefix used by the PurlProcessor's created by this factory
     */
    public void setPrefix(final String prefix)
    {
        this.prefix = prefix;
    }
    
    /**
     * 
     * @param temporaryUri
     *            The temporary URIs that the PurlProcessor created by this factory can handle.
     */
    public void setTemporaryUriArray(final String[] temporaryUri)
    {
        this.temporaryUriArray = temporaryUri;
    }
    
}

@@ -144,16 +144,73 @@
 del.URI;
+%0Aimport org.slf4j.Logger;%0Aimport org.slf4j.LoggerFactory;
 %0A%0Aimport
@@ -571,16 +571,96 @@
 ctory%0A%7B%0A
+    protected final Logger log = LoggerFactory.getLogger(this.getClass());%0A    %0A
     priv
@@ -1215,16 +1215,24 @@
 lder();%0A
+        
 %0A       
@@ -1537,16 +1537,22 @@
         
+final 
 int star
@@ -1607,18 +1607,23 @@
      for
- 
 (
+final 
 String t
@@ -1631,16 +1631,21 @@
 mpUri : 
+this.
 temporar
@@ -1687,17 +1687,16 @@
       if
- 
 (builder
@@ -1833,21 +1833,29 @@
 ppend(%22 
-regex
+strstarts(STR
 (?subjec
@@ -1855,16 +1855,17 @@
 ?subject
+)
 , %22);%0A  
@@ -1888,33 +1888,32 @@
 ilder.append(%22%5C%22
-%5E
 %22);%0A            
@@ -1971,33 +1971,32 @@
 r.append(%22%5C%22)%22);
- 
 %0A               
@@ -2057,24 +2057,30 @@
 d(%22 
-regex(?predicate
+strstarts(STR(?object)
 , %22)
@@ -2119,9 +2119,8 @@
 (%22%5C%22
-%5E
 %22);%0A
@@ -2198,17 +2198,16 @@
 (%22%5C%22)%22);
- 
 %0A       

fix generated SPARQL