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