Search in sources :

Example 71 with StudySubjectDAO

use of org.akaza.openclinica.dao.managestudy.StudySubjectDAO in project OpenClinica by OpenClinica.

the class DataImportService method createDiscrepancyNote.

public DiscrepancyNoteBean createDiscrepancyNote(ItemBean itemBean, String message, EventCRFBean eventCrfBean, DisplayItemBean displayItemBean, Integer parentId, UserAccountBean uab, DataSource ds, StudyBean study) {
    DiscrepancyNoteBean note = new DiscrepancyNoteBean();
    StudySubjectDAO ssdao = new StudySubjectDAO(ds);
    note.setDescription(message);
    note.setDetailedNotes("Failed Validation Check");
    note.setOwner(uab);
    note.setCreatedDate(new Date());
    note.setResolutionStatusId(ResolutionStatus.OPEN.getId());
    note.setDiscrepancyNoteTypeId(DiscrepancyNoteType.FAILEDVAL.getId());
    if (parentId != null) {
        note.setParentDnId(parentId);
    }
    note.setField(itemBean.getName());
    note.setStudyId(study.getId());
    note.setEntityName(itemBean.getName());
    note.setEntityType("ItemData");
    note.setEntityValue(displayItemBean.getData().getValue());
    note.setEventName(eventCrfBean.getName());
    note.setEventStart(eventCrfBean.getCreatedDate());
    note.setCrfName(displayItemBean.getEventDefinitionCRF().getCrfName());
    StudySubjectBean ss = (StudySubjectBean) ssdao.findByPK(eventCrfBean.getStudySubjectId());
    note.setSubjectName(ss.getName());
    note.setEntityId(displayItemBean.getData().getId());
    note.setColumn("value");
    DiscrepancyNoteDAO dndao = new DiscrepancyNoteDAO(ds);
    note = (DiscrepancyNoteBean) dndao.create(note);
    // so that the below method works, need to set the entity above
    logger.debug("trying to create mapping with " + note.getId() + " " + note.getEntityId() + " " + note.getColumn() + " " + note.getEntityType());
    dndao.createMapping(note);
    logger.debug("just created mapping");
    return note;
}
Also used : DiscrepancyNoteDAO(org.akaza.openclinica.dao.managestudy.DiscrepancyNoteDAO) StudySubjectBean(org.akaza.openclinica.bean.managestudy.StudySubjectBean) DiscrepancyNoteBean(org.akaza.openclinica.bean.managestudy.DiscrepancyNoteBean) StudySubjectDAO(org.akaza.openclinica.dao.managestudy.StudySubjectDAO) Date(java.util.Date)

Example 72 with StudySubjectDAO

use of org.akaza.openclinica.dao.managestudy.StudySubjectDAO in project OpenClinica by OpenClinica.

the class ImportCRFDataService method lookupValidationErrors.

