Search in sources :

Example 1 with FormArtifactTransferObj

use of org.akaza.openclinica.service.crfdata.FormArtifactTransferObj in project OpenClinica by OpenClinica.

the class CreateXformCRFVersionServlet method processRequest.

// public final String FM_BASEURL = "http://oc.local:8090/api/protocol/";
@Override
protected void processRequest() throws Exception {
    CrfDao crfDao = (CrfDao) SpringServletAccess.getApplicationContext(context).getBean("crfDao");
    XformMetaDataService xformService = (XformMetaDataService) SpringServletAccess.getApplicationContext(context).getBean("xformService");
    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 crfName = retrieveFormFieldValue(items, "crfName");
    DataBinder dataBinder = new DataBinder(new FormVersion());
    Errors errors = dataBinder.getBindingResult();
    int crfId = Integer.valueOf(retrieveFormFieldValue(items, "crfId"));
    if (crfId != 0) {
        CrfBean crfBean = crfDao.findByCrfId(crfId);
        crfName = crfBean.getName();
    }
    FormArtifactTransferObj transferObj = getFormArtifactsFromFM(items, currentStudy.getOid(), crfName);
    if (transferObj.getErr().size() != 0) {
        for (ErrorObj er : transferObj.getErr()) {
            errors.rejectValue("name", er.getCode(), er.getMessage());
        }
    } else {
        List<OCodmComplexTypeDefinitionFormLayoutDef> formLayoutDefs = new ArrayList<>();
        OCodmComplexTypeDefinitionFormLayoutDef formLayoutDef;
        for (FormVersion version : transferObj.getForm().getVersions()) {
            formLayoutDef = new OCodmComplexTypeDefinitionFormLayoutDef();
            formLayoutDef.setOID(version.getName());
            formLayoutDef.setURL(version.getArtifactURL());
            formLayoutDefs.add(formLayoutDef);
        }
        ExecuteIndividualCrfObject eicObj = new ExecuteIndividualCrfObject(transferObj.getForm(), formLayoutDefs, errors, currentStudy, ub, false, null);
        xformService.executeIndividualCrf(eicObj);
    }
    if (errors.hasErrors()) {
        request.setAttribute("errorList", errors.getAllErrors());
    }
    forwardPage(Page.CREATE_XFORM_CRF_VERSION_SERVLET);
}
Also used : Locale(java.util.Locale) CrfBean(org.akaza.openclinica.domain.datamap.CrfBean) ExecuteIndividualCrfObject(org.akaza.openclinica.service.crfdata.ExecuteIndividualCrfObject) OCodmComplexTypeDefinitionFormLayoutDef(org.openclinica.ns.odm_ext_v130.v31.OCodmComplexTypeDefinitionFormLayoutDef) ArrayList(java.util.ArrayList) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) CrfDao(org.akaza.openclinica.dao.hibernate.CrfDao) FileItem(org.apache.commons.fileupload.FileItem) Errors(org.springframework.validation.Errors) XformMetaDataService(org.akaza.openclinica.service.crfdata.XformMetaDataService) ServletFileUpload(org.apache.commons.fileupload.servlet.ServletFileUpload) FormArtifactTransferObj(org.akaza.openclinica.service.crfdata.FormArtifactTransferObj) DataBinder(org.springframework.validation.DataBinder) FormVersion(org.akaza.openclinica.service.dto.FormVersion) ErrorObj(org.akaza.openclinica.service.crfdata.ErrorObj)

Example 2 with FormArtifactTransferObj

use of org.akaza.openclinica.service.crfdata.FormArtifactTransferObj in project OpenClinica by OpenClinica.

the class CreateXformCRFVersionServlet method getFormArtifactsFromFM.

private FormArtifactTransferObj getFormArtifactsFromFM(List<FileItem> files, String studyOid, String crfName) {
    LinkedMultiValueMap<String, Object> map = new LinkedMultiValueMap<>();
    ArrayList<ByteArrayResource> byteArrayResources = new ArrayList<>();
    RestTemplate restTemplate = new RestTemplate();
    String uploadFilesUrl = FM_BASEURL + studyOid + "/forms/" + crfName + "/artifacts";
    map.add("file", byteArrayResources);
    for (FileItem file : files) {
        String filename = file.getName();
        if (!file.isFormField()) {
            ByteArrayResource contentsAsResource = new ByteArrayResource(file.get()) {

                @Override
                public String getFilename() {
                    return filename;
                }
            };
            map.get("file").add(contentsAsResource);
        }
    }
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.MULTIPART_FORM_DATA);
    HttpEntity<LinkedMultiValueMap<String, Object>> requestEntity = new HttpEntity<>(map, headers);
    // TODO: replace with Crf object instead of String object
    FormArtifactTransferObj response = restTemplate.postForObject(uploadFilesUrl, requestEntity, FormArtifactTransferObj.class);
    return response;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpEntity(org.springframework.http.HttpEntity) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) ArrayList(java.util.ArrayList) ByteArrayResource(org.springframework.core.io.ByteArrayResource) FileItem(org.apache.commons.fileupload.FileItem) FormArtifactTransferObj(org.akaza.openclinica.service.crfdata.FormArtifactTransferObj) RestTemplate(org.springframework.web.client.RestTemplate) ExecuteIndividualCrfObject(org.akaza.openclinica.service.crfdata.ExecuteIndividualCrfObject)

Aggregations

ArrayList (java.util.ArrayList)2 ExecuteIndividualCrfObject (org.akaza.openclinica.service.crfdata.ExecuteIndividualCrfObject)2 FormArtifactTransferObj (org.akaza.openclinica.service.crfdata.FormArtifactTransferObj)2 FileItem (org.apache.commons.fileupload.FileItem)2 Locale (java.util.Locale)1 CrfDao (org.akaza.openclinica.dao.hibernate.CrfDao)1 CrfBean (org.akaza.openclinica.domain.datamap.CrfBean)1 ErrorObj (org.akaza.openclinica.service.crfdata.ErrorObj)1 XformMetaDataService (org.akaza.openclinica.service.crfdata.XformMetaDataService)1 FormVersion (org.akaza.openclinica.service.dto.FormVersion)1 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)1 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)1 OCodmComplexTypeDefinitionFormLayoutDef (org.openclinica.ns.odm_ext_v130.v31.OCodmComplexTypeDefinitionFormLayoutDef)1 ByteArrayResource (org.springframework.core.io.ByteArrayResource)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)1 DataBinder (org.springframework.validation.DataBinder)1 Errors (org.springframework.validation.Errors)1 RestTemplate (org.springframework.web.client.RestTemplate)1