Skip to content

Commit 57a2645

Browse files
committed
WIP: started experiment to better auto-control the context without bringing it to invalid states
1 parent 3274fa3 commit 57a2645

3 files changed

Lines changed: 56 additions & 0 deletions

File tree

src/main/java/edu/kit/datamanager/ro_crate/RoCrate.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,12 +233,45 @@ public void setUntrackedFiles(Collection<File> files) {
233233
this.untrackedFiles = files;
234234
}
235235

236+
private boolean isKeyUnused(String key, ObjectNode node) {
237+
Iterator<Map.Entry<String, JsonNode>> fields = node.fields();
238+
while (fields.hasNext()) {
239+
Map.Entry<String, JsonNode> entry = fields.next();
240+
String fieldName = entry.getKey();
241+
if (fieldName.startsWith(key)) {
242+
return false;
243+
}
244+
JsonNode value = entry.getValue();
245+
if (value.isObject()) {
246+
if (!isKeyUnused(key, (ObjectNode) value)) {
247+
return false;
248+
}
249+
} else if (value.isArray()) {
250+
for (JsonNode element : value) {
251+
if (element.isObject() && !isKeyUnused(key, (ObjectNode) element)) {
252+
return false;
253+
}
254+
}
255+
}
256+
}
257+
return true;
258+
}
259+
260+
public boolean cleanupContext() {
261+
// TODO we are missing the relations between the pairs and the URL, in order to check if a URL can be removed.
262+
var contextUrls = this.metadataContext.getUrls();
263+
var contextAllKeys = this.metadataContext.getKeys();
264+
var contextExplicitKeys = this.metadataContext.getExplicitKeys();
265+
}
266+
236267
@Override
268+
@Deprecated(forRemoval = true)
237269
public void deleteValuePairFromContext(String key) {
238270
this.metadataContext.deleteValuePairFromContext(key);
239271
}
240272

241273
@Override
274+
@Deprecated(forRemoval = true)
242275
public void deleteUrlFromContext(String key) {
243276
this.metadataContext.deleteUrlFromContext(key);
244277
}

src/main/java/edu/kit/datamanager/ro_crate/context/CrateMetadataContext.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,25 @@ public interface CrateMetadataContext {
3838
*/
3939
Set<String> getKeys();
4040

41+
/**
42+
* Get an immutable collection of the keys in the metadata context that are
43+
* explicitly set, not indirectly using a URL.
44+
* @return the explicitly set keys in the metadata context
45+
*/
46+
Set<String> getExplicitKeys();
47+
4148
/**
4249
* Get an immutable map of the context.
4350
* @return an immutable map containing the context key-value pairs
4451
*/
4552
Map<String, String> getPairs();
4653

54+
/**
55+
* Get an immutable collection of the urls in the metadata context.
56+
* @return the urls in the metadata context
57+
*/
58+
Set<String> getUrls();
59+
4760
void deleteValuePairFromContext(String key);
4861

4962
void deleteUrlFromContext(String url);

src/main/java/edu/kit/datamanager/ro_crate/context/RoCrateMetadataContext.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ public Set<String> getKeys() {
214214
return Set.copyOf(merged);
215215
}
216216

217+
@Override
218+
public Set<String> getExplicitKeys() {
219+
return Set.copyOf(this.other.keySet());
220+
}
221+
217222
@Override
218223
public Map<String, String> getPairs() {
219224
Map<String, String> merged = new HashMap<>();
@@ -222,6 +227,11 @@ public Map<String, String> getPairs() {
222227
return Map.copyOf(merged);
223228
}
224229

230+
@Override
231+
public Set<String> getUrls() {
232+
return Set.copyOf(this.urls);
233+
}
234+
225235

226236
@Override
227237
public void deleteValuePairFromContext(String key) {

0 commit comments

Comments
 (0)