• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

kit-data-manager / pit-service / #324

13 Nov 2023 05:22PM UTC coverage: 72.606% (+11.8%) from 60.79%
#324

push

github

web-flow
Merge pull request #174 from kit-data-manager/dev-v2

Development branch for v2.0.0

136 of 205 new or added lines in 14 files covered. (66.34%)

4 existing lines in 3 files now uncovered.

872 of 1201 relevant lines covered (72.61%)

0.73 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

31.17
/src/main/java/edu/kit/datamanager/pit/pitservice/impl/TypingService.java
1
package edu.kit.datamanager.pit.pitservice.impl;
2

3
import com.google.common.cache.LoadingCache;
4
import edu.kit.datamanager.pit.common.InvalidConfigException;
5
import edu.kit.datamanager.pit.common.PidAlreadyExistsException;
6
import edu.kit.datamanager.pit.common.PidNotFoundException;
7
import edu.kit.datamanager.pit.common.RecordValidationException;
8
import edu.kit.datamanager.pit.common.TypeNotFoundException;
9

10
import java.io.IOException;
11
import java.util.Collection;
12
import java.util.HashSet;
13
import java.util.List;
14
import java.util.Optional;
15

16
import edu.kit.datamanager.pit.pidsystem.IIdentifierSystem;
17
import edu.kit.datamanager.pit.typeregistry.ITypeRegistry;
18
import edu.kit.datamanager.pit.pitservice.ITypingService;
19
import edu.kit.datamanager.pit.pitservice.IValidationStrategy;
20
import edu.kit.datamanager.pit.common.ExternalServiceException;
21
import edu.kit.datamanager.pit.domain.Operations;
22
import edu.kit.datamanager.pit.domain.PIDRecord;
23
import edu.kit.datamanager.pit.domain.TypeDefinition;
24
import java.util.concurrent.ExecutionException;
25

26
import org.slf4j.Logger;
27
import org.slf4j.LoggerFactory;
28
import org.springframework.beans.factory.annotation.Autowired;
29

30
/**
31
 * Core implementation class that offers the combined higher-level services
32
 * through a type registry and an identifier system.
33
 *
34
 */
