Project

General

Profile

1
package eu.dnetlib.data.mapreduce.hbase.dataimport;
2

    
3
import com.google.gson.JsonArray;
4
import com.google.gson.JsonObject;
5
import eu.dnetlib.data.proto.FieldTypeProtos;
6
import org.apache.commons.lang3.StringUtils;
7

    
8
import java.util.ArrayList;
9
import java.util.List;
10
import java.util.Map;
11

    
12
public class DumpToActionsUtility {
13

    
14
    public static String getStringValue(final JsonObject root, final String key) {
15
        if (root.has(key) && !root.get(key).isJsonNull())
16
            return root.get(key).getAsString();
17
        return null;
18
    }
19

    
20
    public static List<String> getArrayValues(final JsonObject root, final String key) {
21
        if (root.has(key) && root.get(key).isJsonArray()) {
22
            final JsonArray asJsonArray = root.get(key).getAsJsonArray();
23
            final List<String> result = new ArrayList<>();
24

    
25

    
26
            asJsonArray.forEach(it -> {
27
                if (StringUtils.isNotBlank(it.getAsString())) {
28
                    result.add(it.getAsString());
29
                }
30
            });
31
            return result;
32
        }
33
        return new ArrayList<>();
34
    }
35
    public static List<JsonObject> getArrayObjects(final JsonObject root, final String key) {
36
        if (root.has(key) && root.get(key).isJsonArray()) {
37
            final JsonArray asJsonArray = root.get(key).getAsJsonArray();
38
            final List<JsonObject> result = new ArrayList<>();
39
            asJsonArray.forEach(it -> {
40
                if (it.getAsJsonObject() != null)  {
41
                    result.add(it.getAsJsonObject());
42
                }
43
            });
44
            return result;
45
        }
46
        return new ArrayList<>();
47
    }
48

    
49
    public static boolean isValidDate(final String date) {
50
        return date.matches("\\d{4}-\\d{1,2}-\\d{1,2}");
51
    }
52

    
53
    public static FieldTypeProtos.StructuredProperty getPid(final JsonObject localIdentifier, final Map<String, ScholExplorerConfiguration> conf) {
54
        final String pidType = getStringValue(localIdentifier, "type");
55
        final ScholExplorerConfiguration configuration = conf.get(pidType);
56
        if (configuration.getCleandPidType() == null) {
57
            return null;
58
        }
59
        final String pid = getStringValue(localIdentifier, "id");
60
        return FieldTypeProtos.StructuredProperty.newBuilder()
61
                .setValue(pid)
62
                .setQualifier(getQualifier(configuration.getCleandPidType(), "dnet:pid_types"))
63
                .build();
64
    }
65
    public static FieldTypeProtos.Qualifier getQualifier(final String classValue, final String schemeValue) {
66

    
67
        return FieldTypeProtos.Qualifier.newBuilder()
68
                .setSchemeid(schemeValue)
69
                .setSchemename(schemeValue)
70
                .setClassname(classValue)
71
                .setClassid(classValue)
72
                .build();
73
    }
74

    
75
    public static String getDefaultResulttype(final String cobjcategory) {
76
        switch (cobjcategory) {
77
            case "0029":
78
                return "software";
79
            case "0021":
80
            case "0024":
81
            case "0025":
82
            case "0030":
83
                return "dataset";
84
            case "0000":
85
            case "0010":
86
            case "0018":
87
            case "0020":
88
            case "0022":
89
            case "0023":
90
            case "0026":
91
            case "0027":
92
            case "0028":
93
            case "0037":
94
                return "other";
95
            case "0001":
96
            case "0002":
97
            case "0004":
98
            case "0005":
99
            case "0006":
100
            case "0007":
101
            case "0008":
102
            case "0009":
103
            case "0011":
104
            case "0012":
105
            case "0013":
106
            case "0014":
107
            case "0015":
108
            case "0016":
109
            case "0017":
110
            case "0019":
111
            case "0031":
112
            case "0032":
113
                return "publication";
114
            default:
115
                return "publication";
116
        }
117
    }
118

    
119
}
(6-6/18)