/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nl.uva.cs.lobcder.rest;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import javax.annotation.Nonnull;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import lombok.extern.java.Log;
import nl.uva.cs.lobcder.auth.MyPrincipal;
import nl.uva.cs.lobcder.resources.StorageSite;
import nl.uva.cs.lobcder.util.CatalogueHelper;
import nl.uva.vlet.exception.VlException;

/**
 *
 * @author S. Koulouzis
 */
@Log
@Path("storage_sites/")
public class StorageSites extends CatalogueHelper {

    @Context
    UriInfo info;
    @Context
    HttpServletRequest request;
    @Context
    HttpServletResponse servletResponse;

    public StorageSites() throws NamingException {
    }

    @Path("query/")
    @GET
    @Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
    public List<StorageSiteWrapper> getXml() throws FileNotFoundException, VlException, URISyntaxException, IOException, MalformedURLException, Exception {
        MyPrincipal mp = (MyPrincipal) request.getAttribute("myprincipal");
        if (mp.isAdmin()) {
            try (Connection cn = getCatalogue().getConnection()) {
                List<StorageSiteWrapper> res = queryStorageSites(mp, cn);
                return res;
            } catch (SQLException ex) {
                log.log(Level.SEVERE, null, ex);
                throw new WebApplicationException(Response.Status.INTERNAL_SERVER_ERROR);
            }
        }
        return null;

    }

    private List<StorageSiteWrapper> queryStorageSites(@Nonnull MyPrincipal mp, @Nonnull Connection cn) throws SQLException {
        MultivaluedMap<String, String> queryParameters = info.getQueryParameters();
        List<String> ids = queryParameters.get("id");
        if (ids !=null && ids.size() >0 && ids.get(0).equals("all")) {
            Collection<StorageSite> sites = getCatalogue().getStorageSites(cn);
            Collection<StorageSite> cachesites = getCatalogue().getCacheStorageSites(cn);
            List<StorageSiteWrapper> sitesWarpper = new ArrayList<>();
            for (StorageSite s : sites) {
                StorageSiteWrapper sw = new StorageSiteWrapper();
                CredentialWrapped cw = new CredentialWrapped();
                cw.setStorageSitePassword(s.getCredential().getStorageSitePassword());
                cw.setStorageSiteUsername(s.getCredential().getStorageSiteUsername());
                sw.setCredential(cw);
                sw.setCurrentNum(s.getCurrentNum());
                sw.setEncrypt(s.isEncrypt());
                sw.setQuotaNum(s.getQuotaNum());
                sw.setQuotaSize(s.getQuotaSize());
                sw.setResourceURI(s.getResourceURI());
                sw.setStorageSiteId(s.getStorageSiteId());
                sitesWarpper.add(sw);
            }
            for (StorageSite s : cachesites) {
                StorageSiteWrapper sw = new StorageSiteWrapper();
                CredentialWrapped cw = new CredentialWrapped();
                cw.setStorageSitePassword(s.getCredential().getStorageSitePassword());
                cw.setStorageSiteUsername(s.getCredential().getStorageSiteUsername());
                sw.setCredential(cw);
                sw.setCurrentNum(s.getCurrentNum());
                sw.setEncrypt(s.isEncrypt());
                sw.setQuotaNum(s.getQuotaNum());
                sw.setQuotaSize(s.getQuotaSize());
                sw.setResourceURI(s.getResourceURI());
                sw.setStorageSiteId(s.getStorageSiteId());
                sitesWarpper.add(sw);
            }
            return sitesWarpper;
        }
        return null;
    }
}

@@ -619,18 +619,71 @@
 x.ws.rs.
-GE
+Consumes;%0Aimport javax.ws.rs.GET;%0Aimport javax.ws.rs.PU
 T;%0Aimpor
@@ -727,24 +727,55 @@
 s.Produces;%0A
+import javax.ws.rs.QueryParam;%0A
 import javax
@@ -981,16 +981,51 @@
 riInfo;%0A
+import javax.xml.bind.JAXBElement;%0A
 import l
@@ -1090,16 +1090,59 @@
 ncipal;%0A
+import nl.uva.cs.lobcder.auth.Permissions;%0A
 import n
@@ -2329,16 +2329,497 @@
 rn null;
+%0A    %7D%0A%0A    @PUT%0A    @Consumes(%7BMediaType.APPLICATION_XML, MediaType.APPLICATION_JSON%7D)%0A    public void setStorageSites(@QueryParam(%22site%22) String path, JAXBElement%3CStorageSiteWrapper%3E jbSites) throws SQLException %7B%0A        MyPrincipal mp = (MyPrincipal) request.getAttribute(%22myprincipal%22);%0A        if (mp.isAdmin()) %7B%0A            try (Connection connection = getCatalogue().getConnection()) %7B%0A                StorageSiteWrapper sites = jbSites.getValue();%0A            %7D%0A        %7D
 %0A%0A    %7D%0A
@@ -3101,16 +3101,17 @@
  (ids !=
+ 
 null && 
@@ -3122,16 +3122,17 @@
 size() %3E
+ 
 0 && ids

set Storage Sites