-
Notifications
You must be signed in to change notification settings - Fork 1
/
NormalizationFunction.java
412 lines (367 loc) · 15 KB
/
NormalizationFunction.java
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package org.folio.processing.mapping.defaultmapper.processor.functions;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import org.apache.commons.lang.StringUtils;
import org.folio.AlternativeTitleType;
import org.folio.ClassificationType;
import org.folio.ContributorNameType;
import org.folio.ContributorType;
import org.folio.ElectronicAccessRelationship;
import org.folio.IdentifierType;
import org.folio.InstanceFormat;
import org.folio.InstanceNoteType;
import org.folio.InstanceType;
import org.folio.IssuanceMode;
import org.folio.processing.mapping.defaultmapper.processor.RuleExecutionContext;
import org.folio.processing.mapping.defaultmapper.processor.functions.enums.ElectronicAccessRelationshipEnum;
import org.folio.processing.mapping.defaultmapper.processor.functions.enums.IssuanceModeEnum;
import org.folio.processing.mapping.defaultmapper.processor.publisher.PublisherRole;
import org.marc4j.marc.DataField;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import static io.netty.util.internal.StringUtil.EMPTY_STRING;
import static org.apache.commons.lang3.math.NumberUtils.INTEGER_ZERO;
/**
* Enumeration to store normalization functions
*/
public enum NormalizationFunction implements Function<RuleExecutionContext, String> {
CHAR_SELECT() {
private static final String FROM_PARAMETER = "from";
private static final String TO_PARAMETER = "to";
@Override
public String apply(RuleExecutionContext context) {
String subFieldValue = context.getSubFieldValue();
JsonObject ruleParameter = context.getRuleParameter();
if (ruleParameter != null && ruleParameter.containsKey(FROM_PARAMETER) && ruleParameter.containsKey(TO_PARAMETER)) {
Integer from = context.getRuleParameter().getInteger(FROM_PARAMETER);
Integer to = context.getRuleParameter().getInteger(TO_PARAMETER);
return subFieldValue.substring(from, to);
} else {
return subFieldValue;
}
}
},
REMOVE_ENDING_PUNC() {
private static final String PUNCT_2_REMOVE = ";:,/+= ";
@Override
public String apply(RuleExecutionContext context) {
String subFieldValue = context.getSubFieldValue();
if (!StringUtils.isEmpty(subFieldValue)) {
int lastPosition = subFieldValue.length() - 1;
if (PUNCT_2_REMOVE.contains(String.valueOf(subFieldValue.charAt(lastPosition)))) {
return subFieldValue.substring(INTEGER_ZERO, lastPosition);
}
}
return subFieldValue;
}
},
TRIM() {
@Override
public String apply(RuleExecutionContext context) {
return context.getSubFieldValue().trim();
}
},
TRIM_PERIOD() {
private static final String PERIOD = ".";
@Override
public String apply(RuleExecutionContext context) {
String subFieldData = context.getSubFieldValue();
if (subFieldData.endsWith(PERIOD)) {
return subFieldData.substring(INTEGER_ZERO, subFieldData.length() - 1);
}
return subFieldData;
}
},
REMOVE_SUBSTRING() {
private static final String SUBSTRING_PARAMETER = "substring";
@Override
public String apply(RuleExecutionContext context) {
String subFieldValue = context.getSubFieldValue();
JsonObject ruleParameter = context.getRuleParameter();
if (ruleParameter != null && ruleParameter.containsKey(SUBSTRING_PARAMETER)) {
String substring = context.getRuleParameter().getString(SUBSTRING_PARAMETER);
return StringUtils.remove(subFieldValue, substring);
} else {
return subFieldValue;
}
}
},
REMOVE_PREFIX_BY_INDICATOR() {
@Override
public String apply(RuleExecutionContext context) {
String subFieldData = context.getSubFieldValue();
DataField dataField = context.getDataField();
int from = INTEGER_ZERO;
int to = Character.getNumericValue(dataField.getIndicator2());
if (0 < to && to < subFieldData.length()) {
String prefixToRemove = subFieldData.substring(from, to);
return StringUtils.remove(subFieldData, prefixToRemove);
} else {
return subFieldData;
}
}
},
CAPITALIZE() {
@Override
public String apply(RuleExecutionContext context) {
return StringUtils.capitalize(context.getSubFieldValue());
}
},
SET_PUBLISHER_ROLE() {
@Override
public String apply(RuleExecutionContext context) {
DataField dataField = context.getDataField();
int indicator = Character.getNumericValue(dataField.getIndicator2());
PublisherRole publisherRole = PublisherRole.getByIndicator(indicator);
if (publisherRole == null) {
return EMPTY_STRING;
} else {
return publisherRole.getCaption();
}
}
},
SET_INSTANCE_FORMAT_ID() {
@Override
public String apply(RuleExecutionContext context) {
List<InstanceFormat> instanceFormats = context.getMappingParameters().getInstanceFormats();
if (instanceFormats == null) {
return StringUtils.EMPTY;
}
return instanceFormats.stream()
.filter(instanceFormat -> instanceFormat.getCode().equalsIgnoreCase(context.getSubFieldValue()))
.findFirst()
.map(InstanceFormat::getId)
.orElse(StringUtils.EMPTY);
}
},
SET_CLASSIFICATION_TYPE_ID() {
private static final String NAME_PARAMETER = "name";
@Override
public String apply(RuleExecutionContext context) {
String typeName = context.getRuleParameter().getString(NAME_PARAMETER);
List<ClassificationType> types = context.getMappingParameters().getClassificationTypes();
if (types == null || typeName == null) {
return STUB_FIELD_TYPE_ID;
}
return types.stream()
.filter(classificationType -> classificationType.getName().equalsIgnoreCase(typeName))
.findFirst()
.map(ClassificationType::getId)
.orElse(STUB_FIELD_TYPE_ID);
}
},
SET_CONTRIBUTOR_TYPE_ID() {
@Override
public String apply(RuleExecutionContext context) {
List<ContributorType> types = context.getMappingParameters().getContributorTypes();
if (types == null) {
return StringUtils.EMPTY;
}
return types.stream()
.filter(type -> type.getCode().equalsIgnoreCase(context.getSubFieldValue()))
.findFirst()
.map(ContributorType::getId)
.orElse(StringUtils.EMPTY);
}
},
SET_CONTRIBUTOR_TYPE_TEXT() {
@Override
public String apply(RuleExecutionContext context) {
List<ContributorType> types = context.getMappingParameters().getContributorTypes();
if (types == null) {
return context.getSubFieldValue();
}
return types.stream()
.filter(type -> type.getCode().equalsIgnoreCase(context.getSubFieldValue()))
.findFirst()
.map(ContributorType::getName)
.orElse(context.getSubFieldValue());
}
},
SET_CONTRIBUTOR_NAME_TYPE_ID() {
private static final String NAME_PARAMETER = "name";
@Override
public String apply(RuleExecutionContext context) {
String typeName = context.getRuleParameter().getString(NAME_PARAMETER);
List<ContributorNameType> typeNames = context.getMappingParameters().getContributorNameTypes();
if (typeNames == null || typeName == null) {
return STUB_FIELD_TYPE_ID;
}
return typeNames.stream()
.filter(contributorTypeName -> contributorTypeName.getName().equalsIgnoreCase(typeName))
.findFirst()
.map(ContributorNameType::getId)
.orElse(STUB_FIELD_TYPE_ID);
}
},
SET_INSTANCE_TYPE_ID() {
private static final String NAME_PARAMETER = "unspecifiedInstanceTypeCode";
@Override
public String apply(RuleExecutionContext context) {
List<InstanceType> types = context.getMappingParameters().getInstanceTypes();
if (types == null) {
return STUB_FIELD_TYPE_ID;
}
String unspecifiedTypeCode = context.getRuleParameter().getString(NAME_PARAMETER);
String instanceTypeCode = context.getDataField() != null ? context.getSubFieldValue() : unspecifiedTypeCode;
return getInstanceTypeByCode(instanceTypeCode, types)
.map(InstanceType::getId)
.orElseGet(() -> getInstanceTypeByCode(unspecifiedTypeCode, types)
.map(InstanceType::getId)
.orElse(STUB_FIELD_TYPE_ID));
}
private Optional<InstanceType> getInstanceTypeByCode(String instanceTypeCode, List<InstanceType> instanceTypes) {
return instanceTypes
.stream()
.filter(instanceType -> instanceType.getCode().equalsIgnoreCase(instanceTypeCode))
.findFirst();
}
},
SET_ELECTRONIC_ACCESS_RELATIONS_ID() {
@Override
public String apply(RuleExecutionContext context) {
List<ElectronicAccessRelationship> electronicAccessRelationships = context.getMappingParameters().getElectronicAccessRelationships();
if (electronicAccessRelationships == null || context.getDataField() == null) {
return STUB_FIELD_TYPE_ID;
}
char ind2 = context.getDataField().getIndicator2();
String name = ElectronicAccessRelationshipEnum.getNameByIndicator(ind2);
return electronicAccessRelationships
.stream()
.filter(electronicAccessRelationship -> electronicAccessRelationship.getName().equalsIgnoreCase(name))
.findFirst()
.map(ElectronicAccessRelationship::getId)
.orElse(STUB_FIELD_TYPE_ID);
}
},
SET_IDENTIFIER_TYPE_ID_BY_NAME() {
private static final String NAME_PARAMETER = "name";
@Override
public String apply(RuleExecutionContext context) {
String typeName = context.getRuleParameter().getString(NAME_PARAMETER);
List<IdentifierType> identifierTypes = context.getMappingParameters().getIdentifierTypes();
if (identifierTypes == null || typeName == null) {
return STUB_FIELD_TYPE_ID;
}
return identifierTypes.stream()
.filter(identifierType -> identifierType.getName().trim().equalsIgnoreCase(typeName))
.findFirst()
.map(IdentifierType::getId)
.orElse(STUB_FIELD_TYPE_ID);
}
},
SET_IDENTIFIER_TYPE_ID_BY_VALUE() {
private static final String NAMES_PARAMETER = "names";
private static final String OCLC_REGEX = "oclc_regex";
@Override
public String apply(RuleExecutionContext context) {
JsonArray typeNames = context.getRuleParameter().getJsonArray(NAMES_PARAMETER);
List<IdentifierType> identifierTypes = context.getMappingParameters().getIdentifierTypes();
if (identifierTypes == null || typeNames == null) {
return STUB_FIELD_TYPE_ID;
}
String type = getIdentifierTypeName(context);
return identifierTypes.stream()
.filter(identifierType -> identifierType.getName().equalsIgnoreCase(type))
.findFirst()
.map(IdentifierType::getId)
.orElse(STUB_FIELD_TYPE_ID);
}
private String getIdentifierTypeName(RuleExecutionContext context) {
JsonArray typeNames = context.getRuleParameter().getJsonArray(NAMES_PARAMETER);
String oclcRegex = context.getRuleParameter().getString(OCLC_REGEX);
String type = typeNames.getString(0);
if (oclcRegex != null && context.getSubFieldValue().matches(oclcRegex)) {
type = typeNames.getString(1);
}
return type;
}
},
SET_NOTE_TYPE_ID() {
private static final String NAME_PARAMETER = "name";
private static final String DEFAULT_NOTE_TYPE_NAME = "General note";
@Override
public String apply(RuleExecutionContext context) {
String noteTypeName = context.getRuleParameter().getString(NAME_PARAMETER);
List<InstanceNoteType> instanceNoteTypes = context.getMappingParameters().getInstanceNoteTypes();
if (instanceNoteTypes == null || noteTypeName == null) {
return STUB_FIELD_TYPE_ID;
}
return getNoteTypeByName(noteTypeName, instanceNoteTypes)
.map(InstanceNoteType::getId)
.orElseGet(() -> getNoteTypeByName(DEFAULT_NOTE_TYPE_NAME, instanceNoteTypes)
.map(InstanceNoteType::getId)
.orElse(STUB_FIELD_TYPE_ID));
}
private Optional<InstanceNoteType> getNoteTypeByName(String noteTypeName, List<InstanceNoteType> noteTypes) {
return noteTypes
.stream()
.filter(instanceNoteType -> instanceNoteType.getName().equalsIgnoreCase(noteTypeName))
.findFirst();
}
},
SET_ALTERNATIVE_TITLE_TYPE_ID() {
private static final String NAME_PARAMETER = "name";
@Override
public String apply(RuleExecutionContext context) {
String alternativeTitleTypeName = context.getRuleParameter().getString(NAME_PARAMETER);
List<AlternativeTitleType> alternativeTitleTypes = context.getMappingParameters().getAlternativeTitleTypes();
if (alternativeTitleTypes == null || alternativeTitleTypeName == null) {
return STUB_FIELD_TYPE_ID;
}
return alternativeTitleTypes.stream()
.filter(alternativeTitleType -> alternativeTitleType.getName().equalsIgnoreCase(alternativeTitleTypeName))
.findFirst()
.map(AlternativeTitleType::getId)
.orElse(STUB_FIELD_TYPE_ID);
}
},
SET_ISSUANCE_MODE_ID() {
@Override
public String apply(RuleExecutionContext context) {
String subFieldValue = context.getSubFieldValue();
char seventhChar = subFieldValue.charAt(7); //Regarding "MODSOURMAN-203" is should be 7-th symbol.
List<IssuanceMode> issuanceModes = context.getMappingParameters().getIssuanceModes();
if (issuanceModes == null || issuanceModes.isEmpty()) {
return StringUtils.EMPTY;
}
String defaultIssuanceModeId = findIssuanceModeId(issuanceModes, IssuanceModeEnum.UNSPECIFIED, StringUtils.EMPTY);
return matchIssuanceModeIdViaLeaderSymbol(seventhChar, issuanceModes, defaultIssuanceModeId);
}
private String findIssuanceModeId(List<IssuanceMode> issuanceModes, IssuanceModeEnum issuanceModeType,
String defaultId) {
return issuanceModes.stream()
.filter(issuanceMode -> issuanceMode.getName().equalsIgnoreCase(issuanceModeType.getValue()))
.findFirst()
.map(IssuanceMode::getId)
.orElse(defaultId);
}
private String matchIssuanceModeIdViaLeaderSymbol(char seventhChar, List<IssuanceMode> issuanceModes, String defaultId) {
IssuanceModeEnum issuanceMode = matchSymbolToIssuanceMode(seventhChar);
return findIssuanceModeId(issuanceModes, issuanceMode, defaultId);
}
},
SET_NOTE_STAFF_ONLY_VIA_INDICATOR() {
@Override
public String apply(RuleExecutionContext context) {
DataField dataField = context.getDataField();
char firstIndicator = dataField.getIndicator1();
if (firstIndicator == '0') {
return "true";
}
return "false";
}
};
public IssuanceModeEnum matchSymbolToIssuanceMode(char symbol) {
for (IssuanceModeEnum issuanceMode : IssuanceModeEnum.values()) {
for (int i = 0; i < issuanceMode.getSymbols().length; i++) {
if (issuanceMode.getSymbols()[i] == symbol) {
return issuanceMode;
}
}
}
return IssuanceModeEnum.UNSPECIFIED;
}
private static final String STUB_FIELD_TYPE_ID = "fe19bae4-da28-472b-be90-d442e2428ead";
}