use of org.akaza.openclinica.core.form.xform.QueriesBean 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.core.form.xform.QueriesBean in project OpenClinica by OpenClinica.
the class QueryServiceImpl method process.
@Override
public void process(QueryServiceHelperBean helperBean, SubmissionContainer container, Node itemNode, int itemOrdinal) throws Exception {
String node = itemNode.getTextContent();
if (StringUtils.isEmpty(node))
return;
helperBean.setContainer(container);
helperBean.setItemOrdinal(itemOrdinal);
helperBean.setItemNode(itemNode);
ItemData id = getItemData(helperBean);
if (id == null) {
helperBean.setItemData(createBlankItemData(helperBean));
} else {
helperBean.setItemData(id);
}
QueriesBean queries = null;
try {
ObjectMapper objectMapper = new ObjectMapper();
queries = objectMapper.readValue(node, QueriesBean.class);
} catch (IOException e) {
logger.error(e.getMessage());
throw e;
}
List<Integer> idList = new ArrayList();
List<QueryBean> qBeans = queries.getQueries();
QueryBean queryBean = null;
DiscrepancyNote childDN = null;
DiscrepancyNote parentDN = null;
List<DiscrepancyNote> childDns = null;
if (qBeans.size() > 0) {
for (QueryBean qBean : qBeans) {
idList.add(Integer.valueOf(qBean.getId()));
}
Collections.reverse(idList);
queryBean = qBeans.get(0);
parentDN = findQueryParent(helperBean);
if (parentDN == null) {
parentDN = createQuery(helperBean, queryBean);
parentDN = discrepancyNoteDao.saveOrUpdate(parentDN);
helperBean.setDn(parentDN);
saveQueryItemDatamap(helperBean);
}
childDns = findChildQueries(helperBean);
if (childDns.size() < qBeans.size()) {
// Enketo passes JSON "id" attribute for unsubmitted queries only
// if (StringUtils.isEmpty(queryBean.getId())){
childDN = createQuery(helperBean, queryBean);
childDN.setParentDiscrepancyNote(parentDN);
childDN = discrepancyNoteDao.saveOrUpdate(childDN);
parentDN.setUserAccount(childDN.getUserAccount());
setResolutionStatus(queryBean, parentDN);
parentDN.setUserAccountByOwnerId(helperBean.getContainer().getUser());
parentDN.setDetailedNotes(childDN.getDetailedNotes());
parentDN.setDiscrepancyNoteType(childDN.getDiscrepancyNoteType());
parentDN = discrepancyNoteDao.saveOrUpdate(parentDN);
helperBean.setDn(childDN);
saveQueryItemDatamap(helperBean);
handleEmailNotification(helperBean, queryBean);
}
}
}
use of org.akaza.openclinica.core.form.xform.QueriesBean in project OpenClinica by OpenClinica.
the class EnketoUrlService method buildQueryElement.
public QueriesBean buildQueryElement(ItemData itemdata) {
QueriesBean queryElement = new QueriesBean();
List<QueryBean> queryBeans = new ArrayList<>();
List<LogBean> logBeans = new ArrayList<LogBean>();
List<DiscrepancyNote> dns = discrepancyNoteDao.findChildQueriesByItemData(itemdata.getItemDataId());
int i = 0;
for (DiscrepancyNote dn : dns) {
i++;
QueryBean query = new QueryBean();
query.setId(String.valueOf(i));
query.setAssigned_to(dn.getUserAccountByOwnerId().getUserName());
query.setComment(escapedValue(dn.getDetailedNotes()));
query.setStatus(dn.getResolutionStatus().getName().toLowerCase());
DateTime dateTime = new DateTime(dn.getDateCreated());
query.setDate_time(convertDateFormat(dateTime));
query.setNotify(false);
query.setUser(dn.getUserAccountByOwnerId().getUserName());
query.setType(COMMENT);
queryBeans.add(query);
}
AuditLogEvent auditLog = new AuditLogEvent();
auditLog.setEntityId(new Integer(itemdata.getItemDataId()));
auditLog.setAuditTable(ITEMDATA);
ArrayList<AuditLogEvent> auditLogEvents = auditLogEventDao.findByParam(auditLog, null);
for (AuditLogEvent audit : auditLogEvents) {
LogBean logBean = new LogBean();
String oldValue = audit.getOldValue() != null ? audit.getOldValue() : "";
String newValue = audit.getNewValue() != null ? audit.getNewValue() : "";
logBean.setMessage("Value Changed from \"" + escapedValue(oldValue) + "\" to \"" + escapedValue(newValue) + "\"");
DateTime dateTime = new DateTime(audit.getAuditDate());
logBean.setDate_time(convertDateFormat(dateTime));
UserAccount uAccount = userAccountDao.findById(audit.getUserAccount().getUserId());
logBean.setUser(uAccount.getUserName());
logBean.setAssigned_to(uAccount.getUserName());
logBean.setType(AUDIT);
logBeans.add(logBean);
}
queryElement.setQueries(queryBeans);
queryElement.setLogs(logBeans);
if (queryElement.getQueries().size() != 0 || queryElement.getLogs().size() != 0)
return queryElement;
else
return null;
}
Aggregations