Skip to content
Snippets Groups Projects
Commit 6b53a7e9 authored by anttil's avatar anttil
Browse files

SSDM-4135: Project samples: Fill in proj_id field in samples table using migration

SVN: 37107
parent 73dd4bcd
No related branches found
No related tags found
No related merge requests found
...@@ -76,6 +76,8 @@ public final class DAOFactory extends AuthorizationDAOFactory implements IDAOFac ...@@ -76,6 +76,8 @@ public final class DAOFactory extends AuthorizationDAOFactory implements IDAOFac
{ {
private static Logger operationLog = LogFactory.getLogger(LogCategory.OPERATION, DAOFactory.class); private static Logger operationLog = LogFactory.getLogger(LogCategory.OPERATION, DAOFactory.class);
public static boolean projectSamplesEnabled = false;
static static
{ {
SpringEoDSQLExceptionTranslator.activate(); SpringEoDSQLExceptionTranslator.activate();
...@@ -342,31 +344,78 @@ public final class DAOFactory extends AuthorizationDAOFactory implements IDAOFac ...@@ -342,31 +344,78 @@ public final class DAOFactory extends AuthorizationDAOFactory implements IDAOFac
return operationExecutionDAO; return operationExecutionDAO;
} }
private static String projectConstraintFunction =
"CREATE FUNCTION check_project_is_defined_for_experiment_level_samples() " +
" RETURNS trigger AS " +
"$BODY$ " +
"BEGIN " +
" IF (NEW.proj_id IS NULL AND NEW.expe_id IS NOT NULL) THEN " +
" RAISE EXCEPTION 'Project has to be defined for experiment level samples'; " +
" END IF; " +
" RETURN NEW; " +
"END; " +
"$BODY$ " +
" LANGUAGE 'plpgsql';";
private static String projectConstraintTrigger =
"CREATE TRIGGER check_project_is_defined_for_experiment_level_samples " +
"BEFORE INSERT OR UPDATE " +
"ON samples_all " +
"FOR EACH ROW " +
"EXECUTE PROCEDURE check_project_is_defined_for_experiment_level_samples();";
private static String setProjectsToSamplesWithExperiments =
"UPDATE samples_all AS s " +
"SET proj_id = (SELECT proj_id FROM experiments_all WHERE id = s.expe_id) " +
"WHERE s.proj_id IS NULL AND s.expe_id IS NOT NULL;";
@Override @Override
public void afterPropertiesSet() throws Exception public void afterPropertiesSet() throws Exception
{ {
Properties serviceProperties = configurer.getResolvedProps(); Properties serviceProperties = configurer.getResolvedProps();
boolean projectSamplesEnabled = PropertyUtils.getBoolean(serviceProperties, Constants.PROJECT_SAMPLES_ENABLED_KEY, false); projectSamplesEnabled = PropertyUtils.getBoolean(serviceProperties, Constants.PROJECT_SAMPLES_ENABLED_KEY, false);
Connection connection = null; Connection connection = null;
try try
{ {
connection = context.getDataSource().getConnection(); connection = context.getDataSource().getConnection();
Statement statement = connection.createStatement(); Statement statement = connection.createStatement();
connection.setAutoCommit(false);
ResultSet result = statement.executeQuery("SELECT tgname FROM pg_trigger WHERE tgname='disable_project_level_samples'");
boolean triggerExists = result.next();
if (projectSamplesEnabled) if (projectSamplesEnabled)
{ {
operationLog.info("Enable project samples by dropping the trigger 'disable_project_level_samples'."); if (triggerExists)
statement.executeUpdate("DROP TRIGGER IF EXISTS disable_project_level_samples ON samples_all"); {
operationLog.info("Enable project samples by dropping the trigger 'disable_project_level_samples'.");
statement.executeUpdate("DROP TRIGGER disable_project_level_samples ON samples_all");
statement.executeUpdate(projectConstraintFunction);
statement.executeUpdate(projectConstraintTrigger);
statement.executeUpdate(setProjectsToSamplesWithExperiments);
} else
{
operationLog.info("Project samples already enabled.");
}
} else } else
{ {
ResultSet result = statement.executeQuery("SELECT tgname FROM pg_trigger WHERE tgname='disable_project_level_samples'");
boolean triggerExists = result.next();
if (triggerExists == false) if (triggerExists == false)
{ {
operationLog.warn("It is not possible to disable project samples feature. The system still considers " operationLog.warn("It is not possible to disable project samples feature. The system still considers "
+ Constants.PROJECT_SAMPLES_ENABLED_KEY + "=true."); + Constants.PROJECT_SAMPLES_ENABLED_KEY + "=true.");
projectSamplesEnabled = true;
} }
} }
statement.close(); statement.close();
connection.commit();
} catch (Throwable t)
{
operationLog.info("Failed to enable project level samples.", t);
if (connection != null)
{
connection.rollback();
}
} finally } finally
{ {
if (connection != null) if (connection != null)
......
...@@ -396,6 +396,7 @@ public class ExperimentPE extends AttachmentHolderPE implements ...@@ -396,6 +396,7 @@ public class ExperimentPE extends AttachmentHolderPE implements
{ {
getExperimentSamples().remove(sample); getExperimentSamples().remove(sample);
sample.setExperimentInternal(null); sample.setExperimentInternal(null);
sample.setProject(null);
} }
public void addSample(SamplePE sample) public void addSample(SamplePE sample)
...@@ -406,6 +407,7 @@ public class ExperimentPE extends AttachmentHolderPE implements ...@@ -406,6 +407,7 @@ public class ExperimentPE extends AttachmentHolderPE implements
experiment.getExperimentSamples().remove(sample); experiment.getExperimentSamples().remove(sample);
} }
sample.setExperimentInternal(this); sample.setExperimentInternal(this);
sample.setProject(project);
getExperimentSamples().add(sample); getExperimentSamples().add(sample);
} }
......
...@@ -69,6 +69,7 @@ import org.hibernate.validator.constraints.Length; ...@@ -69,6 +69,7 @@ import org.hibernate.validator.constraints.Length;
import ch.systemsx.cisd.common.collection.UnmodifiableSetDecorator; import ch.systemsx.cisd.common.collection.UnmodifiableSetDecorator;
import ch.systemsx.cisd.common.reflection.ModifiedShortPrefixToStringStyle; import ch.systemsx.cisd.common.reflection.ModifiedShortPrefixToStringStyle;
import ch.systemsx.cisd.openbis.generic.server.dataaccess.db.DAOFactory;
import ch.systemsx.cisd.openbis.generic.shared.IServer; import ch.systemsx.cisd.openbis.generic.shared.IServer;
import ch.systemsx.cisd.openbis.generic.shared.basic.BasicConstant; import ch.systemsx.cisd.openbis.generic.shared.basic.BasicConstant;
import ch.systemsx.cisd.openbis.generic.shared.basic.IIdentityHolder; import ch.systemsx.cisd.openbis.generic.shared.basic.IIdentityHolder;
...@@ -490,7 +491,10 @@ public class SamplePE extends AttachmentHolderPE implements IIdAndCodeHolder, Co ...@@ -490,7 +491,10 @@ public class SamplePE extends AttachmentHolderPE implements IIdAndCodeHolder, Co
public void setProject(ProjectPE project) public void setProject(ProjectPE project)
{ {
this.project = project; if (DAOFactory.projectSamplesEnabled)
{
this.project = project;
}
} }
@ManyToOne(fetch = FetchType.LAZY) @ManyToOne(fetch = FetchType.LAZY)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment