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;
}
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++;
}
}
}
}
}
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++;
}
}
}
}
Aggregations