Search in sources :

Example 1 with StudySubjectService

use of org.akaza.openclinica.service.managestudy.StudySubjectService in project OpenClinica by OpenClinica.

the class ViewStudySubjectServlet method processRequest.

@Override
public void processRequest() throws Exception {
    SubjectDAO sdao = new SubjectDAO(sm.getDataSource());
    StudySubjectDAO subdao = new StudySubjectDAO(sm.getDataSource());
    FormProcessor fp = new FormProcessor(request);
    // studySubjectId
    int studySubId = fp.getInt("id", true);
    String from = fp.getString("from");
    String module = fp.getString(MODULE);
    request.setAttribute(MODULE, module);
    // if coming from change crf version -> display message
    String crfVersionChangeMsg = fp.getString("isFromCRFVersionChange");
    if (crfVersionChangeMsg != null && !crfVersionChangeMsg.equals("")) {
        addPageMessage(crfVersionChangeMsg);
    }
    if (studySubId == 0) {
        addPageMessage(respage.getString("please_choose_a_subject_to_view"));
        forwardPage(Page.LIST_STUDY_SUBJECTS);
    } else {
        if (!StringUtil.isBlank(from)) {
            // form ListSubject or
            request.setAttribute("from", from);
        // ListStudySubject
        } else {
            request.setAttribute("from", "");
        }
        StudySubjectBean studySub = (StudySubjectBean) subdao.findByPK(studySubId);
        request.setAttribute("studySub", studySub);
        int studyId = studySub.getStudyId();
        int subjectId = studySub.getSubjectId();
        StudyDAO studydao = new StudyDAO(sm.getDataSource());
        StudyBean study = (StudyBean) studydao.findByPK(studyId);
        // Check if this StudySubject would be accessed from the Current Study
        if (studySub.getStudyId() != currentStudy.getId()) {
            if (currentStudy.getParentStudyId() > 0) {
                addPageMessage(respage.getString("no_have_correct_privilege_current_study") + " " + respage.getString("change_active_study_or_contact"));
                forwardPage(Page.MENU_SERVLET);
                return;
            } else {
                // The SubjectStudy is not belong to currentstudy and current study is not a site.
                Collection sites = studydao.findOlnySiteIdsByStudy(currentStudy);
                if (!sites.contains(study.getId())) {
                    addPageMessage(respage.getString("no_have_correct_privilege_current_study") + " " + respage.getString("change_active_study_or_contact"));
                    forwardPage(Page.MENU_SERVLET);
                    return;
                }
            }
        }
        // If the study subject derives from a site, and is being viewed
        // from a parent study,
        // then the study IDs will be different. However, since each note is
        // saved with the specific
        // study ID, then its study ID may be different than the study
        // subject's ID.
        boolean subjectStudyIsCurrentStudy = studyId == currentStudy.getId();
        boolean isParentStudy = study.getParentStudyId() < 1;
        // Get any disc notes for this subject : studySubId
        DiscrepancyNoteDAO discrepancyNoteDAO = new DiscrepancyNoteDAO(sm.getDataSource());
        List<DiscrepancyNoteBean> allNotesforSubject = new ArrayList<DiscrepancyNoteBean>();
        // These methods return only parent disc notes
        if (subjectStudyIsCurrentStudy && isParentStudy) {
            allNotesforSubject = discrepancyNoteDAO.findAllSubjectByStudyAndId(study, subjectId);
            allNotesforSubject.addAll(discrepancyNoteDAO.findAllStudySubjectByStudyAndId(study, studySubId));
        } else {
            if (!isParentStudy) {
                StudyBean stParent = (StudyBean) studydao.findByPK(study.getParentStudyId());
                allNotesforSubject = discrepancyNoteDAO.findAllSubjectByStudiesAndSubjectId(stParent, study, subjectId);
                allNotesforSubject.addAll(discrepancyNoteDAO.findAllStudySubjectByStudiesAndStudySubjectId(stParent, study, studySubId));
            } else {
                allNotesforSubject = discrepancyNoteDAO.findAllSubjectByStudiesAndSubjectId(currentStudy, study, subjectId);
                allNotesforSubject.addAll(discrepancyNoteDAO.findAllStudySubjectByStudiesAndStudySubjectId(currentStudy, study, studySubId));
            }
        }
        if (!allNotesforSubject.isEmpty()) {
            setRequestAttributesForNotes(allNotesforSubject);
        }
        SubjectBean subject = (SubjectBean) sdao.findByPK(subjectId);
        if (currentStudy.getStudyParameterConfig().getCollectDob().equals("2")) {
            Date dob = subject.getDateOfBirth();
            if (dob != null) {
                Calendar cal = Calendar.getInstance();
                cal.setTime(dob);
                int year = cal.get(Calendar.YEAR);
                request.setAttribute("yearOfBirth", new Integer(year));
            } else {
                request.setAttribute("yearOfBirth", "");
            }
        }
        request.setAttribute("subject", subject);
        /*
             * StudyDAO studydao = new StudyDAO(sm.getDataSource()); StudyBean
             * study = (StudyBean) studydao.findByPK(studyId);
             */
        // YW 11-26-2007 <<
        StudyParameterValueDAO spvdao = new StudyParameterValueDAO(sm.getDataSource());
        study.getStudyParameterConfig().setCollectDob(spvdao.findByHandleAndStudy(studyId, "collectDob").getValue());
        // YW >>
        request.setAttribute("subjectStudy", study);
        if (study.getParentStudyId() > 0) {
            // this is a site,find parent
            StudyBean parentStudy2 = (StudyBean) studydao.findByPK(study.getParentStudyId());
            request.setAttribute("parentStudy", parentStudy2);
        } else {
            request.setAttribute("parentStudy", new StudyBean());
        }
        ArrayList children = (ArrayList) sdao.findAllChildrenByPK(subjectId);
        request.setAttribute("children", children);
        // find study events
        StudyEventDAO sedao = new StudyEventDAO(sm.getDataSource());
        StudyEventDefinitionDAO seddao = new StudyEventDefinitionDAO(sm.getDataSource());
        StudySubjectService studySubjectService = (StudySubjectService) WebApplicationContextUtils.getWebApplicationContext(getServletContext()).getBean("studySubjectService");
        List<DisplayStudyEventBean> displayEvents = studySubjectService.getDisplayStudyEventsForStudySubject(studySub, ub, currentRole);
        // Mantis Issue 5048: Preventing Investigators from Unlocking Events
        for (int i = 0; i < displayEvents.size(); i++) {
            DisplayStudyEventBean decb = displayEvents.get(i);
            if (!(currentRole.isDirector() || currentRole.isCoordinator()) && decb.getStudyEvent().getSubjectEventStatus().isLocked()) {
                decb.getStudyEvent().setEditable(false);
            }
        }
        // BWP 3212; remove event CRFs that are supposed to be "hidden" >>
        if (currentStudy.getParentStudyId() > 0) {
            HideCRFManager hideCRFManager = HideCRFManager.createHideCRFManager();
            for (DisplayStudyEventBean displayStudyEventBean : displayEvents) {
                hideCRFManager.removeHiddenEventCRF(displayStudyEventBean);
            }
        }
        // >>
        EntityBeanTable table = fp.getEntityBeanTable();
        // sort by start
        table.setSortingIfNotExplicitlySet(1, false);
        // date, desc
        ArrayList allEventRows = DisplayStudyEventRow.generateRowsFromBeans(displayEvents);
        String[] columns = { resword.getString("event") + " (" + resword.getString("occurrence_number") + ")", resword.getString("start_date1"), resword.getString("location"), resword.getString("status"), resword.getString("actions"), resword.getString("CRFs_atrib") };
        table.setColumns(new ArrayList(Arrays.asList(columns)));
        table.hideColumnLink(4);
        table.hideColumnLink(5);
        // YW 11-08-2007 <<
        if (!"removed".equalsIgnoreCase(studySub.getStatus().getName()) && !"auto-removed".equalsIgnoreCase(studySub.getStatus().getName())) {
            if (currentStudy.getStatus().isAvailable() && !currentRole.getRole().equals(Role.MONITOR)) {
                table.addLink(resword.getString("add_new_event"), "CreateNewStudyEvent?" + CreateNewStudyEventServlet.INPUT_STUDY_SUBJECT_ID_FROM_VIEWSUBJECT + "=" + studySub.getId());
            }
        }
        // YW >>
        HashMap args = new HashMap();
        args.put("id", new Integer(studySubId).toString());
        table.setQuery("ViewStudySubject", args);
        table.setRows(allEventRows);
        table.computeDisplay();
        request.setAttribute("table", table);
        // request.setAttribute("displayEvents", displayEvents);
        // find group info
        SubjectGroupMapDAO sgmdao = new SubjectGroupMapDAO(sm.getDataSource());
        ArrayList groupMaps = (ArrayList) sgmdao.findAllByStudySubject(studySubId);
        request.setAttribute("groups", groupMaps);
        // find audit log for events
        AuditEventDAO aedao = new AuditEventDAO(sm.getDataSource());
        ArrayList logs = aedao.findEventStatusLogByStudySubject(studySubId);
        // logger.warning("^^^ retrieved logs");
        UserAccountDAO udao = new UserAccountDAO(sm.getDataSource());
        ArrayList eventLogs = new ArrayList();
        // logger.warning("^^^ starting to iterate");
        for (int i = 0; i < logs.size(); i++) {
            AuditEventBean avb = (AuditEventBean) logs.get(i);
            StudyEventAuditBean sea = new StudyEventAuditBean();
            sea.setAuditEvent(avb);
            StudyEventBean se = (StudyEventBean) sedao.findByPK(avb.getEntityId());
            StudyEventDefinitionBean sed = (StudyEventDefinitionBean) seddao.findByPK(se.getStudyEventDefinitionId());
            sea.setDefinition(sed);
            String old = avb.getOldValue().trim();
            try {
                if (!StringUtil.isBlank(old)) {
                    SubjectEventStatus oldStatus = SubjectEventStatus.get(new Integer(old).intValue());
                    sea.setOldSubjectEventStatus(oldStatus);
                }
                String newValue = avb.getNewValue().trim();
                if (!StringUtil.isBlank(newValue)) {
                    SubjectEventStatus newStatus = SubjectEventStatus.get(new Integer(newValue).intValue());
                    sea.setNewSubjectEventStatus(newStatus);
                }
            } catch (NumberFormatException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            // logger.warning("^^^ caught NFE");
            }
            UserAccountBean updater = (UserAccountBean) udao.findByPK(avb.getUserId());
            sea.setUpdater(updater);
            eventLogs.add(sea);
        }
        // logger.warning("^^^ finished iteration");
        request.setAttribute("eventLogs", eventLogs);
        forwardPage(Page.VIEW_STUDY_SUBJECT);
    }
}
Also used : HashMap(java.util.HashMap) StudySubjectDAO(org.akaza.openclinica.dao.managestudy.StudySubjectDAO) SubjectDAO(org.akaza.openclinica.dao.submit.SubjectDAO) EntityBeanTable(org.akaza.openclinica.web.bean.EntityBeanTable) ArrayList(java.util.ArrayList) StudyEventDefinitionBean(org.akaza.openclinica.bean.managestudy.StudyEventDefinitionBean) StudyEventBean(org.akaza.openclinica.bean.managestudy.StudyEventBean) DisplayStudyEventBean(org.akaza.openclinica.bean.managestudy.DisplayStudyEventBean) SubjectEventStatus(org.akaza.openclinica.bean.core.SubjectEventStatus) StudyEventDAO(org.akaza.openclinica.dao.managestudy.StudyEventDAO) UserAccountBean(org.akaza.openclinica.bean.login.UserAccountBean) AuditEventBean(org.akaza.openclinica.bean.admin.AuditEventBean) StudyEventAuditBean(org.akaza.openclinica.bean.admin.StudyEventAuditBean) StudyDAO(org.akaza.openclinica.dao.managestudy.StudyDAO) DiscrepancyNoteDAO(org.akaza.openclinica.dao.managestudy.DiscrepancyNoteDAO) SubjectGroupMapDAO(org.akaza.openclinica.dao.submit.SubjectGroupMapDAO) FormProcessor(org.akaza.openclinica.control.form.FormProcessor) StudyBean(org.akaza.openclinica.bean.managestudy.StudyBean) Calendar(java.util.Calendar) AuditEventDAO(org.akaza.openclinica.dao.admin.AuditEventDAO) StudySubjectDAO(org.akaza.openclinica.dao.managestudy.StudySubjectDAO) HideCRFManager(org.akaza.openclinica.service.crfdata.HideCRFManager) UserAccountDAO(org.akaza.openclinica.dao.login.UserAccountDAO) Date(java.util.Date) StudySubjectService(org.akaza.openclinica.service.managestudy.StudySubjectService) SubjectBean(org.akaza.openclinica.bean.submit.SubjectBean) StudySubjectBean(org.akaza.openclinica.bean.managestudy.StudySubjectBean) DisplayStudyEventBean(org.akaza.openclinica.bean.managestudy.DisplayStudyEventBean) StudySubjectBean(org.akaza.openclinica.bean.managestudy.StudySubjectBean) StudyEventDefinitionDAO(org.akaza.openclinica.dao.managestudy.StudyEventDefinitionDAO) DiscrepancyNoteBean(org.akaza.openclinica.bean.managestudy.DiscrepancyNoteBean) Collection(java.util.Collection) StudyParameterValueDAO(org.akaza.openclinica.dao.service.StudyParameterValueDAO)

