Search in sources :

Example 1 with ItemData

use of org.akaza.openclinica.domain.datamap.ItemData 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 ItemData

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

the class OpenRosaSubmissionController method createItemData.

protected void createItemData(Item item, String itemValue, EventCrf eventCrf, UserAccount userAccount) {
    ItemData itemData = new ItemData();
    itemData.setItem(item);
    itemData.setEventCrf(eventCrf);
    itemData.setValue(itemValue);
    itemData.setDateCreated(new Date());
    itemData.setStatus(org.akaza.openclinica.domain.Status.AVAILABLE);
    itemData.setOrdinal(1);
    itemData.setUserAccount(userAccount);
    itemData.setDeleted(false);
    itemDataDao.saveOrUpdate(itemData);
}
Also used : Date(java.util.Date) ItemData(org.akaza.openclinica.domain.datamap.ItemData)

Example 3 with ItemData

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

the class FSItemProcessor method processFieldSubmissionGroupItems.

private void processFieldSubmissionGroupItems(ArrayList<HashMap> listOfUploadFilePaths, Node repeatNode, Node itemNode, SubmissionContainer container, ItemGroup itemGroup) throws Exception {
    String itemName;
    Integer itemOrdinal = 1;
    String itemValue;
    // Node repeatGroupNode = itemNode.getParentNode();
    if (repeatNode != null) {
        final NamedNodeMap attributes = repeatNode.getAttributes();
        // check to see if groupNode has any enketo attributes
        for (int attrIndex = 0; attrIndex < attributes.getLength(); attrIndex++) {
            if (attributes.item(attrIndex).getNodeName().equals(ENKETO_ORDINAL)) {
                logger.debug("found enketo attribute");
                logger.debug(attributes.item(attrIndex).getNodeName());
                logger.debug(attributes.item(attrIndex).getNodeValue());
                itemOrdinal = new Integer(attributes.item(attrIndex).getNodeValue());
            }
        }
    } else {
        itemOrdinal = 1;
    }
    FormLayout formLayout = container.getFormLayout();
    CrfVersion crfVersion = crfVersionDao.findAllByCrfId(formLayout.getCrf().getCrfId()).get(0);
    container.setCrfVersion(crfVersion);
    Item item = null;
    ItemGroupMetadata igm = null;
    if (container.getRequestType() == FieldRequestTypeEnum.DELETE_FIELD) {
        List<ItemGroupMetadata> igms = itemGroupMetadataDao.findByItemGroupCrfVersion(itemGroup.getItemGroupId(), crfVersion.getCrfVersionId());
        for (ItemGroupMetadata ig : igms) {
            ItemData existingItemData = itemDataDao.findByItemEventCrfOrdinal(ig.getItem().getItemId(), container.getEventCrf().getEventCrfId(), itemOrdinal);
            // ItemData existingItemData = lookupFieldItemData(itemGroup, itemOrdinal, container);
            if (existingItemData != null) {
                existingItemData.setDeleted(true);
                existingItemData.setValue("");
                existingItemData.setOldStatus(existingItemData.getStatus());
                existingItemData.setUserAccount(container.getUser());
                existingItemData.setStatus(Status.AVAILABLE);
                existingItemData.setUpdateId(container.getUser().getUserId());
                existingItemData.setInstanceId(container.getInstanceId());
                existingItemData = itemDataDao.saveOrUpdate(existingItemData);
                resetSdvStatus(container);
                // Close discrepancy notes
                closeItemDiscrepancyNotes(container, existingItemData);
            }
        }
        return;
    }
    // igm = itemGroupMetadataDao.findByItemCrfVersion(item.getItemId(), crfVersion.getCrfVersionId());
    // Item loop
    QueryServiceHelperBean helperBean = new QueryServiceHelperBean();
    if (queryService.getQueryAttribute(helperBean, itemNode) != null) {
        queryService.process(helperBean, container, itemNode, itemOrdinal);
    } else if (shouldProcessItemNode(itemNode)) {
        itemName = itemNode.getNodeName().trim();
        itemValue = itemNode.getTextContent();
        item = itemDao.findByNameCrfId(itemNode.getNodeName(), crfVersion.getCrf().getCrfId());
        if (item == null) {
            logger.error("Failed to lookup item: '" + itemName + "'.  Continuing with submission.");
        }
        ItemFormMetadata itemFormMetadata = itemFormMetadataDao.findByItemCrfVersion(item.getItemId(), crfVersion.getCrfVersionId());
        // Convert space separated Enketo multiselect values to comma separated OC multiselect values
        Integer responseTypeId = itemFormMetadata.getResponseSet().getResponseType().getResponseTypeId();
        if (responseTypeId == 3 || responseTypeId == 7) {
            itemValue = itemValue.replaceAll(" ", ",");
        }
        if (responseTypeId == 4) {
            /*
                 * for (HashMap uploadFilePath : listOfUploadFilePaths) {
                 * if ((boolean) uploadFilePath.containsKey(itemValue) && itemValue != "") {
                 * itemValue = (String) uploadFilePath.get(itemValue);
                 * break;
                 * }
                 * }
                 */
            FormLayoutMedia media = formLayoutMediaDao.findByEventCrfIdAndFileName(container.getEventCrf().getEventCrfId(), itemValue);
            if (media == null) {
                media = new FormLayoutMedia();
            }
            media.setName(itemValue);
            media.setFormLayout(formLayout);
            media.setEventCrfId(container.getEventCrf().getEventCrfId());
            media.setPath("/" + container.getStudy().getOc_oid() + "/");
            formLayoutMediaDao.saveOrUpdate(media);
        }
        ItemData newItemData = createItemData(item, itemValue, itemOrdinal, container);
        Errors itemErrors = validateItemData(newItemData, item, responseTypeId);
        if (itemErrors.hasErrors()) {
            container.getErrors().addAllErrors(itemErrors);
            throw new Exception("Item validation error.  Rolling back submission changes.");
        }
        ItemData existingItemData = itemDataDao.findByItemEventCrfOrdinal(item.getItemId(), container.getEventCrf().getEventCrfId(), itemOrdinal);
        if (existingItemData == null) {
            newItemData.setStatus(Status.UNAVAILABLE);
            itemDataDao.saveOrUpdate(newItemData);
            resetSdvStatus(container);
        } else if (existingItemData.getValue().equals(newItemData.getValue())) {
        } else {
            // Existing item. Value changed. Update existing value.
            existingItemData.setInstanceId(container.getInstanceId());
            existingItemData.setValue(newItemData.getValue());
            existingItemData.setUpdateId(container.getUser().getUserId());
            existingItemData.setDateUpdated(new Date());
            itemDataDao.saveOrUpdate(existingItemData);
            resetSdvStatus(container);
        }
    }
}
Also used : FormLayout(org.akaza.openclinica.domain.datamap.FormLayout) NamedNodeMap(org.w3c.dom.NamedNodeMap) FormLayoutMedia(org.akaza.openclinica.domain.datamap.FormLayoutMedia) ItemFormMetadata(org.akaza.openclinica.domain.datamap.ItemFormMetadata) Date(java.util.Date) Item(org.akaza.openclinica.domain.datamap.Item) Errors(org.springframework.validation.Errors) CrfVersion(org.akaza.openclinica.domain.datamap.CrfVersion) ItemGroupMetadata(org.akaza.openclinica.domain.datamap.ItemGroupMetadata) ItemData(org.akaza.openclinica.domain.datamap.ItemData)

