Search in sources :

Example 1 with CrfBusinessLogicHelper

use of org.akaza.openclinica.web.job.CrfBusinessLogicHelper in project OpenClinica by OpenClinica.

the class VerifyImportedCRFDataServlet method processRequest.

@Override
@SuppressWarnings(value = "unchecked")
public void processRequest() throws Exception {
    ItemDataDAO itemDataDao = new ItemDataDAO(sm.getDataSource());
    itemDataDao.setFormatDates(false);
    EventCRFDAO eventCrfDao = new EventCRFDAO(sm.getDataSource());
    CrfBusinessLogicHelper crfBusinessLogicHelper = new CrfBusinessLogicHelper(sm.getDataSource());
    String action = request.getParameter("action");
    FormProcessor fp = new FormProcessor(request);
    // checks which module the requests are from
    String module = fp.getString(MODULE);
    request.setAttribute(MODULE, module);
    resetPanel();
    panel.setStudyInfoShown(false);
    panel.setOrderedData(true);
    setToPanel(resword.getString("create_CRF"), respage.getString("br_create_new_CRF_entering"));
    setToPanel(resword.getString("create_CRF_version"), respage.getString("br_create_new_CRF_uploading"));
    setToPanel(resword.getString("revise_CRF_version"), respage.getString("br_if_you_owner_CRF_version"));
    setToPanel(resword.getString("CRF_spreadsheet_template"), respage.getString("br_download_blank_CRF_spreadsheet_from"));
    setToPanel(resword.getString("example_CRF_br_spreadsheets"), respage.getString("br_download_example_CRF_instructions_from"));
    if ("confirm".equalsIgnoreCase(action)) {
        List<DisplayItemBeanWrapper> displayItemBeanWrappers = (List<DisplayItemBeanWrapper>) session.getAttribute("importedData");
        logger.info("Size of displayItemBeanWrappers : " + displayItemBeanWrappers.size());
        forwardPage(Page.VERIFY_IMPORT_CRF_DATA);
    }
    if ("save".equalsIgnoreCase(action)) {
        // setup ruleSets to run if applicable
        RuleSetServiceInterface ruleSetService = (RuleSetServiceInterface) SpringServletAccess.getApplicationContext(context).getBean("ruleSetService");
        List<ImportDataRuleRunnerContainer> containers = this.ruleRunSetup(sm.getDataSource(), currentStudy, ub, ruleSetService);
        List<DisplayItemBeanWrapper> displayItemBeanWrappers = (List<DisplayItemBeanWrapper>) session.getAttribute("importedData");
        // System.out.println("Size of displayItemBeanWrappers : " +
        // displayItemBeanWrappers.size());
        HashMap<Integer, String> importedCRFStatuses = (HashMap<Integer, String>) session.getAttribute("importedCRFStatuses");
        for (DisplayItemBeanWrapper wrapper : displayItemBeanWrappers) {
            boolean resetSDV = false;
            int eventCrfBeanId = -1;
            EventCRFBean eventCrfBean = new EventCRFBean();
            // TODO : tom , the wrapper object has all the necessary data -
            // as you see we check the
            // is to see if this data is Savable if it is then we go ahead
            // and save it. if not we discard.
            // So the change needs to happen here , instead of discarding we
            // need to file discrepancy notes
            // and save the data. If you look in the
            // Page.VERIFY_IMPORT_CRF_DATA jsp file you can see how I am
            // pulling the errors. and use that in the same way.
            logger.info("right before we check to make sure it is savable: " + wrapper.isSavable());
            if (wrapper.isSavable()) {
                ArrayList<Integer> eventCrfInts = new ArrayList<Integer>();
                // wrapper.getValidationErrors().toString());
                for (DisplayItemBean displayItemBean : wrapper.getDisplayItemBeans()) {
                    eventCrfBeanId = displayItemBean.getData().getEventCRFId();
                    eventCrfBean = (EventCRFBean) eventCrfDao.findByPK(eventCrfBeanId);
                    logger.info("found value here: " + displayItemBean.getData().getValue());
                    logger.info("found status here: " + eventCrfBean.getStatus().getName());
                    // System.out.println("found event crf bean name here: "
                    // +
                    // eventCrfBean.getEventName()+" id "+eventCrfBean.getId
                    // ());
                    // SO, items can be created in a wrapper which is set to
                    // overwrite
                    // we get around this by checking the bean first, to
                    // make sure it's not null
                    ItemDataBean itemDataBean = new ItemDataBean();
                    itemDataBean = itemDataDao.findByItemIdAndEventCRFIdAndOrdinal(displayItemBean.getItem().getId(), eventCrfBean.getId(), displayItemBean.getData().getOrdinal());
                    if (wrapper.isOverwrite() && itemDataBean.getStatus() != null) {
                        if (!itemDataBean.getValue().equals(displayItemBean.getData().getValue()))
                            resetSDV = true;
                        logger.info("just tried to find item data bean on item name " + displayItemBean.getItem().getName());
                        itemDataBean.setUpdatedDate(new Date());
                        itemDataBean.setUpdater(ub);
                        itemDataBean.setValue(displayItemBean.getData().getValue());
                        // set status?
                        itemDataDao.update(itemDataBean);
                        logger.info("updated: " + itemDataBean.getItemId());
                        // need to set pk here in order to create dn
                        displayItemBean.getData().setId(itemDataBean.getId());
                    } else {
                        resetSDV = true;
                        itemDataDao.create(displayItemBean.getData());
                        logger.info("created: " + displayItemBean.getData().getItemId() + "event CRF ID = " + eventCrfBean.getId() + "CRF VERSION ID =" + eventCrfBean.getCRFVersionId());
                        // does this dao function work for repeating
                        // events/groups?
                        // ItemDataBean itemDataBean =
                        // itemDataDao.findByEventCRFIdAndItemName(
                        // eventCrfBean,
                        // displayItemBean.getItem().getName());
                        ItemDataBean itemDataBean2 = itemDataDao.findByItemIdAndEventCRFIdAndOrdinal(displayItemBean.getItem().getId(), eventCrfBean.getId(), displayItemBean.getData().getOrdinal());
                        logger.info("found: id " + itemDataBean2.getId() + " name " + itemDataBean2.getName());
                        displayItemBean.getData().setId(itemDataBean2.getId());
                    }
                    // logger.info("created item data bean:
                    // "+displayItemBean.getData().getId());
                    // logger.info("created:
                    // "+displayItemBean.getData().getName());
                    // logger.info("continued:
                    // "+displayItemBean.getData().getItemId());
                    ItemDAO idao = new ItemDAO(sm.getDataSource());
                    ItemBean ibean = (ItemBean) idao.findByPK(displayItemBean.getData().getItemId());
                    // logger.info("continued2: getName " +
                    // ibean.getName());
                    // System.out.println("*** checking for validation errors: "
                    // + ibean.getName());
                    String itemOid = displayItemBean.getItem().getOid() + "_" + wrapper.getStudyEventRepeatKey() + "_" + displayItemBean.getData().getOrdinal() + "_" + wrapper.getStudySubjectOid();
                    if (wrapper.getValidationErrors().containsKey(itemOid)) {
                        ArrayList messageList = (ArrayList) wrapper.getValidationErrors().get(itemOid);
                        // could it be more than one? tbh 08/2008
                        for (int iter = 0; iter < messageList.size(); iter++) {
                            String message = (String) messageList.get(iter);
                            DiscrepancyNoteBean parentDn = ImportSpringJob.createDiscrepancyNote(ibean, message, eventCrfBean, displayItemBean, null, ub, sm.getDataSource(), currentStudy);
                            ImportSpringJob.createDiscrepancyNote(ibean, message, eventCrfBean, displayItemBean, parentDn.getId(), ub, sm.getDataSource(), currentStudy);
                        // System.out.println("*** created disc note with message: "
                        // + message);
                        // displayItemBean);
                        }
                    }
                    // "+displayItemBean.getDbData().getName());
                    if (!eventCrfInts.contains(new Integer(eventCrfBean.getId()))) {
                        String eventCRFStatus = importedCRFStatuses.get(new Integer(eventCrfBean.getId()));
                        if (eventCRFStatus != null && eventCRFStatus.equals(DataEntryStage.INITIAL_DATA_ENTRY.getName()) && eventCrfBean.getStatus().isAvailable()) {
                            crfBusinessLogicHelper.markCRFStarted(eventCrfBean, ub);
                        } else {
                            crfBusinessLogicHelper.markCRFComplete(eventCrfBean, ub);
                        }
                        eventCrfInts.add(new Integer(eventCrfBean.getId()));
                    }
                }
                // Reset the SDV status if item data has been changed or added
                if (eventCrfBean != null && resetSDV)
                    eventCrfDao.setSDVStatus(false, ub.getId(), eventCrfBean.getId());
            // end of item datas, tbh
            // crfBusinessLogicHelper.markCRFComplete(eventCrfBean, ub);
            // System .out.println("*** just updated event crf bean: "+
            // eventCrfBean.getId());
            // need to update the study event status as well, tbh
            // crfBusinessLogicHelper.updateStudyEvent(eventCrfBean,
            // ub);
            // above should do it for us, tbh 08/2008
            }
        }
        addPageMessage(respage.getString("data_has_been_successfully_import"));
        addPageMessage(this.ruleActionWarnings(this.runRules(currentStudy, ub, containers, ruleSetService, ExecutionMode.SAVE)));
        // forwardPage(Page.SUBMIT_DATA_SERVLET);
        forwardPage(Page.LIST_STUDY_SUBJECTS_SERVLET);
    // replaced tbh, 06/2009
    }
}
Also used : DisplayItemBean(org.akaza.openclinica.bean.submit.DisplayItemBean) ItemBean(org.akaza.openclinica.bean.submit.ItemBean) HashMap(java.util.HashMap) ItemDAO(org.akaza.openclinica.dao.submit.ItemDAO) ArrayList(java.util.ArrayList) ImportDataRuleRunnerContainer(org.akaza.openclinica.logic.rulerunner.ImportDataRuleRunnerContainer) ItemDataDAO(org.akaza.openclinica.dao.submit.ItemDataDAO) RuleSetServiceInterface(org.akaza.openclinica.service.rule.RuleSetServiceInterface) EventCRFBean(org.akaza.openclinica.bean.submit.EventCRFBean) ItemDataBean(org.akaza.openclinica.bean.submit.ItemDataBean) DisplayItemBean(org.akaza.openclinica.bean.submit.DisplayItemBean) CrfBusinessLogicHelper(org.akaza.openclinica.web.job.CrfBusinessLogicHelper) ArrayList(java.util.ArrayList) List(java.util.List) EventCRFDAO(org.akaza.openclinica.dao.submit.EventCRFDAO) DisplayItemBeanWrapper(org.akaza.openclinica.bean.submit.DisplayItemBeanWrapper) FormProcessor(org.akaza.openclinica.control.form.FormProcessor) Date(java.util.Date) DiscrepancyNoteBean(org.akaza.openclinica.bean.managestudy.DiscrepancyNoteBean)

