1
|
/**
|
2
|
* Copyright (C) 2016 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.api.service.util;
|
10
|
|
11
|
import java.io.Serializable;
|
12
|
import java.sql.SQLException;
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.List;
|
15
|
import java.util.Map;
|
16
|
import java.util.Set;
|
17
|
|
18
|
import org.apache.log4j.Logger;
|
19
|
import org.hibernate.FlushMode;
|
20
|
import org.hibernate.SQLQuery;
|
21
|
import org.hibernate.Session;
|
22
|
import org.springframework.beans.factory.annotation.Autowired;
|
23
|
import org.springframework.orm.hibernate5.HibernateTransactionManager;
|
24
|
import org.springframework.stereotype.Component;
|
25
|
import org.springframework.transaction.TransactionDefinition;
|
26
|
import org.springframework.transaction.TransactionStatus;
|
27
|
import org.springframework.transaction.support.DefaultTransactionDefinition;
|
28
|
|
29
|
import eu.etaxonomy.cdm.api.service.IAgentService;
|
30
|
import eu.etaxonomy.cdm.api.service.IProgressMonitorService;
|
31
|
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
|
32
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
33
|
import eu.etaxonomy.cdm.api.service.UpdateResult;
|
34
|
import eu.etaxonomy.cdm.api.service.config.SortIndexUpdaterConfigurator;
|
35
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
36
|
import eu.etaxonomy.cdm.common.monitor.SubProgressMonitor;
|
37
|
import eu.etaxonomy.cdm.database.update.SortIndexUpdater;
|
38
|
|
39
|
/**
|
40
|
* @author k.luther
|
41
|
* @since 08.07.2016
|
42
|
*
|
43
|
*/
|
44
|
@Component
|
45
|
public class SortIndexUpdaterWrapper implements Serializable {
|
46
|
|
47
|
private static final long serialVersionUID = 1152526455024556637L;
|
48
|
private static final Logger logger = Logger.getLogger(SortIndexUpdaterWrapper.class);
|
49
|
@Autowired
|
50
|
IAgentService agentService;
|
51
|
@Autowired
|
52
|
ITaxonService taxonService;
|
53
|
@Autowired
|
54
|
ITaxonNodeService taxonNodeService;
|
55
|
@Autowired
|
56
|
protected IProgressMonitorService progressMonitorService;
|
57
|
|
58
|
@Autowired
|
59
|
private HibernateTransactionManager transactionManager;
|
60
|
// @Autowired
|
61
|
// private CdmRepository repository;
|
62
|
|
63
|
public UpdateResult doInvoke(SortIndexUpdaterConfigurator config) {
|
64
|
|
65
|
SortIndexUpdater updater;
|
66
|
UpdateResult result = new UpdateResult();
|
67
|
// CaseType caseType = CaseType.caseTypeOfDatasource(config.getDestination());
|
68
|
|
69
|
IProgressMonitor monitor = config.getMonitor();
|
70
|
|
71
|
if (config.isDoTaxonNode()){
|
72
|
updater = SortIndexUpdater.NewInstance("Update taxonnode sortindex", "TaxonNode", "parent_id", "sortIndex", true);
|
73
|
|
74
|
result.includeResult(update(updater, monitor));
|
75
|
|
76
|
|
77
|
}
|
78
|
if (config.isDoFeatureNode()){
|
79
|
updater = SortIndexUpdater.NewInstance("Update Feature node sortindex", "FeatureNode", "parent_id", "sortIndex", true);
|
80
|
result.includeResult(update(updater, monitor));
|
81
|
}
|
82
|
if (config.isDoPolytomousKeyNode()){
|
83
|
updater = SortIndexUpdater.NewInstance("Update Polytomouskey node sortindex", "PolytomousKeyNode", "parent_id", "sortindex", true);
|
84
|
result.includeResult(update(updater, monitor));
|
85
|
}
|
86
|
return result;
|
87
|
|
88
|
}
|
89
|
|
90
|
private UpdateResult update(SortIndexUpdater updater, IProgressMonitor monitor){
|
91
|
UpdateResult updateResult = new UpdateResult();
|
92
|
try {
|
93
|
|
94
|
TransactionStatus tx;
|
95
|
tx = startTransaction(true);
|
96
|
String query = updater.createIndexMapQuery();
|
97
|
SQLQuery sqlQuery = agentService.getSession().createSQLQuery(query);
|
98
|
|
99
|
List data = sqlQuery.list();
|
100
|
int c = 2;
|
101
|
if (updater.getTableName().equals("TaxonNode")){
|
102
|
c= 3;
|
103
|
}
|
104
|
monitor.beginTask("Update index", data.size()*c);
|
105
|
monitor.subTask("Create new index");
|
106
|
IProgressMonitor subMonitor = new SubProgressMonitor(monitor, data.size());
|
107
|
List<Integer[]> result = new ArrayList<Integer[]>();
|
108
|
int id;
|
109
|
int parentId;
|
110
|
Object oId;
|
111
|
Object oParentId;
|
112
|
Integer[] rowArray = new Integer[2];
|
113
|
int done = 0;
|
114
|
for(Object object : data)
|
115
|
{
|
116
|
Object[] row = (Object[])object;
|
117
|
oId = row[0];
|
118
|
|
119
|
if (oId != null){
|
120
|
id = Integer.valueOf(oId.toString());
|
121
|
rowArray = new Integer[2];
|
122
|
oParentId = row[1];
|
123
|
if (oParentId != null){
|
124
|
parentId = Integer.valueOf(oParentId.toString());
|
125
|
rowArray[1]= parentId;
|
126
|
|
127
|
}else{
|
128
|
rowArray[1]= null;
|
129
|
}
|
130
|
rowArray[0]= id;
|
131
|
result.add(rowArray);
|
132
|
}
|
133
|
done++;
|
134
|
subMonitor.internalWorked(done);
|
135
|
}
|
136
|
subMonitor.done();
|
137
|
monitor.subTask("update indeces");
|
138
|
|
139
|
Map<Integer, Set<Integer>> indexMap = updater.makeIndexMap(result);
|
140
|
subMonitor = new SubProgressMonitor(monitor, indexMap.size());
|
141
|
done = 0;
|
142
|
for (Map.Entry<Integer, Set<Integer>> entry: indexMap.entrySet()){
|
143
|
String idSet = updater.makeIdSetString(entry.getValue());
|
144
|
query = updater.createUpdateIndicesQuery(null,entry.getKey(), idSet);
|
145
|
sqlQuery = agentService.getSession().createSQLQuery(query);
|
146
|
int resultInt = sqlQuery.executeUpdate();
|
147
|
logger.debug("update all indice with index "+entry.getKey()+ " - " + resultInt);
|
148
|
done++;
|
149
|
subMonitor.internalWorked(done);
|
150
|
|
151
|
}
|
152
|
subMonitor.done();
|
153
|
//Update childrenCount
|
154
|
if (updater.getTableName().equals("TaxonNode")){
|
155
|
|
156
|
query = updater.getChildrenCountQuery();
|
157
|
sqlQuery = taxonService.getSession().createSQLQuery(query);
|
158
|
data = sqlQuery.list();
|
159
|
subMonitor = new SubProgressMonitor(monitor, data.size());
|
160
|
int realCount;
|
161
|
int countChildren;
|
162
|
int work = 0;
|
163
|
for(Object object : data)
|
164
|
{
|
165
|
Object[] row = (Object[])object;
|
166
|
realCount = ((Number) row[0]).intValue();
|
167
|
countChildren = ((Number) row[1]).intValue();
|
168
|
id = ((Number) row[2]).intValue();
|
169
|
|
170
|
if (realCount != countChildren){
|
171
|
query = updater.getUpdateChildrenCount(realCount, id);
|
172
|
sqlQuery = taxonNodeService.getSession().createSQLQuery(query);
|
173
|
int resultInt = sqlQuery.executeUpdate();
|
174
|
logger.debug("update all childrenCount "+ resultInt);
|
175
|
}
|
176
|
work ++;
|
177
|
subMonitor.internalWorked(work);
|
178
|
subMonitor.done();
|
179
|
}
|
180
|
}
|
181
|
//TODO: correct handling of updateResult!
|
182
|
monitor.done();
|
183
|
commitTransaction(tx);
|
184
|
return updateResult;
|
185
|
} catch (SQLException e) {
|
186
|
|
187
|
monitor.warning("Stopped sortIndex updater");
|
188
|
updateResult.setAbort();
|
189
|
updateResult.addException(e);
|
190
|
}
|
191
|
return null;
|
192
|
}
|
193
|
|
194
|
public TransactionStatus startTransaction(Boolean readOnly) {
|
195
|
|
196
|
DefaultTransactionDefinition defaultTxDef = new DefaultTransactionDefinition();
|
197
|
defaultTxDef.setReadOnly(readOnly);
|
198
|
TransactionDefinition txDef = defaultTxDef;
|
199
|
|
200
|
// Log some transaction-related debug information.
|
201
|
if (logger.isTraceEnabled()) {
|
202
|
logger.trace("Transaction name = " + txDef.getName());
|
203
|
logger.trace("Transaction facets:");
|
204
|
logger.trace("Propagation behavior = " + txDef.getPropagationBehavior());
|
205
|
logger.trace("Isolation level = " + txDef.getIsolationLevel());
|
206
|
logger.trace("Timeout = " + txDef.getTimeout());
|
207
|
logger.trace("Read Only = " + txDef.isReadOnly());
|
208
|
// org.springframework.orm.hibernate5.HibernateTransactionManager
|
209
|
// provides more transaction/session-related debug information.
|
210
|
}
|
211
|
|
212
|
TransactionStatus txStatus = transactionManager.getTransaction(txDef);
|
213
|
|
214
|
getSession().setFlushMode(FlushMode.COMMIT);
|
215
|
|
216
|
return txStatus;
|
217
|
}
|
218
|
|
219
|
/**
|
220
|
* @return
|
221
|
*/
|
222
|
private Session getSession() {
|
223
|
return agentService.getSession();
|
224
|
}
|
225
|
|
226
|
public void commitTransaction(TransactionStatus txStatus){
|
227
|
logger.debug("commiting transaction ...");
|
228
|
transactionManager.commit(txStatus);
|
229
|
return;
|
230
|
}
|
231
|
|
232
|
}
|