Search in sources :

Example 1 with MediaFile

use of org.akaza.openclinica.web.pform.manifest.MediaFile in project OpenClinica by OpenClinica.

the class OpenRosaServices method getManifest.

/**
 * @api {get} /rest2/openrosa/:studyOID/manifest Get Form Manifest
 * @apiName getManifest
 * @apiPermission admin
 * @apiVersion 3.8.0
 * @apiParam {String} studyOID Study Oid.
 * @apiGroup Form
 * @apiDescription Gets additional information on a particular Form, including links to associated media.
 */
@GET
@Path("/{studyOID}/manifest")
@Produces(MediaType.TEXT_XML)
public String getManifest(@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;
    CRFVersionDAO cVersionDao = new CRFVersionDAO(getDataSource());
    CrfVersionMediaDao mediaDao = (CrfVersionMediaDao) SpringServletAccess.getApplicationContext(context).getBean("crfVersionMediaDao");
    CRFVersionBean crfVersion = cVersionDao.findByOid(crfOID);
    List<MediaFile> mediaFiles = new ArrayList<MediaFile>();
    Manifest manifest = new Manifest();
    List<CrfVersionMedia> mediaList = mediaDao.findByCrfVersionId(crfVersion.getId());
    if (mediaList != null && mediaList.size() > 0) {
        for (CrfVersionMedia media : mediaList) {
            String urlBase = getCoreResources().getDataInfo().getProperty("sysURL").split("/MainMenu")[0];
            MediaFile mediaFile = new MediaFile();
            mediaFile.setFilename(media.getName());
            File image = new File(media.getPath() + media.getName());
            mediaFile.setHash(DigestUtils.md5Hex(media.getName()) + Double.toString(image.length()));
            mediaFile.setDownloadUrl(urlBase + "/rest2/openrosa/" + studyOID + "/downloadMedia?crfVersionMediaId=" + media.getCrfVersionMediaId());
            manifest.add(mediaFile);
        }
    }
    try {
        // Create the XML manifest using a Castor mapping file.
        XMLContext xmlContext = new XMLContext();
        Mapping mapping = xmlContext.createMapping();
        mapping.loadMapping(getCoreResources().getURL("openRosaManifestMapping.xml"));
        xmlContext.addMapping(mapping);
        Marshaller marshaller = xmlContext.createMarshaller();
        StringWriter writer = new StringWriter();
        marshaller.setWriter(writer);
        marshaller.marshal(manifest);
        // 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>";
    }
}
Also used : MediaFile(org.akaza.openclinica.web.pform.manifest.MediaFile) Marshaller(org.exolab.castor.xml.Marshaller) CRFVersionDAO(org.akaza.openclinica.dao.submit.CRFVersionDAO) XMLContext(org.exolab.castor.xml.XMLContext) Calendar(java.util.Calendar) ArrayList(java.util.ArrayList) Mapping(org.exolab.castor.mapping.Mapping) CrfVersionMediaDao(org.akaza.openclinica.dao.hibernate.CrfVersionMediaDao) Manifest(org.akaza.openclinica.web.pform.manifest.Manifest) Date(java.util.Date) WebApplicationException(javax.ws.rs.WebApplicationException) IOException(java.io.IOException) CrfVersionMedia(org.akaza.openclinica.domain.datamap.CrfVersionMedia) StringWriter(java.io.StringWriter) CRFVersionBean(org.akaza.openclinica.bean.submit.CRFVersionBean) MediaFile(org.akaza.openclinica.web.pform.manifest.MediaFile) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) Path(javax.ws.rs.Path) XPath(javax.xml.xpath.XPath) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Aggregations

File (java.io.File)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Calendar (java.util.Calendar)1 Date (java.util.Date)1 GET (javax.ws.rs.GET)1 Path (javax.ws.rs.Path)1 Produces (javax.ws.rs.Produces)1 WebApplicationException (javax.ws.rs.WebApplicationException)1 XPath (javax.xml.xpath.XPath)1 CRFVersionBean (org.akaza.openclinica.bean.submit.CRFVersionBean)1 CrfVersionMediaDao (org.akaza.openclinica.dao.hibernate.CrfVersionMediaDao)1 CRFVersionDAO (org.akaza.openclinica.dao.submit.CRFVersionDAO)1 CrfVersionMedia (org.akaza.openclinica.domain.datamap.CrfVersionMedia)1 Manifest (org.akaza.openclinica.web.pform.manifest.Manifest)1 MediaFile (org.akaza.openclinica.web.pform.manifest.MediaFile)1 Mapping (org.exolab.castor.mapping.Mapping)1 Marshaller (org.exolab.castor.xml.Marshaller)1