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);
}
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);
}
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())));
}
}
}
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()));
}
}
}
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);
}
}
}
}
Aggregations