Example 2 with CrfBusinessLogicHelper

use of org.akaza.openclinica.web.job.CrfBusinessLogicHelper in project OpenClinica by OpenClinica.

the class DataImportService method submitData.

public ArrayList<String> submitData(ODMContainer odmContainer, DataSource dataSource, StudyBean studyBean, UserAccountBean userBean, List<DisplayItemBeanWrapper> displayItemBeanWrappers, Map<Integer, String> importedCRFStatuses) throws Exception {
    boolean discNotesGenerated = false;
    ItemDataDAO itemDataDao = new ItemDataDAO(dataSource);
    itemDataDao.setFormatDates(false);
    EventCRFDAO eventCrfDao = new EventCRFDAO(dataSource);
    StringBuffer auditMsg = new StringBuffer();
    int eventCrfBeanId = -1;
    EventCRFBean eventCrfBean = null;
    ArrayList<Integer> eventCrfInts;
    ItemDataBean itemDataBean;
    CrfBusinessLogicHelper crfBusinessLogicHelper = new CrfBusinessLogicHelper(dataSource);
    for (DisplayItemBeanWrapper wrapper : displayItemBeanWrappers) {
        boolean resetSDV = false;
        logger.debug("right before we check to make sure it is savable: " + wrapper.isSavable());
        if (wrapper.isSavable()) {
            eventCrfInts = new ArrayList<Integer>();
            logger.debug("wrapper problems found : " + wrapper.getValidationErrors().toString());
            if (wrapper.getDisplayItemBeans() != null && wrapper.getDisplayItemBeans().size() == 0) {
                return getReturnList("fail", "", "No items to submit. Please check your XML.");
            }
            for (DisplayItemBean displayItemBean : wrapper.getDisplayItemBeans()) {
                eventCrfBeanId = displayItemBean.getData().getEventCRFId();
                eventCrfBean = (EventCRFBean) eventCrfDao.findByPK(eventCrfBeanId);
                logger.debug("found value here: " + displayItemBean.getData().getValue());
                logger.debug("found status here: " + eventCrfBean.getStatus().getName());
                itemDataBean = itemDataDao.findByItemIdAndEventCRFIdAndOrdinal(displayItemBean.getItem().getId(), eventCrfBean.getId(), displayItemBean.getData().getOrdinal());
                if (wrapper.isOverwrite() && itemDataBean.getStatus() != null) {
                    if (!itemDataBean.getValue().equals(displayItemBean.getData().getValue()))
                        resetSDV = true;
                    logger.debug("just tried to find item data bean on item name " + displayItemBean.getItem().getName());
                    itemDataBean.setUpdatedDate(new Date());
                    itemDataBean.setUpdater(userBean);
                    itemDataBean.setValue(displayItemBean.getData().getValue());
                    // set status?
                    itemDataDao.update(itemDataBean);
                    logger.debug("updated: " + itemDataBean.getItemId());
                    // need to set pk here in order to create dn
                    displayItemBean.getData().setId(itemDataBean.getId());
                } else {
                    resetSDV = true;
                    itemDataDao.create(displayItemBean.getData());
                    logger.debug("created: " + displayItemBean.getData().getItemId());
                    itemDataBean = itemDataDao.findByItemIdAndEventCRFIdAndOrdinal(displayItemBean.getItem().getId(), eventCrfBean.getId(), displayItemBean.getData().getOrdinal());
                    // logger.debug("found: id " + itemDataBean2.getId() + " name " + itemDataBean2.getName());
                    displayItemBean.getData().setId(itemDataBean.getId());
                }
                ItemDAO idao = new ItemDAO(dataSource);
                ItemBean ibean = (ItemBean) idao.findByPK(displayItemBean.getData().getItemId());
                // logger.debug("*** checking for validation errors: " + ibean.getName());
                String itemOid = displayItemBean.getItem().getOid() + "_" + wrapper.getStudyEventRepeatKey() + "_" + displayItemBean.getData().getOrdinal() + "_" + wrapper.getStudySubjectOid();
                // wrapper.getValidationErrors().toString());
                if (wrapper.getValidationErrors().containsKey(itemOid)) {
                    ArrayList<String> messageList = (ArrayList<String>) wrapper.getValidationErrors().get(itemOid);
                    for (String message : messageList) {
                        DiscrepancyNoteBean parentDn = createDiscrepancyNote(ibean, message, eventCrfBean, displayItemBean, null, userBean, dataSource, studyBean);
                        createDiscrepancyNote(ibean, message, eventCrfBean, displayItemBean, parentDn.getId(), userBean, dataSource, studyBean);
                        discNotesGenerated = true;
                        logger.debug("*** created disc note with message: " + message);
                        auditMsg.append(wrapper.getStudySubjectOid() + ": " + ibean.getOid() + ": " + message + "---");
                    // split by this ? later, tbh
                    // displayItemBean);
                    }
                }
                if (!eventCrfInts.contains(new Integer(eventCrfBean.getId()))) {
                    String eventCRFStatus = importedCRFStatuses.get(new Integer(eventCrfBean.getId()));
                    if (eventCRFStatus != null && eventCRFStatus.equals(DataEntryStage.INITIAL_DATA_ENTRY.getName()) && eventCrfBean.getStatus().isAvailable()) {
                        crfBusinessLogicHelper.markCRFStarted(eventCrfBean, userBean, true);
                    } else {
                        crfBusinessLogicHelper.markCRFComplete(eventCrfBean, userBean, true);
                    }
                    eventCrfInts.add(new Integer(eventCrfBean.getId()));
                }
            }
            // Reset the SDV status if item data has been changed or added
            if (eventCrfBean != null && resetSDV)
                eventCrfDao.setSDVStatus(false, userBean.getId(), eventCrfBean.getId());
        }
    }
    if (!discNotesGenerated) {
        return getReturnList("success", "", auditMsg.toString());
    } else {
        return getReturnList("warn", "", auditMsg.toString());
    }
}
Also used : DisplayItemBean(org.akaza.openclinica.bean.submit.DisplayItemBean) ItemBean(org.akaza.openclinica.bean.submit.ItemBean) ItemDAO(org.akaza.openclinica.dao.submit.ItemDAO) ArrayList(java.util.ArrayList) ItemDataDAO(org.akaza.openclinica.dao.submit.ItemDataDAO) Date(java.util.Date) EventCRFBean(org.akaza.openclinica.bean.submit.EventCRFBean) ItemDataBean(org.akaza.openclinica.bean.submit.ItemDataBean) DiscrepancyNoteBean(org.akaza.openclinica.bean.managestudy.DiscrepancyNoteBean) DisplayItemBean(org.akaza.openclinica.bean.submit.DisplayItemBean) CrfBusinessLogicHelper(org.akaza.openclinica.web.job.CrfBusinessLogicHelper) EventCRFDAO(org.akaza.openclinica.dao.submit.EventCRFDAO) DisplayItemBeanWrapper(org.akaza.openclinica.bean.submit.DisplayItemBeanWrapper)

