package com.github.podd.impl.file;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.openrdf.model.Model;
import org.openrdf.model.Resource;
import org.openrdf.model.URI;
import org.openrdf.model.vocabulary.RDF;
import org.openrdf.model.vocabulary.RDFS;
import org.semanticweb.owlapi.model.IRI;

import com.github.podd.api.file.SSHFileReference;
import com.github.podd.api.file.SSHFileReferenceProcessor;
import com.github.podd.utils.PoddRdfConstants;

/**
 * Processor for File References of type <i>http://purl.org/podd/ns/poddBase#SSHFileReference</i>.
 * 
 * @author kutila
 */
public class SSHFileReferenceProcessorImpl implements SSHFileReferenceProcessor
{
    
    @Override
    public boolean canHandle(final Model rdfStatements)
    {
        if(rdfStatements == null || rdfStatements.isEmpty())
        {
            return false;
        }
        
        for(final URI fileType : this.getTypes())
        {
            final Model matchingModels = rdfStatements.filter((Resource)null, null, fileType);
            if(!matchingModels.isEmpty())
            {
                return true;
            }
        }
        
        return false;
    }
    
    @Override
    public Collection<SSHFileReference> createReferences(final Model rdfStatements)
    {
        if(rdfStatements == null || rdfStatements.isEmpty())
        {
            return null;
        }
        
        final Set<SSHFileReference> results = new HashSet<SSHFileReference>();
        
        for(final URI fileType : this.getTypes())
        {
            final Set<Resource> fileRefUris = rdfStatements.filter(null, RDF.TYPE, fileType).subjects();
            
            for(final Resource fileRef : fileRefUris)
            {
                final Model model = rdfStatements.filter(fileRef, null, null);
                
                final SSHFileReference fileReference = new SSHFileReferenceImpl();
                
                // note: artifact ID is not available to us in here and must be added externally
                
                if(fileRef instanceof URI)
                {
                    fileReference.setObjectIri(IRI.create((URI)fileRef));
                }
                
                final String label = model.filter(fileRef, RDFS.LABEL, null).objectString();
                if(label != null)
                {
                    fileReference.setLabel(label);
                }
                
                final String filename =
                        model.filter(fileRef, PoddRdfConstants.PODD_BASE_HAS_FILENAME, null).objectString();
                if(filename != null)
                {
                    fileReference.setFilename(filename);
                }
                
                final String path =
                        model.filter(fileRef, PoddRdfConstants.PODD_BASE_HAS_FILE_PATH, null).objectString();
                if(path != null)
                {
                    fileReference.setPath(path);
                }
                
                final String alias = model.filter(fileRef, PoddRdfConstants.PODD_BASE_HAS_ALIAS, null).objectString();
                if(alias != null)
                {
                    fileReference.setRepositoryAlias(alias);
                }
                
                Model linksToFileReference = rdfStatements.filter(null, null, fileRef);
                
                // TODO: Need to use a SPARQL query to verify that the property is a sub-property of
                // PODD Contains
                if(!linksToFileReference.isEmpty())
                {
                    for(Resource nextResource : linksToFileReference.subjects())
                    {
                        if(nextResource instanceof URI)
                        {
                            fileReference.setParentIri(IRI.create((URI)nextResource));
                            break;
                        }
                    }
                }
                
                results.add(fileReference);
            }
        }
        return results;
    }
    
    @Override
    public Set<URI> getTypes()
    {
        return Collections.singleton(PoddRdfConstants.PODD_BASE_FILE_REFERENCE_TYPE_SSH);
    }
    
}

@@ -234,16 +234,48 @@
 el.URI;%0A
+import org.openrdf.model.Value;%0A
 import o
@@ -500,16 +500,57 @@
 cessor;%0A
+import com.github.podd.utils.DebugUtils;%0A
 import c
@@ -1944,32 +1944,98 @@
                %0A
+                DebugUtils.printContents(model);%0A                %0A
                 
@@ -2425,21 +2425,25 @@
  final S
-tring
+et%3CValue%3E
  label =
@@ -2481,38 +2481,33 @@
 EL, null).object
-String
+s
 ();%0A            
@@ -2517,21 +2517,24 @@
  if(
+!
 label
- != null
+.isEmpty()
 )%0A  
@@ -2597,16 +2597,48 @@
 el(label
+.iterator().next().stringValue()
 );%0A     
@@ -2690,21 +2690,25 @@
  final S
-tring
+et%3CValue%3E
  filenam
@@ -2802,38 +2802,33 @@
 ME, null).object
-String
+s
 ();%0A            
@@ -2834,24 +2834,25 @@
      if(
+!
 filename
  != null
@@ -2843,24 +2843,26 @@
 filename
- != null
+.isEmpty()
 )%0A      
@@ -2927,16 +2927,48 @@
 filename
+.iterator().next().stringValue()
 );%0A     
@@ -3024,44 +3024,24 @@
 al S
-tring path =%0A                       
+et%3CValue%3E path =
  mod
@@ -3105,38 +3105,33 @@
 TH, null).object
-String
+s
 ();%0A            
@@ -3141,20 +3141,23 @@
  if(
+!
 path
- != null
+.isEmpty()
 )%0A  
@@ -3218,16 +3218,48 @@
 ath(path
+.iterator().next().stringValue()
 );%0A     
@@ -3311,21 +3311,25 @@
  final S
-tring
+et%3CValue%3E
  alias =
@@ -3405,14 +3405,9 @@
 ject
-String
+s
 ();%0A
@@ -3429,21 +3429,24 @@
  if(
+!
 alias
- != null
+.isEmpty()
 )%0A  
@@ -3519,16 +3519,48 @@
 as(alias
+.iterator().next().stringValue()
 );%0A     

Fix EditArtifactResourceImplTest.testEditArtifactBasicRdf