public int copyCollectionResource(
      WebDAVRequest webDavRequest,
      Resource resource,
      String destination,
      boolean overwrite,
      long depth)
      throws WebDAVException {

    try {
      String[] destinationArray = WebDAVUtil.getPathArray(destination, true);

      long companyId = webDavRequest.getCompanyId();

      long parentFolderId = IGFolderConstants.DEFAULT_PARENT_FOLDER_ID;

      try {
        parentFolderId = getParentFolderId(companyId, destinationArray);
      } catch (NoSuchFolderException nsfe) {
        return HttpServletResponse.SC_CONFLICT;
      }

      IGFolder folder = (IGFolder) resource.getModel();

      long groupId = WebDAVUtil.getGroupId(companyId, destination);
      String name = WebDAVUtil.getResourceName(destinationArray);
      String description = folder.getDescription();

      int status = HttpServletResponse.SC_CREATED;

      if (overwrite) {
        if (deleteResource(groupId, parentFolderId, name)) {
          status = HttpServletResponse.SC_NO_CONTENT;
        }
      }

      ServiceContext serviceContext = new ServiceContext();

      serviceContext.setAddCommunityPermissions(isAddCommunityPermissions(groupId));
      serviceContext.setAddGuestPermissions(true);
      serviceContext.setScopeGroupId(groupId);

      if (depth == 0) {
        IGFolderServiceUtil.addFolder(parentFolderId, name, description, serviceContext);
      } else {
        IGFolderServiceUtil.copyFolder(
            folder.getFolderId(), parentFolderId, name, description, serviceContext);
      }

      return status;
    } catch (DuplicateFolderNameException dfne) {
      return HttpServletResponse.SC_PRECONDITION_FAILED;
    } catch (PrincipalException pe) {
      return HttpServletResponse.SC_FORBIDDEN;
    } catch (Exception e) {
      throw new WebDAVException(e);
    }
  }
  public int deleteResource(WebDAVRequest webDavRequest) throws WebDAVException {

    try {
      Resource resource = getResource(webDavRequest);

      if (resource == null) {
        return HttpServletResponse.SC_NOT_FOUND;
      }

      Object model = resource.getModel();

      if (model instanceof IGFolder) {
        IGFolder folder = (IGFolder) model;

        IGFolderServiceUtil.deleteFolder(folder.getFolderId());
      } else {
        IGImage image = (IGImage) model;

        IGImageServiceUtil.deleteImage(image.getImageId());
      }

      return HttpServletResponse.SC_NO_CONTENT;
    } catch (PrincipalException pe) {
      return HttpServletResponse.SC_FORBIDDEN;
    } catch (Exception e) {
      throw new WebDAVException(e);
    }
  }
  protected long getFolderId(long companyId, String[] pathArray, boolean parent) throws Exception {

    long folderId = IGFolderConstants.DEFAULT_PARENT_FOLDER_ID;

    if (pathArray.length <= 1) {
      return folderId;
    } else {
      long groupId = WebDAVUtil.getGroupId(companyId, pathArray);

      int x = pathArray.length;

      if (parent) {
        x--;
      }

      for (int i = 2; i < x; i++) {
        String name = pathArray[i];

        IGFolder folder = IGFolderServiceUtil.getFolder(groupId, folderId, name);

        if (groupId == folder.getGroupId()) {
          folderId = folder.getFolderId();
        }
      }
    }

    return folderId;
  }
  protected boolean deleteResource(long groupId, long parentFolderId, String name)
      throws PortalException, SystemException {

    try {
      IGFolder folder = IGFolderServiceUtil.getFolder(groupId, parentFolderId, name);

      IGFolderServiceUtil.deleteFolder(folder.getFolderId());

      return true;
    } catch (NoSuchFolderException nsfe) {
      if (name.indexOf(StringPool.PERIOD) == -1) {
        return false;
      }

      try {
        IGImageServiceUtil.deleteImageByFolderIdAndNameWithExtension(groupId, parentFolderId, name);

        return true;
      } catch (NoSuchImageException nsie) {
      }
    }

    return false;
  }
  protected List<Resource> getFolders(WebDAVRequest webDavRequest, long parentFolderId)
      throws Exception {

    List<Resource> resources = new ArrayList<Resource>();

    long groupId = webDavRequest.getGroupId();

    List<IGFolder> folders = IGFolderServiceUtil.getFolders(groupId, parentFolderId);

    for (IGFolder folder : folders) {
      Resource resource = toResource(webDavRequest, folder, true);

      resources.add(resource);
    }

    return resources;
  }
  public Resource getResource(WebDAVRequest webDavRequest) throws WebDAVException {

    try {
      String[] pathArray = webDavRequest.getPathArray();

      long companyId = webDavRequest.getCompanyId();
      long parentFolderId = getParentFolderId(companyId, pathArray);
      String name = WebDAVUtil.getResourceName(pathArray);

      if (Validator.isNull(name)) {
        String path = getRootPath() + webDavRequest.getPath();

        return new BaseResourceImpl(path, StringPool.BLANK, getToken());
      }

      try {
        IGFolder folder =
            IGFolderServiceUtil.getFolder(webDavRequest.getGroupId(), parentFolderId, name);

        if ((folder.getParentFolderId() != parentFolderId)
            || (webDavRequest.getGroupId() != folder.getGroupId())) {

          throw new NoSuchFolderException();
        }

        return toResource(webDavRequest, folder, false);
      } catch (NoSuchFolderException nsfe) {
        try {
          long groupId = webDavRequest.getGroupId();

          IGImage image =
              IGImageServiceUtil.getImageByFolderIdAndNameWithExtension(
                  groupId, parentFolderId, name);

          return toResource(webDavRequest, image, false);
        } catch (NoSuchImageException nsie) {
          return null;
        }
      }
    } catch (Exception e) {
      throw new WebDAVException(e);
    }
  }
  public Status makeCollection(WebDAVRequest webDavRequest) throws WebDAVException {

    try {
      HttpServletRequest request = webDavRequest.getHttpServletRequest();

      if (request.getContentLength() > 0) {
        return new Status(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
      }

      String[] pathArray = webDavRequest.getPathArray();

      long companyId = webDavRequest.getCompanyId();
      long groupId = webDavRequest.getGroupId();
      long parentFolderId = getParentFolderId(companyId, pathArray);
      String name = WebDAVUtil.getResourceName(pathArray);
      String description = StringPool.BLANK;

      ServiceContext serviceContext = new ServiceContext();

      serviceContext.setAddCommunityPermissions(isAddCommunityPermissions(groupId));
      serviceContext.setAddGuestPermissions(true);
      serviceContext.setPlid(getPlid(webDavRequest.getGroupId()));
      serviceContext.setScopeGroupId(webDavRequest.getGroupId());

      IGFolderServiceUtil.addFolder(parentFolderId, name, description, serviceContext);

      String location = StringUtil.merge(pathArray, StringPool.SLASH);

      return new Status(location, HttpServletResponse.SC_CREATED);
    } catch (DuplicateFolderNameException dfne) {
      return new Status(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
    } catch (NoSuchFolderException nsfe) {
      return new Status(HttpServletResponse.SC_CONFLICT);
    } catch (PrincipalException pe) {
      return new Status(HttpServletResponse.SC_FORBIDDEN);
    } catch (Exception e) {
      throw new WebDAVException(e);
    }
  }
  public int moveCollectionResource(
      WebDAVRequest webDavRequest, Resource resource, String destination, boolean overwrite)
      throws WebDAVException {

    try {
      String[] destinationArray = WebDAVUtil.getPathArray(destination, true);

      IGFolder folder = (IGFolder) resource.getModel();

      long companyId = webDavRequest.getCompanyId();
      long groupId = WebDAVUtil.getGroupId(companyId, destinationArray);
      long folderId = folder.getFolderId();
      long parentFolderId = getParentFolderId(companyId, destinationArray);
      String name = WebDAVUtil.getResourceName(destinationArray);
      String description = folder.getDescription();

      ServiceContext serviceContext = new ServiceContext();

      int status = HttpServletResponse.SC_CREATED;

      if (overwrite) {
        if (deleteResource(groupId, parentFolderId, name)) {
          status = HttpServletResponse.SC_NO_CONTENT;
        }
      }

      IGFolderServiceUtil.updateFolder(
          folderId, parentFolderId, name, description, false, serviceContext);

      return status;
    } catch (PrincipalException pe) {
      return HttpServletResponse.SC_FORBIDDEN;
    } catch (DuplicateFolderNameException dfne) {
      return HttpServletResponse.SC_PRECONDITION_FAILED;
    } catch (Exception e) {
      throw new WebDAVException(e);
    }
  }