Aggregations

ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 DiscrepancyNoteBean (org.akaza.openclinica.bean.managestudy.DiscrepancyNoteBean)2 DisplayItemBean (org.akaza.openclinica.bean.submit.DisplayItemBean)2 DisplayItemBeanWrapper (org.akaza.openclinica.bean.submit.DisplayItemBeanWrapper)2 EventCRFBean (org.akaza.openclinica.bean.submit.EventCRFBean)2 ItemBean (org.akaza.openclinica.bean.submit.ItemBean)2 ItemDataBean (org.akaza.openclinica.bean.submit.ItemDataBean)2 EventCRFDAO (org.akaza.openclinica.dao.submit.EventCRFDAO)2 ItemDAO (org.akaza.openclinica.dao.submit.ItemDAO)2 ItemDataDAO (org.akaza.openclinica.dao.submit.ItemDataDAO)2 CrfBusinessLogicHelper (org.akaza.openclinica.web.job.CrfBusinessLogicHelper)2 HashMap (java.util.HashMap)1 List (java.util.List)1 FormProcessor (org.akaza.openclinica.control.form.FormProcessor)1 ImportDataRuleRunnerContainer (org.akaza.openclinica.logic.rulerunner.ImportDataRuleRunnerContainer)1 RuleSetServiceInterface (org.akaza.openclinica.service.rule.RuleSetServiceInterface)1