/**
   * saveEdit
   *
   * <p>Updates the object given the specified parameters, and form values.
   *
   * <pre>
   * Version	Date		Developer				Description
   * 0.1		26/04/2012	Genevieve Turner (GT)	Initial
   * 0.7		04/06/2012	Genevieve Turner (GT)	Fixed an issue where the fedora object was not returned in the values map
   * 0.8		20/06/2012	Genevieve Turner (GT)	Updated so that page retrieval is now using a map
   * 0.13		25/07/2012	Genevieve Turner (GT)	Added removing of ready for review/publish
   * 0.16		27/08/2012	Genevieve Turner (GT)	Fixed issue where group was not updated when editing
   * 0.22		06/11/2012	Genevieve Turner (GT)	Updated to check if the user has permissions to update the group if not remove those permissions
   * 0.23		12/11/2012	Genevieve Turner (GT)	Added the request id
   * </pre>
   *
   * @param fedoraObject The fedora object to get the page for
   * @param tmplt The template that determines the fields on the screen
   * @param form The form fields of the screen
   * @param rid The request id
   * @return Returns the viewable for the jsp file to pick up.
   */
  @Override
  public Map<String, Object> saveEdit(
      FedoraObject fedoraObject, String tmplt, Map<String, List<String>> form, Long rid) {
    Map<String, Object> values = new HashMap<String, Object>();
    ViewTransform viewTransform = new ViewTransform();
    try {
      if (form.containsKey("ownerGroup")) {
        // TODO Update this so that an error is thrown if the user does not have permissions to
        // update the group
        if (!permissionService.hasSetGroupPermissionsForObject(fedoraObject)) {
          form.remove("ownerGroup");
        }
      }
      fedoraObject = viewTransform.saveData(tmplt, fedoraObject, form, rid);
      removeReviewReady(fedoraObject);
      removePublishReady(fedoraObject);
      if (form.containsKey("ownerGroup")) {
        permissionService.saveObjectPermissions(fedoraObject);
      }
    } catch (JAXBException e) {
      LOGGER.error("Exception transforming jaxb", e);
      values.put("error", "true");
    } catch (FedoraClientException e) {
      LOGGER.error("Exception creating/retrieving objects", e);
      values.put("error", "true");
    }

    return values;
  }
  /**
   * getPage
   *
   * <p>Retrieves a page for the given values
   *
   * <pre>
   * Version	Date		Developer				Description
   * 0.1		26/04/2012	Genevieve Turner (GT)	Initial
   * 0.2		03/05/2012	Genevieve Turner (GT)	Updated to add related links to the page
   * 0.8		20/06/2012	Genevieve Turner (GT)	Updated so that page retrieval is now using a map
   * 0.10		11/07/2012	Genevieve Turner (GT)	Updated to allow or deny access to unpublished pages
   * 0.11		13/07/2012	Rahul Khanna (RK)		Updated filelist displayed on collection page
   * 0.15		20/08/2012	Genevieve Turner (GT)	Updated to use permissionService rather than aclService
   * 0.23		13/11/2012	Genevieve Turner (GT)	Added whether edit mode should be used in retrieving the page (i.e. no published information is retrieved)
   * </pre>
   *
   * @param layout The layout to use with display (i.e. the xsl stylesheet)
   * @param tmplt The template that determines the fields on the screen
   * @param fedoraObject The object of the page to retrieve
   * @param editMode Indicates whether published information should be returned or not
   * @return Returns the viewable for the jsp file to pick up.
   */
  private Map<String, Object> getPage(
      String layout, String template, FedoraObject fedoraObject, boolean editMode) {
    boolean hasPermission = false;
    if (fedoraObject == null) {
      hasPermission = true;
    } else {
      hasPermission = permissionService.checkViewPermission(fedoraObject);
    }
    Map<String, Object> values = new HashMap<String, Object>();
    values.put("topage", "/page.jsp");
    ViewTransform viewTransform = new ViewTransform();
    try {
      if (fedoraObject != null) {
        // Add bag summary to model.
        try {
          RecordDataSummary rdi =
              storageController.getRecordDataSummary(fedoraObject.getObject_id());
          values.put("rdi", rdi);
        } catch (IOException | StorageException e) {
          LOGGER.error(e.getMessage(), e);
        }
      }
      if (hasPermission) {
        values.putAll(viewTransform.getPage(layout, template, fedoraObject, null, editMode, false));
      } else if (fedoraObject.getPublished()) {
        values.putAll(viewTransform.getPage(layout, template, fedoraObject, null, false, true));
      } else {
        throw new AccessDeniedException(
            format(
                "User does not have permission to view page for record {0}",
                fedoraObject.getObject_id()));
      }
    } catch (FedoraClientException e) {
      LOGGER.error("Exception: ", e);
      values.put("topage", "/error.jsp");
    }

    if (!values.containsKey("page")) {
      values.put("topage", "/error.jsp");
    }

    if (fedoraObject != null) {
      // TODO This is should probably be modified
      values.put("fedoraObject", fedoraObject);

      String sidepage = "buttons.jsp";
      values.put("sidepage", sidepage);

      // SparqlResultSet resultSet = getLinks(fedoraObject);
      List<Result> resultSet = getLinks(fedoraObject);
      values.put("resultSet", resultSet);
    }

    return values;
  }
  /**
   * saveNew
   *
   * <p>Saves the information then displays a page with the given information
   *
   * <pre>
   * Version	Date		Developer				Description
   * 0.1		26/04/2012	Genevieve Turner (GT)	Initial
   * 0.8		20/06/2012	Genevieve Turner (GT)	Updated so that page retrieval is now using a map
   * 0.15		20/08/2012	Genevieve Turner (GT)	Updated to use permissionService rather than aclService
   * 0.23		12/11/2012	Genevieve Turner (GT)	Added the request id
   * 0.25		02/01/2012	Genevieve Turner (GT)	Updated to enforce records requriing an ownerGroup, type and name/title
   * </pre>
   *
   * @param layout The layout to display the page
   * @param tmplt The template that determines the fields on the screen
   * @param form Contains the parameters from the request
   * @param rid The request id
   * @return Returns the viewable for the jsp file to pick up.
   * @throws JAXBException
   * @throws FedoraClientException
   */
  @Override
  public FedoraObject saveNew(String tmplt, Map<String, List<String>> form, Long rid)
      throws FedoraClientException, JAXBException {
    FedoraObject fedoraObject = null;
    ViewTransform viewTransform = new ViewTransform();

    List<String> messages = new ArrayList<String>();
    if (form.get("ownerGroup") == null
        || form.get("ownerGroup").size() == 0
        || form.get("ownerGroup").get(0).trim().equals("")) {
      messages.add("No Group Affiliation");
    }
    if (form.get("type") == null
        || form.get("type").size() == 0
        || form.get("type").get(0).trim().equals("")) {
      messages.add("No item type has been set");
    }
    if ((form.get("name") == null
            || form.get("name").size() == 0
            || form.get("name").get(0).trim().equals(""))
        && (form.get("lastName") == null
            || form.get("lastName").size() == 0
            || form.get("lastName").get(0).trim().equals(""))) {
      messages.add("No name/title has been set");
    }

    if (messages.size() == 0) {
      // Check if the user has access to the ownerGroup
      String ownerGroup = form.get("ownerGroup").get(0);
      Long ownerGroupId = new Long(ownerGroup);
      List<Groups> groups = groupService.getCreateGroups();
      boolean groupFound = false;
      for (Groups group : groups) {
        if (group.getId().equals(ownerGroupId)) {
          groupFound = true;
          break;
        }
      }
      if (groupFound == false) {
        throw new AccessDeniedException(
            format("You do not have permissions to create in group {0}", ownerGroup));
      }
    } else {
      throw new ValidateException(messages);
    }

    fedoraObject = viewTransform.saveData(tmplt, null, form, rid);
    permissionService.saveObjectPermissions(fedoraObject);

    return fedoraObject;
  }