Skip to content

Commit

Permalink
Merge pull request #4837 from mtbc/enum-autopopulate
Browse files Browse the repository at this point in the history
have OMERO server populate database with missing enumeration values
  • Loading branch information
sbesson committed Sep 22, 2016
2 parents 14a76ca + ecbd979 commit 6888b58
Show file tree
Hide file tree
Showing 8 changed files with 318 additions and 117 deletions.
1 change: 1 addition & 0 deletions components/model/src/ome/util/SqlAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ private boolean append(final StringBuilder sb, final boolean first,

long countFormat(String name);

@Deprecated // use ome.services.util.EnsureEnum
int insertFormat(String name);

int closeSessions(String uuid);
Expand Down
2 changes: 2 additions & 0 deletions components/model/src/ome/util/actions/PostgresSqlAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,11 @@ public long countFormat(String name) {
}

// Copied from data.vm
@Deprecated // use ome.services.util.EnsureEnum
public final static String insertFormatSql = PsqlStrings
.getString("sql_action.insert_format"); //$NON-NLS-1$

@Deprecated // use ome.services.util.EnsureEnum
public int insertFormat(String name) {
return _jdbc().update(insertFormatSql, name);
}
Expand Down
18 changes: 17 additions & 1 deletion components/server/resources/ome/services/startup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,27 @@
lazy-init="false">
</bean>

<bean id="dbEnumerationCheck"
<bean id="dbFormatEnumerationCheck"
class="ome.services.util.DBEnumCheck"
init-method="start" lazy-init="false">
<constructor-arg ref="executor"/>
<constructor-arg ref="preferenceContext"/>
<constructor-arg ref="ensureEnum"/>
</bean>

<bean id="dbMappedEnumerationCheck"
class="ome.services.util.DBMappedEnumCheck"
init-method="start" lazy-init="false">
<constructor-arg ref="executor"/>
<constructor-arg ref="preferenceContext"/>
<constructor-arg ref="ensureEnum"/>
</bean>

<bean id="ensureEnum"
class="ome.services.util.EnsureEnum">
<constructor-arg ref="executor"/>
<constructor-arg ref="uuid"/>
<constructor-arg ref="roles"/>
</bean>

<bean id="dbUnicodeUnitsCheck" depends-on="dbPatchCheck"
Expand Down
6 changes: 4 additions & 2 deletions components/server/src/ome/logic/TypesImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,11 @@ public <T extends IEnum> Map<Class<T>, List<T>> getEnumerationsWithEntries() {
public <T extends IEnum> List<T> getOriginalEnumerations() {
List<IEnum> original = new ArrayList<IEnum>();
InputStream in = null;
JarFile jarFile = null;
try {
URL file = ResourceUtils.getURL("classpath:enums.properties");
URL jar = ResourceUtils.extractJarFileURL(file);
JarFile jarFile = new JarFile(jar.getPath());
jarFile = new JarFile(jar.getPath());
JarEntry entry = jarFile.getJarEntry("enums.properties");
in = jarFile.getInputStream(entry);
Properties property = new Properties();
Expand Down Expand Up @@ -212,7 +213,8 @@ public <T extends IEnum> List<T> getOriginalEnumerations() {
throw new RuntimeException("No such method. " + e.getMessage());
} finally {
try {
in.close();
if (in != null) in.close();
if (jarFile != null) jarFile.close();
} catch (IOException e) {
}
}
Expand Down
18 changes: 14 additions & 4 deletions components/server/src/ome/services/util/BaseDBCheck.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2014 University of Dundee & Open Microscopy Environment.
* Copyright (C) 2014-2016 University of Dundee & Open Microscopy Environment.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
Expand Down Expand Up @@ -41,9 +41,7 @@ abstract class BaseDBCheck {
private final String version;
private final int patch;

private final String configKey = "DB check " + getClass().getSimpleName();
private final String configValue = getCheckDone();
private final String configKeyValue = configKey + ": " + configValue;
private final String configKey, configValue, configKeyValue;

/**
* @param executor executor to use for configuration map check
Expand All @@ -52,6 +50,11 @@ protected BaseDBCheck(Executor executor, PreferenceContext preferences) {
this.executor = executor;
this.version = preferences.getProperty("omero.db.version");
this.patch = Integer.parseInt(preferences.getProperty("omero.db.patch"));

/* these values may depend upon version and patch above */
configKey = "DB check " + getClass().getSimpleName();
configValue = getCheckDone();
configKeyValue = configKey + ": " + configValue;
}

/**
Expand Down Expand Up @@ -122,4 +125,11 @@ public void start() {
protected String getCheckDone() {
return "done";
}

/**
* @return a string representing the version and patch of this server
*/
protected String getOmeroVersion() {
return version + "__" + patch;
}
}
129 changes: 19 additions & 110 deletions components/server/src/ome/services/util/DBEnumCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,16 @@
package ome.services.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import loci.formats.IFormatReader;
import loci.formats.ImageReader;
import ome.conditions.InternalException;

import ome.model.enums.Format;
import ome.system.PreferenceContext;
import ome.util.SqlAction;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;

/**
* Hook run by the context to guarantee that various enumerations are up to
Expand All @@ -29,124 +25,37 @@
* greatest without a database upgrade.
*
* @author Josh Moore, josh at glencoesoftware.com
* @author m.t.b.carroll@dundee.ac.uk
* @since Beta4.1.1
*/
public class DBEnumCheck extends BaseDBCheck {

public final static Logger log = LoggerFactory.getLogger(DBEnumCheck.class);

public final static Pattern readerClass = Pattern
.compile("^.*?[.]?([^.]+)Reader$");

/**
* Hard-coded list of formats missing from 4.1 which should have a
* "Companion/<reader>" value in the database. Temporary until bio-formats
* provides inspection on this information.
*/
final private static List<String> companionful = Arrays.asList("Analyze",
"APL", "L2D", "Nifti", "TillVision", "Scanr");

/**
* Hard-coded list of formats missing from 4.1 which should NOT have a
* "Companion/<reader>" value in the database. Temporary until bio-formats
* provides inspection on this information.
*/
final private static List<String> companionless = Arrays.asList("Zip",
"APNG", "PCX", "Ivision", "FEI", "NAF", "MINC", "MRW", "ARF",
"Cellomics", "LiFlim", "Amira");

/**
* Hard-coded list of formats missing from 4.1 which should NOT have a
* "Companion/<reader>" NOR a format at all. Temporary until bio-formats
* provides inspection on this information.
*/
final private static List<String> omitlist = Arrays.asList("Fake");

public static List<String> getReaderNames() {
List<String> rv = new ArrayList<String>();
IFormatReader[] readers = new ImageReader().getReaders();
for (IFormatReader formatReader : readers) {

String name = formatReader.getClass().getSimpleName();
Matcher matcher = readerClass.matcher(name);
if (!matcher.matches()) {
log.warn("Reader doesn't match: " + name);
continue;
}

rv.add(matcher.group(1));
}
return rv;
}

public static boolean requiresCompanion(String name) {
return companionful.contains(name);
}

public static boolean shouldBeOmitted(String name) {
return omitlist.contains(name);
}
private final EnsureEnum ensureEnum;

public DBEnumCheck(Executor executor, PreferenceContext preferences) {
public DBEnumCheck(Executor executor, PreferenceContext preferences, EnsureEnum ensureEnum) {
super(executor, preferences);
this.ensureEnum = ensureEnum;
}

@Override
protected void doCheck() {
try {
executor.executeSql(new Executor.SimpleSqlWork(this,
"DBEnumCheck") {
@Transactional(readOnly = false)
public Object doWork(SqlAction sql) {
for (String name : getReaderNames()) {
addFormat(sql, name);
if (requiresCompanion(name)) {
addFormat(sql, "Companion/" + name);
}
}
return null;
final List<String> formatNames = new ArrayList<String>();
for (final IFormatReader formatReader : new ImageReader().getReaders()) {
String name = formatReader.getClass().getSimpleName();
if (name.endsWith("Reader")) {
name = name.substring(0, name.length() - 6);
if ("Fake".equals(name)) {
continue;
}

});
} catch (Exception e) {
final String msg = "Error synchronizing enumerations";
log.error(msg, e); // slf4j migration: fatal() to error()
InternalException ie = new InternalException(msg);
throw ie;
}
}

/**
* Adds an ome.model.enums.Format object if 1) the format value is not
* omitted, 2) it doesn't already exist.
*
* @param jdbc
* @param name
* @return true if the format was added.
*/
private boolean addFormat(SqlAction sql, String name) {

if (shouldBeOmitted(name)) {
log.debug("Omitting: " + name);
return false;
}

long count = sql.countFormat(name);

if (count > 0) {
log.debug("Found reader: " + name);
return false;
}

int inserts = sql.insertFormat(name);
if (inserts != 1) {
throw new InternalException("Expected 1 insert. Found " + inserts
+ " while adding: " + name);
formatNames.add(name);
if (formatReader.hasCompanionFiles()) {
formatNames.add("Companion/" + name);
}
}
}

log.info("Added format: " + name);
return true;

ensureEnum.ensure(Format.class, formatNames);
}

@Override
Expand Down
Loading

0 comments on commit 6888b58

Please sign in to comment.