public List<DisplayItemBeanWrapper> lookupValidationErrors(HttpServletRequest request, ODMContainer odmContainer, UserAccountBean ub, HashMap<String, String> totalValidationErrors, HashMap<String, String> hardValidationErrors, ArrayList<Integer> permittedEventCRFIds) throws OpenClinicaException {
    DisplayItemBeanWrapper displayItemBeanWrapper = null;
    HashMap validationErrors = new HashMap();
    List<DisplayItemBeanWrapper> wrappers = new ArrayList<DisplayItemBeanWrapper>();
    ImportHelper importHelper = new ImportHelper();
    FormDiscrepancyNotes discNotes = new FormDiscrepancyNotes();
    DiscrepancyValidator discValidator = new DiscrepancyValidator(request, discNotes);
    // create a second Validator, this one for hard edit checks
    HashMap<String, String> hardValidator = new HashMap<String, String>();
    StudyEventDAO studyEventDAO = new StudyEventDAO(ds);
    StudyDAO studyDAO = new StudyDAO(ds);
    StudyBean studyBean = studyDAO.findByOid(odmContainer.getCrfDataPostImportContainer().getStudyOID());
    StudySubjectDAO studySubjectDAO = new StudySubjectDAO(ds);
    StudyEventDefinitionDAO sedDao = new StudyEventDefinitionDAO(ds);
    HashMap<String, ItemDataBean> blankCheck = new HashMap<String, ItemDataBean>();
    String hardValidatorErrorMsgs = "";
    ArrayList<SubjectDataBean> subjectDataBeans = odmContainer.getCrfDataPostImportContainer().getSubjectData();
    int totalEventCRFCount = 0;
    int totalItemDataBeanCount = 0;
    for (SubjectDataBean subjectDataBean : subjectDataBeans) {
        ArrayList<DisplayItemBean> displayItemBeans = new ArrayList<DisplayItemBean>();
        logger.debug("iterating through subject data beans: found " + subjectDataBean.getSubjectOID());
        ArrayList<StudyEventDataBean> studyEventDataBeans = subjectDataBean.getStudyEventData();
        totalEventCRFCount += studyEventDataBeans.size();
        StudySubjectBean studySubjectBean = studySubjectDAO.findByOidAndStudy(subjectDataBean.getSubjectOID(), studyBean.getId());
        for (StudyEventDataBean studyEventDataBean : studyEventDataBeans) {
            int parentStudyId = studyBean.getParentStudyId();
            StudyEventDefinitionBean sedBean = sedDao.findByOidAndStudy(studyEventDataBean.getStudyEventOID(), studyBean.getId(), parentStudyId);
            ArrayList<FormDataBean> formDataBeans = studyEventDataBean.getFormData();
            logger.debug("iterating through study event data beans: found " + studyEventDataBean.getStudyEventOID());
            int ordinal = 1;
            try {
                ordinal = new Integer(studyEventDataBean.getStudyEventRepeatKey()).intValue();
            } catch (Exception e) {
            // trying to catch NPEs, because tags can be without the
            // repeat key
            }
            StudyEventBean studyEvent = (StudyEventBean) studyEventDAO.findByStudySubjectIdAndDefinitionIdAndOrdinal(studySubjectBean.getId(), sedBean.getId(), ordinal);
            displayItemBeans = new ArrayList<DisplayItemBean>();
            for (FormDataBean formDataBean : formDataBeans) {
                Map<String, Integer> groupMaxOrdinals = new HashMap<String, Integer>();
                displayItemBeanWrapper = null;
                CRFVersionDAO crfVersionDAO = new CRFVersionDAO(ds);
                EventCRFDAO eventCRFDAO = new EventCRFDAO(ds);
                ArrayList<CRFVersionBean> crfVersionBeans = crfVersionDAO.findAllByOid(formDataBean.getFormOID());
                ArrayList<ImportItemGroupDataBean> itemGroupDataBeans = formDataBean.getItemGroupData();
                if ((crfVersionBeans == null) || (crfVersionBeans.size() == 0)) {
                    MessageFormat mf = new MessageFormat("");
                    mf.applyPattern(respage.getString("your_crf_version_oid_did_not_generate"));
                    Object[] arguments = { formDataBean.getFormOID() };
                    throw new OpenClinicaException(mf.format(arguments), "");
                }
                CRFVersionBean crfVersion = crfVersionBeans.get(0);
                // if you have a mispelled form oid you get an error here
                // need to error out gracefully and post an error
                logger.debug("iterating through form beans: found " + crfVersion.getOid());
                // may be the point where we cut off item groups etc and
                // instead work on sections
                EventCRFBean eventCRFBean = eventCRFDAO.findByEventCrfVersion(studyEvent, crfVersion);
                EventDefinitionCRFDAO eventDefinitionCRFDAO = new EventDefinitionCRFDAO(ds);
                EventDefinitionCRFBean eventDefinitionCRF = eventDefinitionCRFDAO.findByStudyEventIdAndCRFVersionId(studyBean, studyEvent.getId(), crfVersion.getId());
                if (eventCRFBean != null) {
                    if (permittedEventCRFIds.contains(new Integer(eventCRFBean.getId()))) {
                        for (ImportItemGroupDataBean itemGroupDataBean : itemGroupDataBeans) {
                            groupMaxOrdinals.put(itemGroupDataBean.getItemGroupOID(), 1);
                        }
                        // correctness, tbh
                        for (ImportItemGroupDataBean itemGroupDataBean : itemGroupDataBeans) {
                            ArrayList<ItemBean> blankCheckItems = new ArrayList<ItemBean>();
                            ArrayList<ImportItemDataBean> itemDataBeans = itemGroupDataBean.getItemData();
                            logger.debug("iterating through group beans: " + itemGroupDataBean.getItemGroupOID());
                            // put a checker in here
                            ItemGroupDAO itemGroupDAO = new ItemGroupDAO(ds);
                            ItemGroupBean testBean = itemGroupDAO.findByOid(itemGroupDataBean.getItemGroupOID());
                            if (testBean == null) {
                                // TODO i18n of message
                                MessageFormat mf = new MessageFormat("");
                                mf.applyPattern(respage.getString("your_item_group_oid_for_form_oid"));
                                Object[] arguments = { itemGroupDataBean.getItemGroupOID(), formDataBean.getFormOID() };
                                throw new OpenClinicaException(mf.format(arguments), "");
                            }
                            totalItemDataBeanCount += itemDataBeans.size();
                            for (ImportItemDataBean importItemDataBean : itemDataBeans) {
                                logger.debug("   iterating through item data beans: " + importItemDataBean.getItemOID());
                                ItemDAO itemDAO = new ItemDAO(ds);
                                ItemFormMetadataDAO itemFormMetadataDAO = new ItemFormMetadataDAO(ds);
                                List<ItemBean> itemBeans = itemDAO.findByOid(importItemDataBean.getItemOID());
                                if (!itemBeans.isEmpty()) {
                                    ItemBean itemBean = itemBeans.get(0);
                                    logger.debug("   found " + itemBean.getName());
                                    // throw a null pointer? hopefully not if its been checked...
                                    DisplayItemBean displayItemBean = new DisplayItemBean();
                                    displayItemBean.setItem(itemBean);
                                    ArrayList<ItemFormMetadataBean> metadataBeans = itemFormMetadataDAO.findAllByItemId(itemBean.getId());
                                    logger.debug("      found metadata item beans: " + metadataBeans.size());
                                    int groupOrdinal = 1;
                                    if (itemGroupDataBean.getItemGroupRepeatKey() != null) {
                                        try {
                                            groupOrdinal = new Integer(itemGroupDataBean.getItemGroupRepeatKey()).intValue();
                                            if (groupOrdinal > groupMaxOrdinals.get(itemGroupDataBean.getItemGroupOID())) {
                                                groupMaxOrdinals.put(itemGroupDataBean.getItemGroupOID(), groupOrdinal);
                                            }
                                        } catch (Exception e) {
                                            // do nothing here currently, we are
                                            // looking for a number format
                                            // exception
                                            // from the above.
                                            logger.debug("found npe for group ordinals, line 344!");
                                        }
                                    }
                                    ItemDataBean itemDataBean = createItemDataBean(itemBean, eventCRFBean, importItemDataBean.getValue(), ub, groupOrdinal);
                                    blankCheckItems.add(itemBean);
                                    String newKey = groupOrdinal + "_" + itemGroupDataBean.getItemGroupOID() + "_" + itemBean.getOid() + "_" + subjectDataBean.getSubjectOID();
                                    blankCheck.put(newKey, itemDataBean);
                                    logger.info("adding " + newKey + " to blank checks");
                                    if (!metadataBeans.isEmpty()) {
                                        ItemFormMetadataBean metadataBean = metadataBeans.get(0);
                                        // also possible nullpointer
                                        displayItemBean.setData(itemDataBean);
                                        displayItemBean.setMetadata(metadataBean);
                                        displayItemBean.setEventDefinitionCRF(eventDefinitionCRF);
                                        String eventCRFRepeatKey = studyEventDataBean.getStudyEventRepeatKey();
                                        // if you do indeed leave off this in the XML it will pass but return 'null'
                                        // tbh
                                        attachValidator(displayItemBean, importHelper, discValidator, hardValidator, request, eventCRFRepeatKey, studySubjectBean.getOid());
                                        displayItemBeans.add(displayItemBean);
                                    } else {
                                        MessageFormat mf = new MessageFormat("");
                                        mf.applyPattern(respage.getString("no_metadata_could_be_found"));
                                        Object[] arguments = { importItemDataBean.getItemOID() };
                                        throw new OpenClinicaException(mf.format(arguments), "");
                                    }
                                } else {
                                    // report the error there
                                    MessageFormat mf = new MessageFormat("");
                                    mf.applyPattern(respage.getString("no_item_could_be_found"));
                                    Object[] arguments = { importItemDataBean.getItemOID() };
                                    throw new OpenClinicaException(mf.format(arguments), "");
                                }
                            }
                            // end item data beans
                            logger.debug(".. found blank check: " + blankCheck.toString());
                            for (int i = 1; i <= groupMaxOrdinals.get(itemGroupDataBean.getItemGroupOID()); i++) {
                                for (ItemBean itemBean : blankCheckItems) {
                                    String newKey = i + "_" + itemGroupDataBean.getItemGroupOID() + "_" + itemBean.getOid() + "_" + subjectDataBean.getSubjectOID();
                                    if (blankCheck.get(newKey) == null) {
                                        // if it already exists, Do Not Add It.
                                        ItemDataBean itemDataCheck = getItemDataDao().findByItemIdAndEventCRFIdAndOrdinal(itemBean.getId(), eventCRFBean.getId(), i);
                                        logger.debug("found item data bean id: " + itemDataCheck.getId() + " for ordinal " + i);
                                        if (itemDataCheck.getId() == 0) {
                                            ItemDataBean blank = createItemDataBean(itemBean, eventCRFBean, "", ub, i);
                                            DisplayItemBean displayItemBean = new DisplayItemBean();
                                            displayItemBean.setItem(itemBean);
                                            displayItemBean.setData(blank);
                                            // displayItemBean.setMetadata(metadataBean);
                                            // set event def crf?
                                            displayItemBean.setEventDefinitionCRF(eventDefinitionCRF);
                                            String eventCRFRepeatKey = studyEventDataBean.getStudyEventRepeatKey();
                                            // if you do indeed leave off this in the XML it will pass but return
                                            // 'null'
                                            // tbh
                                            displayItemBeans.add(displayItemBean);
                                            logger.debug("... adding display item bean");
                                        }
                                    }
                                    logger.debug("found a blank at " + i + ", adding " + blankCheckItems.size() + " blank items");
                                }
                            }
                            // << tbh #5548
                            blankCheckItems = new ArrayList<ItemBean>();
                        }
                    // end item group data beans
                    }
                    // matches if on permittedCRFIDs
                    CRFDAO crfDAO = new CRFDAO(ds);
                    CRFBean crfBean = crfDAO.findByVersionId(crfVersion.getCrfId());
                    // seems like an extravagance, but is not contained in crf
                    // version or event crf bean
                    validationErrors = discValidator.validate();
                    // totalValidationErrors.addAll(validationErrors);
                    for (Object errorKey : validationErrors.keySet()) {
                        // JN: to avoid duplicate errors
                        if (!totalValidationErrors.containsKey(errorKey.toString()))
                            totalValidationErrors.put(errorKey.toString(), validationErrors.get(errorKey).toString());
                        // assuming that this will be put back in to the core
                        // method's hashmap, updating statically, tbh 06/2008
                        logger.debug("+++ adding " + errorKey.toString());
                    }
                    logger.debug("-- hard validation checks: --");
                    for (Object errorKey : hardValidator.keySet()) {
                        logger.debug(errorKey.toString() + " -- " + hardValidator.get(errorKey));
                        hardValidationErrors.put(errorKey.toString(), hardValidator.get(errorKey));
                        // updating here 'statically' tbh 06/2008
                        hardValidatorErrorMsgs += hardValidator.get(errorKey) + "<br/><br/>";
                    }
                    String studyEventId = studyEvent.getId() + "";
                    String crfVersionId = crfVersion.getId() + "";
                    logger.debug("creation of wrapper: original count of display item beans " + displayItemBeans.size() + ", count of item data beans " + totalItemDataBeanCount + " count of validation errors " + validationErrors.size() + " count of study subjects " + subjectDataBeans.size() + " count of event crfs " + totalEventCRFCount + " count of hard error checks " + hardValidator.size());
                    // check if we need to overwrite
                    DataEntryStage dataEntryStage = eventCRFBean.getStage();
                    Status eventCRFStatus = eventCRFBean.getStatus();
                    boolean overwrite = false;
                    // //JN: Commenting out the following 2 lines, coz the prompt should come in the cases on
                    if (// eventCRFStatus.equals(Status.UNAVAILABLE) ||
                    dataEntryStage.equals(DataEntryStage.DOUBLE_DATA_ENTRY_COMPLETE) || dataEntryStage.equals(DataEntryStage.INITIAL_DATA_ENTRY_COMPLETE) || dataEntryStage.equals(DataEntryStage.INITIAL_DATA_ENTRY) || dataEntryStage.equals(DataEntryStage.DOUBLE_DATA_ENTRY)) {
                        overwrite = true;
                    }
                    // << tbh, adding extra statuses to prevent appending, 06/2009
                    // SummaryStatsBean ssBean = new SummaryStatsBean();
                    // ssBean.setDiscNoteCount(totalValidationErrors);
                    // ssBean.setEventCrfCount(totalEventCRFCount);
                    // ssBean.setStudySubjectCount(subjectDataBeans.size());
                    // // add other stats here, tbh
                    // not working here, need to do it in a different method,
                    // tbh
                    // summary stats added tbh 05/2008
                    // JN: Changed from validationErrors to totalValidationErrors to create discrepancy notes for
                    // all
                    // the
                    displayItemBeanWrapper = new DisplayItemBeanWrapper(displayItemBeans, true, overwrite, validationErrors, studyEventId, crfVersionId, studyEventDataBean.getStudyEventOID(), studySubjectBean.getLabel(), eventCRFBean.getCreatedDate(), crfBean.getName(), crfVersion.getName(), studySubjectBean.getOid(), studyEventDataBean.getStudyEventRepeatKey());
                    // JN: Commenting out the following code, since we shouldn't re-initialize at this point, as
                    // validationErrors would get overwritten and the
                    // older errors will be overriden. Moving it after the form.
                    // Removing the comments for now, since it seems to be creating duplicate Discrepancy Notes.
                    validationErrors = new HashMap();
                    discValidator = new DiscrepancyValidator(request, discNotes);
                // reset to allow for new errors...
                }
            }
            // discValidator = new DiscrepancyValidator(request, discNotes);
            if (displayItemBeanWrapper != null && displayItemBeans.size() > 0)
                wrappers.add(displayItemBeanWrapper);
        }
    // after study events
    // remove repeats here? remove them below by only forwarding the
    // first
    // each wrapper represents an Event CRF and a Form, but we don't
    // have all events for all forms
    // need to not add a wrapper for every event + form combination,
    // but instead for every event + form combination which is present
    // look at the hack below and see what happens
    }
    // thrown, tbh 06/2008
    if (!hardValidator.isEmpty()) {
    // throw new OpenClinicaException(hardValidatorErrorMsgs, "");
    }
    return wrappers;
}
Also used : ItemBean(org.akaza.openclinica.bean.submit.ItemBean) DisplayItemBean(org.akaza.openclinica.bean.submit.DisplayItemBean) HashMap(java.util.HashMap) ItemDAO(org.akaza.openclinica.dao.submit.ItemDAO) ArrayList(java.util.ArrayList) ItemGroupDAO(org.akaza.openclinica.dao.submit.ItemGroupDAO) StudyEventDefinitionBean(org.akaza.openclinica.bean.managestudy.StudyEventDefinitionBean) StudyEventBean(org.akaza.openclinica.bean.managestudy.StudyEventBean) OpenClinicaException(org.akaza.openclinica.exception.OpenClinicaException) EventCRFBean(org.akaza.openclinica.bean.submit.EventCRFBean) ItemDataBean(org.akaza.openclinica.bean.submit.ItemDataBean) ImportItemDataBean(org.akaza.openclinica.bean.submit.crfdata.ImportItemDataBean) StudyEventDAO(org.akaza.openclinica.dao.managestudy.StudyEventDAO) StudyDAO(org.akaza.openclinica.dao.managestudy.StudyDAO) DisplayItemBeanWrapper(org.akaza.openclinica.bean.submit.DisplayItemBeanWrapper) Status(org.akaza.openclinica.bean.core.Status) SubjectEventStatus(org.akaza.openclinica.bean.core.SubjectEventStatus) EventDefinitionCRFDAO(org.akaza.openclinica.dao.managestudy.EventDefinitionCRFDAO) EventCRFDAO(org.akaza.openclinica.dao.submit.EventCRFDAO) CRFDAO(org.akaza.openclinica.dao.admin.CRFDAO) CRFVersionDAO(org.akaza.openclinica.dao.submit.CRFVersionDAO) FormDiscrepancyNotes(org.akaza.openclinica.control.form.FormDiscrepancyNotes) StudyBean(org.akaza.openclinica.bean.managestudy.StudyBean) EventDefinitionCRFDAO(org.akaza.openclinica.dao.managestudy.EventDefinitionCRFDAO) EventDefinitionCRFBean(org.akaza.openclinica.bean.managestudy.EventDefinitionCRFBean) EventCRFBean(org.akaza.openclinica.bean.submit.EventCRFBean) CRFBean(org.akaza.openclinica.bean.admin.CRFBean) DataEntryStage(org.akaza.openclinica.bean.core.DataEntryStage) StudyEventDefinitionDAO(org.akaza.openclinica.dao.managestudy.StudyEventDefinitionDAO) StudySubjectBean(org.akaza.openclinica.bean.managestudy.StudySubjectBean) EventDefinitionCRFBean(org.akaza.openclinica.bean.managestudy.EventDefinitionCRFBean) ItemGroupBean(org.akaza.openclinica.bean.submit.ItemGroupBean) ImportItemGroupDataBean(org.akaza.openclinica.bean.submit.crfdata.ImportItemGroupDataBean) ImportItemDataBean(org.akaza.openclinica.bean.submit.crfdata.ImportItemDataBean) DisplayItemBean(org.akaza.openclinica.bean.submit.DisplayItemBean) EventCRFDAO(org.akaza.openclinica.dao.submit.EventCRFDAO) ItemFormMetadataDAO(org.akaza.openclinica.dao.submit.ItemFormMetadataDAO) MessageFormat(java.text.MessageFormat) StudyEventDataBean(org.akaza.openclinica.bean.submit.crfdata.StudyEventDataBean) StudySubjectDAO(org.akaza.openclinica.dao.managestudy.StudySubjectDAO) OpenClinicaException(org.akaza.openclinica.exception.OpenClinicaException) ParseException(java.text.ParseException) DiscrepancyValidator(org.akaza.openclinica.control.form.DiscrepancyValidator) SubjectDataBean(org.akaza.openclinica.bean.submit.crfdata.SubjectDataBean) FormDataBean(org.akaza.openclinica.bean.submit.crfdata.FormDataBean) CRFVersionBean(org.akaza.openclinica.bean.submit.CRFVersionBean) ItemFormMetadataBean(org.akaza.openclinica.bean.submit.ItemFormMetadataBean)

