Search in sources :

Example 46 with StudyParameterValueBean

use of org.akaza.openclinica.bean.service.StudyParameterValueBean in project OpenClinica by OpenClinica.

the class SubjectTransferValidator method validate.

@Override
public void validate(Object obj, Errors e) {
    SubjectTransferBean subjectTransferBean = (SubjectTransferBean) obj;
    if (subjectTransferBean.getStudyOid() == null) {
        e.reject("studyEventTransferValidator.study_does_not_exist");
        return;
    }
    Status[] included_status = new Status[] { Status.AVAILABLE };
    StudyBean study = helper.verifyStudy(getStudyDAO(), subjectTransferBean.getStudyOid(), included_status, e);
    if (study == null)
        return;
    StudyBean site = null;
    int site_id = -1;
    subjectTransferBean.setStudy(study);
    if (subjectTransferBean.getSiteIdentifier() != null) {
        site = helper.verifySite(getStudyDAO(), subjectTransferBean.getStudyOid(), subjectTransferBean.getSiteIdentifier(), included_status, e);
        if (site == null) {
            return;
        }
        site_id = site.getId();
        subjectTransferBean.setStudy(site);
    }
    boolean isRoleVerified = helper.verifyRole(subjectTransferBean.getOwner(), study.getId(), site_id, Role.MONITOR, e);
    if (!isRoleVerified) {
        return;
    }
    //        StudyBean study = null;
    //        if ( subjectTransferBean.getStudyOid() != null)
    //        {
    //	        study = getStudyDAO().findByUniqueIdentifier(subjectTransferBean.getStudyOid());
    //	        if (study == null) {
    //	            e.reject("subjectTransferValidator.study_does_not_exist", new Object[] { subjectTransferBean.getStudyOid() }, "Study identifier you specified "
    //	                + subjectTransferBean.getStudyOid() + " does not correspond to a valid study.");
    //	            return;
    //	        }
    //	        //validate study status
    //	        if (study != null && !study.getStatus().isAvailable()) {
    //	            e.reject("subjectTransferValidator.study_status_wrong", new Object[] { subjectTransferBean.getStudyOid() }, "Study "
    //	            		+ subjectTransferBean.getStudyOid() +" has wrong status. Subject can be added to an 'AVAILABLE' study only.");
    //	            return;
    //	        }
    //	        subjectTransferBean.setStudy(study);
    //        }
    //        UserAccountBean ua = subjectTransferBean.getOwner();
    //        StudyUserRoleBean role = ua.getRoleByStudy(subjectTransferBean.getStudy());
    //        if (role.getId() == 0 || role.getRole().equals(Role.MONITOR)) {
    //            e.reject("subjectTransferValidator.insufficient_permissions", "You do not have sufficient privileges to proceed with this operation.");
    //            return;
    //        }
    //        StudyBean site = null;
    //        if (subjectTransferBean.getSiteIdentifier() != null) {
    //            site = getStudyDAO().findSiteByUniqueIdentifier(subjectTransferBean.getStudyOid(), subjectTransferBean.getSiteIdentifier());
    // 	        if (site == null) {
    //	            e.reject("subjectTransferValidator.site_does_not_exist", new Object[] { subjectTransferBean.getSiteIdentifier() },
    //	                    "Site identifier you specified does not correspond to a valid site.");
    //	            return;
    //	        }
    //	        //validate site status
    //	        if ( ! site.getStatus().isAvailable()) {
    //	        
    //	            e.reject("subjectTransferValidator.site_status_wrong", new Object[] { subjectTransferBean.getSiteIdentifier() }, "Site "
    //	            		+ site.getName() +" has wrong status. Subject can be added to an 'AVAILABLE' site only.");
    //	            return;
    //	        }
    //	        subjectTransferBean.setStudy(site);//???????????
    //        }
    //        role = ua.getRoleByStudy(site);
    //        if (role.getId() == 0 || role.getRole().equals(Role.MONITOR)) {
    //            e.reject("subjectTransferValidator.insufficient_permissions", "You do not have sufficient privileges to proceed with this operation.");
    //            return;
    //        }
    //      
    int handleStudyId = study.getParentStudyId() > 0 ? study.getParentStudyId() : study.getId();
    StudyParameterValueBean studyParameter = getStudyParameterValueDAO().findByHandleAndStudy(handleStudyId, "subjectPersonIdRequired");
    String personId = subjectTransferBean.getPersonId();
    if (personId.contains("<") || personId.contains(">")) {
        e.reject("subjectTransferValidator.person_id_can_not_contain_html_lessthan_or_greaterthan_elements");
        return;
    }
    if ("required".equals(studyParameter.getValue()) && (personId == null || personId.length() < 1)) {
        e.reject("subjectTransferValidator.personId_required", new Object[] { study.getName() }, "personId is required for the study: " + study.getName());
        return;
    }
    if ("not used".equals(studyParameter.getValue()) && !(personId == null || personId.length() < 1)) {
        e.reject("subjectTransferValidator.personId_not_used", new Object[] { study.getName() }, "personId is not used for the study: " + study.getName());
        return;
    }
    if (personId != null && personId.length() > 255) {
        e.reject("subjectTransferValidator.personId_invalid_length", new Object[] { personId }, "personId: " + personId + " cannot be longer than 255 characters.");
        return;
    }
    // verify that personId is unique 
    if (subjectTransferBean.getPersonId() != null && subjectTransferBean.getPersonId().length() > 0) {
        //	         SubjectBean subjectWithSamePersonId = getSubjectDao().findByUniqueIdentifierAndStudy( subjectTransferBean.getPersonId(), study.getId());
        SubjectBean subjectWithSamePersonId = getSubjectDao().findByUniqueIdentifierAndAnyStudy(subjectTransferBean.getPersonId(), study.getId());
        if (subjectWithSamePersonId.getId() != 0) {
            e.reject("subjectTransferValidator.personId_duplicated", new Object[] { personId }, "A subject with the Person ID: " + personId + " is already enrolled in this study. ");
            return;
        }
    }
    StudyParameterValueBean subjectIdGenerationParameter = getStudyParameterValueDAO().findByHandleAndStudy(handleStudyId, "subjectIdGeneration");
    String idSetting = subjectIdGenerationParameter.getValue();
    if (!(idSetting.equals("auto editable") || idSetting.equals("auto non-editable"))) {
        String studySubjectId = subjectTransferBean.getStudySubjectId();
        if (studySubjectId == null || studySubjectId.length() < 1) {
            e.reject("subjectTransferValidator.studySubjectId_required");
            return;
        } else if (studySubjectId.length() > 30) {
            e.reject("subjectTransferValidator.studySubjectId_invalid_length", new Object[] { studySubjectId }, "studySubjectId: " + studySubjectId + " cannot be longer than 30 characters.");
            return;
        } else {
            // checks whether there is a subject with same id inside current study
            StudySubjectBean subjectWithSame = getStudySubjectDAO().findByLabelAndStudy(studySubjectId, study);
            if (subjectWithSame.getLabel().equals(studySubjectId)) {
                e.reject("subjectTransferValidator.subject_duplicated_label", new Object[] { studySubjectId, study.getIdentifier() }, "studySubjectId: " + studySubjectId + " already exists for " + study.getIdentifier() + " study .");
                return;
            }
        }
        if (studySubjectId.contains("<") || studySubjectId.contains(">")) {
            e.reject("subjectTransferValidator.study_subject_id_can_not_contain_html_lessthan_or_greaterthan_elements");
            return;
        }
    }
    String secondaryId = subjectTransferBean.getSecondaryId();
    if (secondaryId != null && secondaryId.length() > 30) {
        e.reject("subjectTransferValidator.secondaryId_invalid_length", new Object[] { secondaryId }, "secondaryId: " + secondaryId + " cannot be longer than 30 characters.");
        return;
    }
    if (secondaryId.contains("<") || secondaryId.contains(">")) {
        e.reject("subjectTransferValidator.secondary_id_can_not_contain_html_lessthan_or_greaterthan_elements");
        return;
    }
    String gender = String.valueOf(subjectTransferBean.getGender());
    studyParameter = getStudyParameterValueDAO().findByHandleAndStudy(handleStudyId, "genderRequired");
    if ("true".equals(studyParameter.getValue())) {
        if (gender == null || gender.length() < 1) {
            e.reject("subjectTransferValidator.gender_required", new Object[] { study.getName() }, "Gender is required for the study: " + study.getName());
            return;
        }
        if (!"m".equals(gender) && !"f".equals(gender)) {
            e.reject("subjectTransferValidator.gender_is_m_or_f");
            return;
        }
    } else {
        if (gender.trim().length() > 0 && !("m".equals(gender) || "f".equals(gender))) {
            e.reject("subjectTransferValidator.gender_is_m_or_f");
            return;
        }
    }
    Date dateOfBirth = subjectTransferBean.getDateOfBirth();
    String yearOfBirth = subjectTransferBean.getYearOfBirth();
    studyParameter = getStudyParameterValueDAO().findByHandleAndStudy(handleStudyId, "collectDob");
    if ("1".equals(studyParameter.getValue()) && (dateOfBirth == null)) {
        e.reject("subjectTransferValidator.dateOfBirth_required", new Object[] { study.getName() }, "Date of birth is required for the study " + study.getName());
        return;
    } else if ("2".equals(studyParameter.getValue()) && (yearOfBirth == null)) {
        e.reject("subjectTransferValidator.yearOfBirth_required", new Object[] { study.getName() }, "Year of birth is required for the study " + study.getName());
        return;
    } else if ("2".equals(studyParameter.getValue()) && (yearOfBirth != null)) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
            subjectTransferBean.setDateOfBirth(sdf.parse(subjectTransferBean.getYearOfBirth()));
        } catch (ParseException xe) {
            e.reject("subjectTransferValidator.yearOfBirth_invalid", new Object[] { yearOfBirth }, "Year of birth: " + yearOfBirth + " is not valid");
            return;
        }
    //not used
    } else if ("3".equals(studyParameter.getValue())) {
        if (dateOfBirth != null || !(yearOfBirth == null || yearOfBirth.length() < 1)) {
            e.reject("subjectTransferValidator.date_of_birth_notused", new Object[] { study.getName() }, "Date of Birth is not used for the study " + study.getName());
            return;
        }
    }
    Date enrollmentDate = subjectTransferBean.getEnrollmentDate();
    if (enrollmentDate == null) {
        e.reject("subjectTransferValidator.enrollmentDate_required");
        return;
    } else {
        if ((new Date()).compareTo(enrollmentDate) < 0) {
            e.reject("subjectTransferValidator.enrollmentDate_should_be_in_past");
            return;
        }
    }
}
Also used : Status(org.akaza.openclinica.bean.core.Status) StudyBean(org.akaza.openclinica.bean.managestudy.StudyBean) SubjectTransferBean(org.akaza.openclinica.bean.managestudy.SubjectTransferBean) Date(java.util.Date) SubjectBean(org.akaza.openclinica.bean.submit.SubjectBean) StudySubjectBean(org.akaza.openclinica.bean.managestudy.StudySubjectBean) StudyParameterValueBean(org.akaza.openclinica.bean.service.StudyParameterValueBean) StudySubjectBean(org.akaza.openclinica.bean.managestudy.StudySubjectBean) ParseException(java.text.ParseException) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

