Search in sources :

Example 1 with ItemGroup

use of org.akaza.openclinica.domain.datamap.ItemGroup in project OpenClinica by OpenClinica.

the class EnketoUrlService method populateInstance.

private String populateInstance(CrfVersion crfVersion, FormLayout formLayout, EventCrf eventCrf, String studyOid, String flavor) throws Exception {
    Map<String, Object> data = new HashMap<String, Object>();
    List<ItemGroup> igs = itemGroupDao.findByCrfVersionId(crfVersion.getCrfVersionId());
    for (ItemGroup ig : igs) {
        List<HashMap<String, Object>> hashMapList = new ArrayList<HashMap<String, Object>>();
        List<ItemGroupMetadata> igms = itemGroupMetadataDao.findByItemGroupCrfVersion(ig.getItemGroupId(), crfVersion.getCrfVersionId());
        int maxRowCount = itemDataDao.getMaxCountByEventCrfGroup(eventCrf.getEventCrfId(), ig.getItemGroupId());
        HashMap<String, Object> hashMap = null;
        if (igms.get(0).isRepeatingGroup() && maxRowCount == 0) {
            hashMap = new HashMap<>();
            hashMap.put("index", 1);
            hashMap.put("lastUsedOrdinal", 1);
            for (ItemGroupMetadata igm : igms) {
                hashMap.put(igm.getItem().getName(), "");
                if (flavor.equals(QUERY_FLAVOR))
                    hashMap.put(igm.getItem().getName() + QUERY_SUFFIX, "");
            }
            hashMapList.add(hashMap);
            data.put(ig.getName(), hashMapList);
        }
        boolean rowDeleted = false;
        if (igms.get(0).isRepeatingGroup()) {
            for (int i = 0; i < maxRowCount; i++) {
                rowDeleted = false;
                for (ItemGroupMetadata igm : igms) {
                    ItemData itemData = itemDataDao.findByItemEventCrfOrdinalDeleted(igm.getItem().getItemId(), eventCrf.getEventCrfId(), i + 1);
                    if (itemData != null) {
                        rowDeleted = true;
                        break;
                    }
                }
                if (!rowDeleted) {
                    hashMap = new HashMap<>();
                    hashMap.put("index", i + 1);
                    if (i == 0) {
                        hashMap.put("lastUsedOrdinal", maxRowCount);
                    }
                    for (ItemGroupMetadata igm : igms) {
                        ItemData itemData = itemDataDao.findByItemEventCrfOrdinal(igm.getItem().getItemId(), eventCrf.getEventCrfId(), i + 1);
                        String itemValue = getItemValue(itemData, crfVersion);
                        hashMap.put(igm.getItem().getName(), itemData != null ? itemValue : "");
                        if (flavor.equals(QUERY_FLAVOR)) {
                            if (itemData != null) {
                                ObjectMapper mapper = new ObjectMapper();
                                QueriesBean queriesBean = buildQueryElement(itemData);
                                hashMap.put(igm.getItem().getName() + QUERY_SUFFIX, queriesBean != null ? mapper.writeValueAsString(queriesBean) : "");
                            } else {
                                hashMap.put(igm.getItem().getName() + QUERY_SUFFIX, "");
                            }
                        }
                    }
                    hashMapList.add(hashMap);
                }
            }
        }
        if (igms.get(0).isRepeatingGroup() && maxRowCount != 0) {
            data.put(ig.getName(), hashMapList);
        }
        if (!igms.get(0).isRepeatingGroup()) {
            for (ItemGroupMetadata igm : igms) {
                ItemData itemData = itemDataDao.findByItemEventCrfOrdinal(igm.getItem().getItemId(), eventCrf.getEventCrfId(), 1);
                String itemValue = getItemValue(itemData, crfVersion);
                data.put(igm.getItem().getName(), itemData != null ? itemValue : "");
                if (flavor.equals(QUERY_FLAVOR)) {
                    if (itemData != null) {
                        ObjectMapper mapper = new ObjectMapper();
                        QueriesBean queriesBean = buildQueryElement(itemData);
                        data.put(igm.getItem().getName() + QUERY_SUFFIX, queriesBean != null ? mapper.writeValueAsString(queriesBean) : "");
                    } else {
                        data.put(igm.getItem().getName() + QUERY_SUFFIX, "");
                    }
                }
            }
        }
    }
    String templateStr = null;
    CrfBean crfBean = crfDao.findById(formLayout.getCrf().getCrfId());
    String directoryPath = Utils.getCrfMediaFilePath(crfBean.getOcOid(), formLayout.getOcOid());
    File dir = new File(directoryPath);
    File[] directoryListing = dir.listFiles();
    if (directoryListing != null) {
        for (File child : directoryListing) {
            if (flavor.equals(QUERY_FLAVOR) && child.getName().endsWith(INSTANCE_QUERIES_SUFFIX) || flavor.equals(NO_FLAVOR) && child.getName().endsWith(INSTANCE_SUFFIX)) {
                templateStr = new String(Files.readAllBytes(Paths.get(child.getPath())));
                break;
            }
        }
    }
    Template template = new Template("template name", new StringReader(templateStr), new Configuration());
    StringWriter wtr = new StringWriter();
    template.process(data, wtr);
    String instance = wtr.toString();
    System.out.println(instance);
    return instance;
}
Also used : CrfBean(org.akaza.openclinica.domain.datamap.CrfBean) Configuration(freemarker.template.Configuration) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Template(freemarker.template.Template) ItemGroup(org.akaza.openclinica.domain.datamap.ItemGroup) StringWriter(java.io.StringWriter) ItemGroupMetadata(org.akaza.openclinica.domain.datamap.ItemGroupMetadata) QueriesBean(org.akaza.openclinica.core.form.xform.QueriesBean) StringReader(java.io.StringReader) File(java.io.File) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ItemData(org.akaza.openclinica.domain.datamap.ItemData)