Example 73 with StudySubjectDAO

use of org.akaza.openclinica.dao.managestudy.StudySubjectDAO in project OpenClinica by OpenClinica.

the class RestoreStudyServlet method processRequest.

@Override
public void processRequest() throws Exception {
    StudyDAO sdao = new StudyDAO(sm.getDataSource());
    FormProcessor fp = new FormProcessor(request);
    int studyId = fp.getInt("id");
    StudyBean study = (StudyBean) sdao.findByPK(studyId);
    // find all sites
    ArrayList sites = (ArrayList) sdao.findAllByParent(studyId);
    // find all user and roles in the study, include ones in sites
    UserAccountDAO udao = new UserAccountDAO(sm.getDataSource());
    ArrayList userRoles = udao.findAllByStudyId(studyId);
    // find all subjects in the study, include ones in sites
    StudySubjectDAO ssdao = new StudySubjectDAO(sm.getDataSource());
    ArrayList subjects = ssdao.findAllByStudy(study);
    // find all events in the study, include ones in sites
    StudyEventDefinitionDAO sefdao = new StudyEventDefinitionDAO(sm.getDataSource());
    ArrayList definitions = sefdao.findAllByStudy(study);
    String action = request.getParameter("action");
    if (studyId == 0) {
        addPageMessage(respage.getString("please_choose_a_study_to_restore"));
        forwardPage(Page.STUDY_LIST_SERVLET);
    } else {
        if ("confirm".equalsIgnoreCase(action)) {
            request.setAttribute("studyToRestore", study);
            request.setAttribute("sitesToRestore", sites);
            request.setAttribute("userRolesToRestore", userRoles);
            request.setAttribute("subjectsToRestore", subjects);
            request.setAttribute("definitionsToRRestore", definitions);
            forwardPage(Page.RESTORE_STUDY);
        } else {
            logger.info("submit to restore the study");
            // change all statuses to unavailable
            StudyDAO studao = new StudyDAO(sm.getDataSource());
            study.setStatus(study.getOldStatus());
            study.setUpdater(ub);
            study.setUpdatedDate(new Date());
            studao.update(study);
            // YW 09-27-2007 << restore auto-removed sites
            for (int i = 0; i < sites.size(); i++) {
                StudyBean site = (StudyBean) sites.get(i);
                if (site.getStatus() == Status.AUTO_DELETED) {
                    site.setStatus(site.getOldStatus());
                    site.setUpdater(ub);
                    site.setUpdatedDate(new Date());
                    sdao.update(site);
                }
            }
            // restore all users and roles
            for (int i = 0; i < userRoles.size(); i++) {
                StudyUserRoleBean role = (StudyUserRoleBean) userRoles.get(i);
                if (role.getStatus().equals(Status.AUTO_DELETED)) {
                    role.setStatus(Status.AVAILABLE);
                    role.setUpdater(ub);
                    role.setUpdatedDate(new Date());
                    udao.updateStudyUserRole(role, role.getUserName());
                }
            }
            // is current active study
            if (study.getId() == currentStudy.getId()) {
                currentStudy.setStatus(Status.AVAILABLE);
                StudyUserRoleBean r = (new UserAccountDAO(sm.getDataSource())).findRoleByUserNameAndStudyId(ub.getName(), currentStudy.getId());
                currentRole.setRole(r.getRole());
            } else // active site will be restored as well if it was auto-removed
            if (currentStudy.getParentStudyId() == study.getId() && currentStudy.getStatus() == Status.AUTO_DELETED) {
                currentStudy.setStatus(Status.AVAILABLE);
                StudyUserRoleBean r = (new UserAccountDAO(sm.getDataSource())).findRoleByUserNameAndStudyId(ub.getName(), currentStudy.getId());
                StudyUserRoleBean rInParent = (new UserAccountDAO(sm.getDataSource())).findRoleByUserNameAndStudyId(ub.getName(), currentStudy.getParentStudyId());
                // according to logic in SecureController.java: inherited
                // role from parent study, pick the higher role
                currentRole.setRole(Role.get(Role.max(r.getRole(), rInParent.getRole()).getId()));
            }
            // restore all subjects
            for (int i = 0; i < subjects.size(); i++) {
                StudySubjectBean subject = (StudySubjectBean) subjects.get(i);
                if (subject.getStatus().equals(Status.AUTO_DELETED)) {
                    subject.setStatus(Status.AVAILABLE);
                    subject.setUpdater(ub);
                    subject.setUpdatedDate(new Date());
                    ssdao.update(subject);
                }
            }
            // restore all study_group
            StudyGroupDAO sgdao = new StudyGroupDAO(sm.getDataSource());
            StudyGroupClassDAO sgcdao = new StudyGroupClassDAO(sm.getDataSource());
            SubjectGroupMapDAO sgmdao = new SubjectGroupMapDAO(sm.getDataSource());
            ArrayList groups = sgcdao.findAllByStudy(study);
            for (int i = 0; i < groups.size(); i++) {
                StudyGroupClassBean group = (StudyGroupClassBean) groups.get(i);
                if (group.getStatus().equals(Status.AUTO_DELETED)) {
                    group.setStatus(Status.AVAILABLE);
                    group.setUpdater(ub);
                    group.setUpdatedDate(new Date());
                    sgcdao.update(group);
                    // all subject_group_map
                    ArrayList subjectGroupMaps = sgmdao.findAllByStudyGroupClassId(group.getId());
                    for (int j = 0; j < subjectGroupMaps.size(); j++) {
                        SubjectGroupMapBean sgMap = (SubjectGroupMapBean) subjectGroupMaps.get(j);
                        if (sgMap.getStatus().equals(Status.AUTO_DELETED)) {
                            sgMap.setStatus(Status.AVAILABLE);
                            sgMap.setUpdater(ub);
                            sgMap.setUpdatedDate(new Date());
                            sgmdao.update(sgMap);
                        }
                    }
                }
            }
            // restore all event definitions and event
            EventDefinitionCRFDAO edcdao = new EventDefinitionCRFDAO(sm.getDataSource());
            StudyEventDAO sedao = new StudyEventDAO(sm.getDataSource());
            for (int i = 0; i < definitions.size(); i++) {
                StudyEventDefinitionBean definition = (StudyEventDefinitionBean) definitions.get(i);
                if (definition.getStatus().equals(Status.AUTO_DELETED)) {
                    definition.setStatus(Status.AVAILABLE);
                    definition.setUpdater(ub);
                    definition.setUpdatedDate(new Date());
                    sefdao.update(definition);
                    ArrayList edcs = (ArrayList) edcdao.findAllByDefinition(definition.getId());
                    for (int j = 0; j < edcs.size(); j++) {
                        EventDefinitionCRFBean edc = (EventDefinitionCRFBean) edcs.get(j);
                        if (edc.getStatus().equals(Status.AUTO_DELETED)) {
                            edc.setStatus(Status.AVAILABLE);
                            edc.setUpdater(ub);
                            edc.setUpdatedDate(new Date());
                            edcdao.update(edc);
                        }
                    }
                    ArrayList events = (ArrayList) sedao.findAllByDefinition(definition.getId());
                    EventCRFDAO ecdao = new EventCRFDAO(sm.getDataSource());
                    for (int j = 0; j < events.size(); j++) {
                        StudyEventBean event = (StudyEventBean) events.get(j);
                        if (event.getStatus().equals(Status.AUTO_DELETED)) {
                            event.setStatus(Status.AVAILABLE);
                            event.setUpdater(ub);
                            event.setUpdatedDate(new Date());
                            sedao.update(event);
                            ArrayList eventCRFs = ecdao.findAllByStudyEvent(event);
                            ItemDataDAO iddao = new ItemDataDAO(sm.getDataSource());
                            for (int k = 0; k < eventCRFs.size(); k++) {
                                EventCRFBean eventCRF = (EventCRFBean) eventCRFs.get(k);
                                if (eventCRF.getStatus().equals(Status.AUTO_DELETED)) {
                                    eventCRF.setStatus(eventCRF.getOldStatus());
                                    eventCRF.setUpdater(ub);
                                    eventCRF.setUpdatedDate(new Date());
                                    ecdao.update(eventCRF);
                                    ArrayList itemDatas = iddao.findAllByEventCRFId(eventCRF.getId());
                                    for (int a = 0; a < itemDatas.size(); a++) {
                                        ItemDataBean item = (ItemDataBean) itemDatas.get(a);
                                        if (item.getStatus().equals(Status.AUTO_DELETED)) {
                                            item.setStatus(item.getOldStatus());
                                            item.setUpdater(ub);
                                            item.setUpdatedDate(new Date());
                                            iddao.update(item);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            // for definitions
            DatasetDAO datadao = new DatasetDAO(sm.getDataSource());
            ArrayList dataset = datadao.findAllByStudyId(study.getId());
            for (int i = 0; i < dataset.size(); i++) {
                DatasetBean data = (DatasetBean) dataset.get(i);
                if (data.getStatus().equals(Status.AUTO_DELETED)) {
                    data.setStatus(Status.AVAILABLE);
                    data.setUpdater(ub);
                    data.setUpdatedDate(new Date());
                    datadao.update(data);
                }
            }
            addPageMessage(respage.getString("this_study_has_been_restored_succesfully"));
            forwardPage(Page.STUDY_LIST_SERVLET);
        }
    }
}
Also used : SubjectGroupMapBean(org.akaza.openclinica.bean.submit.SubjectGroupMapBean) StudyGroupClassDAO(org.akaza.openclinica.dao.managestudy.StudyGroupClassDAO) DatasetBean(org.akaza.openclinica.bean.extract.DatasetBean) ArrayList(java.util.ArrayList) StudyEventDefinitionBean(org.akaza.openclinica.bean.managestudy.StudyEventDefinitionBean) StudyGroupDAO(org.akaza.openclinica.dao.managestudy.StudyGroupDAO) StudyEventBean(org.akaza.openclinica.bean.managestudy.StudyEventBean) ItemDataDAO(org.akaza.openclinica.dao.submit.ItemDataDAO) EventCRFBean(org.akaza.openclinica.bean.submit.EventCRFBean) ItemDataBean(org.akaza.openclinica.bean.submit.ItemDataBean) StudyEventDAO(org.akaza.openclinica.dao.managestudy.StudyEventDAO) StudyGroupClassBean(org.akaza.openclinica.bean.managestudy.StudyGroupClassBean) StudyDAO(org.akaza.openclinica.dao.managestudy.StudyDAO) EventCRFDAO(org.akaza.openclinica.dao.submit.EventCRFDAO) SubjectGroupMapDAO(org.akaza.openclinica.dao.submit.SubjectGroupMapDAO) FormProcessor(org.akaza.openclinica.control.form.FormProcessor) StudyBean(org.akaza.openclinica.bean.managestudy.StudyBean) StudyUserRoleBean(org.akaza.openclinica.bean.login.StudyUserRoleBean) EventDefinitionCRFDAO(org.akaza.openclinica.dao.managestudy.EventDefinitionCRFDAO) UserAccountDAO(org.akaza.openclinica.dao.login.UserAccountDAO) StudySubjectDAO(org.akaza.openclinica.dao.managestudy.StudySubjectDAO) DatasetDAO(org.akaza.openclinica.dao.extract.DatasetDAO) Date(java.util.Date) StudyEventDefinitionDAO(org.akaza.openclinica.dao.managestudy.StudyEventDefinitionDAO) StudySubjectBean(org.akaza.openclinica.bean.managestudy.StudySubjectBean) EventDefinitionCRFBean(org.akaza.openclinica.bean.managestudy.EventDefinitionCRFBean)

Example 74 with StudySubjectDAO

use of org.akaza.openclinica.dao.managestudy.StudySubjectDAO in project OpenClinica by OpenClinica.

the class RestoreSubjectServlet method processRequest.

@Override
public void processRequest() throws Exception {
    SubjectDAO sdao = new SubjectDAO(sm.getDataSource());
    FormProcessor fp = new FormProcessor(request);
    int subjectId = fp.getInt("id");
    String action = fp.getString("action");
    if (subjectId == 0 || StringUtil.isBlank(action)) {
        addPageMessage(respage.getString("please_choose_a_subject_to_restore"));
        forwardPage(Page.SUBJECT_LIST_SERVLET);
    } else {
        SubjectBean subject = (SubjectBean) sdao.findByPK(subjectId);
        // find all study subjects
        StudySubjectDAO ssdao = new StudySubjectDAO(sm.getDataSource());
        ArrayList studySubs = ssdao.findAllBySubjectId(subjectId);
        // find study events
        StudyEventDAO sedao = new StudyEventDAO(sm.getDataSource());
        ArrayList events = sedao.findAllBySubjectId(subjectId);
        if ("confirm".equalsIgnoreCase(action)) {
            request.setAttribute("subjectToRestore", subject);
            request.setAttribute("studySubs", studySubs);
            request.setAttribute("events", events);
            forwardPage(Page.RESTORE_SUBJECT);
        } else {
            logger.info("submit to restore the subject");
            // change all statuses to AVAILABLE
            subject.setStatus(Status.AVAILABLE);
            subject.setUpdater(ub);
            subject.setUpdatedDate(new Date());
            sdao.update(subject);
            // remove subject references from study
            for (int i = 0; i < studySubs.size(); i++) {
                StudySubjectBean studySub = (StudySubjectBean) studySubs.get(i);
                if (studySub.getStatus().equals(Status.AUTO_DELETED)) {
                    studySub.setStatus(Status.AVAILABLE);
                    studySub.setUpdater(ub);
                    studySub.setUpdatedDate(new Date());
                    ssdao.update(studySub);
                }
            }
            EventCRFDAO ecdao = new EventCRFDAO(sm.getDataSource());
            for (int j = 0; j < events.size(); j++) {
                StudyEventBean event = (StudyEventBean) events.get(j);
                if (event.getStatus().equals(Status.AUTO_DELETED)) {
                    event.setStatus(Status.AVAILABLE);
                    event.setUpdater(ub);
                    event.setUpdatedDate(new Date());
                    sedao.update(event);
                    ArrayList eventCRFs = ecdao.findAllByStudyEvent(event);
                    ItemDataDAO iddao = new ItemDataDAO(sm.getDataSource());
                    for (int k = 0; k < eventCRFs.size(); k++) {
                        EventCRFBean eventCRF = (EventCRFBean) eventCRFs.get(k);
                        if (eventCRF.getStatus().equals(Status.AUTO_DELETED)) {
                            eventCRF.setStatus(Status.AVAILABLE);
                            eventCRF.setUpdater(ub);
                            eventCRF.setUpdatedDate(new Date());
                            ecdao.update(eventCRF);
                            // restore all the item data
                            ArrayList itemDatas = iddao.findAllByEventCRFId(eventCRF.getId());
                            for (int a = 0; a < itemDatas.size(); a++) {
                                ItemDataBean item = (ItemDataBean) itemDatas.get(a);
                                if (item.getStatus().equals(Status.AUTO_DELETED)) {
                                    item.setStatus(Status.AVAILABLE);
                                    item.setUpdater(ub);
                                    item.setUpdatedDate(new Date());
                                    iddao.update(item);
                                }
                            }
                        }
                    }
                }
            }
            String emailBody = respage.getString("the_subject") + subject.getName() + " " + respage.getString("has_been_restored_succesfully");
            addPageMessage(emailBody);
            //                sendEmail(emailBody);
            forwardPage(Page.SUBJECT_LIST_SERVLET);
        }
    }
}
Also used : StudySubjectDAO(org.akaza.openclinica.dao.managestudy.StudySubjectDAO) SubjectDAO(org.akaza.openclinica.dao.submit.SubjectDAO) FormProcessor(org.akaza.openclinica.control.form.FormProcessor) ArrayList(java.util.ArrayList) StudyEventBean(org.akaza.openclinica.bean.managestudy.StudyEventBean) StudySubjectDAO(org.akaza.openclinica.dao.managestudy.StudySubjectDAO) Date(java.util.Date) ItemDataDAO(org.akaza.openclinica.dao.submit.ItemDataDAO) SubjectBean(org.akaza.openclinica.bean.submit.SubjectBean) StudySubjectBean(org.akaza.openclinica.bean.managestudy.StudySubjectBean) EventCRFBean(org.akaza.openclinica.bean.submit.EventCRFBean) StudySubjectBean(org.akaza.openclinica.bean.managestudy.StudySubjectBean) ItemDataBean(org.akaza.openclinica.bean.submit.ItemDataBean) StudyEventDAO(org.akaza.openclinica.dao.managestudy.StudyEventDAO) EventCRFDAO(org.akaza.openclinica.dao.submit.EventCRFDAO)

Example 75 with StudySubjectDAO

use of org.akaza.openclinica.dao.managestudy.StudySubjectDAO in project OpenClinica by OpenClinica.

the class ResolveDiscrepancyServlet method processRequest.

/*
     * (non-Javadoc)
     *
     * @see org.akaza.openclinica.control.core.SecureController#processRequest()
     */
@Override
protected void processRequest() throws Exception {
    FormProcessor fp = new FormProcessor(request);
    int noteId = fp.getInt(INPUT_NOTE_ID);
    String module = (String) session.getAttribute("module");
    StudySubjectDAO studySubjectDAO = new StudySubjectDAO(sm.getDataSource());
    DiscrepancyNoteDAO dndao = new DiscrepancyNoteDAO(sm.getDataSource());
    dndao.setFetchMapping(true);
    // check that the note exists
    DiscrepancyNoteBean discrepancyNoteBean = (DiscrepancyNoteBean) dndao.findByPK(noteId);
    if (!discrepancyNoteBean.isActive()) {
        throw new InconsistentStateException(Page.MANAGE_STUDY_SERVLET, resexception.getString("you_are_trying_resolve_discrepancy_not_exist"));
    }
    // check that the note has not already been closed
    ArrayList children = dndao.findAllByParent(discrepancyNoteBean);
    discrepancyNoteBean.setChildren(children);
    // This logic has been reverted, issue-7459
    // if (parentNoteIsClosed(discrepancyNoteBean)) {
    // throw new InconsistentStateException(Page.VIEW_DISCREPANCY_NOTES_IN_STUDY_SERVLET, respage
    // .getString("the_discrepancy_choose_has_been_closed_resolved_create_new"));
    // }
    // all clear, send the user to the resolved screen
    String entityType = discrepancyNoteBean.getEntityType().toLowerCase();
    discrepancyNoteBean.setResStatus(ResolutionStatus.get(discrepancyNoteBean.getResolutionStatusId()));
    discrepancyNoteBean.setDisType(DiscrepancyNoteType.get(discrepancyNoteBean.getDiscrepancyNoteTypeId()));
    // View Study Events <<
    if (currentRole.getRole().equals(Role.MONITOR) && !"itemdata".equalsIgnoreCase(entityType) && !"eventcrf".equalsIgnoreCase(entityType)) {
        redirectMonitor(module, discrepancyNoteBean);
        return;
    }
    // If Study is Frozen or Locked
    if (currentStudy.getStatus().isFrozen() && !"itemdata".equalsIgnoreCase(entityType) && !"eventcrf".equalsIgnoreCase(entityType)) {
        redirectMonitor(module, discrepancyNoteBean);
        return;
    }
    boolean toView = false;
    boolean isCompleted = false;
    if ("itemdata".equalsIgnoreCase(entityType)) {
        ItemDataDAO iddao = new ItemDataDAO(sm.getDataSource());
        ItemDataBean idb = (ItemDataBean) iddao.findByPK(discrepancyNoteBean.getEntityId());
        EventCRFDAO ecdao = new EventCRFDAO(sm.getDataSource());
        EventCRFBean ecb = (EventCRFBean) ecdao.findByPK(idb.getEventCRFId());
        StudySubjectBean studySubjectBean = (StudySubjectBean) studySubjectDAO.findByPK(ecb.getStudySubjectId());
        discrepancyNoteBean.setSubjectId(studySubjectBean.getId());
        discrepancyNoteBean.setItemId(idb.getItemId());
        if (ecb.getStatus().equals(Status.UNAVAILABLE)) {
            isCompleted = true;
        }
        // we want to go to view note page if the note is
        toView = true;
    // for item data
    }
    // logger.info("set up pop up url: " + createNoteURL);
    // System.out.println("set up pop up url: " + createNoteURL);
    boolean goNext = prepareRequestForResolution(request, sm.getDataSource(), currentStudy, discrepancyNoteBean, isCompleted);
    Page p = getPageForForwarding(discrepancyNoteBean, isCompleted);
    // logger.info("found page for forwarding: " + p.getFileName());
    if (p == null) {
        throw new InconsistentStateException(Page.VIEW_DISCREPANCY_NOTES_IN_STUDY_SERVLET, resexception.getString("the_discrepancy_note_triying_resolve_has_invalid_type"));
    } else {
        if (p.getFileName().contains("?")) {
            if (!p.getFileName().contains("fromViewNotes=1")) {
                p.setFileName(p.getFileName() + "&fromViewNotes=1");
            }
        } else {
            p.setFileName(p.getFileName() + "?fromViewNotes=1");
        }
        String createNoteURL = CreateDiscrepancyNoteServlet.getAddChildURL(discrepancyNoteBean, ResolutionStatus.CLOSED, true);
        setPopUpURL(createNoteURL);
    }
    if (!goNext) {
        setPopUpURL("");
        addPageMessage(respage.getString("you_may_not_perform_admin_edit_on_CRF_not_completed_by_user"));
        p = Page.VIEW_DISCREPANCY_NOTES_IN_STUDY_SERVLET;
    }
    forwardPage(p);
}
Also used : DiscrepancyNoteDAO(org.akaza.openclinica.dao.managestudy.DiscrepancyNoteDAO) FormProcessor(org.akaza.openclinica.control.form.FormProcessor) ArrayList(java.util.ArrayList) Page(org.akaza.openclinica.view.Page) StudySubjectDAO(org.akaza.openclinica.dao.managestudy.StudySubjectDAO) InconsistentStateException(org.akaza.openclinica.web.InconsistentStateException) ItemDataDAO(org.akaza.openclinica.dao.submit.ItemDataDAO) EventCRFBean(org.akaza.openclinica.bean.submit.EventCRFBean) StudySubjectBean(org.akaza.openclinica.bean.managestudy.StudySubjectBean) DiscrepancyNoteBean(org.akaza.openclinica.bean.managestudy.DiscrepancyNoteBean) ItemDataBean(org.akaza.openclinica.bean.submit.ItemDataBean) EventCRFDAO(org.akaza.openclinica.dao.submit.EventCRFDAO)

Aggregations

StudySubjectDAO (org.akaza.openclinica.dao.managestudy.StudySubjectDAO)102 StudySubjectBean (org.akaza.openclinica.bean.managestudy.StudySubjectBean)86 ArrayList (java.util.ArrayList)63 StudyEventBean (org.akaza.openclinica.bean.managestudy.StudyEventBean)62 StudyEventDAO (org.akaza.openclinica.dao.managestudy.StudyEventDAO)62 StudyDAO (org.akaza.openclinica.dao.managestudy.StudyDAO)59 EventCRFDAO (org.akaza.openclinica.dao.submit.EventCRFDAO)58 StudyBean (org.akaza.openclinica.bean.managestudy.StudyBean)54 StudyEventDefinitionDAO (org.akaza.openclinica.dao.managestudy.StudyEventDefinitionDAO)50 EventCRFBean (org.akaza.openclinica.bean.submit.EventCRFBean)47 StudyEventDefinitionBean (org.akaza.openclinica.bean.managestudy.StudyEventDefinitionBean)42 FormProcessor (org.akaza.openclinica.control.form.FormProcessor)41 Date (java.util.Date)38 EventDefinitionCRFDAO (org.akaza.openclinica.dao.managestudy.EventDefinitionCRFDAO)32 ItemDataDAO (org.akaza.openclinica.dao.submit.ItemDataDAO)29 SubjectDAO (org.akaza.openclinica.dao.submit.SubjectDAO)27 ItemDataBean (org.akaza.openclinica.bean.submit.ItemDataBean)26 CRFVersionDAO (org.akaza.openclinica.dao.submit.CRFVersionDAO)24 SubjectBean (org.akaza.openclinica.bean.submit.SubjectBean)23 CRFDAO (org.akaza.openclinica.dao.admin.CRFDAO)23