/*
 * Copyright (C) 2009 eXo Platform SAS.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.exoplatform.services.jcr.ext.registry;

import org.exoplatform.services.jcr.ext.app.SessionProviderService;
import org.exoplatform.services.jcr.ext.common.SessionProvider;
import org.exoplatform.services.jcr.ext.registry.Registry.RegistryNode;
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.exoplatform.services.rest.ext.util.XlinkHref;
import org.exoplatform.services.rest.resource.ResourceContainer;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.dom.DOMSource;

/**
 * RESTRegistryService is RESTfull service intended to manage registry nodes.
 *
 * @author <a href="mailto:andrew00x@gmail.com">Andrey Parfonov</a>
 */
@Path("/registry/")
public class RESTRegistryService implements ResourceContainer
{

   /**
    * Logger.
    */
   private static final Log LOG = ExoLogger.getLogger("exo.jcr.component.ext.RESTRegistryService");

   /**
    * 
    */
   private static final String REGISTRY = "registry";

   private static final String EXO_REGISTRY = "exo:registry/";

   /**
    * See {@link RegistryService}.
    */
   private RegistryService regService;

   /**
    * See {@link SessionProviderService}.
    */
   private SessionProviderService sessionProviderService;

   public RESTRegistryService(RegistryService regService, SessionProviderService sessionProviderService)
      throws Exception
   {
      this.regService = regService;
      this.sessionProviderService = sessionProviderService;
   }

   /**
    * Returns the registry node which wraps a node of type "exo:registry" (the whole registry tree)
    *
    * @response
    * {code}
    * "entryStream" : the output stream corresponding registry node which wraps a node of type "exo:registry" (the whole registry tree)
    * {code}
    * Example :
    * {@code
    * <registry xlinks:href="http://localhost:8080/portal/rest/registry/">
    *    <GroovyScript2RestLoader xlinks:href="http://localhost:8080/portal/rest/registry/exo:services/GroovyScript2RestLoader"/>
    *    <Audit xlinks:href="http://localhost:8080/portal/rest/registry/exo:services/Audit"/>
    * </registry>
    * }
    *
    * @LevelAPI Experimental
    */
   @GET
   @Produces(MediaType.APPLICATION_XML)
   public Response getRegistry(@Context UriInfo uriInfo)
   {
      SessionProvider sessionProvider = sessionProviderService.getSessionProvider(null);
      try
      {
         RegistryNode registryEntry = regService.getRegistry(sessionProvider);
         if (registryEntry != null)
         {
            Node registryNode = registryEntry.getNode();
            NodeIterator registryIterator = registryNode.getNodes();
            Document entry = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();

            String fullURI = uriInfo.getRequestUri().toString();
            XlinkHref xlinkHref = new XlinkHref(fullURI);
            Element root = entry.createElement(REGISTRY);
            xlinkHref.putToElement(root);
            while (registryIterator.hasNext())
            {
               NodeIterator entryIterator = registryIterator.nextNode().getNodes();
               while (entryIterator.hasNext())
               {
                  Node node = entryIterator.nextNode();
                  Element xmlNode = entry.createElement(node.getName());
                  xlinkHref.putToElement(xmlNode, node.getPath().substring(EXO_REGISTRY.length()));
                  root.appendChild(xmlNode);
               }
            }
            entry.appendChild(root);
            return Response.ok(new DOMSource(entry), "text/xml").build();
         }
         return Response.status(Response.Status.NOT_FOUND).build();
      }
      catch (Exception e)
      {
         LOG.error("Get registry failed", e);
         throw new WebApplicationException(e);
      }
   }

   /**
    * Returns the corresponding registry entry which wraps a node of type "exo:registryEntry"
    * @param entryPath The relative path to the registry entry
    *
    *  @response
    *  {code}
    * "entryStream" :  the output stream corresponding registry entry which wraps a node of type "exo:registryEntry
    *  {code}
    *  Example :
    * {@code
    *  <Audit jcr:primaryType="exo:registryEntry">
    *     <adminIdentity jcr:primaryType="nt:unstructured" value="*:/Platform/Administrators"/>
    *  </Audit>
    * }
    * @LevelAPI Experimental
    */
   @GET
   @Path("/{entryPath:.+}")
   @Produces(MediaType.APPLICATION_XML)
   public Response getEntry(@PathParam("entryPath") String entryPath)
   {

      SessionProvider sessionProvider = sessionProviderService.getSessionProvider(null);
      try
      {
         RegistryEntry entry;
         entry = regService.getEntry(sessionProvider, normalizePath(entryPath));
         return Response.ok(new DOMSource(entry.getDocument())).build();
      }
      catch (PathNotFoundException e)
      {
         return Response.status(Response.Status.NOT_FOUND).build();
      }
      catch (RepositoryException e)
      {
         LOG.error("Get registry entry failed", e);
         throw new WebApplicationException(e);
      }
   }

