Search in sources :

Example 1 with XformMetaDataService

use of org.akaza.openclinica.service.crfdata.XformMetaDataService in project OpenClinica by OpenClinica.

the class CreateXformCRFVersionServlet method processRequest.

@Override
protected void processRequest() throws Exception {
    CrfDao crfDao = (CrfDao) SpringServletAccess.getApplicationContext(context).getBean("crfDao");
    CrfVersionDao crfVersionDao = (CrfVersionDao) SpringServletAccess.getApplicationContext(context).getBean("crfVersionDao");
    Locale locale = LocaleResolver.getLocale(request);
    ResourceBundleProvider.updateLocale(locale);
    resword = ResourceBundleProvider.getWordsBundle(locale);
    // Retrieve submission data from multipart request
    DiskFileItemFactory factory = new DiskFileItemFactory();
    ServletFileUpload upload = new ServletFileUpload(factory);
    List<FileItem> items = upload.parseRequest(request);
    String submittedCrfName = retrieveFormFieldValue(items, "crfName");
    String submittedCrfVersionName = retrieveFormFieldValue(items, "versionName");
    String submittedCrfVersionDescription = retrieveFormFieldValue(items, "versionDescription");
    String submittedRevisionNotes = retrieveFormFieldValue(items, "revisionNotes");
    String submittedXformText = retrieveFormFieldValue(items, "xformText");
    CRFVersionBean version = (CRFVersionBean) session.getAttribute("version");
    logger.debug("Found original CRF ID for new CRF Version:" + version.getCrfId());
    // Create container for holding validation errors
    DataBinder dataBinder = new DataBinder(new CrfVersion());
    Errors errors = dataBinder.getBindingResult();
    // Validate all upload form fields were populated
    validateFormFields(errors, version, submittedCrfName, submittedCrfVersionName, submittedCrfVersionDescription, submittedRevisionNotes, submittedXformText);
    if (!errors.hasErrors()) {
        // Parse instance and xform
        XformParser parser = (XformParser) SpringServletAccess.getApplicationContext(context).getBean("xformParser");
        XformContainer container = parseInstance(submittedXformText);
        Html html = parser.unMarshall(submittedXformText);
        // Save meta-data in database
        XformMetaDataService xformService = (XformMetaDataService) SpringServletAccess.getApplicationContext(context).getBean("xformMetaDataService");
        try {
            xformService.createCRFMetaData(version, container, currentStudy, ub, html, submittedCrfName, submittedCrfVersionName, submittedCrfVersionDescription, submittedRevisionNotes, submittedXformText, items, errors);
        } catch (RuntimeException e) {
            logger.error("Error encountered while saving CRF: " + e.getMessage());
            logger.error(ExceptionUtils.getStackTrace(e));
            // and should be allow to crash the page for now
            if (!errors.hasErrors())
                throw e;
        }
    }
    // Save errors to request so they can be displayed to the user
    if (errors.hasErrors()) {
        request.setAttribute("errorList", errors.getAllErrors());
        logger.debug("Found at least one error.  CRF data not saved.");
    } else {
        logger.debug("Didn't find any errors.  CRF data saved.");
        // Save any media files uploaded with xform
        CrfBean crf = (submittedCrfName == null || submittedCrfName.equals("")) ? crfDao.findByCrfId(version.getCrfId()) : crfDao.findByName(submittedCrfName);
        CrfVersion newVersion = crfVersionDao.findByNameCrfId(submittedCrfVersionName, crf.getCrfId());
        saveAttachedMedia(items, crf, newVersion);
    }
    forwardPage(Page.CREATE_XFORM_CRF_VERSION_SERVLET);
}
Also used : Locale(java.util.Locale) CrfBean(org.akaza.openclinica.domain.datamap.CrfBean) Html(org.akaza.openclinica.domain.xform.dto.Html) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) XformContainer(org.akaza.openclinica.domain.xform.XformContainer) CrfDao(org.akaza.openclinica.dao.hibernate.CrfDao) CrfVersionDao(org.akaza.openclinica.dao.hibernate.CrfVersionDao) FileItem(org.apache.commons.fileupload.FileItem) Errors(org.springframework.validation.Errors) XformParser(org.akaza.openclinica.domain.xform.XformParser) XformMetaDataService(org.akaza.openclinica.service.crfdata.XformMetaDataService) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) CrfVersion(org.akaza.openclinica.domain.datamap.CrfVersion) CRFVersionBean(org.akaza.openclinica.bean.submit.CRFVersionBean) DataBinder(org.springframework.validation.DataBinder)

Aggregations

Locale (java.util.Locale)1 CRFVersionBean (org.akaza.openclinica.bean.submit.CRFVersionBean)1 CrfDao (org.akaza.openclinica.dao.hibernate.CrfDao)1 CrfVersionDao (org.akaza.openclinica.dao.hibernate.CrfVersionDao)1 CrfBean (org.akaza.openclinica.domain.datamap.CrfBean)1 CrfVersion (org.akaza.openclinica.domain.datamap.CrfVersion)1 XformContainer (org.akaza.openclinica.domain.xform.XformContainer)1 XformParser (org.akaza.openclinica.domain.xform.XformParser)1 Html (org.akaza.openclinica.domain.xform.dto.Html)1 XformMetaDataService (org.akaza.openclinica.service.crfdata.XformMetaDataService)1 FileItem (org.apache.commons.fileupload.FileItem)1 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)1 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)1 DataBinder (org.springframework.validation.DataBinder)1 Errors (org.springframework.validation.Errors)1