/**
   * This function makes the call to the XML database and retrieves all the file metadata for use in
   * ECGridToolkit. The data is first retrieved in a series of XML blocks, and then they are
   * converted into StudyEntry Objects and returned to the user
   *
   * @param userID - The ID of the user. This is used to check which files the user has submitted
   *     and thus has access to
   * @return The list of StudyEntry Objects taken from the file metadata in the XML database
   */
  public ArrayList<StudyEntry> getEntries(String userID) {
    ArrayList<StudyEntry> tempList = new ArrayList<StudyEntry>();

    try {

      // create first query to get the entire studyEntry block, the collection() XQuery function
      // does this across
      // all documents in the XML Collection

      //  The goal of this query is to find all the data we need for the StudyEntry object based on
      // which user submitted them.  For all the documents searched, the query looks to see
      //  if any files for that subject's ECG repository were submitted by that user.  If any files
      // were submitted, retrieve their metadata contained in the studyEntry block.
      String sQuery =
          studyBuilder.defaultFor()
              + studyBuilder.defaultWhere(userID)
              + studyBuilder.defaultOrderBy()
              + studyBuilder.defaultReturn();

      System.out.println("Query to be executed = " + sQuery);

      ResourceSet resultSet = executeQuery(sQuery);
      ResourceIterator iter = resultSet.getIterator();
      Resource selection = null;
      Resource fileSelection = null;
      int listIndex = 0;
      int subjectCount = 1;

      while (iter.hasMoreResources()) {
        selection = iter.nextResource();
        String studyEntryResult = (selection.getContent()).toString();

        // Now we will create an XStream object and then put that into our StudyEntry objects
        // the StudyEntry objects are de-serialized versions of the studyEntry blocks.
        XStream xmlStream = new XStream();

        xmlStream.alias("studyEntry", StudyEntry.class);
        xmlStream.alias("recordDetails", RecordDetails.class);
        xmlStream.alias("fileDetails", FileDetails.class);
        xmlStream.addImplicitCollection(RecordDetails.class, "fileDetails");

        StudyEntry newStudy = (StudyEntry) xmlStream.fromXML(studyEntryResult);

        // System.out.println(newStudy);

        // Add it to the return array
        tempList.add(newStudy);
      }
    } catch (Exception ex) {
      System.out.println(
          "StudyEntryUtility.getEntries():  AN EXCEPTION HAS BEEN CAUGHT!  IF A LIST IS RETURNED, IT WILL BE EMPTY!!!");
      ex.printStackTrace();
    }

    return tempList;
  }
  public ArrayList<String> queryStudyTypes(String userID, String dataName) {

    System.out.println("In function queryStudyTypes");

    ArrayList<String> tempList = new ArrayList<String>();

    try {

      // create first query to get the entire studyEntry block, the collection() XQuery function
      // does this across
      // all documents in the XML Collection

      //  The goal of this query is to find all the data we need for the StudyEntry object based on
      // which user submitted them.  For all the documents searched, the query looks to see
      //  if any files for that subject's ECG repository were submitted by that user.  If any files
      // were submitted, retrieve their metadata contained in the studyEntry block.

      System.out.println("queryDatatypes:");

      String sQuery =
          studyBuilder.forStudyEntry()
              + studyBuilder.customNameBracket(dataName, EnumStudyTreeNode.DATATYPE)
              + studyBuilder.userDefinedWhere(userID)
              + studyBuilder.customOrderBySubjectID()
              + studyBuilder.returnTreeNodeDistinct(EnumStudyTreeNode.STUDY);

      System.out.println("Query to be executed = " + sQuery);

      ResourceSet resultSet = executeQuery(sQuery);
      ResourceIterator iter = resultSet.getIterator();
      Resource selection = null;

      tempList = this.checkForDuplicates(iter, selection);

      for (String listing : tempList) {
        int i = 1;
        System.out.println("Entry " + i + " = " + listing);
        i++;
      }
    } catch (Exception ex) {
      System.out.println(
          "ExistMainDatabase.queryStudies():  AN EXCEPTION HAS BEEN CAUGHT!  IF A LIST IS RETURNED, IT WILL BE EMPTY!!!");
      ex.printStackTrace();
    }

    return tempList;
  }
  /**
   * This function makes the call to the XML database and retrieves all the file metadata for use in
   * ECGridToolkit. The data is first retrieved in a series of XML blocks, and then they are
   * converted into StudyEntry Objects and returned to the user
   *
   * @param userID - The ID of the user. This is used to check which files the user has submitted
   *     and thus has access to
   * @param studyID - The ID of the study selected to search
   * @param datatype - The type of data that will be observed
   * @return The list of StudyEntry Objects taken from the file metadata in the XML database
   */
  public ArrayList<StudyEntry> getEntries(String userID, String studyID, String datatype) {
    System.out.println("In function getEntries");

    ArrayList<StudyEntry> tempList = new ArrayList<StudyEntry>();

    try {

      // create first query to get the entire studyEntry block, the collection() XQuery function
      // does this across
      // all documents in the XML Collection

      //  The goal of this query is to find all the data we need for the StudyEntry object based on
      // which user submitted them.  For all the documents searched, the query looks to see
      //  if any files for that subject's ECG repository were submitted by that user.  If any files
      // were submitted, retrieve their metadata contained in the studyEntry block.
      // CompiledExpression query = subjectQuery.compile("for $x in collection('" +
      // allConstants.getDBCollection() + "')//record where collection('" +
      // allConstants.getDBCollection() + "')//record/studyEntry/submitterID=\"" + userID + "\"
      // order by $x/studyEntry/subjectID return $x/studyEntry");

      // ***
      // 02/25/2013 - Brandon Benitez:  Commented this out temporarily in case the query builder
      // version of this failed.
      // ***
      // String sQuery = "for $x in collection('" + dbHandle.getMainCollection() +
      // "')//record/studyEntry[studyID=\"" + studyID + "\"] where $x/submitterID=\"" + userID + "\"
      // return $x[datatype=\"" + datatype + "\"]";

      String sQuery =
          studyBuilder.forStudyEntry()
              +
              // studyBuilder.customNameBracket(studyID, EnumStudyTreeNode.STUDY) +
              studyBuilder.whereUser2(userID)
              + studyBuilder.andWhereStudy(studyID)
              + studyBuilder.customOrderBySubjectID()
              + studyBuilder.returnX()
              + studyBuilder.customNameBracket(datatype, EnumStudyTreeNode.DATATYPE);

      System.out.println("Query to be executed = " + sQuery);

      ResourceSet resultSet = executeQuery(sQuery);
      ResourceIterator iter = resultSet.getIterator();
      Resource selection = null;
      Resource fileSelection = null;
      int listIndex = 0;
      int subjectCount = 1;

      while (iter.hasMoreResources()) {
        selection = iter.nextResource();
        String studyEntryResult = (selection.getContent()).toString();

        // Now we will create an XStream object and then put that into our StudyEntry objects
        // the StudyEntry objects are de-serialized versions of the studyEntry blocks.
        XStream xmlStream = new XStream();

        xmlStream.alias("studyEntry", StudyEntry.class);
        xmlStream.alias("recordDetails", RecordDetails.class);
        xmlStream.alias("fileDetails", FileDetails.class);
        xmlStream.addImplicitCollection(RecordDetails.class, "fileDetails");

        StudyEntry newStudy = (StudyEntry) xmlStream.fromXML(studyEntryResult);

        // System.out.println(newStudy);

        // Add it to the return array
        tempList.add(newStudy);
      }
    } catch (Exception ex) {
      System.out.println(
          "StudyEntryUtility.getEntries():  AN EXCEPTION HAS BEEN CAUGHT!  IF A LIST IS RETURNED, IT WILL BE EMPTY!!!");
      ex.printStackTrace();
    }

    return tempList;
  }