-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathTermController.java
More file actions
308 lines (267 loc) · 11.4 KB
/
TermController.java
File metadata and controls
308 lines (267 loc) · 11.4 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package org.ayfaar.app.controllers;
import org.apache.commons.lang.WordUtils;
import org.ayfaar.app.dao.CommonDao;
import org.ayfaar.app.dao.LinkDao;
import org.ayfaar.app.dao.TermDao;
import org.ayfaar.app.dao.TermMorphDao;
import org.ayfaar.app.model.*;
import org.ayfaar.app.spring.Model;
import org.ayfaar.app.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import java.util.*;
import static java.util.Collections.sort;
import static org.ayfaar.app.model.Link.*;
import static org.ayfaar.app.utils.ValueObjectUtils.convertToPlainObjects;
import static org.ayfaar.app.utils.ValueObjectUtils.getModelMap;
import static org.springframework.util.Assert.notNull;
@Controller
@RequestMapping("api/term")
public class TermController {
private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(TermController.class.getName());
@Autowired CommonDao commonDao;
@Autowired TermDao termDao;
@Autowired TermMorphDao termMorphDao;
@Autowired LinkDao linkDao;
@Autowired AliasesMap aliasesMap;
@Autowired
SuggestionsController searchController2;
/*@RequestMapping("import")
@Model
public void _import() throws ParserConfigurationException, SAXException, IOException {
for (Termin termin : commonDao.getAll(Termin.class)) {
add(termin.getName());
}
}*/
@RequestMapping(value = "add", method = RequestMethod.POST)
@Model
public void add(Term term) {
add(term.getName(), term.getShortDescription(), term.getDescription());
}
@RequestMapping(value = "/", method = RequestMethod.GET)
@Model
public ModelMap get(@RequestParam("name") String termName) {
Term term = termDao.getByName(termName);
notNull(term, "Термин не найден");
return get(term);
}
public ModelMap get(Term term) {
String termName = term.getName();
Term alias = null;
if (!aliasesMap.get(termName).getTerm().getUri().equals(term.getUri())) {
alias = term;
term = aliasesMap.get(termName).getTerm();
}
// может быть аббравиатурой, сокращением, кодом или синонимов
Link _link = linkDao.getForAbbreviationOrAliasOrCode(term.getUri());
if (_link != null && _link.getUid1() instanceof Term) {
alias = term;
term = (Term) _link.getUid1();
}
// }
ModelMap modelMap = (ModelMap) getModelMap(term);
modelMap.put("from", alias);
// LINKS
List<ModelMap> quotes = new ArrayList<ModelMap>();
Set<UID> related = new LinkedHashSet<UID>();
Set<UID> aliases = new LinkedHashSet<UID>();
UID code = null;
List<Link> links = linkDao.getAllLinks(term.getUri());
for (Link link : links) {
UID source = link.getUid1().getUri().equals(term.getUri())
? link.getUid2()
: link.getUid1();
if (link.getQuote() != null || source instanceof Item) {
quotes.add(getQuote(link, source));
} else if (ABBREVIATION.equals(link.getType()) || ALIAS.equals(link.getType())) {
aliases.add(source);
} else if (CODE.equals(link.getType())) {
code = source;
} else {
related.add(source);
}
}
// Нужно также включить цитаты всех синонимов и сокращений и кода
Set<UID> aliasesQuoteSources = new HashSet<UID>(aliases);
if (code != null) {
aliasesQuoteSources.add(code);
}
for (UID uid : aliasesQuoteSources) {
List<Link> aliasLinksWithQuote = linkDao.getAllLinks(uid.getUri());
for (Link link : aliasLinksWithQuote) {
UID source = link.getUid1().getUri().equals(uid.getUri())
? link.getUid2()
: link.getUid1();
if (link.getQuote() != null || source instanceof Item) {
quotes.add(getQuote(link, source));
} else if (ABBREVIATION.equals(link.getType()) || ALIAS.equals(link.getType()) || CODE.equals(link.getType())) {
// Синонимы синонимов :) по идее их не должно быть, но если вдруг...
// как минимум один есть и этот наш основной термин
if (!source.getUri().equals(term.getUri())) {
aliases.add(source);
}
} else {
related.add(source);
}
}
}
sort(quotes, new Comparator<ModelMap>() {
@Override
public int compare(ModelMap o1, ModelMap o2) {
return ((String) o1.get("uri")).compareTo((String) o2.get("uri"));
}
});
modelMap.put("code", code);
modelMap.put("quotes", quotes);
modelMap.put("related", toPlainObjectWithoutContent(related));
modelMap.put("aliases", toPlainObjectWithoutContent(aliases));
return modelMap;
}
private Object toPlainObjectWithoutContent(Set<UID> related) {
return convertToPlainObjects(related, new ValueObjectUtils.Modifier<UID>() {
@Override
public void modify(UID entity, ModelMap map) {
map.remove("content");
}
});
}
private ModelMap getQuote(Link link, UID source) {
ModelMap map = new ModelMap();
map.put("quote", link.getQuote() != null ? link.getQuote() : ((Item) source).getContent());
map.put("uri", source.getUri());
return map;
}
@RequestMapping("related")
@Model
@ResponseBody
public Collection<ModelMap> getRelated(@RequestParam String uri) {
Set<UID> related = new LinkedHashSet<UID>();
for (Link link : linkDao.getRelated(uri)) {
if (!ABBREVIATION.equals(link.getType()) && link.getQuote() == null) {
if (link.getUid1().getUri().equals(uri)) {
related.add(link.getUid2());
} else {
related.add(link.getUid1());
}
}
}
return convertToPlainObjects(related, new ValueObjectUtils.Modifier<UID>() {
@Override
public void modify(UID entity, ModelMap map) {
map.remove("content");
}
});
}
// @RequestMapping(value = "/", method = POST)
// @Model
public Term add(String name, String description) {
return add(name, null, description);
}
public Term add(String name, String shortDescription, String description) {
name = name.replace("\"", "").replace("«", "").replace("»", "").trim();
name = WordUtils.capitalize(name, new char[]{'@'}); // Делаем первую букву большой, @ - знак который не появляеться в названии, чтобы поднялась только первая буква всей фразы
Term term = termDao.getByName(name);
if (term == null) {
term = termDao.save(new Term(name, shortDescription, description));
String termName = term.getName();
log.info("Added: "+ termName);
if (TermUtils.isComposite(termName)) {
String target = TermUtils.getNonCosmicCodePart(termName);
if (target != null) {
findAliases(term, target, termName.replace(target, ""));
}
} else if (!TermUtils.isCosmicCode(termName)) {
findAliases(term, termName, "");
}
} else {
term.setShortDescription(shortDescription);
term.setDescription(description);
termDao.save(term);
}
new Thread(new Runnable() {
@Override
public void run() {
aliasesMap.reload();
}
}).start();
return term;
}
private void findAliases(Term primeTerm, String target, String prefix) {
Morpher morpher = null;
try {
morpher = new Morpher(target);
if (morpher.getData()) {
Set<String> aliases = new HashSet<String>();
for (Morpher.Morph morph : morpher.getAllMorph()) {
if (morph == null) {
return;
}
String alias = prefix+morph.text;
if (morph != null && !alias.isEmpty()
&& !alias.equals(primeTerm.getName())) {
if (!aliases.contains(alias)) {
TermMorph termMorph = commonDao.get(TermMorph.class, alias);
if (termMorph == null) {
commonDao.save(new TermMorph(alias, primeTerm.getUri()));
aliasesMap.put(alias, primeTerm);
log.info("Alias added: "+alias);
}
aliases.add(alias);
}
}
}
/* for (Map.Entry<String, Term> entry : aliases.entrySet()) {
if (primeTerm.getUri().equals(entry.getValue().generateUri())) continue;
commonDao.save(new Link(primeTerm, entry.getValue(), Link.ALIAS, Link.MORPHEME_WEIGHT));
}*/
}
} catch (Exception e) {
log.throwing(getClass().getName(), "findAliases", e);
throw new RuntimeException(e);
}
}
public Term getPrime(Term term) {
return (Term) linkDao.getPrimeForAlias(term.getUri());
}
public Term add(String termName) {
return add(termName, null);
}
@RequestMapping("autocomplete")
@ResponseBody
public List<String> autoComplete(@RequestParam("filter[filters][0][value]") String filter) {
return searchController2.suggestions(filter);
}
@RequestMapping("get-short-description")
@ResponseBody
public String getShortDescription(@RequestParam String name) {
return aliasesMap.getTerm(name).getShortDescription();
}
@RequestMapping("remove/{name}")
public void remove(@PathVariable String name) {
termDao.remove(termDao.getByName(name).getUri());
}
@RequestMapping("reload-aliases-map")
public void reloadAliasesMap() {
aliasesMap.reload();
}
@RequestMapping(value = "rename")
@ResponseBody
public void renameTerm(@RequestParam("oldTerm") String oldName, @RequestParam("newTerm") String newName) {
Term oldTerm = aliasesMap.getTerm(oldName);
add(newName, oldTerm.getShortDescription(), oldTerm.getDescription());
Term newTerm = termDao.getByName(newName);
List<Link> links = linkDao.getAllLinks(oldTerm.getUri());
for(Link link : links) {
if(link.getUid1().getUri().equals(oldTerm.getUri())) {
link.setUid1(newTerm);
}
else if(link.getUid2().getUri().equals(oldTerm.getUri())) {
link.setUid2(newTerm);
}
linkDao.save(link);
}
remove(oldName);
}
}