-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTargetedMSUpgradeCode.java
More file actions
149 lines (133 loc) · 6.05 KB
/
TargetedMSUpgradeCode.java
File metadata and controls
149 lines (133 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
* Copyright (c) 2013-2019 LabKey Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.labkey.targetedms;
import org.apache.logging.log4j.Logger;
import org.labkey.api.data.Container;
import org.labkey.api.data.ContainerManager;
import org.labkey.api.data.DeferredUpgrade;
import org.labkey.api.data.SQLFragment;
import org.labkey.api.data.SqlExecutor;
import org.labkey.api.data.UpgradeCode;
import org.labkey.api.module.Module;
import org.labkey.api.module.ModuleContext;
import org.labkey.api.module.ModuleLoader;
import org.labkey.api.security.User;
import org.labkey.api.targetedms.TargetedMSService;
import org.labkey.api.util.logging.LogHelper;
import org.labkey.targetedms.query.QCAnnotationTypeTable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
/**
* User: jeckels
* Date: 5/13/13
*/
public class TargetedMSUpgradeCode implements UpgradeCode
{
private static final Logger LOG = LogHelper.getLogger(TargetedMSUpgradeCode.class, "TargetedMS schema upgrade operations");
// called at every bootstrap to initialize annotation types
@SuppressWarnings({"UnusedDeclaration"})
public void populateDefaultAnnotationTypes(final ModuleContext moduleContext)
{
if (ModuleLoader.getInstance().shouldInsertData())
{
insertAnnotationType("Instrumentation Change", "FF0000", moduleContext.getUpgradeUser());
insertAnnotationType("Reagent Change", "00FF00", moduleContext.getUpgradeUser());
insertAnnotationType("Technician Change", "0000FF", moduleContext.getUpgradeUser());
insertAnnotationType(QCAnnotationTypeTable.INSTRUMENT_DOWNTIME, "CCCC00", moduleContext.getUpgradeUser());
// Enable the module in the /Shared container so that it can be resolved
Set<Module> activeModules = new HashSet<>(ContainerManager.getSharedContainer().getActiveModules());
activeModules.add(ModuleLoader.getInstance().getModule(TargetedMSModule.class));
ContainerManager.getSharedContainer().setActiveModules(activeModules);
}
}
private void insertAnnotationType(String name, String color, User user)
{
SQLFragment sql = new SQLFragment("INSERT INTO ");
sql.append(TargetedMSManager.getTableInfoQCAnnotationType());
sql.append(" (Container, Created, Modified, CreatedBy, ModifiedBy, Name, Color) VALUES (?, ?, ?, ?, ?, ?, ?)");
sql.add(ContainerManager.getSharedContainer());
Date now = new Date();
sql.add(now);
sql.add(now);
sql.add(user.getUserId());
sql.add(user.getUserId());
sql.add(name);
sql.add(color);
new SqlExecutor(TargetedMSManager.getSchema()).execute(sql);
}
/** Populate PTMPercentsGroupedPrepivotCache for all runs in existing ExperimentMAM folders */
@SuppressWarnings("UnusedDeclaration")
@DeferredUpgrade
public void populatePTMPercentsGroupedPrepivotCache(final ModuleContext moduleContext)
{
if (moduleContext.isNewInstall())
{
return;
}
User user = moduleContext.getUpgradeUser();
LOG.info("Populating PTMPercentsGroupedPrepivotCache for existing ExperimentMAM folders");
for (TargetedMSRun run : TargetedMSManager.getAllNonDeletedRuns())
{
Container container = run.getContainer();
if (container != null && TargetedMSManager.getFolderType(container) == TargetedMSService.FolderType.ExperimentMAM)
{
try
{
TargetedMSManager.populatePTMPercentsGroupedPrepivotCache(run, user, container);
}
catch (Exception e)
{
LOG.error("Error populating PTMPercentsGroupedPrepivotCache for run " + run.getId() + " in " + container.getPath(), e);
}
}
}
LOG.info("Finished populating PTMPercentsGroupedPrepivotCache for existing ExperimentMAM folders");
}
@SuppressWarnings("UnusedDeclaration")
public void reparentOrphanedTargetedMSData(final ModuleContext moduleContext)
{
if (moduleContext.isNewInstall())
{
return;
}
Container sharedContainer = ContainerManager.getSharedContainer();
SqlExecutor executor = new SqlExecutor(TargetedMSManager.getSchema());
String[] tablesToDeleteOrphans = {"QCAnnotation", "GuideSet", "AutoQCPing"};
for (String tableName : tablesToDeleteOrphans)
{
SQLFragment deleteSql = new SQLFragment("DELETE FROM targetedms.").append(tableName)
.append(" WHERE Container NOT IN (SELECT EntityId FROM core.Containers)");
int deletedCount = executor.execute(deleteSql);
if (deletedCount > 0)
{
LOG.info("Deleted " + deletedCount + " orphaned rows from targetedms." + tableName);
}
}
String[] tablesToReparentOrphans = {"Runs", "PrecursorChromInfo", "SampleFileChromInfo"};
for (String tableName : tablesToReparentOrphans)
{
SQLFragment updateSql = new SQLFragment("UPDATE targetedms.").append(tableName)
.append(" SET Container = ? WHERE Container NOT IN (SELECT EntityId FROM core.Containers)")
.add(sharedContainer.getEntityId());
int updatedCount = executor.execute(updateSql);
if (updatedCount > 0)
{
LOG.info("Reparented " + updatedCount + " orphaned rows from targetedms." + tableName + " to /Shared");
}
}
}
}