/** Finds all the studies */
  @Override
  public void processRequest() throws Exception {

    StudyDAO sdao = new StudyDAO(sm.getDataSource());
    ArrayList studies = (ArrayList) sdao.findAll();
    // find all parent studies
    ArrayList parents = (ArrayList) sdao.findAllParents();
    ArrayList displayStudies = new ArrayList();

    for (int i = 0; i < parents.size(); i++) {
      StudyBean parent = (StudyBean) parents.get(i);
      ArrayList children = (ArrayList) sdao.findAllByParent(parent.getId());
      DisplayStudyBean displayStudy = new DisplayStudyBean();
      displayStudy.setParent(parent);
      displayStudy.setChildren(children);
      displayStudies.add(displayStudy);
    }

    FormProcessor fp = new FormProcessor(request);
    EntityBeanTable table = fp.getEntityBeanTable();
    ArrayList allStudyRows = DisplayStudyRow.generateRowsFromBeans(displayStudies);

    String[] columns = {
      resword.getString("name"),
      resword.getString("unique_identifier"),
      resword.getString("OID"),
      resword.getString("principal_investigator"),
      resword.getString("facility_name"),
      resword.getString("date_created"),
      resword.getString("status"),
      resword.getString("actions")
    };
    table.setColumns(new ArrayList(Arrays.asList(columns)));
    table.hideColumnLink(2);
    table.hideColumnLink(6);
    table.setQuery("ListStudy", new HashMap());
    table.addLink(resword.getString("create_a_new_study"), "CreateStudy");
    table.setRows(allStudyRows);
    table.computeDisplay();

    request.setAttribute("table", table);
    // request.setAttribute("studies", studies);
    session.setAttribute("fromListSite", "no");

    resetPanel();
    panel.setStudyInfoShown(false);
    panel.setOrderedData(true);
    setToPanel(resword.getString("in_the_application"), "");
    if (parents.size() > 0) {
      setToPanel(resword.getString("studies"), new Integer(parents.size()).toString());
    }
    if (studies.size() > 0) {
      setToPanel(
          resword.getString("sites"), new Integer(studies.size() - parents.size()).toString());
    }
    forwardPage(Page.STUDY_LIST);
  }
  private void populateRows() {
    UserAccountBean a = new UserAccountBean();
    UserAccountBean b = new UserAccountBean();
    UserAccountBean c = new UserAccountBean();

    // order the beans a,b,c by username
    a.setName("abc");
    b.setName("def");
    c.setName("ghi");

    // order the beans b,c,a by first name
    a.setFirstName("Zack");
    b.setFirstName("Bob abc");
    c.setFirstName("Cindy");

    // order the beans c,b,a by last name
    a.setLastName("Xerxes");
    b.setLastName("Zuckerman");
    c.setLastName("Connor");

    UserAccountRow rowA = new UserAccountRow();
    rowA.setBean(a);
    UserAccountRow rowB = new UserAccountRow();
    rowB.setBean(b);
    UserAccountRow rowC = new UserAccountRow();
    rowC.setBean(c);
    UserAccountRow[] rows = {rowA, rowB, rowC};

    ArrayList tempRows = new ArrayList(Arrays.asList(rows));

    // add 15 dummy rows so we can test out pagination
    for (int i = 0; i < 15; i++) {
      UserAccountBean uab = new UserAccountBean();
      // force these beans into the middle of the alphabet so they don't
      // ruin the sorting results
      uab.setName("efg");
      uab.setFirstName("jjj");
      uab.setLastName("jjj");
      UserAccountRow uar = new UserAccountRow();
      uar.setBean(uab);
      tempRows.add(uar);
    }

    table.setRows(tempRows);
  }
  /**
   * Compute the subset of rows which should be shown on the screen. Note that the tabling
   * parameters should be set properly before this method is called!
   */
  public void computeDisplay() {
    ArrayList displayRows;
    Set temprows = new HashSet();

    // *****************
    // FILTER BY KEYWORD
    // *****************

    // the filter is considered to have been executed if the keyword filter
    // bit is on,
    // and if there is at least one keyword to search by
    boolean filterExecuted = false;

    displayRows = new ArrayList();
    if (filtered) {
      String[] keywords = keywordFilter.split("\\s");

      if (keywords != null) {
        for (int j = 0; j < keywords.length; j++) {
          String keyword = keywords[j];
          if (keyword == null || "".equals(keyword)) {
            continue;
          }

          keyword = keyword.toLowerCase();

          filterExecuted = true;

          for (int i = 0; i < rows.size(); i++) {
            EntityBeanRow row = (EntityBeanRow) rows.get(i);

            String searchString = row.getSearchString().toLowerCase();

            if (searchString.indexOf(keyword) >= 0) {
              temprows.add(row);
            }
          } // end of loop iterating over rows
        } // end of loop iterating over keywords
      }
      Iterator it = temprows.iterator();
      while (it.hasNext()) {
        displayRows.add(it.next());
      }
    } // end of filtering by keywords

    if (!filterExecuted) {
      displayRows = rows;
    }

    // this seems redundant, since we set the rows property below before
    // returning from the method
    // the reason for this call is to reset the totalNumPages property,
    // to reflect the number of rows that matched the search terms (if any)
    setRows(displayRows);

    // *************
    // SORT THE ROWS
    // *************

    for (int i = 0; i < displayRows.size(); i++) {
      EntityBeanRow row = (EntityBeanRow) displayRows.get(i);
      row.setSortingColumn(sortingColumnInd);
      row.setAscendingSort(ascendingSort);
      displayRows.set(i, row);
    }
    Collections.sort(displayRows);

    // ****************
    // APPLY PAGINATION
    // ****************
    if (paginated) {
      if (currPageNumber < 1) {
        currPageNumber = 1;
      }
      if (currPageNumber > totalPageNumbers && totalPageNumbers > 0) {
        currPageNumber = totalPageNumbers;
      }

      int firstInd = (currPageNumber - 1) * NUM_ROWS_PER_PAGE;
      int lastInd = currPageNumber * NUM_ROWS_PER_PAGE;
      lastInd = lastInd > displayRows.size() ? displayRows.size() : lastInd;

      // JRWS>> This block added to catch issue 1223, where searching a
      // large list of studies fails when search criteria result in zero
      // studies in the list, but you are on the third page of the list
      // when you perform the search
      if (firstInd > lastInd && lastInd == 0) {
        firstInd = 0;
      }

      ArrayList currPage = new ArrayList(displayRows.subList(firstInd, lastInd));

      // it's important not to use setRows here
      // calling setRows will change totalNumPages to be the number of
      // pages in currPage (always 1)
      // we don't want to change totalNumPages since it'll screw up the
      // display of "Previous" and "Next" page links
      rows = currPage;
    } else {
      rows = displayRows;
    }
  }