Example 2 with ItemGroup

use of org.akaza.openclinica.domain.datamap.ItemGroup in project OpenClinica by OpenClinica.

the class XformMetaDataService method createGroups.

private void createGroups(XformContainer container, CrfBean crf, CrfVersion crfVersion, FormLayout formLayout, Section section, UserAccountBean ub, Errors errors) throws Exception {
    Integer itemOrdinal = 1;
    ArrayList<String> usedGroupOids = new ArrayList<String>();
    ArrayList<String> usedItemOids = new ArrayList<String>();
    // for (Group htmlGroup : htmlGroups) {
    for (XformGroup xformGroup : container.getGroups()) {
        // XformGroup xformGroup = container.findGroupByRef(htmlGroup.getRef());
        ItemGroup itemGroup = itemGroupDao.findByNameCrfId(xformGroup.getGroupName(), crf);
        if (itemGroup == null) {
            itemGroup = new ItemGroup();
            itemGroup.setName(xformGroup.getGroupName());
            itemGroup.setLayoutGroupPath(xformGroup.getGroupPath());
            itemGroup.setCrf(crf);
            itemGroup.setStatus(org.akaza.openclinica.domain.Status.AVAILABLE);
            itemGroup.setUserAccount(userDao.findById(ub.getId()));
            itemGroup.setOcOid(xformGroup.getGroupOid());
            usedGroupOids.add(itemGroup.getOcOid());
            itemGroup = itemGroupDao.saveOrUpdate(itemGroup);
        } else {
            itemGroup.setName(xformGroup.getGroupName());
            itemGroup = itemGroupDao.saveOrUpdate(itemGroup);
        }
        boolean isRepeating = xformGroup.isRepeating();
        for (XformItem xformItem : xformGroup.getItems()) {
            Item item = createItem(xformGroup, xformItem, crf, ub, usedItemOids, errors);
            if (item != null) {
                ResponseType responseType = getResponseType(xformItem);
                ResponseSet responseSet = responseSetService.getResponseSet(xformItem, crfVersion, responseType, item, errors);
                // add if statement
                ItemFormMetadata ifmd = itemFormMetadataDao.findByItemCrfVersion(item.getItemId(), crfVersion.getCrfVersionId());
                if (ifmd == null) {
                    ifmd = createItemFormMetadata(xformItem, item, responseSet, section, crfVersion, itemOrdinal);
                } else {
                    ifmd.setRequired(xformItem.isRequired());
                    ifmd.setLeftItemText(xformItem.getLeftItemText());
                    ifmd.setItem(item);
                    ifmd.setResponseSet(responseSet);
                    ifmd = itemFormMetadataDao.saveOrUpdate(ifmd);
                }
                ArrayList<VersioningMap> vm = versioningMapDao.findByVersionIdFormLayoutIdAndItemId(crfVersion.getCrfVersionId(), formLayout.getFormLayoutId(), item.getItemId(), itemOrdinal);
                if (vm.size() == 0) {
                    createVersioningMap(crfVersion, item, formLayout, xformItem.getItemOrderInForm());
                }
                //
                ItemGroupMetadata igmd = itemGroupMetadataDao.findByItemCrfVersion(item.getItemId(), crfVersion.getCrfVersionId());
                if (igmd == null) {
                    igmd = createItemGroupMetadata(item, crfVersion, itemGroup, isRepeating, itemOrdinal);
                }
                itemOrdinal++;
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) XformGroup(org.akaza.openclinica.domain.xform.XformGroup) ResponseSet(org.akaza.openclinica.domain.datamap.ResponseSet) ItemFormMetadata(org.akaza.openclinica.domain.datamap.ItemFormMetadata) VersioningMap(org.akaza.openclinica.domain.datamap.VersioningMap) ResponseType(org.akaza.openclinica.domain.datamap.ResponseType) Item(org.akaza.openclinica.domain.datamap.Item) XformItem(org.akaza.openclinica.domain.xform.XformItem) FileItem(org.apache.commons.fileupload.FileItem) DiskFileItem(org.apache.commons.fileupload.disk.DiskFileItem) XformItem(org.akaza.openclinica.domain.xform.XformItem) ItemGroup(org.akaza.openclinica.domain.datamap.ItemGroup) ItemGroupMetadata(org.akaza.openclinica.domain.datamap.ItemGroupMetadata)

Example 3 with ItemGroup

use of org.akaza.openclinica.domain.datamap.ItemGroup in project OpenClinica by OpenClinica.

the class FSItemProcessor method process.

public ProcessorEnum process(SubmissionContainer container) throws Exception {
    logger.info("Executing FSItem Processor.");
    // TODO keep this flag
    if (container.isFieldSubmissionFlag() != true)
        return ProcessorEnum.PROCEED;
    ArrayList<HashMap> listOfUploadFilePaths = container.getListOfUploadFilePaths();
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(container.getRequestBody()));
    Document doc = db.parse(is);
    Set<Node> itemNodeSet = new HashSet();
    Set<Node> repeatNodeSet = new HashSet();
    Node itemNode = null;
    Node repeatNode = null;
    NodeList instanceNodeList = doc.getElementsByTagName("instance");
    // Instance loop
    for (int i = 0; i < instanceNodeList.getLength(); i = i + 1) {
        Node instanceNode = instanceNodeList.item(i);
        if (instanceNode instanceof Element) {
            repeatNodeSet = xformParserHelper.instanceEnketoAttr(instanceNode, repeatNodeSet);
            if (repeatNodeSet.size() != 0) {
                repeatNode = repeatNodeSet.iterator().next();
            }
            ItemGroup itemGroup = null;
            if (container.getRequestType() == FieldRequestTypeEnum.DELETE_FIELD) {
                List<String> instanceItemsPath = new ArrayList<>();
                instanceItemsPath = xformParserHelper.instanceItemPaths(instanceNode, instanceItemsPath, "", null);
                List<ItemGroup> itemGroups = itemGroupDao.findByCrfVersionId(container.getCrfVersion().getCrfVersionId());
                int idx = instanceItemsPath.get(0).lastIndexOf("/");
                String rPath = instanceItemsPath.get(0).substring(idx + 1);
                for (ItemGroup ig : itemGroups) {
                    if (ig.getLayoutGroupPath() != null && ig.getLayoutGroupPath().equals(rPath)) {
                        itemGroup = ig;
                        break;
                    }
                }
            }
            itemNodeSet = xformParserHelper.instanceItemNodes(instanceNode, itemNodeSet);
            if (itemNodeSet.size() != 0) {
                itemNode = itemNodeSet.iterator().next();
                processFieldSubmissionGroupItems(listOfUploadFilePaths, repeatNode, itemNode, container, itemGroup);
            }
        }
    }
    return ProcessorEnum.PROCEED;
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) HashMap(java.util.HashMap) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) ItemGroup(org.akaza.openclinica.domain.datamap.ItemGroup) DocumentBuilder(javax.xml.parsers.DocumentBuilder) StringReader(java.io.StringReader) HashSet(java.util.HashSet)

Example 4 with ItemGroup

use of org.akaza.openclinica.domain.datamap.ItemGroup in project OpenClinica by OpenClinica.

the class ItemProcessor method process.

public ProcessorEnum process(SubmissionContainer container) throws Exception {
    logger.info("Executing Item Processor.");
    if (container.isFieldSubmissionFlag()) {
        return ProcessorEnum.PROCEED;
    }
    ArrayList<HashMap> listOfUploadFilePaths = container.getListOfUploadFilePaths();
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    InputSource is = new InputSource();
    is.setCharacterStream(new StringReader(container.getRequestBody()));
    Document doc = db.parse(is);
    String itemName;
    String itemValue;
    String groupNodeName = "";
    NodeList instanceNodeList = doc.getElementsByTagName("instance");
    // Instance loop
    for (int i = 0; i < instanceNodeList.getLength(); i = i + 1) {
        Node instanceNode = instanceNodeList.item(i);
        if (instanceNode instanceof Element) {
            NodeList crfNodeList = instanceNode.getChildNodes();
            // Form loop
            for (int j = 0; j < crfNodeList.getLength(); j = j + 1) {
                Node crfNode = crfNodeList.item(j);
                if (crfNode instanceof Element) {
                    CrfVersion crfVersion = container.getCrfVersion();
                    HashMap<Integer, Set<Integer>> groupOrdinalMapping = new HashMap<Integer, Set<Integer>>();
                    NodeList groupNodeList = crfNode.getChildNodes();
                    // Group loop
                    for (int k = 0; k < groupNodeList.getLength(); k = k + 1) {
                        Node groupNode = groupNodeList.item(k);
                        if (groupNode instanceof Element && !groupNode.getNodeName().startsWith("SECTION_")) {
                            groupNodeName = groupNode.getNodeName();
                            ItemGroup itemGroup = lookupItemGroup(groupNodeName, crfVersion);
                            if (itemGroup == null) {
                                logger.error("Failed to lookup item group: '" + groupNodeName + "'.  Continuing with submission.");
                                continue;
                            }
                            processGroupItems(listOfUploadFilePaths, groupOrdinalMapping, groupNode, itemGroup, container);
                        }
                    }
                    // Delete rows that have been removed
                    removeDeletedRows(groupOrdinalMapping, container);
                }
            }
        }
    }
    return ProcessorEnum.PROCEED;
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TreeSet(java.util.TreeSet) Set(java.util.Set) HashMap(java.util.HashMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) ItemGroup(org.akaza.openclinica.domain.datamap.ItemGroup) DocumentBuilder(javax.xml.parsers.DocumentBuilder) CrfVersion(org.akaza.openclinica.domain.datamap.CrfVersion) StringReader(java.io.StringReader)

Aggregations

ItemGroup (org.akaza.openclinica.domain.datamap.ItemGroup)4 StringReader (java.io.StringReader)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 DocumentBuilder (javax.xml.parsers.DocumentBuilder)2 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)2 ItemGroupMetadata (org.akaza.openclinica.domain.datamap.ItemGroupMetadata)2 Document (org.w3c.dom.Document)2 Element (org.w3c.dom.Element)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2 InputSource (org.xml.sax.InputSource)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Configuration (freemarker.template.Configuration)1 Template (freemarker.template.Template)1 File (java.io.File)1 StringWriter (java.io.StringWriter)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 TreeSet (java.util.TreeSet)1