package com.terradue.warhol;

/*
 *    Copyright 2011-2012 Terradue srl
 *
 *    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.
 */

import static com.terradue.warhol.lang.Preconditions.checkNotNullArgument;

import org.w3._2005.atom.Feed;

import com.a9.opensearch._11.OpenSearchDescription;
import com.a9.opensearch._11.OpenSearchUrl;
import com.sun.jersey.api.client.Client;
import com.terradue.warhol.settings.Catalogue;
import com.terradue.warhol.traverse.CatalogueTraverseHandler;
import com.terradue.warhol.traverse.CatalogueTraverseHandlerBuilder;

public final class CatalogueConnector
{

    private static final String ATOM_XML = "application/atom+xml";

    private final Catalogue catalogue;

    private final Client restClient;

    public CatalogueConnector( Catalogue catalogue )
    {
        this.catalogue = checkNotNullArgument( catalogue, "Impossible to initialize a CatalogueConnector froma  null Catalogue" );
        restClient = null;
    }

    public OpenSearchDescription getDescription()
    {
        return restClient.resource( catalogue.getDescriptionUrl() ).get( OpenSearchDescription.class );
    }

    public CatalogueTraverseHandlerBuilder traverse()
    {
        return new CatalogueTraverseHandlerBuilder()
        {

            @Override
            public void with( CatalogueTraverseHandler traverseHandler )
            {
                traverseHandler = checkNotNullArgument( traverseHandler, "Impossible to traverse the Catalogue <%s> with a null handler" );

                OpenSearchDescription description = getDescription();

                for ( OpenSearchUrl url : description.getUrl() )
                {
                    if ( ATOM_XML.equals( url.getType() ) )
                    {
                        String urlTemplate = url.getTemplate();
                        // TODO handle it
                    }
                }
            }

        };
    }

    Feed parse( String uri )
    {
        return restClient.resource( uri ).accept( ATOM_XML ).get( Feed.class );
    }

}

@@ -692,47 +692,186 @@
 hol.
-lang.Preconditions.checkNotNullArgument
+urltemplate.UrlTemplate.parseTemplate;%0A%0Aimport static com.terradue.warhol.lang.Preconditions.checkNotNullArgument;%0Aimport static com.terradue.warhol.lang.Preconditions.checkState
 ;%0A%0Ai
@@ -1215,16 +1215,68 @@
 Builder;
+%0Aimport com.terradue.warhol.urltemplate.UrlTemplate;
 %0A%0Apublic
@@ -1456,16 +1456,125 @@
 lient;%0A%0A
+    private final OpenSearchDescription openSearchDescription;%0A%0A    private UrlTemplate catalogueSearchUrl;%0A%0A
     publ
@@ -1786,27 +1786,18 @@
 ll;%0A
+%0A
     
-%7D%0A%0A
     
-public O
+o
 penS
@@ -1817,45 +1817,9 @@
 ion 
-getDescription()%0A    %7B%0A        return
+=
  res
@@ -1908,73 +1908,92 @@
  );%0A
+%0A
     
-%7D%0A%0A
     
-public CatalogueTraverseHandlerBuilder traverse()%0A
+dance : for ( OpenSearchUrl url : openSearchDescription.getUrl() )%0A   
     
+ 
 %7B%0A  
@@ -2002,158 +2002,138 @@
     
+  
-return new CatalogueTraverseHandlerBuilder()%0A  
+  if ( ATOM_XML.equals( url.getType() ) )%0A
       
-%7B%0A%0A
       
+%7B%0A
       
-@Override%0A            public void with( CatalogueTraverseHandler traverseHandler
+          catalogueSearchUrl = parseTemplate( url.getTemplate()
  )
+;
 %0A   
@@ -2133,33 +2133,48 @@
  );%0A            
-%7B
+    break dance;
 %0A               
@@ -2174,152 +2174,162 @@
     
+%7D%0A
     
-traverseHandler = checkNotNullArgument( traverseHandler, %22Impossible to traverse the Catalogue %3C%25s%3E with a null handler%22
+    %7D%0A%0A        checkState( catalogueSearchUrl != null, %22Catalogue description does not contain a valid %25s search URL%22, ATOM_XML
  );%0A
-%0A
     
-  
+%7D%0A%0A
     
-     
+public
  Ope
@@ -2350,22 +2350,8 @@
 tion
- description =
  get
@@ -2367,23 +2367,23 @@
 on()
-;%0A
 %0A    
+%7B%0A
         
     
@@ -2382,137 +2382,156 @@
     
-    for ( OpenSearchUrl url : description.getUrl() )%0A 
+return openSearchDescription;%0A
     
+%7D%0A%0A
     
-       %7B%0A                    if ( ATOM_XML.equals( url.getType() ) 
+public CatalogueTraverseHandlerBuilder traverse()%0A    %7B%0A        return new CatalogueTraverseHandlerBuilder(
 )%0A  
@@ -2528,32 +2528,35 @@
 ilder()%0A        
+%7B%0A%0A
             %7B%0A  
@@ -2543,33 +2543,41 @@
  %7B%0A%0A            
-%7B
+@Override
 %0A               
@@ -2577,59 +2577,68 @@
     
-            String urlTemplate = url.getTemplate();
+public void with( CatalogueTraverseHandler traverseHandler )
 %0A   
@@ -2638,32 +2638,34 @@
 r )%0A            
+%7B%0A
             // T
@@ -2664,65 +2664,135 @@
     
-// TODO handle it%0A                    %7D%0A                %7D
+    traverseHandler = checkNotNullArgument( traverseHandler, %22Impossible to traverse the Catalogue %3C%25s%3E with a null handler%22 );
 %0A   

handle OpenSearch url template with UrlTemplate