Project

General

Profile

1
package eu.dnetlib.domain.functionality;
2

    
3
import java.util.ArrayList;
4
import java.util.List;
5
import java.util.Locale;
6
import java.util.Map;
7

    
8
import eu.dnetlib.domain.data.Document;
9

    
10
public class DocumentField {
11

    
12
	public enum Cutpoint {
13
		LINE, VALUE, PART;
14
	}
15

    
16
	private String name = null;
17
	private Map<Locale, String> descriptionMap = null;
18
	private Boolean forceDescription = null;
19
	private Map<Locale, String> secondaryLinkMap;
20
	private String vocabulary = null;
21
	private List<DisplayType> displays = new ArrayList<DisplayType>();
22
	private int maxCharacters = Integer.MAX_VALUE;
23
	private Cutpoint cutpoint = Cutpoint.LINE;
24
	private String cssClass = null;
25
	
26
	public DocumentField() {
27

    
28
	}
29

    
30
	public DocumentField(String name, Map<Locale, String> description, String vocabulary,
31
			List<DisplayType> displays, String cssClass) {
32
		this.name = name;
33
		this.setDescriptionMap(description);
34
		this.vocabulary = vocabulary;
35
		this.displays.addAll(displays);
36
		this.cssClass = cssClass;
37
	}
38

    
39
	public DocumentField(String name, Map<Locale, String>  description, String vocabulary,
40
			List<DisplayType> displays, String cssClass, int maxCharacters) {
41
		this.name = name;
42
		this.setDescriptionMap(description);
43
		this.vocabulary = vocabulary;
44
		this.displays.addAll(displays);
45
		this.cssClass = cssClass;
46
		if (maxCharacters < 1) {
47
			throw new RuntimeException(
48
					"The maximum number of characters must be greater than 0.");
49
		} else {
50
			this.maxCharacters = maxCharacters;
51
		}
52
	}
53

    
54
	public DocumentField(String name, Map<Locale, String> description, String vocabulary,
55
			List<DisplayType> displays, String cssClass, Cutpoint cutpoint,
56
			int maxCharacters) {
57
		this.name = name;
58
		this.setDescriptionMap(description);
59
		this.vocabulary = vocabulary;
60
		this.displays.addAll(displays);
61
		this.cssClass = cssClass;
62
		this.cutpoint = cutpoint;
63
		if (maxCharacters < 1) {
64
			throw new RuntimeException(
65
					"The maximum number of characters must be greater than 0.");
66
		} else {
67
			this.maxCharacters = maxCharacters;
68
		}
69
	}
70

    
71
/*	public List<List<String>> getMessage(Document document) {
72

    
73
		List<List<String>> messages = new ArrayList<List<String>>();
74
		int characterCount = 0;
75

    
76
		List<String> fieldValues = document.getFieldValues(name);
77
		if (fieldValues.isEmpty() && this.forceDescription != null
78
				&& forceDescription.equals(true)) {
79

    
80
			String fieldName = getDescription().substring(2, getDescription().length() - 1);
81
			fieldValues = document.getFieldValues(fieldName);
82
			List<DisplayType> simpleDisplay = new ArrayList<DisplayType>();
83
			simpleDisplay.add(new PlainTextDisplayType(fieldName));
84
			generateValues(fieldName, fieldValues, characterCount, document,
85
					simpleDisplay, messages);
86
		} else {
87
			generateValues(name, fieldValues, characterCount, document,
88
					displays, messages);
89
		}
90

    
91
		return messages;
92
	}
93

    
94
	public void generateValues(String fieldName, List<String> fieldValues,
95
			int characterCount, Document document, List<DisplayType> displays,
96
			List<List<String>> messages) {
97

    
98
		for (int i = 0; i < fieldValues.size()
99
				&& (characterCount < maxCharacters); i++) {
100
			
101
			List<String> values = new ArrayList<String>();
102
			String value = document.getFieldValues(fieldName).get(i);
103
			
104
			for (DisplayType type : displays) {
105
				StringBuilder builder = new StringBuilder();
106
	
107
				boolean useSecondaryLink = false;
108
				if (messages.size() > 0 && getSecondaryLink()!= null && !getSecondaryLink().isEmpty()) {
109
					useSecondaryLink = true;
110
				}
111
				
112
				int messageSize = 0;
113
				if (useSecondaryLink == true) {
114
					messageSize = type.getDisplayMessage(builder, document,
115
						value, getSecondaryLink());
116
				} else {
117
					messageSize = type.getDisplayMessage(builder, document,
118
							value);
119
				}
120
				
121
				if (characterCount + messageSize > maxCharacters) {
122
					switch (cutpoint) {
123
					case LINE:
124
						values.add(builder.toString());
125
						break;
126
					case VALUE:
127
						if (characterCount <= maxCharacters) {
128
							values.add(builder.toString());
129
						}
130
						break;
131
					case PART:
132
						String s = builder.toString();
133
						s = s.substring(0, maxCharacters - characterCount)
134
								+ "...";
135
						values.add(s);
136
						break;
137
					}
138
				} else {
139
					values.add(builder.toString());
140
				}
141

    
142
				characterCount += messageSize;
143
			}		
144
			messages.add(values);
145
		}
146
	}
147

    
148
	public String getDescription(Document document) {
149
		return getDescription();
150
	}
151

    
152
	public String getDescription(){
153
		String localeDescription = descriptionMap.get(LocaleHolder.getLocale());
154
		if (localeDescription != null) {
155
			return localeDescription;
156
		}
157
		return descriptionMap.get(new Locale("en", "GB"));
158
	}
159
*/
160
	public void setSecondaryLinkMap(Map<Locale, String> secondaryLinkMap) {
161
		this.secondaryLinkMap = secondaryLinkMap;
162
	}
163

    
164
	public String getCssClass(Document document) {
165
		return getCssClass();
166
	}
167

    
168
	public String getName() {
169
		return name;
170
	}
171

    
172
	public void setName(String name) {
173
		this.name = name;
174
	}
175

    
176
	public String getVocabulary() {
177
		return vocabulary;
178
	}
179

    
180
	public void setVocabulary(String vocabulary) {
181
		this.vocabulary = vocabulary;
182
	}
183

    
184
	public List<DisplayType> getDisplays() {
185
		return displays;
186
	}
187

    
188
	public void setDisplays(List<DisplayType> displays) {
189
		this.displays = displays;
190
	}
191

    
192
	public void setMaxCharacters(int maxCharacters) {
193
		this.maxCharacters = maxCharacters;
194
	}
195

    
196
	public int getMaxCharacters() {
197
		return maxCharacters;
198
	}
199

    
200
	public Cutpoint getCutpoint() {
201
		return cutpoint;
202
	}
203

    
204
	public void setCutpoint(Cutpoint cutpoint) {
205
		this.cutpoint = cutpoint;
206
	}
207

    
208
	public String getCssClass() {
209
		return cssClass;
210
	}
211

    
212
	public void setCssClass(String cssClass) {
213
		this.cssClass = cssClass;
214
	}
215

    
216
	public void setDescriptionMap(Map<Locale, String> descriptionMap) {
217
		this.descriptionMap = descriptionMap;
218
	}
219

    
220
	public Map<Locale, String> getDescriptionMap() {
221
		return descriptionMap;
222
	}
223
	
224
	public Map<Locale, String> getSecondaryLinkMap() {
225
		return secondaryLinkMap;
226
	}
227

    
228
/*	public String getSecondaryLink() {
229
		return secondaryLinkMap.get(LocaleHolder.getLocale());
230
	}
231
*/	
232
	public Boolean getForceDescription() {
233
		return forceDescription;
234
	}
235

    
236
	public void setForceDescription(Boolean forceDescription) {
237
		this.forceDescription = forceDescription;
238
	}
239

    
240
}
(17-17/59)