1
|
package eu.dnetlib.data.mdstore.modular.inspector;
|
2
|
|
3
|
import static eu.dnetlib.miscutils.collections.MappedCollection.listMap;
|
4
|
|
5
|
import java.util.List;
|
6
|
|
7
|
import javax.annotation.Resource;
|
8
|
|
9
|
import org.apache.commons.logging.Log;
|
10
|
import org.apache.commons.logging.LogFactory;
|
11
|
import org.springframework.beans.factory.annotation.Autowired;
|
12
|
import org.springframework.stereotype.Controller;
|
13
|
import org.springframework.ui.Model;
|
14
|
import org.springframework.web.bind.annotation.RequestMapping;
|
15
|
import org.springframework.web.bind.annotation.RequestMethod;
|
16
|
import org.springframework.web.bind.annotation.RequestParam;
|
17
|
|
18
|
import com.google.common.collect.Iterables;
|
19
|
import com.google.common.collect.Lists;
|
20
|
|
21
|
import eu.dnetlib.data.mdstore.MDStoreServiceException;
|
22
|
import eu.dnetlib.data.mdstore.modular.MDStoreFeeder;
|
23
|
import eu.dnetlib.data.mdstore.modular.connector.MDStore;
|
24
|
import eu.dnetlib.data.mdstore.modular.connector.MDStoreDao;
|
25
|
import eu.dnetlib.data.mdstore.modular.connector.MDStoreManagerInfo;
|
26
|
import eu.dnetlib.data.mdstore.modular.connector.MDStoreTransactionManager;
|
27
|
import eu.dnetlib.data.mdstore.modular.mongodb.MongoMDStore;
|
28
|
import eu.dnetlib.enabling.inspector.AbstractInspectorController;
|
29
|
import eu.dnetlib.enabling.resultset.ResultSetListener;
|
30
|
import eu.dnetlib.miscutils.collections.MappedCollection;
|
31
|
import eu.dnetlib.miscutils.functional.UnaryFunction;
|
32
|
import eu.dnetlib.miscutils.functional.string.EscapeHtml;
|
33
|
|
34
|
@Controller
|
35
|
public class MDStoreInspector extends AbstractInspectorController {
|
36
|
|
37
|
private static final Log log = LogFactory.getLog(MDStoreInspector.class); // NOPMD by marko on 11/24/08 5:02 PM
|
38
|
|
39
|
@Resource(name = "mongodbMDStoreDao")
|
40
|
private MDStoreDao mdstoreDao;
|
41
|
|
42
|
@Autowired
|
43
|
private MDStoreTransactionManager transactionManager;
|
44
|
|
45
|
@Resource
|
46
|
private MDStoreFeeder feeder;
|
47
|
|
48
|
class MDStoreDescription {
|
49
|
|
50
|
private String id;
|
51
|
|
52
|
private int size;
|
53
|
|
54
|
public MDStoreDescription(final String id, final int size) {
|
55
|
super();
|
56
|
this.id = id;
|
57
|
this.size = size;
|
58
|
}
|
59
|
|
60
|
public String getFormat() {
|
61
|
return mdstoreDao.getMDStore(id).getFormat();
|
62
|
}
|
63
|
|
64
|
public String getLayout() {
|
65
|
return mdstoreDao.getMDStore(id).getLayout();
|
66
|
}
|
67
|
|
68
|
public String getInterpretation() {
|
69
|
return mdstoreDao.getMDStore(id).getInterpretation();
|
70
|
}
|
71
|
|
72
|
public boolean getIndexed() {
|
73
|
|
74
|
String currentid;
|
75
|
try {
|
76
|
currentid = transactionManager.getMDStoreCollection(id);
|
77
|
return ((MongoMDStore) mdstoreDao.getMDStore(currentid)).isIndexed();
|
78
|
} catch (MDStoreServiceException e) {
|
79
|
return false;
|
80
|
}
|
81
|
|
82
|
}
|
83
|
|
84
|
public String getId() {
|
85
|
return id;
|
86
|
}
|
87
|
|
88
|
public void setId(final String id) {
|
89
|
this.id = id;
|
90
|
}
|
91
|
|
92
|
public int getSize() {
|
93
|
return size;
|
94
|
}
|
95
|
|
96
|
public void setSize(final int size) {
|
97
|
this.size = size;
|
98
|
}
|
99
|
}
|
100
|
|
101
|
@RequestMapping(value = "/inspector/mdstores.do")
|
102
|
public void mdstores(final Model model) {
|
103
|
model.addAttribute("mdstores", MappedCollection.listMap(mdstoreDao.listMDStores(), new UnaryFunction<MDStoreDescription, String>() {
|
104
|
|
105
|
@Override
|
106
|
public MDStoreDescription evaluate(final String arg) {
|
107
|
try {
|
108
|
String currentid = transactionManager.getMDStoreCollection(arg);
|
109
|
MDStore mdstore = mdstoreDao.getMDStore(currentid);
|
110
|
return new MDStoreDescription(arg, mdstore.getSize());
|
111
|
} catch (MDStoreServiceException e) {
|
112
|
return null;
|
113
|
}
|
114
|
|
115
|
}
|
116
|
}));
|
117
|
}
|
118
|
|
119
|
@RequestMapping(value = "/inspector/mdstore.do", method = RequestMethod.GET)
|
120
|
public void mdstore(final Model model,
|
121
|
@RequestParam("id") final String id,
|
122
|
@RequestParam(value = "start", required = false) final Integer startParam,
|
123
|
@RequestParam(value = "regex", required = false) final String regex) throws MDStoreServiceException {
|
124
|
int pageSize = 10;
|
125
|
int start = 0;
|
126
|
|
127
|
if (startParam != null) {
|
128
|
start = startParam;
|
129
|
}
|
130
|
|
131
|
String currentId = transactionManager.getMDStoreCollection(id);
|
132
|
|
133
|
MDStore mdstore = mdstoreDao.getMDStore(currentId);
|
134
|
|
135
|
ResultSetListener rs = mdstore.deliver(null, null, regex);
|
136
|
List<String> page = rs.getResult(1 + start, start + pageSize);
|
137
|
|
138
|
model.addAttribute("id", id);
|
139
|
model.addAttribute("start", start);
|
140
|
model.addAttribute("regex", regex);
|
141
|
model.addAttribute("nextPage", start + pageSize);
|
142
|
model.addAttribute("prevPage", Math.max(0, start - pageSize));
|
143
|
model.addAttribute("size", rs.getSize());
|
144
|
model.addAttribute("page", listMap(page, new EscapeHtml()));
|
145
|
}
|
146
|
|
147
|
@RequestMapping(value = "/inspector/mdstore.do", method = RequestMethod.POST)
|
148
|
public String bulkReplace(final Model model,
|
149
|
@RequestParam("id") final String id,
|
150
|
@RequestParam("regex") final String regex,
|
151
|
@RequestParam("replace") final String replace,
|
152
|
@RequestParam(value = "checkReplace", required = false) final Boolean checkReplace) throws MDStoreServiceException {
|
153
|
|
154
|
log.debug("regex: " + regex);
|
155
|
log.debug("replace: " + replace);
|
156
|
String currentId = transactionManager.getMDStoreCollection(id);
|
157
|
|
158
|
MongoMDStore mdstore = (MongoMDStore) mdstoreDao.getMDStore(currentId);
|
159
|
|
160
|
boolean replaceEnable = (checkReplace != null) && (checkReplace == true);
|
161
|
|
162
|
if (replaceEnable) {
|
163
|
mdstore.replace(regex, replace);
|
164
|
} else {
|
165
|
model.addAttribute("regex", regex);
|
166
|
}
|
167
|
|
168
|
return "redirect:mdstore.do?id=" + id;
|
169
|
}
|
170
|
|
171
|
@RequestMapping(value = "/inspector/mdstoreEditResult.do")
|
172
|
public void mdstoreEditRecord(final Model model,
|
173
|
@RequestParam("id") final String id,
|
174
|
@RequestParam("start") final int start,
|
175
|
@RequestParam("index") final int index) throws MDStoreServiceException {
|
176
|
String currentId = transactionManager.getMDStoreCollection(id);
|
177
|
MDStore mdstore = mdstoreDao.getMDStore(currentId);
|
178
|
|
179
|
ResultSetListener rs = mdstore.deliver(null, null, null);
|
180
|
List<String> page = rs.getResult(1 + index + start, 1 + index + start);
|
181
|
|
182
|
model.addAttribute("id", id);
|
183
|
model.addAttribute("start", start);
|
184
|
model.addAttribute("record", new EscapeHtml().evaluate(Iterables.getOnlyElement(page)));
|
185
|
}
|
186
|
|
187
|
@RequestMapping(value = "/inspector/mdstoreSaveRecord.do")
|
188
|
public String mdstoreSaveRecord(final Model model,
|
189
|
@RequestParam("id") final String id,
|
190
|
@RequestParam("start") final int start,
|
191
|
@RequestParam("record") final String record) {
|
192
|
MDStore mdstore = mdstoreDao.getMDStore(id);
|
193
|
|
194
|
mdstore.feed(Lists.newArrayList(record), true);
|
195
|
|
196
|
return "redirect:mdstore.do?id=" + id + "&start=" + start;
|
197
|
}
|
198
|
|
199
|
@RequestMapping(value = "/inspector/mdstoreDeleteRecord.do")
|
200
|
public String mdstoreDeleteRecord(final Model model,
|
201
|
@RequestParam("id") final String id,
|
202
|
@RequestParam("start") final int start,
|
203
|
@RequestParam("index") final int index) throws MDStoreServiceException {
|
204
|
|
205
|
String currentId = transactionManager.getMDStoreCollection(id);
|
206
|
MDStore mdstore = mdstoreDao.getMDStore(currentId);
|
207
|
|
208
|
ResultSetListener rs = mdstore.deliverIds(null, null, null);
|
209
|
List<String> page = rs.getResult(1 + index + start, 1 + index + start);
|
210
|
String recordId = Iterables.getOnlyElement(page);
|
211
|
|
212
|
log.fatal("deleting record " + recordId);
|
213
|
mdstore.deleteRecord(recordId);
|
214
|
|
215
|
return "redirect:mdstore.do?id=" + id + "&start=" + start;
|
216
|
}
|
217
|
|
218
|
@RequestMapping(value = "/inspector/mdstoresRefreshSizes.do")
|
219
|
public String mdstoresRefreshSizes(final Model model) throws MDStoreServiceException {
|
220
|
|
221
|
for (String mdId : mdstoreDao.listMDStores()) {
|
222
|
String currentId = transactionManager.getMDStoreCollection(mdId);
|
223
|
feeder.touchSize(mdId, mdstoreDao.getMDStore(currentId).getSize());
|
224
|
}
|
225
|
|
226
|
return "redirect:mdstores.do";
|
227
|
}
|
228
|
|
229
|
@RequestMapping(value = "/inspector/ensure.do")
|
230
|
public String mdstoreEnsureIndex(final Model model, @RequestParam("id") final String id) throws MDStoreServiceException {
|
231
|
|
232
|
String currentId = transactionManager.getMDStoreCollection(id);
|
233
|
MongoMDStore mdStore = (MongoMDStore) mdstoreDao.getMDStore(currentId);
|
234
|
|
235
|
log.info("manual ensureIndex for mdId: " + id);
|
236
|
mdStore.ensureIndices();
|
237
|
|
238
|
return "redirect:mdstores.do";
|
239
|
}
|
240
|
|
241
|
@RequestMapping(value = "/inspector/infoTransaction.do")
|
242
|
public void mdstoreInfoTransaction(final Model model, @RequestParam("id") final String id) throws MDStoreServiceException {
|
243
|
MDStoreManagerInfo info = transactionManager.getInfoForCurrentMdStore(id);
|
244
|
model.addAttribute("info", info);
|
245
|
}
|
246
|
|
247
|
@RequestMapping(value = "/inspector/dropUsedCollection.do")
|
248
|
public String dropUsedCollection(final Model model, @RequestParam("mdId") final String mdId, @RequestParam("id") final String id)
|
249
|
throws MDStoreServiceException {
|
250
|
transactionManager.dropUsed(mdId, id);
|
251
|
return "redirect:mdstores.do?id=" + mdId;
|
252
|
}
|
253
|
|
254
|
@RequestMapping(value = "/inspector/invalidTransactionCollection.do")
|
255
|
public String invalidTransactionCollection(final Model model, @RequestParam("mdId") final String mdId, @RequestParam("id") final String id)
|
256
|
throws MDStoreServiceException {
|
257
|
transactionManager.dropTransaction(mdId, id);
|
258
|
return "redirect:mdstores.do?id=" + mdId;
|
259
|
}
|
260
|
}
|