Example #1
0
  public static String getLanguageCode(HttpServletRequest request) {
    String context = ServletUtilities.getContext(request);

    // worst case scenario default to English
    String langCode = "en";

    // try to detect a default if defined
    if (CommonConfiguration.getProperty("defaultLanguage", context) != null) {
      langCode = CommonConfiguration.getProperty("defaultLanguage", context);
    }

    ArrayList<String> supportedLanguages = new ArrayList<String>();
    if (CommonConfiguration.getSequentialPropertyValues("language", context) != null) {
      supportedLanguages = CommonConfiguration.getSequentialPropertyValues("language", context);
    }

    // if specified directly, always accept the override
    if (request.getParameter("langCode") != null) {
      if (supportedLanguages.contains(request.getParameter("langCode"))) {
        return request.getParameter("langCode");
      }
    }

    // the request cookie is the next thing we check. this should be the primary means of figuring
    // langCode out
    Cookie[] cookies = request.getCookies();
    if (cookies != null) {
      for (Cookie cookie : cookies) {
        if ("wildbookLangCode".equals(cookie.getName())) {
          if (supportedLanguages.contains(cookie.getValue())) {
            return cookie.getValue();
          }
        }
      }
    }

    // finally, we will check the URL vs values defined in context.properties to see if we can set
    // the right context
    // TBD - future - detect browser supported language codes and locale from the HTTPServletRequest
    // object

    return langCode;
  }
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    String context = "context0";
    context = ServletUtilities.getContext(request);
    String langCode = ServletUtilities.getLanguageCode(request);
    Shepherd myShepherd = new Shepherd(context);
    // set up for response
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    boolean locked = false;
    boolean isOwner = true;

    if (request.getParameter("number") != null) {
      myShepherd.beginDBTransaction();
      Encounter enc2reject = myShepherd.getEncounter(request.getParameter("number"));
      setDateLastModified(enc2reject);
      boolean isOK = enc2reject.isAssignedToMarkedIndividual().equals("Unassigned");
      myShepherd.rollbackDBTransaction();
      if (isOK) {

        myShepherd.beginDBTransaction();
        try {

          // enc2reject.reject();
          enc2reject.setState("unidentifiable");
          enc2reject.addComments(
              "<p><em>"
                  + request.getRemoteUser()
                  + " on "
                  + (new java.util.Date()).toString()
                  + "</em><br>Set this encounter as unidentifiable in the database.</p>");
          // enc2reject.approved = false;
          enc2reject.setState("unidentifiable");
        } catch (Exception le) {
          locked = true;
          le.printStackTrace();
          myShepherd.rollbackDBTransaction();
        }

        if (!locked) {
          String submitterEmail = enc2reject.getSubmitterEmail();
          myShepherd.commitDBTransaction();
          out.println(ServletUtilities.getHeader(request));
          out.println(
              "<strong>Success:</strong> I have set encounter "
                  + request.getParameter("number")
                  + " as unidentifiable in the database.");
          out.println(
              "<p><a href=\"http://"
                  + CommonConfiguration.getURLLocation(request)
                  + "/encounters/encounter.jsp?number="
                  + request.getParameter("number")
                  + "\">View unidentifiable encounter #"
                  + request.getParameter("number")
                  + "</a></p>\n");
          ArrayList<String> allStates =
              CommonConfiguration.getSequentialPropertyValues("encounterState", context);
          int allStatesSize = allStates.size();
          if (allStatesSize > 0) {
            for (int i = 0; i < allStatesSize; i++) {
              String stateName = allStates.get(i);
              out.println(
                  "<p><a href=\"encounters/searchResults.jsp?state="
                      + stateName
                      + "\">View all "
                      + stateName
                      + " encounters</a></font></p>");
            }
          }
          out.println(ServletUtilities.getFooter(context));
          String message =
              "Encounter "
                  + request.getParameter("number")
                  + " was set as unidentifiable in the database.";
          ServletUtilities.informInterestedParties(
              request, request.getParameter("number"), message, context);

          // Email submitter about change
          Map<String, String> tagMap = NotificationMailer.createBasicTagMap(request, enc2reject);
          tagMap.put("@TEXT_CONTENT@", message);
          ThreadPoolExecutor es = MailThreadExecutorService.getExecutorService();
          NotificationMailer mailer =
              new NotificationMailer(context, null, submitterEmail, "encounterDataUpdate", tagMap);
          es.execute(mailer);
          es.shutdown();

        } else {
          out.println(ServletUtilities.getHeader(request));
          out.println(
              "<strong>Failure:</strong> I have NOT modified encounter "
                  + request.getParameter("number")
                  + " in the database because another user is currently modifying its entry. Please try this operation again in a few seconds.");
          out.println(
              "<p><a href=\"http://"
                  + CommonConfiguration.getURLLocation(request)
                  + "/encounters/encounter.jsp?number="
                  + request.getParameter("number")
                  + "\">View unidentifiable encounter #"
                  + request.getParameter("number")
                  + "</a></p>\n");
          ArrayList<String> allStates =
              CommonConfiguration.getSequentialPropertyValues("encounterState", context);
          int allStatesSize = allStates.size();
          if (allStatesSize > 0) {
            for (int i = 0; i < allStatesSize; i++) {
              String stateName = allStates.get(i);
              out.println(
                  "<p><a href=\"encounters/searchResults.jsp?state="
                      + stateName
                      + "\">View all "
                      + stateName
                      + " encounters</a></font></p>");
            }
          }
          out.println(ServletUtilities.getFooter(context));
        }

      } else {
        out.println(ServletUtilities.getHeader(request));
        out.println(
            "Encounter# "
                + request.getParameter("number")
                + " is assigned to an individual and cannot be set as unidentifiable until it has been removed from that individual.");
        out.println(ServletUtilities.getFooter(context));
      }
    } else {
      out.println(ServletUtilities.getHeader(request));
      out.println(
          "<strong>Error:</strong> I do not know which encounter you are trying to remove.");
      out.println(ServletUtilities.getFooter(context));
    }

    out.close();
    myShepherd.closeDBTransaction();
  }