Search in sources :

Example 1 with CrfVersionMediaDao

use of org.akaza.openclinica.dao.hibernate.CrfVersionMediaDao 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>";
    }
}
Also used : CRFDAO(org.akaza.openclinica.dao.admin.CRFDAO) XFormList(org.akaza.openclinica.web.pform.formlist.XFormList) Marshaller(org.exolab.castor.xml.Marshaller) CRFVersionDAO(org.akaza.openclinica.dao.submit.CRFVersionDAO) XForm(org.akaza.openclinica.web.pform.formlist.XForm) XMLContext(org.exolab.castor.xml.XMLContext) StudyBean(org.akaza.openclinica.bean.managestudy.StudyBean) Calendar(java.util.Calendar) Mapping(org.exolab.castor.mapping.Mapping) CrfVersionMediaDao(org.akaza.openclinica.dao.hibernate.CrfVersionMediaDao) Date(java.util.Date) WebApplicationException(javax.ws.rs.WebApplicationException) IOException(java.io.IOException) CRFBean(org.akaza.openclinica.bean.admin.CRFBean) CrfVersionMedia(org.akaza.openclinica.domain.datamap.CrfVersionMedia) StringWriter(java.io.StringWriter) CRFVersionBean(org.akaza.openclinica.bean.submit.CRFVersionBean) StudyDAO(org.akaza.openclinica.dao.managestudy.StudyDAO) SimpleDateFormat(java.text.SimpleDateFormat) Path(javax.ws.rs.Path) XPath(javax.xml.xpath.XPath) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with CrfVersionMediaDao

use of org.akaza.openclinica.dao.hibernate.CrfVersionMediaDao in project OpenClinica by OpenClinica.

the class OpenRosaServices method getMediaFile.

/**
 * @api {get} /rest2/openrosa/:studyOID/downloadMedia Download media
 * @apiName getMediaFile
 * @apiPermission admin
 * @apiVersion 3.8.0
 * @apiParam {String} studyOID Study Oid.
 * @apiGroup Form
 * @apiDescription Downloads media associated with a form, including images and video.
 */
@GET
@Path("/{studyOID}/downloadMedia")
public Response getMediaFile(@Context HttpServletRequest request, @Context HttpServletResponse response, @PathParam("studyOID") String studyOID, @QueryParam("crfVersionMediaId") String crfVersionMediaId, @RequestHeader("Authorization") String authorization, @Context ServletContext context) throws Exception {
    if (!mayProceedPreview(studyOID))
        return null;
    CrfVersionMediaDao mediaDao = (CrfVersionMediaDao) SpringServletAccess.getApplicationContext(context).getBean("crfVersionMediaDao");
    CrfVersionMedia media = mediaDao.findById(Integer.valueOf(crfVersionMediaId));
    File image = new File(media.getPath() + media.getName());
    FileInputStream fis = new FileInputStream(image);
    StreamingOutput stream = new MediaStreamingOutput(fis);
    ResponseBuilder builder = Response.ok(stream);
    // Set content type, if known
    FileNameMap fileNameMap = URLConnection.getFileNameMap();
    String type = fileNameMap.getContentTypeFor(media.getPath() + media.getName());
    if (type != null && !type.isEmpty())
        builder = builder.header("Content-Type", type);
    return builder.build();
}
Also used : CrfVersionMedia(org.akaza.openclinica.domain.datamap.CrfVersionMedia) StreamingOutput(javax.ws.rs.core.StreamingOutput) CrfVersionMediaDao(org.akaza.openclinica.dao.hibernate.CrfVersionMediaDao) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) FileNameMap(java.net.FileNameMap) MediaFile(org.akaza.openclinica.web.pform.manifest.MediaFile) File(java.io.File) FileInputStream(java.io.FileInputStream) Path(javax.ws.rs.Path) XPath(javax.xml.xpath.XPath) GET(javax.ws.rs.GET)

Example 3 with CrfVersionMediaDao

use of org.akaza.openclinica.dao.hibernate.CrfVersionMediaDao 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

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