/** Delete the main image and the thumbnail, and go to the "add image" screen. */
  private ResponseValues doDeleteThenEdit(VitroRequest vreq, Individual entity) {
    ImageUploadHelper helper =
        new ImageUploadHelper(fileStorage, vreq.getFullWebappDaoFactory(), getServletContext());

    helper.removeExistingImage(entity);

    return showAddImagePage(vreq, entity);
  }
 /** Are we writing the error message to the "Add" page or to the "Replace" page? */
 private ResponseValues showErrorMessage(VitroRequest vreq, Individual entity, String message) {
   ImageInfo imageInfo = ImageInfo.instanceFromEntityUri(vreq.getFullWebappDaoFactory(), entity);
   if (imageInfo == null) {
     return showAddImagePageWithError(vreq, entity, message);
   } else {
     return showReplaceImagePageWithError(vreq, entity, imageInfo, message);
   }
 }
 /** Show the first screen in the upload process: Add or Replace. */
 private ResponseValues doIntroScreen(VitroRequest vreq, Individual entity) {
   ImageInfo imageInfo = ImageInfo.instanceFromEntityUri(vreq.getFullWebappDaoFactory(), entity);
   if (imageInfo == null) {
     return showAddImagePage(vreq, entity);
   } else {
     return showReplaceImagePage(vreq, entity, imageInfo);
   }
 }
  /** We need to be talking about an actual Individual here. */
  private Individual validateEntityUri(VitroRequest vreq) throws UserMistakeException {
    String entityUri = vreq.getParameter(PARAMETER_ENTITY_URI);
    if (entityUri == null) {
      throw new UserMistakeException("No entity URI was provided");
    }

    Individual entity =
        vreq.getFullWebappDaoFactory().getIndividualDao().getIndividualByURI(entityUri);
    if (entity == null) {
      throw new UserMistakeException(
          "This URI is not recognized as belonging to anyone: '" + entityUri + "'");
    }
    return entity;
  }
  /** The user has specified how to crop the thumbnail. Crop it and attach it to the main image. */
  private ResponseValues doCreateThumbnail(VitroRequest vreq, Individual entity) {
    ImageUploadHelper helper =
        new ImageUploadHelper(fileStorage, vreq.getFullWebappDaoFactory(), getServletContext());

    try {
      CropRectangle crop = validateCropCoordinates(vreq);
      FileInfo newImage = helper.getNewImageInfo(vreq);
      FileInfo thumbnail = helper.generateThumbnail(crop, newImage);

      helper.removeExistingImage(entity);
      helper.storeImageFiles(entity, newImage, thumbnail);

      return showExitPage(vreq, entity);
    } catch (UserMistakeException e) {
      return showErrorMessage(vreq, entity, e.getMessage());
    }
  }
  /**
   * The user has selected their main image file. Remove any previous main image (and thumbnail),
   * and attach the new main image.
   */
  private ResponseValues doUploadImage(VitroRequest vreq, Individual entity) {
    ImageUploadHelper helper =
        new ImageUploadHelper(fileStorage, vreq.getFullWebappDaoFactory(), getServletContext());

    try {
      // Did they provide a file to upload? If not, show an error.
      FileItem fileItem = helper.validateImageFromRequest(vreq);

      // Put it in the file system, and store a reference in the session.
      FileInfo fileInfo = helper.storeNewImage(fileItem, vreq);

      // How big is the new image? If not big enough, show an error.
      Dimensions size = helper.getNewImageSize(fileInfo);

      // Go to the cropping page.
      return showCropImagePage(vreq, entity, fileInfo.getBytestreamAliasUrl(), size);
    } catch (UserMistakeException e) {
      return showErrorMessage(vreq, entity, e.getMessage());
    }
  }
  @Override
  public void doPost(HttpServletRequest req, HttpServletResponse response) {
    if (!isAuthorizedToDisplayPage(req, response, new Actions(new EditOntology()))) {
      return;
    }

    VitroRequest request = new VitroRequest(req);

    EditProcessObject epo = super.createEpo(request);
    request.setAttribute("epoKey", epo.getKey());

    OntologyDao oDao = request.getFullWebappDaoFactory().getOntologyDao();
    Ontology o = null;
    if (request.getParameter("uri") == null) {
      log.error("doPost() expects non-null uri parameter");
    } else {
      o = oDao.getOntologyByURI(request.getParameter("uri"));
      if (o == null) {
        if (!VitroVocabulary.vitroURI.equals(request.getParameter("uri"))) {
          log.debug(
              "doPost(): no ontology object found for the namespace "
                  + request.getParameter("uri"));
        }
      } else {
        request.setAttribute("Ontology", o);
      }
    }
    ArrayList<String> results = new ArrayList<String>();
    results.add("Ontology");
    results.add("Namespace");
    results.add("Prefix");
    String name = o == null ? "" : (o.getName() == null) ? "" : o.getName();
    results.add(name);
    String namespace = o == null ? "" : (o.getURI() == null) ? "" : o.getURI();
    results.add(namespace);
    String prefix = o == null ? "" : (o.getPrefix() == null) ? "" : o.getPrefix();
    results.add(prefix);
    request.setAttribute("results", results);
    request.setAttribute("columncount", 3);
    request.setAttribute("suppressquery", "true");

    epo.setDataAccessObject(oDao);
    FormObject foo = new FormObject();
    HashMap<String, List<Option>> OptionMap = new HashMap<String, List<Option>>();

    HashMap formSelect =
        new HashMap(); // tells the JSP what select lists are populated, and thus should be
                       // displayed
    request.setAttribute("formSelect", formSelect);

    // add the options
    foo.setOptionLists(OptionMap);
    epo.setFormObject(foo);

    // funky hack because Ontology.getURI() will append a hash for a hash namespace
    // See OntologyDaoJena.ontologyFromOntologyResource() comments
    String realURI = OntologyDaoJena.adjustOntologyURI(o.getURI());
    request.setAttribute("realURI", realURI);
    request.setAttribute("exportURL", request.getContextPath() + Controllers.EXPORT_RDF);

    RequestDispatcher rd = request.getRequestDispatcher(Controllers.BASIC_JSP);
    request.setAttribute("epoKey", epo.getKey());
    request.setAttribute("bodyJsp", "/templates/edit/specific/ontologies_edit.jsp");
    request.setAttribute("title", "Ontology Control Panel");
    request.setAttribute(
        "css",
        "<link rel=\"stylesheet\" type=\"text/css\" href=\""
            + request.getAppBean().getThemeDir()
            + "css/edit.css\"/>");

    try {
      rd.forward(request, response);
    } catch (Exception e) {
      log.error("OntologyEditController could not forward to view.");
      log.error(e.getMessage());
      log.error(e.getStackTrace());
    }
  }
  public void doGet(HttpServletRequest request, HttpServletResponse response) {
    if (!isAuthorizedToDisplayPage(request, response, SimplePermission.EDIT_ONTOLOGY.ACTIONS)) {
      return;
    }

    VitroRequest vrequest = new VitroRequest(request);

    epo = super.createEpo(request);

    OntModel ontModel = (OntModel) getServletContext().getAttribute("jenaOntModel");

    ObjectPropertyDao opDao = vrequest.getFullWebappDaoFactory().getObjectPropertyDao();
    VClassDao vcDao = vrequest.getFullWebappDaoFactory().getVClassDao();
    IndividualDao iDao = vrequest.getFullWebappDaoFactory().getIndividualDao();

    ArrayList results = new ArrayList();
    request.setAttribute("results", results);
    results.add("XX");
    results.add("property");
    results.add("restriction");
    results.add("filler(s)");
    results.add(LAMBDA);

    String vClassURI = request.getParameter("VClassURI");

    if (vClassURI != null) {
      ontModel.enterCriticalSection(Lock.READ);
      try {
        OntClass ontClass = ontModel.getOntClass(vClassURI);
        if (ontClass != null) {
          ClosableIterator superClassIt = ontClass.listSuperClasses();
          try {
            for (Iterator i = superClassIt; i.hasNext(); ) {
              OntClass superClass = (OntClass) i.next();
              tryRestriction(superClass, vcDao, opDao, iDao, results, vClassURI);
            }
          } finally {
            superClassIt.close();
          }
          ClosableIterator equivClassIt = ontClass.listEquivalentClasses();
          try {
            for (Iterator i = equivClassIt; i.hasNext(); ) {
              OntClass superClass = (OntClass) i.next();
              tryRestriction(superClass, vcDao, opDao, iDao, results, vClassURI);
            }
          } finally {
            equivClassIt.close();
          }
        } else {
          doClassNotFound(results);
        }
      } finally {
        ontModel.leaveCriticalSection();
      }
    } else {
      doClassNotFound(results);
    }

    request.setAttribute("columncount", new Integer(5));
    request.setAttribute("suppressquery", "true");
    request.setAttribute("title", "Restrictions");
    request.setAttribute("bodyJsp", Controllers.HORIZONTAL_JSP);
    RequestDispatcher rd = request.getRequestDispatcher(Controllers.BASIC_JSP);
    try {
      rd.forward(request, response);
    } catch (Throwable t) {
      t.printStackTrace();
    }
  }
  public void doPost(HttpServletRequest request, HttpServletResponse response) {
    final int NUM_COLS = 17;

    if (!checkLoginStatus(request, response)) return;

    try {
      super.doGet(request, response);
    } catch (Exception e) {
      log.error("PropertyEditController caught exception calling doGet()");
    }

    VitroRequest vreq = new VitroRequest(request);
    Portal portal = vreq.getPortal();

    ObjectPropertyDao propDao = vreq.getFullWebappDaoFactory().getObjectPropertyDao();
    VClassDao vcDao = vreq.getFullWebappDaoFactory().getVClassDao();
    PropertyGroupDao pgDao = vreq.getFullWebappDaoFactory().getPropertyGroupDao();
    ObjectProperty p = (ObjectProperty) propDao.getObjectPropertyByURI(request.getParameter("uri"));
    request.setAttribute("property", p);

    ArrayList<String> results = new ArrayList<String>();
    results.add("Property"); // column 1
    results.add("parent property"); // column 2
    results.add("domain"); // column 3
    results.add("range"); // column 4
    results.add("display name"); // column 5
    results.add("group"); // column 6
    results.add("display tier"); // column 7
    results.add("example"); // column 8
    results.add("description"); // column 9
    results.add("public description"); // column 10
    results.add("display level"); // column 11
    results.add("update level"); // column 12
    results.add("custom entry form"); // column 13
    results.add("select from existing"); // column 14
    results.add("offer create new option"); // column 15
    results.add("force stub object deletion"); // column 16
    results.add("URI"); // column 17

    String displayName = (p.getDomainPublic() == null) ? p.getLocalName() : p.getDomainPublic();
    try {
      results.add(
          "<a href=\"propertyEdit?uri="
              + URLEncoder.encode(p.getURI(), "UTF-8")
              + "\">"
              + displayName
              + "</a> <em>"
              + p.getLocalNameWithPrefix()
              + "</em>"); // column 1
    } catch (UnsupportedEncodingException e) {
      log.error(
          "Could not encode URI for property (domain public: "
              + p.getDomainPublic()
              + ", local name with prefix: "
              + p.getLocalNameWithPrefix()
              + ", URI: "
              + p.getURI()
              + ").");
      results.add(displayName + "<em>" + p.getLocalNameWithPrefix() + "</em>"); // column 1
    }

    String parentPropertyStr = "";
    if (p.getParentURI() != null) {
      ObjectProperty parent = propDao.getObjectPropertyByURI(p.getParentURI());
      if (parent != null && parent.getURI() != null) {
        try {
          parentPropertyStr =
              "<a href=\"propertyEdit?uri="
                  + URLEncoder.encode(parent.getURI(), "UTF-8")
                  + "&amp;home="
                  + portal.getPortalId()
                  + "\">"
                  + parent.getLocalNameWithPrefix()
                  + "</a>";
        } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
        }
      }
    }
    results.add(parentPropertyStr); // column 2

    String domainStr = "";
    if (p.getDomainVClassURI() != null) {
      VClass domainClass = vcDao.getVClassByURI(p.getDomainVClassURI());
      if (domainClass != null
          && domainClass.getURI() != null
          && domainClass.getLocalNameWithPrefix() != null) {
        try {
          if (domainClass.isAnonymous()) {
            domainStr = domainClass.getLocalNameWithPrefix();
          } else {
            domainStr =
                "<a href=\"vclassEdit?uri="
                    + URLEncoder.encode(domainClass.getURI(), "UTF-8")
                    + "&amp;home="
                    + portal.getPortalId()
                    + "\">"
                    + domainClass.getLocalNameWithPrefix()
                    + "</a>";
          }
        } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
        }
      }
    }
    results.add(domainStr); // column 3

    String rangeStr = "";
    if (p.getRangeVClassURI() != null) {
      VClass rangeClass = vcDao.getVClassByURI(p.getRangeVClassURI());
      if (rangeClass != null
          && rangeClass.getURI() != null
          && rangeClass.getLocalNameWithPrefix() != null) {
        try {
          if (rangeClass.isAnonymous()) {
            rangeStr = rangeClass.getLocalNameWithPrefix();
          } else {
            rangeStr =
                "<a href=\"vclassEdit?uri="
                    + URLEncoder.encode(rangeClass.getURI(), "UTF-8")
                    + "&amp;home="
                    + portal.getPortalId()
                    + "\">"
                    + rangeClass.getLocalNameWithPrefix()
                    + "</a>";
          }
        } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
        }
      }
    }
    results.add(rangeStr); // column 4

    results.add(p.getDomainPublic() == null ? "" : p.getDomainPublic()); // column 5
    if (p.getGroupURI() != null) {
      PropertyGroup pGroup = pgDao.getGroupByURI(p.getGroupURI());
      if (pGroup != null) {
        results.add(pGroup.getName()); // column 6
      } else {
        results.add("unnamed group"); // column 6
      }
    } else {
      results.add("unspecified"); // column 6
    }
    results.add(
        "domain: " + p.getDomainDisplayTier() + ", range: " + p.getRangeDisplayTier()); // column 7
    String exampleStr = (p.getExample() == null) ? "" : p.getExample();
    results.add(exampleStr); // column 8
    String descriptionStr = (p.getDescription() == null) ? "" : p.getDescription();
    results.add(descriptionStr); // column 9
    String publicDescriptionStr =
        (p.getPublicDescription() == null) ? "" : p.getPublicDescription();
    results.add(publicDescriptionStr); // column 10

    results.add(
        p.getHiddenFromDisplayBelowRoleLevel() == null
            ? "unspecified"
            : p.getHiddenFromDisplayBelowRoleLevel().getLabel()); // column 11
    results.add(
        p.getProhibitedFromUpdateBelowRoleLevel() == null
            ? "unspecified"
            : p.getProhibitedFromUpdateBelowRoleLevel().getLabel()); // column 12

    results.add(
        p.getCustomEntryForm() == null ? "unspecified" : p.getCustomEntryForm()); // column 13
    results.add(p.getSelectFromExisting() ? "true" : "false"); // column 14
    results.add(p.getOfferCreateNewOption() ? "true" : "false"); // column 15
    results.add(p.getStubObjectRelation() ? "true" : "false"); // column 16
    results.add(p.getURI()); // column 17
    request.setAttribute("results", results);
    request.setAttribute("columncount", NUM_COLS);
    request.setAttribute("suppressquery", "true");

    boolean FORCE_NEW = true;

    EditProcessObject epo = super.createEpo(request, FORCE_NEW);
    FormObject foo = new FormObject();
    HashMap OptionMap = new HashMap();
    foo.setOptionLists(OptionMap);
    epo.setFormObject(foo);

    // superproperties and subproperties

    ObjectPropertyDao opDao;
    if (vreq.getAssertionsWebappDaoFactory() != null) {
      opDao = vreq.getAssertionsWebappDaoFactory().getObjectPropertyDao();
    } else {
      opDao = vreq.getFullWebappDaoFactory().getObjectPropertyDao();
    }
    List superURIs = opDao.getSuperPropertyURIs(p.getURI(), false);
    List superProperties = new ArrayList();
    Iterator superURIit = superURIs.iterator();
    while (superURIit.hasNext()) {
      String superURI = (String) superURIit.next();
      if (superURI != null) {
        ObjectProperty superProperty = opDao.getObjectPropertyByURI(superURI);
        if (superProperty != null) {
          superProperties.add(superProperty);
        }
      }
    }
    request.setAttribute("superproperties", superProperties);

    List subURIs = opDao.getSubPropertyURIs(p.getURI());
    List subProperties = new ArrayList();
    Iterator subURIit = subURIs.iterator();
    while (subURIit.hasNext()) {
      String subURI = (String) subURIit.next();
      ObjectProperty subProperty = opDao.getObjectPropertyByURI(subURI);
      if (subProperty != null) {
        subProperties.add(subProperty);
      }
    }
    request.setAttribute("subproperties", subProperties);

    List eqURIs = opDao.getEquivalentPropertyURIs(p.getURI());
    List eqProperties = new ArrayList();
    Iterator eqURIit = eqURIs.iterator();
    while (eqURIit.hasNext()) {
      String eqURI = (String) eqURIit.next();
      ObjectProperty eqProperty = opDao.getObjectPropertyByURI(eqURI);
      if (eqProperty != null) {
        eqProperties.add(eqProperty);
      }
    }
    request.setAttribute("equivalentProperties", eqProperties);

    RequestDispatcher rd = request.getRequestDispatcher(Controllers.BASIC_JSP);
    request.setAttribute("epoKey", epo.getKey());
    request.setAttribute("propertyWebapp", p);
    request.setAttribute("bodyJsp", "/templates/edit/specific/props_edit.jsp");
    request.setAttribute("portalBean", portal);
    request.setAttribute("title", "Object Property Control Panel");
    request.setAttribute(
        "css",
        "<link rel=\"stylesheet\" type=\"text/css\" href=\""
            + portal.getThemeDir()
            + "css/edit.css\"/>");

    try {
      rd.forward(request, response);
    } catch (Exception e) {
      log.error("PropertyEditController could not forward to view.");
      log.error(e.getMessage());
      log.error(e.getStackTrace());
    }
  }