Aggregations

ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Collection (java.util.Collection)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 AuditEventBean (org.akaza.openclinica.bean.admin.AuditEventBean)1 StudyEventAuditBean (org.akaza.openclinica.bean.admin.StudyEventAuditBean)1 SubjectEventStatus (org.akaza.openclinica.bean.core.SubjectEventStatus)1 UserAccountBean (org.akaza.openclinica.bean.login.UserAccountBean)1 DiscrepancyNoteBean (org.akaza.openclinica.bean.managestudy.DiscrepancyNoteBean)1 DisplayStudyEventBean (org.akaza.openclinica.bean.managestudy.DisplayStudyEventBean)1 StudyBean (org.akaza.openclinica.bean.managestudy.StudyBean)1 StudyEventBean (org.akaza.openclinica.bean.managestudy.StudyEventBean)1 StudyEventDefinitionBean (org.akaza.openclinica.bean.managestudy.StudyEventDefinitionBean)1 StudySubjectBean (org.akaza.openclinica.bean.managestudy.StudySubjectBean)1 SubjectBean (org.akaza.openclinica.bean.submit.SubjectBean)1 FormProcessor (org.akaza.openclinica.control.form.FormProcessor)1 AuditEventDAO (org.akaza.openclinica.dao.admin.AuditEventDAO)1 UserAccountDAO (org.akaza.openclinica.dao.login.UserAccountDAO)1 DiscrepancyNoteDAO (org.akaza.openclinica.dao.managestudy.DiscrepancyNoteDAO)1