Search in sources :

Example 1 with ResponseType

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

the class EditFormController method getPopulatedInstance.

private String getPopulatedInstance(CrfVersion crfVersion, EventCrf eventCrf) throws Exception {
    boolean isXform = false;
    if (crfVersion.getXform() != null && !crfVersion.getXform().equals(""))
        isXform = true;
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder build = docFactory.newDocumentBuilder();
    Document doc = build.newDocument();
    Element crfElement = null;
    if (isXform)
        crfElement = doc.createElement(crfVersion.getXformName());
    else
        crfElement = doc.createElement(crfVersion.getOcOid());
    doc.appendChild(crfElement);
    ArrayList<ItemGroup> itemGroups = itemGroupDao.findByCrfVersionId(crfVersion.getCrfVersionId());
    for (ItemGroup itemGroup : itemGroups) {
        ItemGroupMetadata itemGroupMetadata = itemGroupMetadataDao.findByItemGroupCrfVersion(itemGroup.getItemGroupId(), crfVersion.getCrfVersionId()).get(0);
        ArrayList<Item> items = (ArrayList<Item>) itemDao.findByItemGroupCrfVersionOrdered(itemGroup.getItemGroupId(), crfVersion.getCrfVersionId());
        // Get max repeat in item data
        int maxGroupRepeat = itemDataDao.getMaxGroupRepeat(eventCrf.getEventCrfId(), items.get(0).getItemId());
        // loop thru each repeat creating items in instance
        String repeatGroupMin = itemGroupMetadata.getRepeatNumber().toString();
        Boolean isrepeating = itemGroupMetadata.isRepeatingGroup();
        // TODO: Test empty group here (no items). make sure doesn't get nullpointer exception
        for (int i = 0; i < maxGroupRepeat; i++) {
            Element groupElement = null;
            if (isXform)
                groupElement = doc.createElement(itemGroup.getName());
            else
                groupElement = doc.createElement(itemGroup.getOcOid());
            Element repeatOrdinal = null;
            if (isrepeating) {
                repeatOrdinal = doc.createElement("OC.REPEAT_ORDINAL");
                repeatOrdinal.setTextContent(String.valueOf(i + 1));
                groupElement.appendChild(repeatOrdinal);
            }
            boolean hasItemData = false;
            for (Item item : items) {
                ItemFormMetadata itemMetadata = itemFormMetadataDao.findByItemCrfVersion(item.getItemId(), crfVersion.getCrfVersionId());
                ItemData itemData = itemDataDao.findByItemEventCrfOrdinal(item.getItemId(), eventCrf.getEventCrfId(), i + 1);
                Element question = null;
                if (crfVersion.getXform() != null && !crfVersion.getXform().equals(""))
                    question = doc.createElement(item.getName());
                else
                    question = doc.createElement(item.getOcOid());
                if (itemData != null && itemData.getValue() != null && !itemData.getValue().equals("")) {
                    ResponseType responseType = responseTypeDao.findByItemFormMetaDataId(itemMetadata.getItemFormMetadataId());
                    String itemValue = itemData.getValue();
                    if (responseType.getResponseTypeId() == 3 || responseType.getResponseTypeId() == 7) {
                        itemValue = itemValue.replaceAll(",", " ");
                    }
                    question.setTextContent(itemValue);
                }
                if (itemData == null || !itemData.isDeleted()) {
                    hasItemData = true;
                    groupElement.appendChild(question);
                }
            }
            // end of item
            if (hasItemData) {
                crfElement.appendChild(groupElement);
            }
        }
    }
    // end of group
    TransformerFactory transformFactory = TransformerFactory.newInstance();
    Transformer transformer = transformFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    DOMSource source = new DOMSource(doc);
    transformer.transform(source, result);
    String instance = writer.toString();
    System.out.println("Editable instance = " + instance);
    return instance;
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) ItemFormMetadata(org.akaza.openclinica.domain.datamap.ItemFormMetadata) ResponseType(org.akaza.openclinica.domain.datamap.ResponseType) Item(org.akaza.openclinica.domain.datamap.Item) ItemGroup(org.akaza.openclinica.domain.datamap.ItemGroup) StringWriter(java.io.StringWriter) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ItemGroupMetadata(org.akaza.openclinica.domain.datamap.ItemGroupMetadata) ItemData(org.akaza.openclinica.domain.datamap.ItemData)

Example 2 with ResponseType

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

the class XformMetaDataService method createGroups.

