private void importReportFromFilePerformed(AjaxRequestTarget target) { OperationResult result = new OperationResult(OPERATION_IMPORT_REPORT); FileUploadField file = (FileUploadField) get(createComponentPath(ID_MAIN_FORM, ID_INPUT, ID_INPUT_FILE, ID_FILE_INPUT)); final FileUpload uploadedFile = file.getFileUpload(); if (uploadedFile == null) { error(getString("PageNewReport.message.nullFile")); target.add(getFeedbackPanel()); return; } InputStream stream = null; File newFile = null; try { // Create new file MidPointApplication application = getMidpointApplication(); WebApplicationConfiguration config = application.getWebApplicationConfiguration(); File folder = new File(config.getImportFolder()); if (!folder.exists() || !folder.isDirectory()) { folder.mkdir(); } newFile = new File(folder, uploadedFile.getClientFileName()); // Check new file, delete if it already exists if (newFile.exists()) { newFile.delete(); } // Save file // Task task = createSimpleTask(OPERATION_IMPORT_FILE); newFile.createNewFile(); uploadedFile.writeTo(newFile); InputStreamReader reader = new InputStreamReader(new FileInputStream(newFile), "utf-8"); // reader. stream = new ReaderInputStream(reader, reader.getEncoding()); byte[] reportIn = IOUtils.toByteArray(stream); setResponsePage(new PageReport(new ReportDto(Base64.encodeBase64(reportIn)))); } catch (Exception ex) { result.recordFatalError("Couldn't import file.", ex); LoggingUtils.logException(LOGGER, "Couldn't import file", ex); } finally { if (stream != null) { IOUtils.closeQuietly(stream); } if (newFile != null) { FileUtils.deleteQuietly(newFile); } } showResult(result); target.add(getFeedbackPanel()); }
@SuppressWarnings("unchecked") private void setSelectedCustomFieldsFromFile() { if (fileUploadField.getFileUpload() != null) { ArkFunction arkFunction = iArkCommonService.getArkFunctionByName( au.org.theark.core.Constants.FUNCTION_KEY_VALUE_PHENO_COLLECTION); Long studyId = (Long) SecurityUtils.getSubject() .getSession() .getAttribute(au.org.theark.core.Constants.STUDY_CONTEXT_ID); Study study = iArkCommonService.getStudy(studyId); ArrayList<CustomField> selectedCustomFields = (ArrayList<CustomField>) iArkCommonService.matchCustomFieldsFromInputFile( fileUploadField.getFileUpload(), study, arkFunction); cpModel.getObject().setSelectedCustomFields(selectedCustomFields); } initCustomFieldPalette(); arkCrudContainerVO.getDetailPanelFormContainer().addOrReplace(customFieldPalette); }
/** @see Form#onSubmit() */ @Override protected void onSubmit() { final String project = identifier.getModel().getObject(); try { final FileUpload upload = file.getFileUpload(); if (upload != null) { File dir = projects.reserveResourceId(project); // File target = new File(dir, upload.getClientFileName()); String type = upload.getContentType(); if (checkContentType(type)) { // IOUtils.copy(upload.getInputStream(), new FileOutputStream(target)); // try extracting the archive IOUtils.extract(dir, new BufferedInputStream(upload.getInputStream())); // trigger scan after upload projects.triggerScan(); info("Successfully uploaded project"); onUploadSuccess(); } else { projects.releaseResourceId(project); error(getTypeErrorMessage(type)); } } else { warn("Please provide a file for upload"); } } catch (ScavengerException e) { error(e.getMessage()); } catch (Exception e) { projects.releaseResourceId(project); log.error("Error while uploading file", e); error("Error saving the file"); } }
private FileUpload getUploadedFile() { FileUploadField file = (FileUploadField) get(createComponentPath(ID_MAIN_FORM, ID_INPUT, ID_INPUT_FILE, ID_FILE_INPUT)); return file.getFileUpload(); }