/* (non-Javadoc)
   * @see uk.ac.ox.oucs.oxam.logic.PaperFileService#deposit(uk.ac.ox.oucs.oxam.logic.PaperFile, uk.ac.ox.oucs.oxam.logic.Callback)
   */
  public void deposit(PaperFile paperFile, InputStream in) {
    PaperFileImpl impl = castToImpl(paperFile);
    String path = impl.getPath();
    ContentResourceEdit resource = null;
    try {

      try {
        contentHostingService.checkResource(path);
        resource = contentHostingService.editResource(path);
        // Ignore PermissionException, IdUnusedException, TypeException
        // As they are too serious to continue.
      } catch (IdUnusedException iue) {
        // Will attempt to create containing folders.

        resource = contentHostingService.addResource(path);
        // Like the basename function.
        String filename = StringUtils.substringAfterLast(path, "/");
        ResourceProperties props = resource.getPropertiesEdit();
        props.addProperty(ResourceProperties.PROP_DISPLAY_NAME, filename);
        resource.setContentType(mimeTypes.getContentType(filename));
      }
      resource.setContent(in);
      contentHostingService.commitResource(resource, NotificationService.NOTI_NONE);
      LOG.debug("Sucessfully copied file to: " + path);
    } catch (OverQuotaException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (ServerOverloadException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (PermissionException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (InUseException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (TypeException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IdUsedException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IdInvalidException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (InconsistentException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } finally {
      // Close the stream here as this is where it's created.
      if (resource.isActiveEdit()) {
        contentHostingService.cancelResource(resource);
      }
    }
  }
  @Override
  public void setDefaultPrivacyState(String userId, String visibility) {
    if (userId == null) {
      log.warn("Cannot set priavacy status for a null userId");
      return;
    }

    if (visibility == null) {
      visibility = PrivacyManager.VISIBLE;
    }

    PreferencesEdit editPref;
    try {
      editPref = preferencesService.edit(userId);

      ResourcePropertiesEdit props = editPref.getPropertiesEdit(PRIVACY_PREFS);
      props.addProperty(PrivacyManager.DEFAULT_PRIVACY_KEY, visibility);

      preferencesService.commit(editPref);
    } catch (PermissionException e) {
      log.warn(
          "You do not have the appropriate permissions to edit preferences for user: "******". "
              + e.getMessage());
    } catch (InUseException e) {
      log.warn(
          "Preferences for user: "******" are currently being edited. " + e.getMessage());
    } catch (IdUnusedException e) {
      try {
        editPref = preferencesService.add(userId);

        ResourcePropertiesEdit props = editPref.getPropertiesEdit(PRIVACY_PREFS);
        props.addProperty(PrivacyManager.DEFAULT_PRIVACY_KEY, visibility);

        preferencesService.commit(editPref);
      } catch (PermissionException e1) {
        // TODO Auto-generated catch block
        log.warn(
            "You do not have the appropriate permissions to edit preferences for user: "******". "
                + e1.getMessage());
      } catch (IdUsedException e1) {
        log.warn(
            "No preferences for user: "******" found intially, attempted to add new preferences. "
                + e1.getMessage());
      }
    }
  }