// helper method
 protected void doUpdate(Database db, Tuple request)
     throws DatabaseException, IOException, ParseException {
   Entity entity = getModel().create();
   ScreenMessage msg = null;
   try {
     entity.set(request, false);
     int updatedRows = db.update(entity);
     msg = new ScreenMessage("UPDATE SUCCESS: affected " + updatedRows, null, true);
   } catch (Exception e) {
     logger.error("doUpdate(): " + e);
     e.printStackTrace();
     msg = new ScreenMessage("UPDATE FAILED: " + e.getMessage(), null, false);
   }
   getModel().getMessages().add(msg);
   if (msg.isSuccess()) {
     pager.setDirty(true);
     // resetChildren();
   }
 }
  /**
   * Helper function to add a record from UI
   *
   * @param request
   * @return true if add was successfull, false if it wassen't
   * @throws ParseException
   * @throws DatabaseException
   * @throws IOException
   */
  public boolean doAdd(Database db, Tuple request)
      throws ParseException, DatabaseException, IOException {
    ScreenMessage msg = null;
    Entity entity = getModel().create();
    boolean result = false;

    try {
      db.beginTx();
      entity.set(request, false);
      int updatedRows = 0;
      if (request.getObject(FormModel.INPUT_BATCHADD) != null
          && request.getInt(FormModel.INPUT_BATCHADD) > 1) {
        // batch
        int i;
        for (i = 0; i < request.getInt(FormModel.INPUT_BATCHADD); i++) {
          updatedRows += db.add(entity);
        }
      } else {
        updatedRows = db.add(entity);
      }
      db.commitTx();
      msg = new ScreenMessage("ADD SUCCESS: affected " + updatedRows, null, true);
      result = true;
      // navigate to newly added record
      pager.last(db);

    } catch (Exception e) {
      db.rollbackTx();
      msg = new ScreenMessage("ADD FAILED: " + e.getMessage(), null, false);
      result = false;
    }
    getModel().getMessages().add(msg);

    /* make sure the user sees the newly added record(s) */
    // view.setMode(FormScreen.Mode.RECORD_VIEW);
    // pager.setLimit(1);
    pager.resetOrderBy();
    pager.last(db);
    // should reset to an order that shows the record on the end

    return result;
  }
  private static void showSelectFilterDialogForEntity(Entity entity, PrintWriter out, Database db) {
    logger.debug("show 'set filters' dialogue");
    out.println("<html><body>");
    out.println(
        "<head><script src=\"../../../res/scripts/rest.js\" language=\"javascript\"></script></head>");
    out.println("<h1>REST url wizard:</h1>");
    out.println("Step 2: choose filters<br>");
    out.println("<form>");
    out.println(
        "You choose to use the REST interface for retrieval of '"
            + entity.getClass().getName()
            + "' data. (<a href=\"../find\">back</a>)<br><br> Here you can add filters:<br>");

    SelectInput fieldInput = new SelectInput("field", null);
    fieldInput.setOptions(entity.getFields().toArray(new String[entity.getFields().size()]));

    SelectInput operatorInput = new SelectInput("operator", null);
    List<String> operators = new ArrayList<String>();
    operators.add(QueryRule.Operator.EQUALS.toString());
    operators.add(QueryRule.Operator.GREATER_EQUAL.toString());
    operators.add(QueryRule.Operator.NOT.toString());
    operators.add(QueryRule.Operator.LESS.toString());
    operators.add(QueryRule.Operator.LESS_EQUAL.toString());
    operators.add(QueryRule.Operator.LIKE.toString());
    operatorInput.setOptions(operators.toArray(new String[operators.size()]));

    StringInput valueInput = new StringInput("value", null);

    out.println(fieldInput.getHtml() + operatorInput.getHtml() + valueInput.getHtml());
    out.println("<br>");

    out.println(fieldInput.getHtml() + operatorInput.getHtml() + valueInput.getHtml());
    out.println("<br>");

    out.println(
        "<input type=\"submit\" value=\"generate url\" onclick=\"generateRestUrl(this.form.elements); return false\">");

    out.println("TIP: notice how the url is bookmarkeable for future downloads!");
    out.println("TIP: click 'save as...' and name it as '.txt' file.");
    out.println("</form></body></html>");
  }
  // helper method
  protected void doRemove(Database db, Tuple request)
      throws DatabaseException, ParseException, IOException {
    Entity entity = getModel().create();
    ScreenMessage msg = null;
    try {
      entity.set(request);
      int updatedRows = db.remove(entity);
      if (updatedRows > 0)
        msg = new ScreenMessage("REMOVE SUCCESS: affected " + updatedRows, null, true);
      else msg = new ScreenMessage("REMOVE FAILED: call system administrator", null, false);
    } catch (Exception e) {
      msg = new ScreenMessage("REMOVE FAILED: " + e.getMessage(), null, false);
    }
    getModel().getMessages().add(msg);

    // **make sure the user sees a record**/
    if (msg.isSuccess()) {
      pager.prev(db);
      // resetChildren();
    }
  }