/*
   * (non-Javadoc)
   *
   * @see
   * it.geosolutions.georepo.gui.server.service.IWorkspacesManagerService#getWorkspaces(com.extjs
   * .gxt.ui.client.data.PagingLoadConfig, java.lang.String)
   */
  public PagingLoadResult<Workspace> getWorkspaces(
      int offset, int limit, String remoteURL, GSInstance gsInstance) throws ApplicationException {

    List<Workspace> workspacesListDTO = new ArrayList<Workspace>();
    workspacesListDTO.add(new Workspace("*"));

    if ((remoteURL != null) && !remoteURL.equals("*") && !remoteURL.contains("?")) {
      try {
        GeoServerRESTReader gsreader =
            new GeoServerRESTReader(remoteURL, gsInstance.getUsername(), gsInstance.getPassword());

        RESTWorkspaceList workspaces = gsreader.getWorkspaces();
        if ((workspaces != null) && !workspaces.isEmpty()) {
          Iterator<RESTShortWorkspace> wkIT = workspaces.iterator();
          while (wkIT.hasNext()) {
            RESTShortWorkspace workspace = wkIT.next();

            workspacesListDTO.add(new Workspace(workspace.getName()));
          }
        }
      } catch (MalformedURLException e) {
        logger.error(e.getLocalizedMessage(), e);
        throw new ApplicationException(e.getLocalizedMessage(), e);
      }
    }

    return new RpcPageLoadResult<Workspace>(workspacesListDTO, 0, workspacesListDTO.size());
  }
  /*
   * (non-Javadoc)
   *
   * @see
   * it.geosolutions.georepo.gui.server.service.IWorkspacesManagerService#getStyles(it.geosolutions
   * .georepo.gui.client.model.GSInstance)
   */
  public List<LayerStyle> getStyles(Rule rule) throws ApplicationException {

    List<LayerStyle> layerStyles = new ArrayList<LayerStyle>();

    Set<String> allowedStyles = null;
    int size = 0;

    try {
      LayerDetails layerDetails =
          georepoRemoteService.getRuleAdminService().get(rule.getId()).getLayerDetails();

      if (layerDetails != null) {
        allowedStyles = layerDetails.getAllowedStyles();

        if (allowedStyles != null) {
          size = allowedStyles.size();
        }
      }

      GeoServerRESTReader gsreader =
          new GeoServerRESTReader(
              rule.getInstance().getBaseURL(),
              rule.getInstance().getUsername(),
              rule.getInstance().getPassword());

      RESTStyleList styles = gsreader.getStyles();
      List<String> names = styles.getNames();
      Iterator<String> iterator = names.iterator();

      while (iterator.hasNext()) {
        String name = iterator.next();

        LayerStyle layerStyle = new LayerStyle();

        if (size > 0) {
          Iterator<String> styleIterator = allowedStyles.iterator();
          while (styleIterator.hasNext()) {
            String allowed = styleIterator.next();

            if (allowed.equalsIgnoreCase(name)) {
              layerStyle.setEnabled(true);
            }
          }
        } else {
          layerStyle.setEnabled(false);
        }

        layerStyle.setStyle(name);

        layerStyles.add(layerStyle);
      }

    } catch (MalformedURLException e) {
      logger.error(e.getLocalizedMessage(), e);
      throw new ApplicationException(e.getLocalizedMessage(), e);
    } catch (NotFoundServiceEx e) {
      logger.error(e.getLocalizedMessage(), e);
      throw new ApplicationException(e.getLocalizedMessage(), e);
    }

    return layerStyles;
  }
  /*
   * (non-Javadoc)
   *
   * @see
   * it.geosolutions.georepo.gui.server.service.IWorkspacesManagerService#getLayers(com.extjs.
   * gxt.ui.client.data.PagingLoadConfig, java.lang.String, java.lang.String)
   */
  public PagingLoadResult<Layer> getLayers(
      int offset,
      int limit,
      String baseURL,
      GSInstance gsInstance,
      String workspace,
      String service)
      throws ApplicationException {

    List<Layer> layersListDTO = new ArrayList<Layer>();
    layersListDTO.add(new Layer("*"));

    if ((baseURL != null)
        && !baseURL.equals("*")
        && !baseURL.contains("?")
        && (workspace != null)
        && (workspace.length() > 0)) {
      try {
        GeoServerRESTReader gsreader =
            new GeoServerRESTReader(baseURL, gsInstance.getUsername(), gsInstance.getPassword());

        RESTAbstractList<NameLinkElem> layers;

        if (workspace.equals("*")
            && workspaceConfigOpts.isShowDefaultGroups()
            && service.equals("WMS")) {
          layers = gsreader.getLayerGroups();

          if ((layers != null) && !layers.isEmpty()) {
            Iterator<NameLinkElem> lrIT = layers.iterator();
            while (lrIT.hasNext()) {
              NameLinkElem layerLink = lrIT.next();
              RESTLayerGroup group = gsreader.getLayerGroup(layerLink.getName());

              if (group != null) {
                layersListDTO.add(new Layer(group.getName()));
              }
            }
          }
        } else {
          layers = gsreader.getLayers();

          if ((layers != null) && !layers.isEmpty()) {
            Iterator<NameLinkElem> lrIT = layers.iterator();
            while (lrIT.hasNext()) {
              NameLinkElem layerLink = lrIT.next();
              RESTLayer layer = gsreader.getLayer(layerLink.getName());

              if (checkLayerIsInWorkspace(layer.getResourceUrl(), workspace)) {
                layersListDTO.add(new Layer(layer.getName()));
              }
            }
          }
        }
      } catch (MalformedURLException e) {
        logger.error(e.getLocalizedMessage(), e);
        throw new ApplicationException(e.getLocalizedMessage(), e);
      }
    }

    return new RpcPageLoadResult<Layer>(layersListDTO, 0, layersListDTO.size());
  }