1
|
package eu.dnetlib.data.mdstore.modular.inspector;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.ArrayList;
|
5
|
import java.util.HashMap;
|
6
|
import java.util.List;
|
7
|
import java.util.Map;
|
8
|
import java.util.function.Consumer;
|
9
|
import java.util.function.Function;
|
10
|
import java.util.stream.Collectors;
|
11
|
import javax.annotation.Resource;
|
12
|
|
13
|
import com.google.common.collect.Lists;
|
14
|
import com.mongodb.DBObject;
|
15
|
import com.mongodb.client.FindIterable;
|
16
|
import com.mongodb.client.model.Filters;
|
17
|
import com.mongodb.client.model.Sorts;
|
18
|
import eu.dnetlib.data.mdstore.modular.MDStoreDescription;
|
19
|
import eu.dnetlib.data.mdstore.modular.MDStoreFeeder;
|
20
|
import eu.dnetlib.data.mdstore.modular.connector.MDStore;
|
21
|
import eu.dnetlib.data.mdstore.modular.connector.MDStoreDao;
|
22
|
import eu.dnetlib.data.mdstore.modular.connector.MDStoreManagerInfo;
|
23
|
import eu.dnetlib.data.mdstore.modular.connector.MDStoreTransactionManager;
|
24
|
import eu.dnetlib.data.mdstore.modular.mongodb.MongoMDStore;
|
25
|
import eu.dnetlib.enabling.inspector.AbstractInspectorController;
|
26
|
import eu.dnetlib.miscutils.functional.xml.XMLIndenter;
|
27
|
import eu.dnetlib.rmi.data.MDStoreServiceException;
|
28
|
import org.apache.commons.lang3.StringUtils;
|
29
|
import org.apache.commons.logging.Log;
|
30
|
import org.apache.commons.logging.LogFactory;
|
31
|
import org.bson.conversions.Bson;
|
32
|
import org.dom4j.Document;
|
33
|
import org.dom4j.DocumentException;
|
34
|
import org.dom4j.io.SAXReader;
|
35
|
import org.springframework.beans.factory.annotation.Autowired;
|
36
|
import org.springframework.stereotype.Controller;
|
37
|
import org.springframework.ui.Model;
|
38
|
import org.springframework.web.bind.annotation.RequestMapping;
|
39
|
import org.springframework.web.bind.annotation.RequestMethod;
|
40
|
import org.springframework.web.bind.annotation.RequestParam;
|
41
|
|
42
|
@Controller
|
43
|
public class MDStoreInspector extends AbstractInspectorController {
|
44
|
|
45
|
private static final Log log = LogFactory.getLog(MDStoreInspector.class); // NOPMD by marko on 11/24/08 5:02 PM
|
46
|
private Bson sortByIdAsc = Sorts.orderBy(Sorts.ascending("id"));
|
47
|
|
48
|
@Resource(name = "mongodbMDStoreDao")
|
49
|
private MDStoreDao dao;
|
50
|
|
51
|
@Autowired
|
52
|
private MDStoreTransactionManager transactionManager;
|
53
|
|
54
|
@Autowired
|
55
|
private MDStoreFeeder feeder;
|
56
|
|
57
|
@RequestMapping(value = "/inspector/mdstores.do")
|
58
|
public void mdstores(final Model model) throws MDStoreServiceException {
|
59
|
model.addAttribute("mdstores", dao.listMDStores());
|
60
|
}
|
61
|
|
62
|
@RequestMapping(value = "/inspector/mdstore.do", method = RequestMethod.GET)
|
63
|
public void mdstore(final Model model,
|
64
|
@RequestParam("id") final String id,
|
65
|
@RequestParam(value = "start", required = false) final Integer startParam,
|
66
|
@RequestParam(value = "regex", required = false) final String regex) throws MDStoreServiceException {
|
67
|
int pageSize = 10;
|
68
|
int start = 0;
|
69
|
|
70
|
if (startParam != null) {
|
71
|
start = startParam;
|
72
|
}
|
73
|
|
74
|
MongoMDStore mdstore = (MongoMDStore) dao.getMDStore(id);
|
75
|
|
76
|
Function<String, Map<String, String>> function = new Function<String, Map<String, String>>() {
|
77
|
|
78
|
private SAXReader reader = new SAXReader();
|
79
|
|
80
|
@Override
|
81
|
public Map<String, String> apply(final String input) {
|
82
|
try {
|
83
|
|
84
|
Document doc = reader.read(new StringReader(input));
|
85
|
final Map<String, String> result = new HashMap<>();
|
86
|
result.put("docId", doc.valueOf("//*[local-name()='objIdentifier']"));
|
87
|
result.put("body", input.replaceAll("<", "<").replaceAll(">", ">"));
|
88
|
result.put("mdId", id);
|
89
|
return result;
|
90
|
} catch (DocumentException e) {
|
91
|
throw new RuntimeException(e);
|
92
|
}
|
93
|
|
94
|
}
|
95
|
};
|
96
|
|
97
|
FindIterable<DBObject> mongoIterator;
|
98
|
int size = 0;
|
99
|
if (!StringUtils.isBlank(regex)) {
|
100
|
Bson query = Filters.regex("body", regex);
|
101
|
size = (int) mdstore.getCollection().count(query);
|
102
|
mongoIterator = mdstore.getCollection().find(query).sort(sortByIdAsc).skip(start).limit(pageSize);
|
103
|
} else {
|
104
|
size = (int) mdstore.getCollection().count();
|
105
|
mongoIterator = mdstore.getCollection().find().sort(sortByIdAsc).skip(start).limit(pageSize);
|
106
|
}
|
107
|
|
108
|
XMLIndenter indenter = new XMLIndenter();
|
109
|
List<String> currentList = new ArrayList<>();
|
110
|
mongoIterator.forEach
|
111
|
(
|
112
|
(Consumer<DBObject>) dbObject ->
|
113
|
currentList.add(indenter.apply((String) dbObject.get("body")))
|
114
|
);
|
115
|
final List<Map<String, String>> page = currentList.stream().map(function).collect(Collectors.toList());
|
116
|
model.addAttribute("id", id);
|
117
|
model.addAttribute("start", start);
|
118
|
model.addAttribute("regex", regex);
|
119
|
model.addAttribute("nextPage", start + pageSize);
|
120
|
model.addAttribute("prevPage", Math.max(0, start - pageSize));
|
121
|
model.addAttribute("size", size);
|
122
|
model.addAttribute("page", page);
|
123
|
}
|
124
|
|
125
|
@RequestMapping(value = "/inspector/mdstore.do", method = RequestMethod.POST)
|
126
|
public String bulkReplace(final Model model,
|
127
|
@RequestParam("id") final String id,
|
128
|
@RequestParam("regex") final String regex,
|
129
|
@RequestParam("replace") final String replace,
|
130
|
@RequestParam(value = "checkReplace", required = false) final Boolean checkReplace) throws MDStoreServiceException {
|
131
|
|
132
|
log.debug("regex: " + regex);
|
133
|
log.debug("replace: " + replace);
|
134
|
|
135
|
MongoMDStore mdstore = (MongoMDStore) dao.getMDStore(id);
|
136
|
|
137
|
boolean replaceEnable = checkReplace != null && checkReplace == true;
|
138
|
|
139
|
if (replaceEnable) {
|
140
|
mdstore.replace(regex, replace);
|
141
|
} else {
|
142
|
model.addAttribute("regex", regex);
|
143
|
}
|
144
|
|
145
|
return "redirect:mdstore.do?id=" + id;
|
146
|
}
|
147
|
|
148
|
@RequestMapping(value = "/inspector/mdstoreEditResult.do")
|
149
|
public void mdstoreEditRecord(final Model model, @RequestParam("mdId") final String mdId, @RequestParam("docId") final String docId)
|
150
|
throws MDStoreServiceException {
|
151
|
MDStore mdstore = dao.getMDStore(mdId);
|
152
|
String record = mdstore.getRecord(docId);
|
153
|
log.debug("Displaying record for editing :" + record);
|
154
|
model.addAttribute("mdId", mdId);
|
155
|
XMLIndenter tryIndent = new XMLIndenter();
|
156
|
String escaped = tryIndent.apply(record);
|
157
|
model.addAttribute("body", escaped.replace("&", "&"));
|
158
|
}
|
159
|
|
160
|
@RequestMapping(value = "/inspector/mdstoreSaveRecord.do")
|
161
|
public String mdstoreSaveRecord(final Model model, @RequestParam("mdId") final String mdId, @RequestParam("body") final String body)
|
162
|
throws MDStoreServiceException {
|
163
|
MDStore mdstore = dao.getMDStore(mdId);
|
164
|
|
165
|
mdstore.feed(Lists.newArrayList(body), true);
|
166
|
|
167
|
return "redirect:mdstore.do?id=" + mdId;
|
168
|
|
169
|
}
|
170
|
|
171
|
@RequestMapping(value = "/inspector/mdstoreDeleteRecord.do")
|
172
|
public String mdstoreDeleteRecord(final Model model, @RequestParam("mdId") final String mdId, @RequestParam("docId") final String docId)
|
173
|
throws MDStoreServiceException {
|
174
|
MDStore mdstore = dao.getMDStore(mdId);
|
175
|
log.info("deleting record " + docId);
|
176
|
mdstore.deleteRecord(docId);
|
177
|
return "redirect:mdstore.do?id=" + mdId;
|
178
|
}
|
179
|
|
180
|
@RequestMapping(value = "/inspector/mdstoresRefreshSizes.do")
|
181
|
public String mdstoresRefreshSizes(final Model model) throws MDStoreServiceException {
|
182
|
|
183
|
for (MDStoreDescription mdstore : dao.listMDStores()) {
|
184
|
feeder.touchSize(mdstore.getId(), dao.getMDStore(mdstore.getId()).getSize());
|
185
|
}
|
186
|
return "redirect:mdstores.do";
|
187
|
}
|
188
|
|
189
|
@RequestMapping(value = "/inspector/ensure.do")
|
190
|
public String mdstoreEnsureIndex(final Model model, @RequestParam("id") final String mdId) throws MDStoreServiceException {
|
191
|
|
192
|
MongoMDStore mdStore = (MongoMDStore) dao.getMDStore(mdId);
|
193
|
|
194
|
log.info("manual ensureIndex for mdId: " + mdId);
|
195
|
mdStore.ensureIndices();
|
196
|
|
197
|
return "redirect:mdstores.do";
|
198
|
}
|
199
|
|
200
|
@RequestMapping(value = "/inspector/infoTransaction.do")
|
201
|
public void mdstoreInfoTransaction(final Model model, @RequestParam("id") final String id) throws MDStoreServiceException {
|
202
|
MDStoreManagerInfo info = transactionManager.getInfoForCurrentMdStore(id);
|
203
|
model.addAttribute("info", info);
|
204
|
}
|
205
|
|
206
|
@RequestMapping(value = "/inspector/dropUsedCollection.do")
|
207
|
public String dropUsedCollection(final Model model, @RequestParam("mdId") final String mdId, @RequestParam("id") final String id)
|
208
|
throws MDStoreServiceException {
|
209
|
transactionManager.dropUsed(mdId, id);
|
210
|
return "redirect:mdstore.do?id=" + mdId;
|
211
|
}
|
212
|
|
213
|
@RequestMapping(value = "/inspector/invalidTransactionCollection.do")
|
214
|
public String invalidTransactionCollection(final Model model, @RequestParam("mdId") final String mdId, @RequestParam("id") final String id)
|
215
|
throws MDStoreServiceException {
|
216
|
transactionManager.dropTransaction(mdId, id);
|
217
|
return "redirect:mdstore.do?id=" + mdId;
|
218
|
}
|
219
|
|
220
|
@RequestMapping(value = "/inspector/refreshSizes.do")
|
221
|
public String refreshSizes(final Model model) throws MDStoreServiceException {
|
222
|
|
223
|
dao.refreshSizes();
|
224
|
return "redirect:mdstores.do";
|
225
|
}
|
226
|
|
227
|
@RequestMapping(value = "/inspector/doGarbage.do")
|
228
|
public String doGarbage(final Model model) throws MDStoreServiceException {
|
229
|
|
230
|
dao.startGarbage();
|
231
|
return "redirect:mdstores.do";
|
232
|
}
|
233
|
}
|