Example 4 with ItemData

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

the class ItemProcessor method processGroupItems.

private void processGroupItems(ArrayList<HashMap> listOfUploadFilePaths, HashMap<Integer, Set<Integer>> groupOrdinalMapping, Node groupNode, ItemGroup itemGroup, SubmissionContainer container) throws Exception {
    String itemName;
    String itemValue;
    if (itemGroup != null && !groupOrdinalMapping.containsKey(itemGroup.getItemGroupId())) {
        groupOrdinalMapping.put(itemGroup.getItemGroupId(), new TreeSet<Integer>());
    }
    NodeList itemNodeList = groupNode.getChildNodes();
    // Item loop
    for (int m = 0; m < itemNodeList.getLength(); m = m + 1) {
        Node itemNode = itemNodeList.item(m);
        if (ShouldProcessItemNode(itemNode)) {
            itemName = itemNode.getNodeName().trim();
            itemValue = itemNode.getTextContent();
            Item item = lookupItem(itemName, container.getCrfVersion());
            if (item == null) {
                logger.error("Failed to lookup item: '" + itemName + "'.  Continuing with submission.");
                continue;
            }
            ItemMetadata im = itemGroupMetadataDao.findMetadataByItemCrfVersion(item.getItemId(), container.getCrfVersion().getCrfVersionId());
            ItemGroupMetadata itemGroupMeta = im.getIgm();
            ItemFormMetadata itemFormMetadata = im.getIfm();
            // ItemFormMetadata itemFormMetadata = itemFormMetadataDao.findByItemCrfVersion(item.getItemId(),
            // crfVersion.getCrfVersionId());
            Integer itemOrdinal = getItemOrdinal(groupNode, itemGroupMeta.isRepeatingGroup(), container.getItems(), item);
            // Convert space separated Enketo multiselect values to comma separated OC multiselect values
            Integer responseTypeId = itemFormMetadata.getResponseSet().getResponseType().getResponseTypeId();
            if (responseTypeId == 3 || responseTypeId == 7) {
                itemValue = itemValue.replaceAll(" ", ",");
            }
            if (responseTypeId == 4) {
                for (HashMap uploadFilePath : listOfUploadFilePaths) {
                    if ((boolean) uploadFilePath.containsKey(itemValue) && itemValue != "") {
                        itemValue = (String) uploadFilePath.get(itemValue);
                        break;
                    }
                }
            }
            // Build set of submitted row numbers to be used to find deleted DB rows later
            Set<Integer> ordinals = groupOrdinalMapping.get(itemGroup.getItemGroupId());
            ordinals.add(itemOrdinal);
            groupOrdinalMapping.put(itemGroup.getItemGroupId(), ordinals);
            ItemData newItemData = createItemData(item, itemValue, itemOrdinal, container);
            Errors itemErrors = validateItemData(newItemData, item, responseTypeId);
            if (itemErrors.hasErrors()) {
                container.getErrors().addAllErrors(itemErrors);
                throw new Exception("Item validation error.  Rolling back submission changes.");
            } else {
                container.getItems().add(newItemData);
            }
            ItemData existingItemData = itemDataDao.findByItemEventCrfOrdinal(item.getItemId(), container.getEventCrf().getEventCrfId(), itemOrdinal);
            if (existingItemData == null) {
                // No existing value, create new item.
                if (newItemData.getOrdinal() < 0) {
                    newItemData.setOrdinal(itemDataDao.getMaxGroupRepeat(container.getEventCrf().getEventCrfId(), item.getItemId()) + 1);
                    groupOrdinalMapping.get(itemGroup.getItemGroupId()).add(newItemData.getOrdinal());
                }
                newItemData.setStatus(Status.UNAVAILABLE);
                newItemData.setInstanceId(container.getInstanceId());
                itemDataDao.saveOrUpdate(newItemData);
            } else if (existingItemData.getValue().equals(newItemData.getValue())) {
            // Existing item. Value unchanged. Do nothing.
            } else {
                // Existing item. Value changed. Update existing value.
                existingItemData.setValue(newItemData.getValue());
                existingItemData.setUpdateId(container.getUser().getUserId());
                existingItemData.setDateUpdated(new Date());
                itemDataDao.saveOrUpdate(existingItemData);
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ItemFormMetadata(org.akaza.openclinica.domain.datamap.ItemFormMetadata) Date(java.util.Date) ItemMetadata(org.akaza.openclinica.domain.datamap.ItemMetadata) Item(org.akaza.openclinica.domain.datamap.Item) Errors(org.springframework.validation.Errors) ItemGroupMetadata(org.akaza.openclinica.domain.datamap.ItemGroupMetadata) ItemData(org.akaza.openclinica.domain.datamap.ItemData)

Example 5 with ItemData

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

the class ItemProcessor method getItemOrdinal.

private Integer getItemOrdinal(Node groupNode, boolean isRepeating, List<ItemData> itemDataList, Item item) {
    if (!isRepeating)
        return 1;
    int ordinal = -1;
    NodeList items = groupNode.getChildNodes();
    for (int i = 0; i < items.getLength(); i++) {
        Node xmlItem = items.item(i);
        if (xmlItem instanceof Element && ((Element) xmlItem).getTagName().equals("OC.REPEAT_ORDINAL") && !((Element) xmlItem).getTextContent().equals(""))
            ordinal = Integer.valueOf(((Element) xmlItem).getTextContent());
    }
    // value must be reset to -1 as this is a new repeating group row.
    for (ItemData itemdata : itemDataList) {
        if (itemdata.getItem().getItemId() == item.getItemId() && itemdata.getOrdinal() == ordinal) {
            ordinal = -1;
            break;
        }
    }
    return ordinal;
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ItemData(org.akaza.openclinica.domain.datamap.ItemData)

Aggregations

ItemData (org.akaza.openclinica.domain.datamap.ItemData)12 Date (java.util.Date)5 ItemGroupMetadata (org.akaza.openclinica.domain.datamap.ItemGroupMetadata)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Item (org.akaza.openclinica.domain.datamap.Item)3 QueriesBean (org.akaza.openclinica.core.form.xform.QueriesBean)2 ItemFormMetadata (org.akaza.openclinica.domain.datamap.ItemFormMetadata)2 Query (org.hibernate.query.Query)2 Errors (org.springframework.validation.Errors)2 Node (org.w3c.dom.Node)2 NodeList (org.w3c.dom.NodeList)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Configuration (freemarker.template.Configuration)1 Template (freemarker.template.Template)1 File (java.io.File)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 LinkedHashMap (java.util.LinkedHashMap)1