private UserAccountBean getUserAccount() {
   Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
   String username = null;
   if (principal instanceof UserDetails) {
     username = ((UserDetails) principal).getUsername();
   } else {
     username = principal.toString();
   }
   UserAccountDAO userAccountDao = new UserAccountDAO(dataSource);
   return (UserAccountBean) userAccountDao.findByUserName(username);
 }
  @RequestMapping(value = "/studies/{study}/metadata", method = RequestMethod.GET)
  public ModelAndView studyMetadata(
      Model model,
      HttpSession session,
      @PathVariable("study") String studyOid,
      HttpServletResponse response)
      throws Exception {
    ResourceBundleProvider.updateLocale(new Locale("en_US"));
    StudyBean currentStudy = (StudyBean) session.getAttribute("study");
    UserAccountBean userAccount = (UserAccountBean) session.getAttribute("userBean");

    UserAccountDAO userAccountDao = new UserAccountDAO(dataSource);
    userAccount = (UserAccountBean) userAccountDao.findByUserName("root");

    StudyDAO studyDao = new StudyDAO(dataSource);
    currentStudy = studyDao.findByOid(studyOid);

    MetaDataCollector mdc = new MetaDataCollector(dataSource, currentStudy, getRuleSetRuleDao());
    AdminDataCollector adc = new AdminDataCollector(dataSource, currentStudy);
    // RulesDataCollector rdc = new RulesDataCollector(sm.getDataSource(),
    // currentStudy,getRuleSetRuleDao());
    MetaDataCollector.setTextLength(200);

    ODMBean odmb = mdc.getODMBean();
    odmb.setSchemaLocation("http://www.cdisc.org/ns/odm/v1.3 OpenClinica-ODM1-3-0-OC2-0.xsd");
    ArrayList<String> xmlnsList = new ArrayList<String>();
    xmlnsList.add("xmlns=\"http://www.cdisc.org/ns/odm/v1.3\"");
    // xmlnsList.add("xmlns:OpenClinica=\"http://www.openclinica.org/ns/openclinica_odm/v1.3\"");
    xmlnsList.add("xmlns:OpenClinica=\"http://www.openclinica.org/ns/odm_ext_v130/v3.1\"");
    xmlnsList.add("xmlns:OpenClinicaRules=\"http://www.openclinica.org/ns/rules/v3.1\"");
    odmb.setXmlnsList(xmlnsList);
    odmb.setODMVersion("oc1.3");
    mdc.setODMBean(odmb);
    adc.setOdmbean(odmb);
    // rdc.setOdmbean(odmb);

    mdc.collectFileData();
    MetaDataReportBean metaReport = new MetaDataReportBean(mdc.getOdmStudyMap(), coreResources);
    metaReport.setODMVersion("oc1.3");
    metaReport.setOdmBean(mdc.getODMBean());
    metaReport.createChunkedOdmXml(Boolean.FALSE);

    adc.collectFileData();
    AdminDataReportBean adminReport = new AdminDataReportBean(adc.getOdmAdminDataMap());
    adminReport.setODMVersion("oc1.3");
    adminReport.setOdmBean(mdc.getODMBean());
    adminReport.createChunkedOdmXml(Boolean.FALSE);

    // rdc.collectFileData();
    // RulesDataReportBean rulesReport = new
    // RulesDataReportBean(rdc.getOdmRulesDataMap(),getCoreResources());
    // rulesReport.setODMVersion("oc1.3");
    // rulesReport.setOdmBean(mdc.getODMBean());
    // rulesReport.createChunkedOdmXml(Boolean.FALSE);

    FullReportBean report = new FullReportBean();
    report.setAdminDataMap(adc.getOdmAdminDataMap());
    report.setOdmStudyMap(mdc.getOdmStudyMap());
    report.setCoreResources(coreResources);
    // report.setRulesDataMap(rdc.getOdmRulesDataMap());
    report.setOdmBean(mdc.getODMBean());
    report.setODMVersion("oc1.3");
    report.createStudyMetaOdmXml(Boolean.FALSE);

    response.setContentType("application/xml");
    PrintWriter out = response.getWriter();
    out.print(report.getXmlOutput().toString().trim());

    return null;
  }
  @Override
  public void processRequest() throws Exception {
    UserAccountDAO udao = new UserAccountDAO(sm.getDataSource());
    StudyDAO sdao = new StudyDAO(sm.getDataSource());
    String name = request.getParameter("name");
    String studyIdString = request.getParameter("studyId");
    if (StringUtil.isBlank(name) || StringUtil.isBlank(studyIdString)) {
      addPageMessage(respage.getString("please_choose_a_user_to_set_role_for"));
      forwardPage(Page.LIST_USER_IN_STUDY_SERVLET);
    } else {
      String action = request.getParameter("action");
      FormProcessor fp = new FormProcessor(request);
      UserAccountBean user = (UserAccountBean) udao.findByUserName(name);
      StudyBean userStudy = (StudyBean) sdao.findByPK(fp.getInt("studyId"));
      if ("confirm".equalsIgnoreCase(action)) {
        int studyId = Integer.valueOf(studyIdString.trim()).intValue();

        request.setAttribute("user", user);

        StudyUserRoleBean uRole = udao.findRoleByUserNameAndStudyId(name, studyId);
        uRole.setStudyName(userStudy.getName());
        request.setAttribute("uRole", uRole);

        ArrayList roles = Role.toArrayList();
        roles.remove(Role.ADMIN); // admin is not a user role, only used for tomcat

        StudyBean studyBean = (StudyBean) sdao.findByPK(uRole.getStudyId());

        if (currentStudy.getParentStudyId() > 0) {
          roles.remove(Role.COORDINATOR);
          roles.remove(Role.STUDYDIRECTOR);
        } else if (studyBean.getParentStudyId() > 0) {
          roles.remove(Role.COORDINATOR);
          roles.remove(Role.STUDYDIRECTOR);
          // TODO: redo this fix
          Role r = Role.RESEARCHASSISTANT;
          r.setDescription("site_Data_Entry_Person");
          roles.remove(Role.RESEARCHASSISTANT);
          roles.add(r);
          Role ri = Role.INVESTIGATOR;
          ri.setDescription("site_investigator");
          roles.remove(Role.INVESTIGATOR);

          roles.add(ri);
        }
        request.setAttribute("roles", roles);

        forwardPage(Page.SET_USER_ROLE_IN_STUDY);
      } else {
        // set role

        String userName = fp.getString("name");
        int studyId = fp.getInt("studyId");
        int roleId = fp.getInt("roleId");
        StudyUserRoleBean sur = new StudyUserRoleBean();
        sur.setName(userName);
        sur.setRole(Role.get(roleId));
        sur.setStudyId(studyId);
        sur.setStudyName(userStudy.getName());
        sur.setStatus(Status.AVAILABLE);
        sur.setUpdater(ub);
        sur.setUpdatedDate(new Date());
        udao.updateStudyUserRole(sur, userName);
        addPageMessage(sendEmail(user, sur));
        forwardPage(Page.LIST_USER_IN_STUDY_SERVLET);
      }
    }
  }