Search in sources :

Example 11 with FileMeta

use of org.molgenis.data.file.model.FileMeta in project molgenis by molgenis.

the class StyleServiceTest method getBootstrap4FallBackThemeData.

@Test
public void getBootstrap4FallBackThemeData() throws MolgenisStyleException {
    String styleName = "my-style";
    StyleSheet styleSheet = mock(StyleSheet.class);
    StyleSheet fallBackTheme = mock(StyleSheet.class);
    FileMeta fileMeta = mock(FileMeta.class);
    String fileId = "fileId";
    when(fileMeta.getId()).thenReturn(fileId);
    when(styleSheet.getBootstrap4Theme()).thenReturn(null);
    when(fallBackTheme.getBootstrap4Theme()).thenReturn(fileMeta);
    Query<StyleSheet> expectedQuery = new QueryImpl<StyleSheet>().eq(StyleSheetMetadata.NAME, styleName);
    Query<StyleSheet> fallBackQuery = new QueryImpl<StyleSheet>().eq(StyleSheetMetadata.NAME, BOOTSTRAP_FALL_BACK_THEME);
    when(dataService.findOne(STYLE_SHEET, expectedQuery, StyleSheet.class)).thenReturn(styleSheet);
    when(dataService.findOne(STYLE_SHEET, fallBackQuery, StyleSheet.class)).thenReturn(fallBackTheme);
    File styleFile = mock(File.class);
    String mockFilePath = "mock/file/path";
    when(styleFile.getPath()).thenReturn(mockFilePath);
    when(fileStore.getFile(fileId)).thenReturn(styleFile);
    BootstrapVersion version = BootstrapVersion.BOOTSTRAP_VERSION_4;
    FileSystemResource themeData = styleService.getThemeData(styleName, version);
    assertEquals(themeData.getPath(), mockFilePath);
}
Also used : FileSystemResource(org.springframework.core.io.FileSystemResource) File(java.io.File) FileMeta(org.molgenis.data.file.model.FileMeta) Test(org.testng.annotations.Test)

Example 12 with FileMeta

use of org.molgenis.data.file.model.FileMeta in project molgenis by molgenis.

the class SavedScriptRunner method runScript.

/**
 * Run a script with parameters.
 *
 * @param scriptName name of the script to run
 * @param parameters parameters for the script
 * @return ScriptResult
 * @throws UnknownScriptException  if scriptName is unknown
 * @throws GenerateScriptException , if parameter is missing
 */
public ScriptResult runScript(String scriptName, Map<String, Object> parameters) {
    Script script = dataService.findOne(SCRIPT, new QueryImpl<Script>().eq(ScriptMetaData.NAME, scriptName), Script.class);
    if (script == null) {
        throw new UnknownScriptException("Unknown script [" + scriptName + "]");
    }
    if (script.getParameters() != null) {
        for (ScriptParameter param : script.getParameters()) {
            if (!parameters.containsKey(param.getName())) {
                throw new GenerateScriptException("Missing parameter [" + param + "]");
            }
        }
    }
    if (script.isGenerateToken()) {
        String token = tokenService.generateAndStoreToken(SecurityUtils.getCurrentUsername(), "For script " + script.getName());
        parameters.put("molgenisToken", token);
    }
    FileMeta fileMeta = null;
    if (StringUtils.isNotEmpty(script.getResultFileExtension())) {
        String name = generateRandomString();
        File file = fileStore.getFile(name + "." + script.getResultFileExtension());
        parameters.put("outputFile", file.getAbsolutePath());
        fileMeta = createFileMeta(name, file);
        dataService.add(FILE_META, fileMeta);
    }
    ScriptRunner scriptRunner = scriptRunnerFactory.getScriptRunner(script.getScriptType().getName());
    String output = scriptRunner.runScript(script, parameters);
    return new ScriptResult(fileMeta, output);
}
Also used : QueryImpl(org.molgenis.data.support.QueryImpl) File(java.io.File) FileMeta(org.molgenis.data.file.model.FileMeta)

Example 13 with FileMeta

use of org.molgenis.data.file.model.FileMeta in project molgenis by molgenis.

the class AppRepositoryDecorator method validateResourceZip.

private void validateResourceZip(App app) {
    FileMeta appZipMeta = app.getSourceFiles();
    if (appZipMeta != null) {
        File fileStoreFile = fileStore.getFile(appZipMeta.getId());
        if (fileStoreFile == null) {
            LOG.error("Resource zip '{}' for app '{}' missing in file store", appZipMeta.getId(), app.getName());
            throw new RuntimeException("An error occurred trying to create or update app");
        }
        ZipFile zipFile;
        try {
            zipFile = new ZipFile(fileStoreFile);
        } catch (ZipException e) {
            LOG.error("Error creating zip file object", e);
            throw new RuntimeException("An error occurred trying to create or update app");
        }
        if (!zipFile.isValidZipFile()) {
            throw new MolgenisValidationException(new ConstraintViolation(String.format("'%s' is not a valid zip file.", appZipMeta.getFilename())));
        }
    }
}
Also used : ZipFile(net.lingala.zip4j.core.ZipFile) ConstraintViolation(org.molgenis.data.validation.ConstraintViolation) ZipException(net.lingala.zip4j.exception.ZipException) ZipFile(net.lingala.zip4j.core.ZipFile) File(java.io.File) MolgenisValidationException(org.molgenis.data.validation.MolgenisValidationException) FileMeta(org.molgenis.data.file.model.FileMeta)

