use of org.akaza.openclinica.domain.xform.XformParser 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