Search in sources :

Example 1 with XformContainer

use of org.akaza.openclinica.domain.xform.XformContainer in project OpenClinica by OpenClinica.

the class XformMetaDataService method executeIndividualCrf.

public void executeIndividualCrf(ExecuteIndividualCrfObject eicObject) {
    for (OCodmComplexTypeDefinitionFormLayoutDef formLayoutDef : eicObject.formLayoutDefs) {
        List<String> fileLinks = null;
        String vForm = "";
        RestTemplate rest = new RestTemplate();
        if (eicObject.form != null) {
            List<FormVersion> versions = eicObject.form.getVersions();
            for (FormVersion version : versions) {
                if (version.getName().equals(formLayoutDef.getOID())) {
                    fileLinks = version.getFileLinks();
                    for (String fileLink : fileLinks) {
                        if (fileLink.endsWith(VERSION)) {
                            vForm = rest.getForObject(fileLink, String.class);
                            break;
                        }
                    }
                    if (!eicObject.errors.hasErrors()) {
                        ObjectMapper mapper = new ObjectMapper();
                        TypeReference<List<XformGroup>> mapType = new TypeReference<List<XformGroup>>() {
                        };
                        List<XformGroup> jsonList = null;
                        try {
                            jsonList = mapper.readValue(vForm, mapType);
                        } catch (JsonParseException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (JsonMappingException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                        XformContainer xformContainer = new XformContainer();
                        xformContainer.setGroups(jsonList);
                        eicObject.setContainer(xformContainer);
                        if (eicObject.errors.hasErrors()) {
                            return;
                        }
                        // Save meta-data in database
                        saveFormMetadata(eicObject, version, eicObject.container, formLayoutDef, fileLinks);
                    }
                }
            }
        }
    }
}
Also used : OCodmComplexTypeDefinitionFormLayoutDef(org.openclinica.ns.odm_ext_v130.v31.OCodmComplexTypeDefinitionFormLayoutDef) XformGroup(org.akaza.openclinica.domain.xform.XformGroup) IOException(java.io.IOException) JsonParseException(com.fasterxml.jackson.core.JsonParseException) XformContainer(org.akaza.openclinica.domain.xform.XformContainer) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) RestTemplate(org.springframework.web.client.RestTemplate) List(java.util.List) ArrayList(java.util.ArrayList) TypeReference(com.fasterxml.jackson.core.type.TypeReference) FormVersion(org.akaza.openclinica.service.dto.FormVersion) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with XformContainer

use of org.akaza.openclinica.domain.xform.XformContainer in project OpenClinica by OpenClinica.

the class CreateXformCRFVersionServlet method parseInstance.

private XformContainer parseInstance(String xform) throws Exception {
    // Could use the following xpath to get all leaf nodes in the case
    // of multiple levels of groups: //*[count(./*) = 0]
    // For now will assume a structure of /form/item or /form/group/item
    Document doc = null;
    try {
        InputStream stream = new ByteArrayInputStream(xform.getBytes(StandardCharsets.UTF_8));
        doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(stream);
        NodeList instances = doc.getElementsByTagName("instance");
        // All whitespace outside tags gets parsed as Text objects and returned
        // by the various Node methods. We need to ignore these and
        // focus on actual Elements
        Element instance = null;
        // List<XformItem> items = new ArrayList<XformItem>();
        List<XformGroup> groups = new ArrayList<XformGroup>();
        // Get the primary instance
        for (int i = 0; i < instances.getLength(); i++) {
            Element curInstance = (Element) instances.item(i);
            if (curInstance instanceof Element) {
                instance = curInstance;
                break;
            }
        }
        // Get the form element
        Element form = null;
        for (int i = 0; i < instance.getChildNodes().getLength(); i++) {
            Node curNode = instance.getChildNodes().item(i);
            if (curNode instanceof Element) {
                form = (Element) curNode;
                break;
            }
        }
        // Get the groups and grouped items
        for (int i = 0; i < form.getChildNodes().getLength(); i++) {
            if (form.getChildNodes().item(i) instanceof Element && ((Element) form.getChildNodes().item(i)).hasChildNodes() && !((Element) form.getChildNodes().item(i)).getTagName().equals("meta")) {
                Element group = (Element) form.getChildNodes().item(i);
                XformGroup newGroup = new XformGroup();
                newGroup.setGroupName(group.getTagName());
                newGroup.setGroupPath("/" + form.getTagName() + "/" + group.getTagName());
                groups.add(newGroup);
                for (int j = 0; j < group.getChildNodes().getLength(); j++) {
                    if (group.getChildNodes().item(j) instanceof Element) {
                        Element item = (Element) group.getChildNodes().item(j);
                        XformItem newItem = new XformItem();
                        newItem.setItemPath("/" + form.getTagName() + "/" + group.getTagName() + "/" + item.getTagName());
                        newItem.setItemName(item.getTagName());
                        // group is null;
                        newGroup.getItems().add(newItem);
                    }
                }
            }
        }
        XformContainer container = new XformContainer();
        container.setGroups(groups);
        container.setInstanceName(form.getTagName());
        return container;
    } catch (Exception e) {
        logger.error(e.getMessage());
        logger.error(ExceptionUtils.getStackTrace(e));
        throw new Exception(e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) XformGroup(org.akaza.openclinica.domain.xform.XformGroup) Document(org.w3c.dom.Document) XformContainer(org.akaza.openclinica.domain.xform.XformContainer) InsufficientPermissionException(org.akaza.openclinica.web.InsufficientPermissionException) OpenClinicaSystemException(org.akaza.openclinica.exception.OpenClinicaSystemException) XformItem(org.akaza.openclinica.domain.xform.XformItem) ByteArrayInputStream(java.io.ByteArrayInputStream)

Example 3 with XformContainer

use of org.akaza.openclinica.domain.xform.XformContainer 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

XformContainer (org.akaza.openclinica.domain.xform.XformContainer)3 ArrayList (java.util.ArrayList)2 XformGroup (org.akaza.openclinica.domain.xform.XformGroup)2 JsonParseException (com.fasterxml.jackson.core.JsonParseException)1 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 List (java.util.List)1 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 XformItem (org.akaza.openclinica.domain.xform.XformItem)1 XformParser (org.akaza.openclinica.domain.xform.XformParser)1 Html (org.akaza.openclinica.domain.xform.dto.Html)1