Search in sources :

Example 6 with FormLayoutMedia

use of org.akaza.openclinica.domain.datamap.FormLayoutMedia 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;
}
Also used : FormLayout(org.akaza.openclinica.domain.datamap.FormLayout) CrfBean(org.akaza.openclinica.domain.datamap.CrfBean) XFormList(org.akaza.openclinica.web.pform.formlist.XFormList) XForm(org.akaza.openclinica.web.pform.formlist.XForm) FormLayoutMedia(org.akaza.openclinica.domain.datamap.FormLayoutMedia) LoggerContext(ch.qos.logback.classic.LoggerContext) MediaFile(org.akaza.openclinica.web.pform.manifest.MediaFile) File(java.io.File) WebApplicationException(javax.ws.rs.WebApplicationException) IOException(java.io.IOException) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 7 with FormLayoutMedia

use of org.akaza.openclinica.domain.datamap.FormLayoutMedia 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 uniqueId, @RequestHeader("Authorization") String authorization, @Context ServletContext context) throws Exception {
    if (!mayProceedPreview(studyOID))
        return null;
    XFormList formList = null;
    try {
        if (StringUtils.isEmpty(uniqueId)) {
            List<CrfBean> crfs = crfDao.findAll();
            List<FormLayout> formLayouts = formLayoutDao.findAll();
            formList = new XFormList();
            for (CrfBean crf : crfs) {
                for (FormLayout formLayout : formLayouts) {
                    if (formLayout.getCrf().getCrfId() == crf.getCrfId()) {
                        XForm form = new XForm(crf, formLayout);
                        // TODO: Need to generate hash based on contents of
                        // XForm. Will be done in a later story.
                        // TODO: For now all XForms get a date based hash to
                        // trick Enketo into always downloading
                        // TODO: them.
                        String urlBase = getCoreResources().getDataInfo().getProperty("sysURL").split("/MainMenu")[0];
                        form.setDownloadURL(urlBase + "/rest2/openrosa/" + studyOID + "/formXml?formId=" + formLayout.getOcOid());
                        List<FormLayoutMedia> mediaList = formLayoutMediaDao.findByFormLayoutIdForNoteTypeMedia(formLayout.getFormLayoutId());
                        if (mediaList != null && mediaList.size() > 0) {
                            form.setManifestURL(urlBase + "/rest2/openrosa/" + studyOID + "/manifest?formId=" + formLayout.getOcOid());
                        }
                        formList.add(form);
                    }
                }
            }
        } else {
            formList = getForm(request, response, studyOID, uniqueId, authorization, context);
        }
        // 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 : FormLayout(org.akaza.openclinica.domain.datamap.FormLayout) CrfBean(org.akaza.openclinica.domain.datamap.CrfBean) XFormList(org.akaza.openclinica.web.pform.formlist.XFormList) Marshaller(org.exolab.castor.xml.Marshaller) XForm(org.akaza.openclinica.web.pform.formlist.XForm) XMLContext(org.exolab.castor.xml.XMLContext) FormLayoutMedia(org.akaza.openclinica.domain.datamap.FormLayoutMedia) Calendar(java.util.Calendar) Mapping(org.exolab.castor.mapping.Mapping) Date(java.util.Date) WebApplicationException(javax.ws.rs.WebApplicationException) IOException(java.io.IOException) StringWriter(java.io.StringWriter) SimpleDateFormat(java.text.SimpleDateFormat) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 8 with FormLayoutMedia

use of org.akaza.openclinica.domain.datamap.FormLayoutMedia in project OpenClinica by OpenClinica.

the class FormLayoutMediaDao method findByFormLayoutIdFileNameForNoteTypeMedia.

public FormLayoutMedia findByFormLayoutIdFileNameForNoteTypeMedia(int formLayoutId, String fileName, String dir) {
    String query = "from " + getDomainClassName() + " flm where flm.formLayout.formLayoutId = :formlayoutid and flm.eventCrfId=0 and flm.path = :dir and flm.name = :fileName";
    Query q = getCurrentSession().createQuery(query);
    q.setInteger("formlayoutid", formLayoutId);
    q.setString("dir", dir);
    q.setString("fileName", fileName);
    return (FormLayoutMedia) q.uniqueResult();
}
Also used : Query(org.hibernate.Query) FormLayoutMedia(org.akaza.openclinica.domain.datamap.FormLayoutMedia)

Example 9 with FormLayoutMedia

use of org.akaza.openclinica.domain.datamap.FormLayoutMedia in project OpenClinica by OpenClinica.

the class EnketoUrlService method getEditUrl.

public String getEditUrl(String subjectContextKey, PFormCacheSubjectContextEntry subjectContext, String studyOid, FormLayout formLayout, StudyEvent studyEvent, String flavor) throws Exception {
    Study study = enketoCredentials.getParentStudy(studyOid);
    studyOid = study.getOc_oid();
    String editURL = null;
    StudyEventDefinition eventDef;
    StudySubject subject;
    String goTo = null;
    if (subjectContext.getItemName() != null) {
        goTo = (subjectContext.isItemInRepeatingGroup()) ? "//" + subjectContext.getItemRepeatGroupName() + "[" + subjectContext.getItemRepeatOrdinal() + "]//" + subjectContext.getItemName() : "//" + subjectContext.getItemName();
    }
    if (studyEvent == null) {
        // Lookup relevant data
        eventDef = studyEventDefinitionDao.findByStudyEventDefinitionId(subjectContext.getStudyEventDefinitionId());
        subject = studySubjectDao.findByOcOID(subjectContext.getStudySubjectOid());
        studyEvent = studyEventDao.fetchByStudyEventDefOIDAndOrdinal(eventDef.getOc_oid(), Integer.valueOf(subjectContext.getOrdinal()), subject.getStudySubjectId());
    } else {
        eventDef = studyEvent.getStudyEventDefinition();
        subject = studyEvent.getStudySubject();
    }
    if (formLayout == null) {
        formLayout = formLayoutDao.findByOcOID(subjectContext.getFormLayoutOid());
    }
    EventCrf eventCrf = eventCrfDao.findByStudyEventIdStudySubjectIdFormLayoutId(studyEvent.getStudyEventId(), subject.getStudySubjectId(), formLayout.getFormLayoutId());
    CrfVersion crfVersion = eventCrf.getCrfVersion();
    // Load populated instance
    String populatedInstance = populateInstance(crfVersion, formLayout, eventCrf, studyOid, flavor);
    // Call Enketo api to get edit url
    EnketoAPI enketo = new EnketoAPI(EnketoCredentials.getInstance(studyOid));
    // Build redirect url
    String redirectUrl = getRedirectUrl(subject.getOcOid(), studyOid);
    boolean markComplete = true;
    if (eventCrf.getStatusId() == Status.UNAVAILABLE.getCode()) {
        markComplete = false;
    }
    // Return Enketo URL
    List<FormLayoutMedia> mediaList = formLayoutMediaDao.findByEventCrfId(eventCrf.getEventCrfId());
    EnketoURLResponse eur = enketo.getEditURL(formLayout, flavor, populatedInstance, subjectContextKey, redirectUrl, markComplete, studyOid, mediaList, goTo);
    editURL = eur.getEdit_url();
    int hashIndex = editURL.lastIndexOf("#");
    String part1 = "";
    String part2 = "";
    if (hashIndex != -1) {
        part1 = editURL.substring(0, hashIndex);
        part2 = editURL.substring(hashIndex);
        editURL = part1 + "&ecid=" + subjectContextKey + part2;
    } else {
        editURL = editURL + "&ecid=" + subjectContextKey;
    }
    logger.debug("Generating Enketo edit url for form: " + editURL);
    return editURL;
}
Also used : EventCrf(org.akaza.openclinica.domain.datamap.EventCrf) Study(org.akaza.openclinica.domain.datamap.Study) EnketoAPI(org.akaza.openclinica.service.crfdata.xform.EnketoAPI) StudyEventDefinition(org.akaza.openclinica.domain.datamap.StudyEventDefinition) StudySubject(org.akaza.openclinica.domain.datamap.StudySubject) CrfVersion(org.akaza.openclinica.domain.datamap.CrfVersion) FormLayoutMedia(org.akaza.openclinica.domain.datamap.FormLayoutMedia) EnketoURLResponse(org.akaza.openclinica.service.crfdata.xform.EnketoURLResponse)

Example 10 with FormLayoutMedia

use of org.akaza.openclinica.domain.datamap.FormLayoutMedia in project OpenClinica by OpenClinica.

the class XformMetaDataService method saveMediaFiles.

private void saveMediaFiles(List<String> fileLinks, String crfOid, FormLayout formLayout) throws IOException {
    // Create the directory structure for saving the media
    String dir = Utils.getCrfMediaFilePathWithoutSysPath(crfOid, formLayout.getOcOid());
    for (String fileLink : fileLinks) {
        String fileName = "";
        int startIndex = fileLink.lastIndexOf('/');
        if (startIndex != -1) {
            fileName = fileLink.substring(startIndex + 1);
        }
        if (!fileLink.endsWith(FORM_SUFFIX) && !fileLink.endsWith(INSTANCEQUERIES_SUFFIX) && !fileLink.endsWith(FORMQUERIES_SUFFIX) && !fileLink.endsWith(XLS_SUFFIX) && !fileLink.endsWith(INSTANCE_SUFFIX)) {
            FormLayoutMedia media = formLayoutMediaDao.findByFormLayoutIdFileNameForNoteTypeMedia(formLayout.getFormLayoutId(), fileName, dir);
            if (media == null) {
                media = new FormLayoutMedia();
                media.setFormLayout(formLayout);
                media.setName(fileName);
                media.setPath(dir);
                media.setEventCrfId(0);
                formLayoutMediaDao.saveOrUpdate(media);
            }
        }
    }
}
Also used : FormLayoutMedia(org.akaza.openclinica.domain.datamap.FormLayoutMedia)

Aggregations

FormLayoutMedia (org.akaza.openclinica.domain.datamap.FormLayoutMedia)12 Date (java.util.Date)4 GET (javax.ws.rs.GET)4 Path (javax.ws.rs.Path)4 FormLayout (org.akaza.openclinica.domain.datamap.FormLayout)4 Query (org.hibernate.Query)4 File (java.io.File)3 IOException (java.io.IOException)3 Calendar (java.util.Calendar)3 Produces (javax.ws.rs.Produces)3 WebApplicationException (javax.ws.rs.WebApplicationException)3 MediaFile (org.akaza.openclinica.web.pform.manifest.MediaFile)3 StringWriter (java.io.StringWriter)2 SimpleDateFormat (java.text.SimpleDateFormat)2 CrfBean (org.akaza.openclinica.domain.datamap.CrfBean)2 CrfVersion (org.akaza.openclinica.domain.datamap.CrfVersion)2 XForm (org.akaza.openclinica.web.pform.formlist.XForm)2 XFormList (org.akaza.openclinica.web.pform.formlist.XFormList)2 Mapping (org.exolab.castor.mapping.Mapping)2 Marshaller (org.exolab.castor.xml.Marshaller)2