35
public class TypingService implements ITypingService {
36

37
    private static final Logger LOG = LoggerFactory.getLogger(TypingService.class);
1✔
38
    private static final String LOG_MSG_TYPING_SERVICE_MISCONFIGURED = "Typing service misconfigured.";
39
    private static final String LOG_MSG_QUERY_TYPE = "Querying for type with identifier {}.";
40

41

42
    protected final LoadingCache<String, TypeDefinition> typeCache;
43
    protected final IIdentifierSystem identifierSystem;
44
    protected final ITypeRegistry typeRegistry;
45

46
    /**
47
     * A validation strategy. Will never be null.
48
     * 
49
     * ApplicationProperties::defaultValidationStrategy there is always either a
50
     * default strategy or a noop strategy assigned. Therefore, autowiring will
51
     * always work. Assigning null is done to avoid warnings on constructor.
52
     */
53
    @Autowired
1✔
54
    protected IValidationStrategy defaultStrategy = null;
55

56
    public TypingService(IIdentifierSystem identifierSystem, ITypeRegistry typeRegistry,
57
            LoadingCache<String, TypeDefinition> typeCache) {
58
        super();
1✔
59
        this.identifierSystem = identifierSystem;
1✔
60
        this.typeRegistry = typeRegistry;
1✔
61
        this.typeCache = typeCache;
1✔
62
    }
1✔
63

64
    @Override
65
    public Optional<String> getPrefix() {
66
        return this.identifierSystem.getPrefix();
1✔
67
    }
68

69
    @Override
70
    public void setValidationStrategy(IValidationStrategy strategy) {
71
        this.defaultStrategy = strategy;
1✔
72
    }
1✔
73

74
    @Override
75
    public void validate(PIDRecord pidRecord)
76
            throws RecordValidationException, ExternalServiceException {
77
        this.defaultStrategy.validate(pidRecord);
1✔
78
    }
1✔
79

80
    @Override
81
    public boolean isIdentifierRegistered(String pid) throws ExternalServiceException {
82
        LOG.trace("Performing isIdentifierRegistered({}).", pid);
1✔
83
        return identifierSystem.isIdentifierRegistered(pid);
1✔
84
    }
85

86
    @Override
87
    public String queryProperty(String pid, TypeDefinition typeDefinition) throws PidNotFoundException, ExternalServiceException {
88
        LOG.trace("Performing queryProperty({}, TypeDefinition#{}).", pid, typeDefinition.getIdentifier());
×
89
        return identifierSystem.queryProperty(pid, typeDefinition);
×
90
    }
91

92
    @Override
93
    public String registerPidUnchecked(final PIDRecord pidRecord) throws PidAlreadyExistsException, ExternalServiceException {
94
        LOG.trace("Performing registerPID({}).", pidRecord);
1✔
95
        return identifierSystem.registerPidUnchecked(pidRecord);
1✔
96
    }
97

98
    @Override
99
    public PIDRecord queryByType(String pid, TypeDefinition typeDefinition) throws PidNotFoundException, ExternalServiceException {
100
        LOG.trace("Performing queryByType({}, TypeDefinition#{}).", pid, typeDefinition.getIdentifier());
×
101
        return identifierSystem.queryByType(pid, typeDefinition);
×
102
    }
103

104
    @Override
105
    public boolean deletePID(String pid) throws ExternalServiceException {
106
        LOG.trace("Performing deletePID({}).", pid);
×
107
        return identifierSystem.deletePID(pid);
×
108
    }
109

110
    @Override
111
    public TypeDefinition describeType(String typeIdentifier) throws IOException {
UNCOV
112
        LOG.trace("Performing describeType({}).", typeIdentifier);
×
113
        try {
NEW
114
            LOG.trace(LOG_MSG_QUERY_TYPE, typeIdentifier);
×
NEW
115
            return typeCache.get(typeIdentifier);
×
116
        } catch (ExecutionException ex) {
×
117
            LOG.error("Failed to query for type with identifier " + typeIdentifier + ".", ex);
×
NEW
118
            throw new InvalidConfigException(LOG_MSG_TYPING_SERVICE_MISCONFIGURED);
×
119
        }
120
    }
121

122
    @Override
123
    public PIDRecord queryAllProperties(String pid) throws PidNotFoundException, ExternalServiceException {
124
        LOG.trace("Performing queryAllProperties({}).", pid);
1✔
125
        PIDRecord pidRecord = identifierSystem.queryAllProperties(pid);
1✔
126
        if (pidRecord == null) {
1✔
127
            throw new PidNotFoundException(pid);
1✔
128
        }
129
        // ensure the PID is always contained
130
        pidRecord.setPid(pid);
1✔
131
        return pidRecord;
1✔
132
    }
133

134
    @Override
135
    public PIDRecord queryAllProperties(String pid, boolean includePropertyNames)
136
            throws IOException {
137
        LOG.trace("Performing queryAllProperties({}, {}).", pid, includePropertyNames);
×
138
        PIDRecord pidInfo = identifierSystem.queryAllProperties(pid);
×
139
        LOG.trace("PID record found. {}", (includePropertyNames) ? "Adding property names." : "Returning result.");
×
140

141
        if (includePropertyNames) {
×
142
            enrichPIDInformationRecord(pidInfo);
×
143
        }
144
        return pidInfo;
×
145
    }
146

147
    @Override
148
    public PIDRecord queryProperty(String pid, String propertyIdentifier) throws IOException {
149
        LOG.trace("Performing queryProperty({}, {}).", pid, propertyIdentifier);
×
150
        PIDRecord pidInfo = new PIDRecord();
×
151
        // query type registry
152
        TypeDefinition typeDef;
153
        try {
NEW
154
            LOG.trace(LOG_MSG_QUERY_TYPE, propertyIdentifier);
×
NEW
155
            typeDef = typeCache.get(propertyIdentifier);
×
156
        } catch (ExecutionException ex) {
×
NEW
157
            LOG.error(LOG_MSG_QUERY_TYPE, propertyIdentifier);
×
158

NEW
159
            throw new InvalidConfigException(LOG_MSG_TYPING_SERVICE_MISCONFIGURED);
×
160
        }
×
161

162
        if (typeDef != null) {
×
163
            pidInfo.addEntry(propertyIdentifier, typeDef.getName(), identifierSystem.queryProperty(pid, typeDef));
×
164
            return pidInfo;
×
165
        }
166
        return null;
×
167
    }
168

169
    private void enrichPIDInformationRecord(PIDRecord pidInfo) {
170
        // enrich record by querying type registry for all property definitions
171
        // to get the property names
172
        for (String typeIdentifier : pidInfo.getPropertyIdentifiers()) {
×
173
            TypeDefinition typeDef;
174
            try {
NEW
175
                typeDef = typeCache.get(typeIdentifier);
×
176
            } catch (ExecutionException ex) {
×
NEW
177
                throw new InvalidConfigException(LOG_MSG_TYPING_SERVICE_MISCONFIGURED);
×
178
            }
×
179

180
            if (typeDef != null) {
×
181
                pidInfo.setPropertyName(typeIdentifier, typeDef.getName());
×
182
            } else {
183
                pidInfo.setPropertyName(typeIdentifier, typeIdentifier);
×
184
            }
185
        }
×
186
    }
×
187

188
    @Override
189
    public PIDRecord queryByType(String pid, String typeIdentifier, boolean includePropertyNames)
190
            throws IOException {
191
        TypeDefinition typeDef;
192
        try {
NEW
193
            typeDef = typeCache.get(typeIdentifier);
×
194
        } catch (ExecutionException ex) {
×
NEW
195
            throw new InvalidConfigException(LOG_MSG_TYPING_SERVICE_MISCONFIGURED);
×
196
        }
×
197

198
        if (typeDef == null) {
×
199
            return null;
×
200
        }
201
        // now query PID record and fill in information based on property keys in type definition
202
        PIDRecord result = identifierSystem.queryByType(pid, typeDef);
×
203
        if (includePropertyNames) {
×
204
            enrichPIDInformationRecord(result);
×
205
        }
206
        return result;
×
207
    }
208

209
    public ITypeRegistry getTypeRegistry() {
UNCOV
210
        return typeRegistry;
×
211
    }
212

213
    public IIdentifierSystem getIdentifierSystem() {
214
        return identifierSystem;
×
215
    }
216

217
    @Override
218
    public boolean updatePID(PIDRecord pidRecord) throws PidNotFoundException, ExternalServiceException, RecordValidationException {
219
        return this.identifierSystem.updatePID(pidRecord);
1✔
220
    }
221

222
    @Override
223
    public Collection<String> resolveAllPidsOfPrefix() throws ExternalServiceException, InvalidConfigException {
224
        return this.identifierSystem.resolveAllPidsOfPrefix();
×
225
    }
226

227
    public Operations getOperations()  {
228
        return new Operations(this);
1✔
229
    }
230

231
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc