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