private boolean mayProceedPreview(String studyOid) throws Exception {
    boolean accessPermission = false;
    StudyBean study = getParentStudy(studyOid);
    StudyParameterValueDAO spvdao = new StudyParameterValueDAO(dataSource);

    StudyParameterValueBean pStatus =
        spvdao.findByHandleAndStudy(study.getId(), "participantPortal");
    participantPortalRegistrar = new ParticipantPortalRegistrar();
    String pManageStatus =
        participantPortalRegistrar.getRegistrationStatus(studyOid).toString(); // ACTIVE ,
    // PENDING ,
    // INACTIVE
    String participateStatus = pStatus.getValue().toString(); // enabled , disabled
    String studyStatus =
        study.getStatus().getName().toString(); // available , pending , frozen , locked
    logger.info(
        "pManageStatus: "
            + pManageStatus
            + "  participantStatus: "
            + participateStatus
            + "   studyStatus: "
            + studyStatus);
    if (participateStatus.equalsIgnoreCase("enabled")
        && (studyStatus.equalsIgnoreCase("available")
            || studyStatus.equalsIgnoreCase("pending")
            || studyStatus.equalsIgnoreCase("frozen")
            || studyStatus.equalsIgnoreCase("locked"))
        && (pManageStatus.equalsIgnoreCase("ACTIVE")
            || pManageStatus.equalsIgnoreCase("PENDING")
            || pManageStatus.equalsIgnoreCase("INACTIVE"))) {
      accessPermission = true;
    }
    return accessPermission;
  }
 private void updateParameter(StudyParameterValueDAO spvdao, StudyParameterValueBean spv) {
   StudyParameterValueBean spv1 =
       spvdao.findByHandleAndStudy(spv.getStudyId(), spv.getParameter());
   if (spv1.getId() > 0) {
     spvdao.update(spv);
   } else {
     spvdao.create(spv);
   }
 }
 private void updateParameter(StudyParameterValueDAO spvdao, StudyParameterValueBean spv) {
   StudyParameterValueBean spv1 =
       spvdao.findByHandleAndStudy(spv.getStudyId(), spv.getParameter());
   System.out.println("found parameter: " + spv.getParameter());
   if (spv1.getId() > 0) {
     spvdao.update(spv);
     System.out.println("updating");
   } else {
     spvdao.create(spv);
     System.out.println("creating");
   }
 }
  /** Inserts the new study into database */
  private void submitStudy() {
    FormProcessor fp = new FormProcessor(request);
    StudyDAO sdao = new StudyDAO(sm.getDataSource());
    StudyBean study = (StudyBean) session.getAttribute("newStudy");

    ArrayList parameters = study.getStudyParameters();
    logger.info("study bean to be created:\n");
    logger.info(
        study.getName()
            + "\n"
            + study.getIdentifier()
            + "\n"
            + study.getParentStudyId()
            + "\n"
            + study.getSummary()
            + "\n"
            + study.getPrincipalInvestigator()
            + "\n"
            + study.getDatePlannedStart()
            + "\n"
            + study.getDatePlannedEnd()
            + "\n"
            + study.getFacilityName()
            + "\n"
            + study.getFacilityCity()
            + "\n"
            + study.getFacilityState()
            + "\n"
            + study.getFacilityZip()
            + "\n"
            + study.getFacilityCountry()
            + "\n"
            + study.getFacilityRecruitmentStatus()
            + "\n"
            + study.getFacilityContactName()
            + "\n"
            + study.getFacilityContactEmail()
            + "\n"
            + study.getFacilityContactPhone()
            + "\n"
            + study.getFacilityContactDegree());

    study.setOwner(ub);
    study.setCreatedDate(new Date());
    StudyBean parent = (StudyBean) sdao.findByPK(study.getParentStudyId());
    study.setType(parent.getType());
    // YW 10-10-2007, enable setting site status
    study.setStatus(study.getStatus());
    // YW >>

    study.setGenetic(parent.isGenetic());
    study = (StudyBean) sdao.create(study);

    StudyParameterValueDAO spvdao = new StudyParameterValueDAO(sm.getDataSource());
    for (int i = 0; i < parameters.size(); i++) {
      StudyParamsConfig config = (StudyParamsConfig) parameters.get(i);
      StudyParameterValueBean spv = config.getValue();
      spv.setStudyId(study.getId());
      spv = (StudyParameterValueBean) spvdao.create(config.getValue());
    }

    // YW << here only "collectDob" and "genderRequired" have been corrected
    // for sites.
    StudyParameterValueBean spv = new StudyParameterValueBean();
    StudyParameterValueBean parentSPV = spvdao.findByHandleAndStudy(parent.getId(), "collectDob");
    spv.setStudyId(study.getId());
    spv.setParameter("collectDob");
    spv.setValue(parentSPV.getValue());
    spvdao.create(spv);

    parentSPV = spvdao.findByHandleAndStudy(parent.getId(), "genderRequired");
    spv.setParameter("genderRequired");
    spv.setValue(parentSPV.getValue());
    spvdao.create(spv);
    // YW >>

    // switch user to the newly created site
    session.setAttribute("study", session.getAttribute("newStudy"));
    currentStudy = (StudyBean) session.getAttribute("study");

    session.removeAttribute("newStudy");
    addPageMessage(respage.getString("the_new_site_created_succesfully_current"));
    forwardPage(Page.SITE_LIST_SERVLET);
  }
  private void collectMetaDataVersion() {
    ArrayList<StudyEventDefinitionBean> sedBeansInStudy =
        (ArrayList<StudyEventDefinitionBean>) studyBase.getSedBeansInStudy();
    if (sedBeansInStudy == null || sedBeansInStudy.size() < 1) {
      logger.info("null, because there is no study event definition in this study.");
      return;
    }

    StudyBean study = studyBase.getStudy();
    MetaDataVersionBean metadata = this.odmStudy.getMetaDataVersion();

    StudyParameterValueDAO spvdao = new StudyParameterValueDAO(this.ds);
    int parentId = study.getParentStudyId() > 0 ? study.getParentStudyId() : study.getId();
    StudyParameterValueBean spv = spvdao.findByHandleAndStudy(parentId, "discrepancyManagement");
    metadata.setSoftHard(spv.getValue().equalsIgnoreCase("true") ? "Hard" : "Soft");

    OdmExtractDAO oedao = new OdmExtractDAO(this.ds);
    int studyId = study.getId();
    int parentStudyId = study.getParentStudyId() > 0 ? study.getParentStudyId() : studyId;
    if (this.getCategory() == 1 && study.isSite(study.getParentStudyId())) {
      // populate MetaDataVersion attributes
      if (dataset != null) {
        metadata.setOid(dataset.getODMMetaDataVersionOid() + "-" + study.getOid());
        metadata.setName(dataset.getODMMetaDataVersionName() + "-" + study.getOid());
        this.setParentMetaDataVersionOid(dataset.getODMMetaDataVersionOid());
      }
      if (metadata.getOid() == null || metadata.getOid().length() <= 0) {
        metadata.setOid("v1.0.0" + "-" + study.getOid());
        this.setParentMetaDataVersionOid("v1.0.0");
      }
      if (metadata.getName() == null || metadata.getName().length() <= 0) {
        metadata.setName("MetaDataVersion_v1.0.0" + "-" + study.getOid());
      }

      // populate Include
      this.collectIncludeFromParentInSameFile();

      // populate protocol
      oedao.getUpdatedSiteMetadata(parentStudyId, studyId, metadata, this.odmBean.getODMVersion());
    } else {
      if (dataset != null) {
        metadata.setOid(dataset.getODMMetaDataVersionOid());
        metadata.setName(dataset.getODMMetaDataVersionName());
      }
      if (metadata.getOid() == null || metadata.getOid().length() <= 0) {
        metadata.setOid("v1.0.0");
      }
      if (metadata.getName() == null || metadata.getName().length() <= 0) {
        metadata.setName("MetaDataVersion_v1.0.0");
      }

      // populate Include
      String psOid = new String();
      String pmOid = new String();
      if (dataset != null) {
        psOid = dataset.getODMPriorStudyOid();
        pmOid = dataset.getODMPriorMetaDataVersionOid();
      }
      if (pmOid != null && pmOid.length() > 0) {
        MetaDataVersionIncludeBean ib = metadata.getInclude();
        ib.setMetaDataVersionOID(pmOid);
        if (psOid != null && psOid.length() > 0) {
          ib.setStudyOID(psOid);
        } else {
          ib.setStudyOID(study.getOid());
        }
      }

      // populate protocol
      // Set<Integer> nullCodeSet = oedao.getMetadata(parentStudyId,
      // studyId,
      // metadata, this.getODMBean().getODMVersion());
      // studyBase.setNullClSet(nullCodeSet);
      oedao.getMetadata(parentStudyId, studyId, metadata, this.odmBean.getODMVersion());
    }
  }
  public boolean validate(SubjectTransferBean subjectTransferBean)
      throws OpenClinicaSystemException {

    StudyDAO stdao = new StudyDAO(this.getDataSource());
    StudyBean study = stdao.findByUniqueIdentifier(subjectTransferBean.getStudyOid());
    if (study == null) {
      throw new OpenClinicaSystemException("Study you specified does not exist");
    }

    UserAccountBean ua = subjectTransferBean.getOwner();
    StudyUserRoleBean role = ua.getRoleByStudy(study);
    if (role.getId() == 0 || role.getRole().equals(Role.MONITOR)) {
      throw new OpenClinicaSystemException(
          "You do not have sufficient priviliges to run this service");
    }

    if (subjectTransferBean.getSiteIdentifier() != null) {
      study =
          stdao.findSiteByUniqueIdentifier(
              subjectTransferBean.getStudyOid(), subjectTransferBean.getSiteIdentifier());
    }
    subjectTransferBean.setStudy(study);
    if (study == null) {
      throw new OpenClinicaSystemException("Site you specified does not exist");
    }
    int handleStudyId = study.getParentStudyId() > 0 ? study.getParentStudyId() : study.getId();
    org.akaza.openclinica.dao.service.StudyParameterValueDAO spvdao =
        new StudyParameterValueDAO(this.getDataSource());
    StudyParameterValueBean studyParameter =
        spvdao.findByHandleAndStudy(handleStudyId, "subjectPersonIdRequired");
    String personId = subjectTransferBean.getPersonId();
    if ("required".equals(studyParameter.getValue())
        && (personId == null || personId.length() < 1)) {
      throw new OpenClinicaSystemException(
          "personId is required for the study: " + study.getName());
      // return false;
    }

    if (personId != null && personId.length() > 255) {
      throw new OpenClinicaSystemException("personId should not be longer than 255.");
      // return false;
    }

    String idSetting = "";
    StudyParameterValueBean subjectIdGenerationParameter =
        spvdao.findByHandleAndStudy(handleStudyId, "subjectIdGeneration");
    idSetting = subjectIdGenerationParameter.getValue();
    if (idSetting.equals("auto editable") || idSetting.equals("auto non-editable")) {
      int nextLabel = getStudySubjectDao().findTheGreatestLabel() + 1;
      subjectTransferBean.setStudySubjectId(new Integer(nextLabel).toString());
    }
    String studySubjectId = subjectTransferBean.getStudySubjectId();
    if (studySubjectId == null || studySubjectId.length() < 1) {
      logger.info("studySubjectId is required.");
      throw new OpenClinicaSystemException("studySubjectId is required.");
      // return false;
    } else if (studySubjectId.length() > 30) {
      throw new OpenClinicaSystemException("studySubjectId should not be longer than 30.");
      // return false;
    }

    String secondaryId = subjectTransferBean.getSecondaryId();
    if (secondaryId != null && secondaryId.length() > 30) {
      throw new OpenClinicaSystemException("secondaryId should not be longer than 30.");
      // return false;
    }
    String gender = subjectTransferBean.getGender() + "";
    studyParameter = spvdao.findByHandleAndStudy(handleStudyId, "genderRequired");
    if ("true".equals(studyParameter.getValue()) && (gender == null || gender.length() < 1)) {
      throw new OpenClinicaSystemException("gender is required for the study:" + study.getName());
      // return false;
    }

    Date dateOfBirth = subjectTransferBean.getDateOfBirth();
    String yearOfBirth = subjectTransferBean.getYearOfBirth();
    studyParameter = spvdao.findByHandleAndStudy(handleStudyId, "collectDob");
    if ("1".equals(studyParameter.getValue()) && (dateOfBirth == null)) {
      throw new OpenClinicaSystemException("date Of Birth is required:" + study.getName());
      // return false;
    } else if ("2".equals(studyParameter.getValue()) && (yearOfBirth == null)) {
      throw new OpenClinicaSystemException("Year Of Birth is required:" + study.getName());
    } else if ("2".equals(studyParameter.getValue()) && (yearOfBirth != null)) {
      try {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
        subjectTransferBean.setDateOfBirth(sdf.parse(subjectTransferBean.getYearOfBirth()));
      } catch (ParseException e) {
        throw new OpenClinicaSystemException("Year Of Birth not Valid:" + study.getName());
      }
    }

    Date enrollmentDate = subjectTransferBean.getEnrollmentDate();
    if (enrollmentDate == null) {
      throw new OpenClinicaSystemException("enrollmentDate is required.");
      // return false;
    } else {
      if ((new Date()).compareTo(enrollmentDate) < 0) {
        throw new OpenClinicaSystemException("enrollmentDate should be in the past.");
        // return false;
      }
    }

    return true;
  }