StudyParameterValueBean (org.akaza.openclinica.bean.service.StudyParameterValueBean)46 StudyBean (org.akaza.openclinica.bean.managestudy.StudyBean)31 StudyParameterValueDAO (org.akaza.openclinica.dao.service.StudyParameterValueDAO)27 StudyDAO (org.akaza.openclinica.dao.managestudy.StudyDAO)18 ArrayList (java.util.ArrayList)16 Date (java.util.Date)9 HashMap (java.util.HashMap)9 ParticipantPortalRegistrar (org.akaza.openclinica.service.pmanage.ParticipantPortalRegistrar)9 StudySubjectBean (org.akaza.openclinica.bean.managestudy.StudySubjectBean)6 FormProcessor (org.akaza.openclinica.control.form.FormProcessor)6 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)6 Iterator (java.util.Iterator)5 UserAccountBean (org.akaza.openclinica.bean.login.UserAccountBean)4 StudyParamsConfig (org.akaza.openclinica.bean.service.StudyParamsConfig)4 ParseException (java.text.ParseException)3 SimpleDateFormat (java.text.SimpleDateFormat)3 StudyEventDefinitionBean (org.akaza.openclinica.bean.managestudy.StudyEventDefinitionBean)3 StudyParameter (org.akaza.openclinica.bean.service.StudyParameter)3 RandomizationRegistrar (org.akaza.openclinica.service.pmanage.RandomizationRegistrar)3 IOException (java.io.IOException)2