private void createGroups(XformContainer container, Html html, String submittedXformText, CrfBean crf, CrfVersion version, Section section, UserAccountBean ub, Errors errors) throws Exception {
    Integer itemOrdinal = 1;
    ArrayList<String> usedGroupOids = new ArrayList<String>();
    ArrayList<String> usedItemOids = new ArrayList<String>();
    List<Group> htmlGroups = html.getBody().getGroup();
    for (Group htmlGroup : htmlGroups) {
        XformGroup xformGroup = container.findGroupByRef(htmlGroup.getRef());
        ItemGroup itemGroup = itemGroupDao.findByNameCrfId(xformGroup.getGroupName(), crf);
        if (itemGroup == null) {
            itemGroup = new ItemGroup();
            itemGroup.setName(xformGroup.getGroupName());
            itemGroup.setCrf(crf);
            itemGroup.setStatus(org.akaza.openclinica.domain.Status.AVAILABLE);
            itemGroup.setUserAccount(userDao.findById(ub.getId()));
            itemGroup.setOcOid(itemGroupDao.getValidOid(new ItemGroup(), crf.getName(), xformGroup.getGroupName(), usedGroupOids));
            usedGroupOids.add(itemGroup.getOcOid());
            Integer itemGroupId = (Integer) itemGroupDao.save(itemGroup);
            itemGroup.setItemGroupId(itemGroupId);
        }
        List<UserControl> widgets = null;
        boolean isRepeating = false;
        if (htmlGroup.getRepeat() != null && htmlGroup.getRepeat().getUsercontrol() != null) {
            widgets = htmlGroup.getRepeat().getUsercontrol();
            isRepeating = true;
        } else {
            widgets = htmlGroup.getUsercontrol();
        }
        // Create Item specific DB entries: item, response_set,item_form_metadata,versioning_map,item_group_metadata
        for (UserControl widget : widgets) {
            // Skip reserved name and read-only items here
            XformItem xformItem = container.findItemByGroupAndRef(xformGroup, widget.getRef());
            String readonly = html.getHead().getModel().getBindByNodeSet(widget.getRef()).getReadOnly();
            if (!xformItem.getItemName().equals("OC.STUDY_SUBJECT_ID") && !xformItem.getItemName().equals("OC.STUDY_SUBJECT_ID_CONFIRM") && (readonly == null || !readonly.trim().equals("true()"))) {
                Item item = createItem(html, widget, xformGroup, xformItem, crf, ub, usedItemOids, errors);
                if (item != null) {
                    ResponseType responseType = getResponseType(html, xformItem);
                    ResponseSet responseSet = responseSetService.getResponseSet(html, submittedXformText, xformItem, version, responseType, item, errors);
                    createItemFormMetadata(html, xformItem, item, responseSet, section, version, itemOrdinal);
                    createVersioningMap(version, item);
                    createItemGroupMetadata(html, item, version, itemGroup, isRepeating, itemOrdinal);
                    itemOrdinal++;
                }
            }
        }
    }
}
Also used : XformGroup(org.akaza.openclinica.domain.xform.XformGroup) Group(org.akaza.openclinica.domain.xform.dto.Group) ItemGroup(org.akaza.openclinica.domain.datamap.ItemGroup) ArrayList(java.util.ArrayList) XformGroup(org.akaza.openclinica.domain.xform.XformGroup) ResponseSet(org.akaza.openclinica.domain.datamap.ResponseSet) ResponseType(org.akaza.openclinica.domain.datamap.ResponseType) UserControl(org.akaza.openclinica.domain.xform.dto.UserControl) Item(org.akaza.openclinica.domain.datamap.Item) XformItem(org.akaza.openclinica.domain.xform.XformItem) FileItem(org.apache.commons.fileupload.FileItem) XformItem(org.akaza.openclinica.domain.xform.XformItem) ItemGroup(org.akaza.openclinica.domain.datamap.ItemGroup)

Example 3 with ResponseType

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

Aggregations

ArrayList (java.util.ArrayList)3 Item (org.akaza.openclinica.domain.datamap.Item)3 ItemGroup (org.akaza.openclinica.domain.datamap.ItemGroup)3 ResponseType (org.akaza.openclinica.domain.datamap.ResponseType)3 ItemFormMetadata (org.akaza.openclinica.domain.datamap.ItemFormMetadata)2 ItemGroupMetadata (org.akaza.openclinica.domain.datamap.ItemGroupMetadata)2 ResponseSet (org.akaza.openclinica.domain.datamap.ResponseSet)2 XformGroup (org.akaza.openclinica.domain.xform.XformGroup)2 XformItem (org.akaza.openclinica.domain.xform.XformItem)2 FileItem (org.apache.commons.fileupload.FileItem)2 StringWriter (java.io.StringWriter)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 Transformer (javax.xml.transform.Transformer)1 TransformerFactory (javax.xml.transform.TransformerFactory)1 DOMSource (javax.xml.transform.dom.DOMSource)1 StreamResult (javax.xml.transform.stream.StreamResult)1 ItemData (org.akaza.openclinica.domain.datamap.ItemData)1 VersioningMap (org.akaza.openclinica.domain.datamap.VersioningMap)1 Group (org.akaza.openclinica.domain.xform.dto.Group)1