• 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

90.48
/src/main/java/edu/kit/datamanager/pit/pidsystem/impl/local/LocalPidSystem.java
1
package edu.kit.datamanager.pit.pidsystem.impl.local;
2

3
import java.util.Collection;
4
import java.util.Optional;
5
import java.util.Set;
6
import java.util.stream.Collectors;
7
import edu.kit.datamanager.pit.common.ExternalServiceException;
8
import edu.kit.datamanager.pit.common.InvalidConfigException;
9
import edu.kit.datamanager.pit.common.PidAlreadyExistsException;
10
import edu.kit.datamanager.pit.common.PidNotFoundException;
11
import edu.kit.datamanager.pit.common.RecordValidationException;
12
import edu.kit.datamanager.pit.configuration.ApplicationProperties;
13
import edu.kit.datamanager.pit.domain.PIDRecord;
14
import edu.kit.datamanager.pit.domain.TypeDefinition;
15
import edu.kit.datamanager.pit.pidsystem.IIdentifierSystem;
16

17
import org.slf4j.Logger;
18
import org.slf4j.LoggerFactory;
19
import org.springframework.beans.factory.annotation.Autowired;
20
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
21
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
22
import org.springframework.stereotype.Component;
23
import org.springframework.transaction.annotation.Transactional;
24

25
/**
26
 * A system that stores PIDs on the local machine, in its configured database.
27
 * 
28
 * Purpose: This local system is made for demonstrations, preparations or local
29
 * tests, but may also be used for other cases where PIDs should not be public
30
 * (yet).
31
 * 
32
 * Note: This system has its own PID string format and we can not guarantee that
33
 * you'll be able to register your PIDs later in another system with the same
34
 * format. If you need this feature, feel free to open an issue on GitHub:
35
 * https://github.com/kit-data-manager/pit-service
36
 * 
37
 * Configuration: The database configuration of this service is done via the
38
 * `spring.datasource.*` properties. There is no configuration that controls a
39
 * separate database only for this system. Consider the InMemoryIdentifierSystem
40
 * for this.
41
 */
42
@Component
43
@AutoConfigureAfter(value = ApplicationProperties.class)
44
@ConditionalOnExpression(
45
    "#{ '${pit.pidsystem.implementation}' eq T(edu.kit.datamanager.pit.configuration.ApplicationProperties.IdentifierSystemImpl).LOCAL.name() }"
46
)
47
@Transactional
48
public class LocalPidSystem implements IIdentifierSystem {
49

50
    private static final Logger LOG = LoggerFactory.getLogger(LocalPidSystem.class);
1✔
51
    
52
    @Autowired
53
    private PidDatabaseObjectDao db;
54

55
    private static final String PREFIX = "sandboxed/";
56

57
    public LocalPidSystem() {
1✔
58
        LOG.warn("Using local identifier system to store PIDs. REGISTERED PIDs ARE NOT PERMANENTLY OR PUBLICLY STORED.");
1✔
59
    }
1✔
60

61
    /**
62
     * For testing only. Allows to inject the database access object afterwards.
63
     * 
64
     * @param db the new DAO.
65
     */
66
    protected void setDatabase(PidDatabaseObjectDao db) {
67
        this.db = db;
×
68
    }
×
69

70
    /**
71
     * For testing purposes.
72
     */
73
    protected PidDatabaseObjectDao getDatabase() {
74
        return this.db;
1✔
75
    }
76

77
    @Override
78
    public Optional<String> getPrefix() {
79
        return Optional.of(PREFIX);
1✔
80
    }
81

82
    @Override
83
    public boolean isIdentifierRegistered(String pid) throws ExternalServiceException {
84
        return this.db.existsById(pid);
1✔
85
    }
86

87
    @Override
88
    public PIDRecord queryAllProperties(String pid) throws PidNotFoundException, ExternalServiceException {
89
        Optional<PidDatabaseObject> dbo = this.db.findByPid(pid);
1✔
90
        if (dbo.isEmpty()) { return null; }
1✔
91
        return new PIDRecord(dbo.get());
1✔
92
    }
93

94
    @Override
95
    public String queryProperty(String pid, TypeDefinition typeDefinition) throws PidNotFoundException, ExternalServiceException {
96
        Optional<PidDatabaseObject> dbo = this.db.findByPid(pid);
1✔
97
        if (dbo.isEmpty()) { throw new PidNotFoundException(pid); }
1✔
98
        PIDRecord rec = new PIDRecord(dbo.get());
1✔
99
        if (!rec.hasProperty(typeDefinition.getIdentifier())) { return null; }
1✔
100
        return rec.getPropertyValue(typeDefinition.getIdentifier());
1✔
101
    }
102
    
103
    @Override
104
    public String registerPidUnchecked(final PIDRecord pidRecord) throws PidAlreadyExistsException, ExternalServiceException {
105
        if (this.db.existsById(pidRecord.getPid())) {
1✔
NEW
106
            throw new PidAlreadyExistsException(pidRecord.getPid());
×
107
        }
108
        this.db.save(new PidDatabaseObject(pidRecord));
1✔
109
        LOG.debug("Registered record with PID: {}", pidRecord.getPid());
1✔
110
        return pidRecord.getPid();
1✔
111
    }
112

113
    @Override
114
    public boolean updatePID(PIDRecord rec) throws PidNotFoundException, ExternalServiceException, RecordValidationException {
115
        if (this.db.existsById(rec.getPid())) {
1✔
116
            this.db.save(new PidDatabaseObject(rec));
1✔
117
            return true;
1✔
118
        }
119
        return false;
×
120
    }
121

122
    @Override
123
    public PIDRecord queryByType(String pid, TypeDefinition typeDefinition) throws PidNotFoundException, ExternalServiceException {
124
        PIDRecord allProps = this.queryAllProperties(pid);
1✔
125
        if (allProps == null) {return null;}
1✔
126
        // only return properties listed in the type def
127
        Set<String> typeProps = typeDefinition.getAllProperties();
1✔
128
        PIDRecord result = new PIDRecord();
1✔
129
        for (String propID : allProps.getPropertyIdentifiers()) {
1✔
130
            if (typeProps.contains(propID)) {
1✔
131
                String[] values = allProps.getPropertyValues(propID);
1✔
132
                for (String value : values) {
1✔
133
                    result.addEntry(propID, "", value);
1✔
134
                }
135
            }
136
        }
1✔
137
        return result;
1✔
138
    }
139

140
    @Override
141
    public boolean deletePID(String pid) {
142
        throw new UnsupportedOperationException("Deleting PIDs is against the P in PID.");
1✔
143
    }
144

145
    @Override
146
    public Collection<String> resolveAllPidsOfPrefix() throws ExternalServiceException, InvalidConfigException {
147
        return this.db.findAll().parallelStream()
1✔
148
                .map(dbo -> dbo.getPid())
1✔
149
                .filter(pid -> pid.startsWith(PREFIX))
1✔
150
                .collect(Collectors.toSet());
1✔
151
    }
152
}
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