Example 14 with FileMeta

use of org.molgenis.data.file.model.FileMeta in project molgenis by molgenis.

the class AppRepositoryDecorator method activateApp.

private void activateApp(App app) {
    FileMeta appSourceArchive = app.getSourceFiles();
    if (appSourceArchive != null) {
        File fileStoreFile = fileStore.getFile(appSourceArchive.getId());
        if (fileStoreFile == null) {
            LOG.error("Source archive '{}' for app '{}' missing in file store", appSourceArchive.getId(), app.getName());
            throw new RuntimeException("An error occurred trying to activate app");
        }
        try {
            ZipFile zipFile = new ZipFile(fileStoreFile);
            if (!app.getUseFreemarkerTemplate()) {
                FileHeader fileHeader = zipFile.getFileHeader("index.html");
                if (fileHeader == null) {
                    LOG.error("Missing index.html in {} while option Use freemarker template as index.html was set 'No'", app.getName());
                    throw new RuntimeException(format("Missing index.html in %s while option 'Use freemarker template as index.html' was set 'No'", app.getName()));
                }
            }
            // noinspection StringConcatenationMissingWhitespace
            zipFile.extractAll(fileStore.getStorageDir() + separatorChar + FILE_STORE_PLUGIN_APPS_PATH + separatorChar + app.getId() + separatorChar);
        } catch (ZipException e) {
            LOG.error("", e);
            throw new RuntimeException(format("An error occurred activating app '%s'", app.getName()));
        }
    }
}
Also used : ZipFile(net.lingala.zip4j.core.ZipFile) ZipException(net.lingala.zip4j.exception.ZipException) ZipFile(net.lingala.zip4j.core.ZipFile) File(java.io.File) FileHeader(net.lingala.zip4j.model.FileHeader) FileMeta(org.molgenis.data.file.model.FileMeta)

Example 15 with FileMeta

use of org.molgenis.data.file.model.FileMeta in project molgenis by molgenis.

the class AppRepositoryDecorator method updateApp.

private void updateApp(App app, App existingApp) {
    FileMeta appSourceFiles = app.getSourceFiles();
    FileMeta existingAppSourceFiles = existingApp.getSourceFiles();
    if (appSourceFiles != null) {
        if (existingAppSourceFiles != null) {
            if (!appSourceFiles.getId().equals(existingAppSourceFiles.getId())) {
                validateResourceZip(app);
                if (existingApp.isActive()) {
                    deactivateApp(existingApp);
                }
                if (app.isActive()) {
                    activateApp(app);
                }
            } else {
                if (app.isActive() && !existingApp.isActive()) {
                    activateApp(app);
                } else if (!app.isActive() && existingApp.isActive()) {
                    deactivateApp(app);
                }
            }
        } else {
            validateResourceZip(app);
            if (app.isActive()) {
                activateApp(app);
            }
        }
    } else {
        // noinspection VariableNotUsedInsideIf
        if (existingAppSourceFiles != null) {
            if (existingApp.isActive()) {
                deactivateApp(existingApp);
            }
        }
    }
}
Also used : FileMeta(org.molgenis.data.file.model.FileMeta)

Aggregations

FileMeta (org.molgenis.data.file.model.FileMeta)32 Test (org.testng.annotations.Test)14 File (java.io.File)12 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)6 Attribute (org.molgenis.data.meta.model.Attribute)5 Entity (org.molgenis.data.Entity)4 FileSystemResource (org.springframework.core.io.FileSystemResource)4 InputStream (java.io.InputStream)3 MolgenisDataException (org.molgenis.data.MolgenisDataException)3 EntityType (org.molgenis.data.meta.model.EntityType)3 IOException (java.io.IOException)2 Stream (java.util.stream.Stream)2 ZipFile (net.lingala.zip4j.core.ZipFile)2 ZipException (net.lingala.zip4j.exception.ZipException)2 Repository (org.molgenis.data.Repository)2 EntityImportReport (org.molgenis.data.importer.EntityImportReport)2 ImportService (org.molgenis.data.importer.ImportService)2 FileIngestJobExecution (org.molgenis.file.ingest.meta.FileIngestJobExecution)2 ServletUriComponentsBuilder (org.springframework.web.servlet.support.ServletUriComponentsBuilder)2 UriComponents (org.springframework.web.util.UriComponents)2