use of org.akaza.openclinica.domain.xform.XformContainer in project OpenClinica by OpenClinica.
the class XformMetaDataService method executeIndividualCrf.
public void executeIndividualCrf(ExecuteIndividualCrfObject eicObject) {
for (OCodmComplexTypeDefinitionFormLayoutDef formLayoutDef : eicObject.formLayoutDefs) {
List<String> fileLinks = null;
String vForm = "";
RestTemplate rest = new RestTemplate();
if (eicObject.form != null) {
List<FormVersion> versions = eicObject.form.getVersions();
for (FormVersion version : versions) {
if (version.getName().equals(formLayoutDef.getOID())) {
fileLinks = version.getFileLinks();
for (String fileLink : fileLinks) {
if (fileLink.endsWith(VERSION)) {
vForm = rest.getForObject(fileLink, String.class);
break;
}
}
if (!eicObject.errors.hasErrors()) {
ObjectMapper mapper = new ObjectMapper();
TypeReference<List<XformGroup>> mapType = new TypeReference<List<XformGroup>>() {
};
List<XformGroup> jsonList = null;
try {
jsonList = mapper.readValue(vForm, mapType);
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
XformContainer xformContainer = new XformContainer();
xformContainer.setGroups(jsonList);
eicObject.setContainer(xformContainer);
if (eicObject.errors.hasErrors()) {
return;
}
// Save meta-data in database
saveFormMetadata(eicObject, version, eicObject.container, formLayoutDef, fileLinks);
}
}
}
}
}
}
use of org.akaza.openclinica.domain.xform.XformContainer in project OpenClinica by OpenClinica.
the class CreateXformCRFVersionServlet method parseInstance.
private XformContainer parseInstance(String xform) throws Exception {
// Could use the following xpath to get all leaf nodes in the case
// of multiple levels of groups: //*[count(./*) = 0]
// For now will assume a structure of /form/item or /form/group/item
Document doc = null;
try {
InputStream stream = new ByteArrayInputStream(xform.getBytes(StandardCharsets.UTF_8));
doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(stream);
NodeList instances = doc.getElementsByTagName("instance");
// All whitespace outside tags gets parsed as Text objects and returned
// by the various Node methods. We need to ignore these and
// focus on actual Elements
Element instance = null;
// List<XformItem> items = new ArrayList<XformItem>();
List<XformGroup> groups = new ArrayList<XformGroup>();
// Get the primary instance
for (int i = 0; i < instances.getLength(); i++) {
Element curInstance = (Element) instances.item(i);
if (curInstance instanceof Element) {
instance = curInstance;
break;
}
}
// Get the form element
Element form = null;
for (int i = 0; i < instance.getChildNodes().getLength(); i++) {
Node curNode = instance.getChildNodes().item(i);
if (curNode instanceof Element) {
form = (Element) curNode;
break;
}
}
// Get the groups and grouped items
for (int i = 0; i < form.getChildNodes().getLength(); i++) {
if (form.getChildNodes().item(i) instanceof Element && ((Element) form.getChildNodes().item(i)).hasChildNodes() && !((Element) form.getChildNodes().item(i)).getTagName().equals("meta")) {
Element group = (Element) form.getChildNodes().item(i);
XformGroup newGroup = new XformGroup();
newGroup.setGroupName(group.getTagName());
newGroup.setGroupPath("/" + form.getTagName() + "/" + group.getTagName());
groups.add(newGroup);
for (int j = 0; j < group.getChildNodes().getLength(); j++) {
if (group.getChildNodes().item(j) instanceof Element) {
Element item = (Element) group.getChildNodes().item(j);
XformItem newItem = new XformItem();
newItem.setItemPath("/" + form.getTagName() + "/" + group.getTagName() + "/" + item.getTagName());
newItem.setItemName(item.getTagName());
// group is null;
newGroup.getItems().add(newItem);
}
}
}
}
XformContainer container = new XformContainer();
container.setGroups(groups);
container.setInstanceName(form.getTagName());
return container;
} catch (Exception e) {
logger.error(e.getMessage());
logger.error(ExceptionUtils.getStackTrace(e));
throw new Exception(e);
}
}
use of org.akaza.openclinica.domain.xform.XformContainer in project OpenClinica by OpenClinica.
the class CreateXformCRFVersionServlet method processRequest.
@Override
protected void processRequest() throws Exception {
CrfDao crfDao = (CrfDao) SpringServletAccess.getApplicationContext(context).getBean("crfDao");
CrfVersionDao crfVersionDao = (CrfVersionDao) SpringServletAccess.getApplicationContext(context).getBean("crfVersionDao");
Locale locale = LocaleResolver.getLocale(request);
ResourceBundleProvider.updateLocale(locale);
resword = ResourceBundleProvider.getWordsBundle(locale);
// Retrieve submission data from multipart request
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = upload.parseRequest(request);
String submittedCrfName = retrieveFormFieldValue(items, "crfName");
String submittedCrfVersionName = retrieveFormFieldValue(items, "versionName");
String submittedCrfVersionDescription = retrieveFormFieldValue(items, "versionDescription");
String submittedRevisionNotes = retrieveFormFieldValue(items, "revisionNotes");
String submittedXformText = retrieveFormFieldValue(items, "xformText");
CRFVersionBean version = (CRFVersionBean) session.getAttribute("version");
logger.debug("Found original CRF ID for new CRF Version:" + version.getCrfId());
// Create container for holding validation errors
DataBinder dataBinder = new DataBinder(new CrfVersion());
Errors errors = dataBinder.getBindingResult();
// Validate all upload form fields were populated
validateFormFields(errors, version, submittedCrfName, submittedCrfVersionName, submittedCrfVersionDescription, submittedRevisionNotes, submittedXformText);
if (!errors.hasErrors()) {
// Parse instance and xform
XformParser parser = (XformParser) SpringServletAccess.getApplicationContext(context).getBean("xformParser");
XformContainer container = parseInstance(submittedXformText);
Html html = parser.unMarshall(submittedXformText);
// Save meta-data in database
XformMetaDataService xformService = (XformMetaDataService) SpringServletAccess.getApplicationContext(context).getBean("xformMetaDataService");
try {
xformService.createCRFMetaData(version, container, currentStudy, ub, html, submittedCrfName, submittedCrfVersionName, submittedCrfVersionDescription, submittedRevisionNotes, submittedXformText, items, errors);
} catch (RuntimeException e) {
logger.error("Error encountered while saving CRF: " + e.getMessage());
logger.error(ExceptionUtils.getStackTrace(e));
// and should be allow to crash the page for now
if (!errors.hasErrors())
throw e;
}
}
// Save errors to request so they can be displayed to the user
if (errors.hasErrors()) {
request.setAttribute("errorList", errors.getAllErrors());
logger.debug("Found at least one error. CRF data not saved.");
} else {
logger.debug("Didn't find any errors. CRF data saved.");
// Save any media files uploaded with xform
CrfBean crf = (submittedCrfName == null || submittedCrfName.equals("")) ? crfDao.findByCrfId(version.getCrfId()) : crfDao.findByName(submittedCrfName);
CrfVersion newVersion = crfVersionDao.findByNameCrfId(submittedCrfVersionName, crf.getCrfId());
saveAttachedMedia(items, crf, newVersion);
}
forwardPage(Page.CREATE_XFORM_CRF_VERSION_SERVLET);
}
Aggregations