use of org.akaza.openclinica.web.pform.formlist.XForm 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.web.pform.formlist.XForm in project OpenClinica by OpenClinica.
the class OpenRosaServices method getFormList.
/**
* @api {get} /rest2/openrosa/:studyOID/formList Get Form List
* @apiName getFormList
* @apiPermission admin
* @apiVersion 3.8.0
* @apiParam {String} studyOID Study Oid.
* @apiGroup Form
* @apiDescription Retrieves a listing of the available OpenClinica forms.
* @apiParamExample {json} Request-Example:
* {
* "studyOid": "S_SAMPLTE",
* }
* @apiSuccessExample {xml} Success-Response:
* HTTP/1.1 200 OK
* {
* <xforms xmlns="http://openrosa.org/xforms/xformsList">
* <xform>
* <formID>F_FIRSTFORM_1</formID>
* <name>First Form</name>
* <majorMinorVersion>1</majorMinorVersion>
* <version>1</version>
* <hash>8678370cd92814d4e3216d58d821403f</hash>
* <downloadUrl>http://oc1.openclinica.com/OpenClinica-web/rest2/openrosa/S_SAMPLTE/formXml?
* formId=F_FIRSTFORM_1</downloadUrl>
* </xform>
* <xform>
* <formID>F_SECONDFORM_1</formID>
* <name>Second Form</name>
* <majorMinorVersion>1</majorMinorVersion>
* <version>1</version>
* <hash>7ee60d1c6516b730bbe9bdbd7cad942f</hash>
* <downloadUrl>http://oc1.openclinica.com/OpenClinica-web/rest2/openrosa/S_SAMPLTE/formXml?
* formId=F_SECONDFORM_1</downloadUrl>
* </xform>
* </xforms>
*/
@GET
@Path("/{studyOID}/formList")
@Produces(MediaType.TEXT_XML)
public String getFormList(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathParam("studyOID") String studyOID, @QueryParam("formID") String crfOID, @RequestHeader("Authorization") String authorization, @Context ServletContext context) throws Exception {
if (!mayProceedPreview(studyOID))
return null;
StudyDAO sdao = new StudyDAO(getDataSource());
StudyBean study = sdao.findByOid(studyOID);
CRFDAO cdao = new CRFDAO(getDataSource());
Collection<CRFBean> crfs = cdao.findAll();
CRFVersionDAO cVersionDao = new CRFVersionDAO(getDataSource());
Collection<CRFVersionBean> crfVersions = cVersionDao.findAll();
CrfVersionMediaDao mediaDao = (CrfVersionMediaDao) SpringServletAccess.getApplicationContext(context).getBean("crfVersionMediaDao");
try {
XFormList formList = new XFormList();
for (CRFBean crf : crfs) {
for (CRFVersionBean version : crfVersions) {
if (version.getCrfId() == crf.getId()) {
XForm form = new XForm(crf, version);
// TODO: them.
if (version.getXformName() != null) {
form.setHash(DigestUtils.md5Hex(version.getXform()));
} else {
Calendar cal = Calendar.getInstance();
cal.setTime(new Date());
form.setHash(DigestUtils.md5Hex(String.valueOf(cal.getTimeInMillis())));
}
String urlBase = getCoreResources().getDataInfo().getProperty("sysURL").split("/MainMenu")[0];
form.setDownloadURL(urlBase + "/rest2/openrosa/" + studyOID + "/formXml?formId=" + version.getOid());
List<CrfVersionMedia> mediaList = mediaDao.findByCrfVersionId(version.getId());
if (mediaList != null && mediaList.size() > 0) {
form.setManifestURL(urlBase + "/rest2/openrosa/" + studyOID + "/manifest?formId=" + version.getOid());
}
formList.add(form);
}
}
}
// Create the XML formList using a Castor mapping file.
XMLContext xmlContext = new XMLContext();
Mapping mapping = xmlContext.createMapping();
mapping.loadMapping(getCoreResources().getURL("openRosaFormListMapping.xml"));
xmlContext.addMapping(mapping);
Marshaller marshaller = xmlContext.createMarshaller();
StringWriter writer = new StringWriter();
marshaller.setWriter(writer);
marshaller.marshal(formList);
// Set response headers
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
Date currentDate = new Date();
cal.setTime(currentDate);
SimpleDateFormat format = new SimpleDateFormat("E, dd MMM yyyy HH:mm:ss zz");
format.setCalendar(cal);
response.setHeader("Content-Type", "text/xml; charset=UTF-8");
response.setHeader("Date", format.format(currentDate));
response.setHeader("X-OpenRosa-Version", "1.0");
return writer.toString();
} catch (Exception e) {
LOGGER.error(e.getMessage());
LOGGER.error(ExceptionUtils.getStackTrace(e));
return "<Error>" + e.getMessage() + "</Error>";
}
}
Aggregations