/*
 * Copyright 2013 Esri.
 *
 * 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 com.esri.gpt.control.webharvest.client.dcat;

import com.esri.gpt.catalog.harvest.protocols.HarvestProtocolDCAT;
import com.esri.gpt.control.webharvest.IterationContext;
import com.esri.gpt.control.webharvest.common.CommonCapabilities;
import com.esri.gpt.framework.resource.api.Native;
import com.esri.gpt.framework.resource.query.Capabilities;
import com.esri.gpt.framework.resource.query.Criteria;
import com.esri.gpt.framework.resource.query.Query;
import com.esri.gpt.framework.resource.query.QueryBuilder;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * DCAT query builder.
 */
public class DCATQueryBuilder implements QueryBuilder {

  /** logger */
  private static final Logger LOGGER = Logger.getLogger(DCATQuery.class.getCanonicalName());
  /** capabilities */
  private static final Capabilities capabilities = new DCATCommonCapabilities();
  /** iteration context */
  private IterationContext context;
  /** service info */
  private DCATInfo info;

  /**
   * Creates instance of the builder.
   * @param context iteration context
   * @param protocol protocol
   * @param url URL
   */
  public DCATQueryBuilder(IterationContext context, HarvestProtocolDCAT protocol, String url) {
    if (context == null) {
      throw new IllegalArgumentException("No context provided.");
    }
    this.context = context;
    this.info = new DCATInfo(url,protocol.getFormat());
  }

  @Override
  public Capabilities getCapabilities() {
    return capabilities;
  }

  @Override
  public Query newQuery(Criteria crt) {
    Query q = new DCATQuery(context, info, crt);
    LOGGER.log(Level.FINER, "Query created: {0}", q);
    return q;
  }

  @Override
  public Native getNativeResource() {
    return null;
  }

  /**
   * DCAT capabilities.
   */
  private static class DCATCommonCapabilities extends CommonCapabilities {

    @Override
    public boolean canQueryFromDate() {
      return true;
    }

    @Override
    public boolean canQueryToDate() {
      return true;
    }
  }
  
}

@@ -861,27 +861,357 @@
 ork.
-resource.api.Native
+dcat.DcatParser;%0Aimport com.esri.gpt.framework.dcat.DcatParserAdaptor;%0Aimport com.esri.gpt.framework.resource.api.Native;%0Aimport com.esri.gpt.framework.resource.api.Publishable;%0Aimport com.esri.gpt.framework.resource.api.SourceUri;%0Aimport com.esri.gpt.framework.resource.common.CommonPublishable;%0Aimport com.esri.gpt.framework.resource.common.UrlUri
 ;%0Aim
@@ -1429,24 +1429,100 @@
 eryBuilder;%0A
+import java.io.IOException;%0Aimport java.net.URL;%0Aimport java.util.Iterator;%0A
 import java.
@@ -1573,16 +1573,65 @@
 .Logger;
+%0Aimport javax.xml.transform.TransformerException;
 %0A%0A/**%0A *
@@ -2773,25 +2773,1011 @@
     
-return null;%0A  %7D%0A
+DCATIteratorAdaptor adaptor = null;%0A    try %7B%0A      URL url = new URL(info.getUrl());%0A      adaptor = new DCATIteratorAdaptor(info.getFormat(), new DcatParserAdaptor(new DcatParser(url.openStream())));%0A      Iterator%3CPublishable%3E iterator = adaptor.iterator();%0A      if (iterator.hasNext()) %7B%0A        return new NativeImpl(iterator.next().getContent());%0A      %7D%0A    %7D catch (Exception ex) %7B%0A      LOGGER.log(Level.WARNING, %22Error reading native resource.%22, ex);%0A    %7D finally %7B%0A      if (adaptor!=null) %7B%0A        adaptor.close();%0A      %7D%0A    %7D%0A    return null;%0A  %7D%0A%0A%0A/**%0A * Native implementation.%0A */%0A  private class NativeImpl extends CommonPublishable implements Native %7B%0A      private String content;%0A%0A      public NativeImpl(String content) %7B%0A        this.content = content;%0A      %7D%0A%0A      @Override%0A      public SourceUri getSourceUri() %7B%0A        return new UrlUri(info.getUrl());%0A      %7D%0A%0A      @Override%0A      public String getContent() throws IOException %7B%0A        return content;%0A      %7D%0A  %7D%0A  
 %0A  /

Support for Native resource in DCAT harvester.