   /**
    * Creates an entry in the group. In a case the group does not exist already it will be automatically
    * created.
    * @param entryStream the input stream corresponding to the content of the registry entry
    * @param groupName the relative path to the group
    * @request
    * {code}
    * "entryStream" : the input stream corresponding to the content of the registry entry
    * {code}
    * Example :
    * {@code
    * <registry xlinks:href="http://localhost:8080/portal/rest/registry/">
    *    <GroovyScript2RestLoader xlinks:href="http://localhost:8080/portal/rest/registry/exo:services/GroovyScript2RestLoader"/>
    *    <Audit xlinks:href="http://localhost:8080/portal/rest/registry/exo:services/Audit"/>
    * </registry>
    * }
    *
    * @LevelAPI Experimental
    */
   @POST
   @Path("/{groupName:.+}")
   @Consumes(MediaType.APPLICATION_XML)
   public Response createEntry(InputStream entryStream, @PathParam("groupName") String groupName, @Context UriInfo uriInfo)
   {

      SessionProvider sessionProvider = sessionProviderService.getSessionProvider(null);
      try
      {
         RegistryEntry entry = RegistryEntry.parse(entryStream);
         regService.createEntry(sessionProvider, normalizePath(groupName), entry);
         URI location = uriInfo.getRequestUriBuilder().path(entry.getName()).build();
         return Response.created(location).build();
      }
      catch (IllegalArgumentException e)
      {
         LOG.error("Create registry entry failed", e);
         throw new WebApplicationException(e);
      }
      catch (IOException e)
      {
         LOG.error("Create registry entry failed", e);
         throw new WebApplicationException(e);
      }
      catch (SAXException e)
      {
         LOG.error("Create registry entry failed", e);
         throw new WebApplicationException(e);
      }
      catch (ParserConfigurationException e)
      {
         LOG.error("Create registry entry failed", e);
         throw new WebApplicationException(e);
      }
      catch (RepositoryException e)
      {
         LOG.error("Create registry entry failed", e);
         throw new WebApplicationException(e);
      }
   }

   /**
    * Recreates a registry entry
    * 
    * @param entryStream the input stream corresponding to the content of the registry entry
    * @param groupName the relative path to the group
    * @param createIfNotExist if set to true, it will try to create the registry if it doesn't exist
    * yet
    * @request
    * {code}
    * "entryStream" : the input stream corresponding to the content of the registry entry
    * {code}
    * Example :
    * {@code
    *  <Audit jcr:primaryType="exo:registryEntry">
    *     <adminIdentity jcr:primaryType="nt:unstructured" value="*:/Platform/Administrators"/>
    *  </Audit>
    * }
    * @LevelAPI Experimental
    */
   @PUT
   @Path("/{groupName:.+}")
   @Consumes(MediaType.APPLICATION_XML)
   public Response recreateEntry(InputStream entryStream, @PathParam("groupName") String groupName,
            @Context UriInfo uriInfo, @QueryParam("createIfNotExist") boolean createIfNotExist)
   {

      SessionProvider sessionProvider = sessionProviderService.getSessionProvider(null);
      try
      {
         RegistryEntry entry = RegistryEntry.parse(entryStream);
         if (createIfNotExist)
         {
            regService.updateEntry(sessionProvider, normalizePath(groupName), entry);
         }
         else
         {
            regService.recreateEntry(sessionProvider, normalizePath(groupName), entry);
         }
         URI location = uriInfo.getRequestUriBuilder().path(entry.getName()).build();
         return Response.created(location).build();
      }
      catch (IllegalArgumentException e)
      {
         LOG.error("Re-create registry entry failed", e);
         throw new WebApplicationException(e);
      }
      catch (IOException e)
      {
         LOG.error("Re-create registry entry failed", e);
         throw new WebApplicationException(e);
      }
      catch (SAXException e)
      {
         LOG.error("Re-create registry entry failed", e);
         throw new WebApplicationException(e);
      }
      catch (ParserConfigurationException e)
      {
         LOG.error("Re-create registry entry failed", e);
         throw new WebApplicationException(e);
      }
      catch (RepositoryException e)
      {
         LOG.error("Re-create registry entry failed", e);
         throw new WebApplicationException(e);
      }
   }

   /**
    * Removes the entry at the given absolute path (concatenation of group path / entry name)
    * @param entryPath the absolute path of the entry to remove
    * @LevelAPI Experimental
    */
   @DELETE
   @Path("/{entryPath:.+}")
   public Response removeEntry(@PathParam("entryPath") String entryPath)
   {

      SessionProvider sessionProvider = sessionProviderService.getSessionProvider(null);
      try
      {
         regService.removeEntry(sessionProvider, normalizePath(entryPath));
         return null; // minds status 204 'No content'
      }
      catch (PathNotFoundException e)
      {
         return Response.status(Response.Status.NOT_FOUND).build();
      }
      catch (Exception e)
      {
         LOG.error("Remove registry entry failed", e);
         throw new WebApplicationException(e);
      }
   }

   private static String normalizePath(String path)
   {
      if (path.endsWith("/"))
         return path.substring(0, path.length() - 1);
      return path;
   }

}
