public void doEdit(RenderRequest request, RenderResponse response)
      throws PortletException, IOException {

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String title = getTitleString(request);
    if (title != null) response.setTitle(title);

    Context context = new VelocityContext();
    context.put("tlang", rb);
    context.put("validator", validator);
    sendAlert(request, context);

    PortletURL url = response.createActionURL();
    context.put("actionUrl", url.toString());
    context.put("doCancel", "sakai.cancel");
    context.put("doUpdate", "sakai.update");

    // get current site
    Placement placement = ToolManager.getCurrentPlacement();
    String siteId = "";

    // find the right LTIContent object for this page
    String source = placement.getPlacementConfig().getProperty(SOURCE);
    Long key = getContentIdFromSource(source);
    if (key == null) {
      out.println(rb.getString("get.info.notconfig"));
      M_log.warn("Cannot find content id placement=" + placement.getId() + " source=" + source);
      return;
    }

    Map<String, Object> content = m_ltiService.getContent(key);
    if (content == null) {
      out.println(rb.getString("get.info.notconfig"));
      M_log.warn("Cannot find content item placement=" + placement.getId() + " key=" + key);
      return;
    }

    // attach the ltiToolId to each model attribute, so that we could have the tool configuration
    // page for multiple tools
    String foundLtiToolId = content.get(m_ltiService.LTI_TOOL_ID).toString();
    Map<String, Object> tool = m_ltiService.getTool(Long.valueOf(foundLtiToolId));
    if (tool == null) {
      out.println(rb.getString("get.info.notconfig"));
      M_log.warn("Cannot find tool placement=" + placement.getId() + " key=" + foundLtiToolId);
      return;
    }

    String[] contentToolModel = m_ltiService.getContentModel(Long.valueOf(foundLtiToolId));
    String formInput = m_ltiService.formInput(content, contentToolModel);
    context.put("formInput", formInput);

    vHelper.doTemplate(vengine, "/vm/edit.vm", context, out);
  }
  // Render the portlet - this is not supposed to change the state of the portlet
  // Render may be called many times so if it changes the state - that is tacky
  // Render will be called when someone presses "refresh" or when another portlet
  // onthe same page is handed an Action.
  public void doView(RenderRequest request, RenderResponse response)
      throws PortletException, IOException {
    response.setContentType("text/html");

    // System.out.println("==== doView called ====");

    // Grab that underlying request to get a GET parameter
    ServletRequest req = (ServletRequest) ThreadLocalManager.get(CURRENT_HTTP_REQUEST);
    String popupDone = req.getParameter("sakai.popup");

    PrintWriter out = response.getWriter();
    Placement placement = ToolManager.getCurrentPlacement();
    response.setTitle(placement.getTitle());
    String source = placement.getPlacementConfig().getProperty(SOURCE);
    if (source == null) source = "";
    String height = placement.getPlacementConfig().getProperty(HEIGHT);
    if (height == null) height = "1200px";
    boolean maximize = "true".equals(placement.getPlacementConfig().getProperty(MAXIMIZE));

    boolean popup = false; // Comes from content item
    boolean oldPopup = "true".equals(placement.getPlacementConfig().getProperty(POPUP));

    // Retrieve the corresponding content item and tool to check the launch
    Map<String, Object> content = null;
    Map<String, Object> tool = null;
    Long key = getContentIdFromSource(source);
    if (key == null) {
      out.println(rb.getString("get.info.notconfig"));
      M_log.warn("Cannot find content id placement=" + placement.getId() + " source=" + source);
      return;
    }
    try {
      content = m_ltiService.getContent(key);
      // If we are supposed to popup (per the content), do so and optionally
      // copy the calue into the placement to communicate with the portal
      popup = getLongNull(content.get("newpage")) == 1;
      if (oldPopup != popup) {
        placement.getPlacementConfig().setProperty(POPUP, popup ? "true" : "false");
        placement.save();
      }
      String launch = (String) content.get("launch");
      Long tool_id = getLongNull(content.get("tool_id"));
      if (launch == null && tool_id != null) {
        tool = m_ltiService.getTool(tool_id);
        launch = (String) tool.get("launch");
      }

      // Force http:// to pop-up if we are https://
      String serverUrl = ServerConfigurationService.getServerUrl();
      if (request.isSecure() || (serverUrl != null && serverUrl.startsWith("https://"))) {
        if (launch != null && launch.startsWith("http://")) popup = true;
      }
    } catch (Exception e) {
      out.println(rb.getString("get.info.notconfig"));
      e.printStackTrace();
      return;
    }

    if (source != null && source.trim().length() > 0) {

      Context context = new VelocityContext();
      context.put("tlang", rb);
      context.put("validator", validator);
      context.put("source", source);
      context.put("height", height);
      sendAlert(request, context);
      context.put("popupdone", Boolean.valueOf(popupDone != null));
      context.put("popup", Boolean.valueOf(popup));
      context.put("maximize", Boolean.valueOf(maximize));

      vHelper.doTemplate(vengine, "/vm/main.vm", context, out);
    } else {
      out.println(rb.getString("get.info.notconfig"));
    }

    // System.out.println("==== doView complete ====");
  }