forked from pkiraly/metadata-qa-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchemaFactory.java
More file actions
72 lines (55 loc) · 2.14 KB
/
SchemaFactory.java
File metadata and controls
72 lines (55 loc) · 2.14 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
package de.gwdg.metadataqa.api.util;
import de.gwdg.metadataqa.api.configuration.SchemaConfiguration;
import de.gwdg.metadataqa.api.configuration.schema.Field;
import de.gwdg.metadataqa.api.json.JsonBranch;
import de.gwdg.metadataqa.api.schema.BaseSchema;
import de.gwdg.metadataqa.api.schema.Format;
import de.gwdg.metadataqa.api.schema.Schema;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
public class SchemaFactory {
private static final Logger LOGGER = Logger.getLogger(SchemaFactory.class.getCanonicalName());
private SchemaFactory() {}
public static Schema fromConfig(SchemaConfiguration config) {
BaseSchema schema = new BaseSchema()
.setFormat(Format.valueOf(config.getFormat().toUpperCase()));
boolean hasCategories = config.hasCategories();
if (hasCategories)
schema.setCategories(config.getCategories());
for (Field field : config.getFields()) {
var branch = new JsonBranch(field.getName());
if (StringUtils.isNotBlank(field.getPath()))
branch.setJsonPath(field.getPath());
if (field.getCategories() != null) {
List<String> categories = new ArrayList<>();
for (String category : field.getCategories()) {
if (hasCategories) {
if (config.getCategories().contains(category))
categories.add(category);
else
LOGGER.warning(String.format("Invalid category for field '%s': '%s'",
field.getName(), category));
} else {
categories.add(category);
}
}
branch.setCategories(categories);
}
if (field.isExtractable())
branch.setExtractable();
if (field.isEcho())
branch.setEcho();
if (field.getRules() != null)
branch.setRule(field.getRules());
if (StringUtils.isNotBlank(field.getIndexField()))
branch.setIndexField(field.getIndexField());
schema.addField(branch);
}
if (config.getNamespaces() != null)
schema.setNamespaces(config.getNamespaces());
schema.checkConsistency();
return schema;
}
}