use of org.akaza.openclinica.domain.datamap.CrfBean in project OpenClinica by OpenClinica.
the class XformMetaDataService method createCRFMetaData.
@Transactional
public FormLayout createCRFMetaData(CrfMetaDataObject cmdObject) throws Exception {
CrfVersion crfVersion = null;
FormLayout formLayout = null;
CrfBean crfBean = null;
Section section = null;
crfBean = (CrfBean) crfDao.findByOcOID(cmdObject.crf.getOcoid());
if (crfBean != null) {
crfBean.setUpdateId(cmdObject.ub.getId());
crfBean.setDateUpdated(new Date());
crfBean = crfDao.saveOrUpdate(crfBean);
formLayout = formLayoutDao.findByOcOID(cmdObject.version.getOcoid());
if (formLayout == null) {
formLayout = new FormLayout();
formLayout = populateFormLayout(formLayout, crfBean, cmdObject);
formLayout = formLayoutDao.saveOrUpdate(formLayout);
}
crfVersion = crfVersionDao.findAllByCrfId(crfBean.getCrfId()).get(0);
section = sectionDao.findByCrfVersionOrdinal(crfVersion.getCrfVersionId(), 1);
} else {
crfBean = new CrfBean();
crfBean = populateCrf(crfBean, cmdObject);
Integer crfId = (Integer) crfDao.save(crfBean);
crfBean.setCrfId(crfId);
// Create new Form Layout
formLayout = new FormLayout();
formLayout = populateFormLayout(formLayout, crfBean, cmdObject);
formLayout = formLayoutDao.saveOrUpdate(formLayout);
// Create new CRF Version
crfVersion = new CrfVersion();
crfVersion = populateCrfVersion(crfBean, crfVersion, cmdObject);
crfVersion = crfVersionDao.saveOrUpdate(crfVersion);
// Create Section
section = sectionDao.findByCrfVersionOrdinal(crfVersion.getCrfVersionId(), 1);
if (section == null) {
section = new Section();
section = populateCrfVersion(section, crfVersion, cmdObject);
sectionDao.saveOrUpdate(section);
section = sectionDao.findByCrfVersionOrdinal(crfVersion.getCrfVersionId(), 1);
}
}
createGroups(cmdObject.container, crfBean, crfVersion, formLayout, section, cmdObject.ub, cmdObject.errors);
if (cmdObject.errors.hasErrors()) {
logger.error("Encounter validation errors while saving CRF. Rolling back transaction.");
throw new RuntimeException("Encountered validation errors while saving CRF.");
}
return formLayout;
}
use of org.akaza.openclinica.domain.datamap.CrfBean 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.CrfBean in project OpenClinica by OpenClinica.
the class OpenRosaServices method getForm.
@GET
@Path("/{studyOID}/form")
@Produces(MediaType.TEXT_XML)
public XFormList getForm(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathParam("studyOID") String studyOID, @QueryParam("formID") String uniqueId, @RequestHeader("Authorization") String authorization, @Context ServletContext context) throws Exception {
LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
// print logback's internal status
StatusPrinter.print(lc);
if (!mayProceedPreview(studyOID))
return null;
String flavor = getQuerySet(uniqueId);
String formLayoutOid = getFormLayoutOid(uniqueId);
FormLayout formLayout = formLayoutDao.findByOcOID(formLayoutOid);
CrfBean crf = crfDao.findById(formLayout.getCrf().getCrfId());
String xformOutput = "";
String directoryPath = Utils.getCrfMediaFilePath(crf.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(QUERY_SUFFIX) || flavor.equals(NO_FLAVOR) && child.getName().endsWith(NO_SUFFIX)) {
xformOutput = new String(Files.readAllBytes(Paths.get(child.getPath())));
break;
}
}
}
XFormList formList = null;
try {
formList = new XFormList();
XForm form = new XForm(crf, formLayout);
// TODO Uncomment this before checking in
if (StringUtils.isNotEmpty(xformOutput)) {
form.setHash(DigestUtils.md5Hex(xformOutput));
}
String urlBase = getCoreResources().getDataInfo().getProperty("sysURL").split("/MainMenu")[0];
List<FormLayoutMedia> mediaList = formLayoutMediaDao.findByFormLayoutIdForNoteTypeMedia(formLayout.getFormLayoutId());
if (flavor.equals(QUERY_FLAVOR)) {
form.setDownloadURL(urlBase + "/rest2/openrosa/" + studyOID + "/formXml?formId=" + formLayout.getOcOid() + QUERY);
form.setManifestURL(urlBase + "/rest2/openrosa/" + studyOID + "/manifest?formId=" + formLayout.getOcOid() + QUERY);
form.setFormID(formLayout.getOcOid() + QUERY);
} else {
form.setDownloadURL(urlBase + "/rest2/openrosa/" + studyOID + "/formXml?formId=" + formLayout.getOcOid());
form.setManifestURL(urlBase + "/rest2/openrosa/" + studyOID + "/manifest?formId=" + formLayout.getOcOid());
}
formList.add(form);
} catch (Exception e) {
LOGGER.error(e.getMessage());
LOGGER.error(ExceptionUtils.getStackTrace(e));
// return "<Error>" + e.getMessage() + "</Error>";
}
return formList;
}
use of org.akaza.openclinica.domain.datamap.CrfBean in project OpenClinica by OpenClinica.
the class GenerateClinicalDataServiceImpl method getFormDataForClinicalStudy.
private ArrayList<ExportFormDataBean> getFormDataForClinicalStudy(StudySubject ss, StudyEvent se, String formVersionOID) {
List<ExportFormDataBean> formDataBean = new ArrayList<ExportFormDataBean>();
boolean formCheck = true;
if (formVersionOID != null)
formCheck = false;
boolean hiddenCrfCheckPassed = true;
List<CrfBean> hiddenCrfs = new ArrayList<CrfBean>();
for (EventCrf ecrf : se.getEventCrfs()) {
List<EventDefinitionCrf> edcs = se.getStudyEventDefinition().getEventDefinitionCrfs();
hiddenCrfCheckPassed = true;
int siteId = 0;
int parentStudyId = 0;
Study study = ss.getStudy();
if (study.getStudy() != null && isActiveRoleAtSite) {
// it is site subject
siteId = study.getStudyId();
parentStudyId = study.getStudy().getStudyId();
hiddenCrfs = listOfHiddenCrfs(siteId, parentStudyId, edcs, ecrf);
if (hiddenCrfs.contains(ecrf.getCrfVersion().getCrf())) {
hiddenCrfCheckPassed = false;
}
}
// This logic is to use the same method for both S_OID/SS_OID/*/* and full path
if (hiddenCrfCheckPassed) {
if (!formCheck) {
if (ecrf.getCrfVersion().getOcOid().equals(formVersionOID))
formCheck = true;
else
formCheck = false;
}
if (formCheck) {
ExportFormDataBean dataBean = new ExportFormDataBean();
dataBean.setItemGroupData(fetchItemData(ecrf.getCrfVersion().getItemGroupMetadatas(), ecrf.getEventCrfId(), ecrf.getCrfVersion().getVersioningMaps(), ecrf));
dataBean.setFormOID(ecrf.getCrfVersion().getOcOid());
if (ecrf.getDateInterviewed() != null)
dataBean.setInterviewDate(ecrf.getDateInterviewed() + "");
if (ecrf.getInterviewerName() != null)
dataBean.setInterviewerName(ecrf.getInterviewerName());
// dataBean.setStatus(EventCRFStatus.getByCode(Integer.valueOf(ecrf.getStatus().getCode())).getI18nDescription(getLocale()));
dataBean.setStatus(fetchEventCRFStatus(ecrf));
if (ecrf.getCrfVersion().getName() != null)
dataBean.setCrfVersion(ecrf.getCrfVersion().getName());
if (collectAudits)
dataBean.setAuditLogs(fetchAuditLogs(ecrf.getEventCrfId(), "event_crf", ecrf.getCrfVersion().getOcOid(), null));
if (collectDns)
dataBean.setDiscrepancyNotes(fetchDiscrepancyNotes(ecrf));
formDataBean.add(dataBean);
if (formVersionOID != null)
formCheck = false;
}
}
}
return (ArrayList<ExportFormDataBean>) formDataBean;
}
use of org.akaza.openclinica.domain.datamap.CrfBean in project OpenClinica by OpenClinica.
the class XformMetaDataService method createCRFMetaData.
@Transactional
public CrfVersion createCRFMetaData(CRFVersionBean version, XformContainer container, StudyBean currentStudy, UserAccountBean ub, Html html, String submittedCrfName, String submittedCrfVersionName, String submittedCrfVersionDescription, String submittedRevisionNotes, String submittedXformText, List<FileItem> formItems, Errors errors) throws Exception {
// Retrieve CrfBean. Create one if it doesn't exist yet.
CrfBean crf = null;
if (version.getCrfId() > 0) {
crf = (CrfBean) crfDao.findById(version.getCrfId());
crf.setUpdateId(ub.getId());
crf.setDateUpdated(new Date());
crfDao.saveOrUpdate(crf);
} else {
crf = new CrfBean();
crf.setName(submittedCrfName);
crf.setDescription(submittedCrfVersionDescription);
crf.setUserAccount(userDao.findById(ub.getId()));
crf.setStatus(org.akaza.openclinica.domain.Status.AVAILABLE);
crf.setStudy(studyDao.findById(currentStudy.getId()));
crf.setOcOid(crfDao.getValidOid(new CrfBean(), submittedCrfName));
crf.setUpdateId(ub.getId());
crf.setDateUpdated(new Date());
Integer crfId = (Integer) crfDao.save(crf);
crf.setCrfId(crfId);
}
// Create new CRF Version
CrfVersion crfVersion = new CrfVersion();
crfVersion.setName(submittedCrfVersionName);
crfVersion.setDescription(submittedCrfVersionDescription);
crfVersion.setCrf(crf);
crfVersion.setUserAccount(userDao.findById(ub.getId()));
crfVersion.setStatus(org.akaza.openclinica.domain.Status.AVAILABLE);
crfVersion.setRevisionNotes(submittedRevisionNotes);
crfVersion.setOcOid(crfVersionDao.getValidOid(new CrfVersion(), crf.getOcOid(), crfVersion.getName()));
crfVersion.setXform(submittedXformText);
crfVersion.setXformName(container.getInstanceName());
Integer crfVersionId = (Integer) crfVersionDao.save(crfVersion);
crfVersion.setCrfVersionId(crfVersionId);
// Create Section
Section section = new Section();
section.setCrfVersion(crfVersion);
section.setStatus(org.akaza.openclinica.domain.Status.AVAILABLE);
section.setLabel("");
section.setTitle("");
section.setSubtitle("");
section.setPageNumberLabel("");
section.setOrdinal(1);
// not null
section.setUserAccount(userDao.findById(ub.getId()));
section.setBorders(0);
sectionDao.saveOrUpdate(section);
section = sectionDao.findByCrfVersionOrdinal(crfVersion.getCrfVersionId(), 1);
createGroups(container, html, submittedXformText, crf, crfVersion, section, ub, errors);
saveMedia(formItems, crf, crfVersion);
if (errors.hasErrors()) {
logger.error("Encounter validation errors while saving CRF. Rolling back transaction.");
throw new RuntimeException("Encountered validation errors while saving CRF.");
}
return crfVersion;
}
Aggregations