1
|
package gr.uoa.di.web.utils;
|
2
|
|
3
|
import eu.dnetlib.domain.data.Document;
|
4
|
import eu.dnetlib.domain.enabling.Vocabulary;
|
5
|
import eu.dnetlib.domain.functionality.DocumentField;
|
6
|
import eu.dnetlib.domain.functionality.SwitchDocumentField;
|
7
|
import gr.uoa.di.web.utils.bibtex.BibTeX;
|
8
|
|
9
|
import java.util.List;
|
10
|
import java.util.Locale;
|
11
|
|
12
|
import org.apache.log4j.Logger;
|
13
|
|
14
|
public class BibTeXGenerator {
|
15
|
|
16
|
private static Logger logger = Logger.getLogger(BibTeXGenerator.class);
|
17
|
|
18
|
public static BibTeX generateBibTeX(List<DocumentField> fields, Document document, Vocabulary bibTeXVocabulary) {
|
19
|
BibTeX bibTeX = new BibTeX();
|
20
|
|
21
|
//Create entry
|
22
|
List<String> entryValues = document.getMap().get("CobjCategory");
|
23
|
if (entryValues != null && !entryValues.isEmpty()) {
|
24
|
String entry = bibTeXVocabulary.getEnglishName(entryValues.get(0));
|
25
|
if (entry == null) { bibTeX.setEntry("article"); }
|
26
|
else { bibTeX.setEntry(entry); }
|
27
|
|
28
|
} else {
|
29
|
bibTeX.setEntry("article");
|
30
|
}
|
31
|
|
32
|
bibTeX.setKey(generateBibTeXKey(fields, document));
|
33
|
|
34
|
for (DocumentField field: fields) {
|
35
|
if (field instanceof SwitchDocumentField) {
|
36
|
String conditionField = ((SwitchDocumentField) field).getConditionField();
|
37
|
String conditionFieldValue = null;
|
38
|
|
39
|
/* if there is no value in the document for the condition field, consider it 'default' */
|
40
|
if ( document.getMap().get(conditionField) == null || document.getMap().get(conditionField).isEmpty() ) {
|
41
|
conditionFieldValue = "default";
|
42
|
|
43
|
} else { //else get the one from the document
|
44
|
conditionFieldValue = document.getMap().get(conditionField).get(0);
|
45
|
}
|
46
|
|
47
|
/* in case where there is no field for the given conditionFieldValue
|
48
|
* because this was not part of the configuration use "default". If
|
49
|
* there is no field for "default" just continue */
|
50
|
|
51
|
if (((SwitchDocumentField) field).getDocumentField(conditionFieldValue) != null ) {
|
52
|
field = ((SwitchDocumentField) field).getDocumentField(conditionFieldValue);
|
53
|
|
54
|
} else if (((SwitchDocumentField) field).getDocumentField("default") != null) {
|
55
|
field = ((SwitchDocumentField) field).getDocumentField("default");
|
56
|
|
57
|
} else {
|
58
|
continue;
|
59
|
}
|
60
|
}
|
61
|
|
62
|
String fieldDescription = LocaleDescriptionUtil.getFieldDescription(field.getDescriptionMap(), Locale.ENGLISH);
|
63
|
String value = generate(field.getName(), document);
|
64
|
if (!value.isEmpty() && !value.equals("\"\"")) {
|
65
|
bibTeX.getFields().put(fieldDescription, value);
|
66
|
}
|
67
|
}
|
68
|
|
69
|
return bibTeX;
|
70
|
}
|
71
|
|
72
|
public static String generate(String indexField, Document document) {
|
73
|
List<String> values = document.getFieldValues(indexField);
|
74
|
|
75
|
if (values == null) return "";
|
76
|
|
77
|
StringBuffer buffer = new StringBuffer();
|
78
|
buffer.append("\"");
|
79
|
int i = 0;
|
80
|
|
81
|
for (String value:values) {
|
82
|
if ( i > 0 ) { buffer.append(" and "); }
|
83
|
buffer.append(escapeCharacters(value));
|
84
|
i++;
|
85
|
}
|
86
|
|
87
|
buffer.append("\"");
|
88
|
|
89
|
return buffer.toString();
|
90
|
}
|
91
|
|
92
|
public static String escapeCharacters(String value) {
|
93
|
return value.replace("\"", "\\\"").replace("{", "\\{").replace("}", "\\}").replace("$", "\\$");
|
94
|
}
|
95
|
|
96
|
public static String generateBibTeXKey(List<DocumentField> fields, Document document) {
|
97
|
|
98
|
String index4author = null;
|
99
|
//Get the index field for author BibTeX field
|
100
|
for ( DocumentField field:fields ) {
|
101
|
if(field.getDescriptionMap().get(new Locale("en", "GB")) != null) {
|
102
|
if (field.getDescriptionMap().get(new Locale("en", "GB")).equals("author")) {
|
103
|
index4author = field.getName();
|
104
|
break;
|
105
|
}
|
106
|
}
|
107
|
}
|
108
|
|
109
|
StringBuffer buffer = new StringBuffer();
|
110
|
|
111
|
if (index4author != null) {
|
112
|
List<String> authornames = document.getFieldValues(index4author);
|
113
|
|
114
|
if (authornames != null && !authornames.isEmpty()) {
|
115
|
for (String fullname: authornames) {
|
116
|
String parts[] = fullname.split(",");
|
117
|
String part = parts[0].replace(" ", "");
|
118
|
if (authornames.size() > 1){
|
119
|
if(part.length() > 4){
|
120
|
buffer.append(part.substring(0, 4));
|
121
|
} else {
|
122
|
buffer.append(part);
|
123
|
}
|
124
|
} else {
|
125
|
buffer.append(part);
|
126
|
}
|
127
|
}
|
128
|
}
|
129
|
}
|
130
|
|
131
|
//TODO: reconsider following the same procedure as for author to get the index field name
|
132
|
if (document.getMap().get("publicationyear") != null && !document.getMap().get("publicationyear").isEmpty()) {
|
133
|
String publicationYear = document.getMap().get("publicationyear").get(0);
|
134
|
if (publicationYear.length() > 3) {
|
135
|
buffer.append(publicationYear.substring(publicationYear.length()-2, publicationYear.length()));
|
136
|
} else {
|
137
|
buffer.append(publicationYear);
|
138
|
}
|
139
|
}
|
140
|
|
141
|
return buffer.toString();
|
142
|
|
143
|
}
|
144
|
|
145
|
}
|