Skip to content

Commit

Permalink
Merge pull request #9 from markusweigelt/audio-waveform
Browse files Browse the repository at this point in the history
Audio waveform
  • Loading branch information
markusweigelt authored Sep 19, 2023
2 parents e429e8c + 2fb25e6 commit a046c8d
Show file tree
Hide file tree
Showing 114 changed files with 2,101 additions and 579 deletions.
2 changes: 1 addition & 1 deletion Kitodo-API/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<parent>
<artifactId>kitodo-production</artifactId>
<groupId>org.kitodo</groupId>
<version>3.6.0-SNAPSHOT</version>
<version>3.6.0</version>
</parent>

<name>Kitodo - API</name>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.util.Map;
import java.util.Optional;

import org.kitodo.api.Metadata;

/**
* Interface for a service that provides access to the ruleset.
*
Expand Down Expand Up @@ -143,4 +145,20 @@ StructuralElementViewInterface getStructuralElementView(String structuralElement
* @return the “always showing” value or its default value
*/
boolean isAlwaysShowingForKey(String keyId);

/**
* Updates metadata during a repeated catalog import, depending on the
* reimport settings specified in the ruleset.
*
* @param division
* current division
* @param metadata
* current metadata
* @param acquisitionStage
* current acquisition stage
* @param updateItems
* items obtained from import
* @return number of added metadata items
*/
int updateMetadata(String division, Collection<Metadata> metadata, String acquisitionStage, Collection<Metadata> updateItems);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package org.kitodo.api.dataformat;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.GregorianCalendar;
import java.util.List;
Expand Down Expand Up @@ -187,9 +188,20 @@ public List<LogicalDivision> getAllLogicalDivisions() {
* @return all physical divisions with type "page", sorted by their {@code order}
*/
public List<PhysicalDivision> getAllPhysicalDivisionChildrenFilteredByTypePageAndSorted() {
return getAllPhysicalDivisionChildrenFilteredByTypes(Collections.singletonList(PhysicalDivision.TYPE_PAGE));
}

/**
* Returns all child physical divisions of the physical division of the workpiece with any of the types in the
* provided list "types".
*
* @param types list of types
* @return child physical division of given types
*/
public List<PhysicalDivision> getAllPhysicalDivisionChildrenFilteredByTypes(List<String> types) {
return physicalStructure.getChildren().stream()
.flatMap(Workpiece::treeStream)
.filter(division -> Objects.equals(division.getType(), PhysicalDivision.TYPE_PAGE))
.filter(physicalDivisionToCheck -> types.contains(physicalDivisionToCheck.getType()))
.sorted(Comparator.comparing(PhysicalDivision::getOrder)).collect(Collectors.toUnmodifiableList());
}

Expand Down
2 changes: 1 addition & 1 deletion Kitodo-Command/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<parent>
<artifactId>kitodo-production</artifactId>
<groupId>org.kitodo</groupId>
<version>3.6.0-SNAPSHOT</version>
<version>3.6.0</version>
</parent>

<name>Kitodo - Command</name>
Expand Down
2 changes: 1 addition & 1 deletion Kitodo-DataEditor/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<parent>
<artifactId>kitodo-production</artifactId>
<groupId>org.kitodo</groupId>
<version>3.6.0-SNAPSHOT</version>
<version>3.6.0</version>
</parent>

<name>Kitodo - Data Editor</name>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/*
* (c) Kitodo. Key to digital objects e. V. <[email protected]>
*
* This file is part of the Kitodo project.
*
* It is licensed under GNU General Public License version 3 or later.
*
* For the full copyright and license information, please read the
* GPL3-License.txt file that was distributed with this source code.
*/

package org.kitodo.dataeditor.ruleset;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Objects;
import java.util.function.Supplier;

import org.apache.commons.lang3.ArrayUtils;
import org.kitodo.api.Metadata;
import org.kitodo.dataeditor.ruleset.xml.Reimport;

/**
* Determines the result of re-importing metadata for a single metadata key.
*/
class ReimportMetadata implements Supplier<Collection<Metadata>> {

// Metadata key for which the result of the reimport is determined.
private final String key;

// existing metadata for this key before reimport
private final Collection<Metadata> currentEntries;

// configured reimport behavior
private Reimport reimport;

// metadata fetched on reimport
private final Collection<Metadata> updateEntries;

// the maximum amount of metadata applicable here for this key
private int maxOccurs;

/**
* <b>Constructor.</b><!-- --> Generates a data set for the re-import of
* metadata for a specific key.
*
* @param key
* metadata key for which the result of the reimport is
* determined
*/
ReimportMetadata(String key) {
this.currentEntries = new ArrayList<>();
this.updateEntries = new ArrayList<>();
this.key = key;
this.maxOccurs = Integer.MAX_VALUE;
}

/**
* Sets the repeated import behavior model for this metadata key. Must be a
* value from {@link Reimport}.
*
* @param reimport
* configured reimport behavior to set
*/
void setReimport(Reimport reimport) {
this.reimport = reimport;
}

/**
* Sets the maximum number of metadata values allowed for this key. The
* setting only affects newly added metadata.
*
* @param maxOccurs
* maximum amount of metadata to set
*/
void setMaxOccurs(int maxOccurs) {
this.maxOccurs = maxOccurs;
}

/**
* Returns the metadata key. The key is that of the contained metadata.
*
* @return the metadata key
*/
String getKey() {
return key;
}

/**
* Adds a metadata entry to the current metadata entries.
*
* @param metadata
* metadata to add
*/
void addToCurrentEntries(Metadata metadata) {
assert Objects.equals(metadata.getKey(), key) : "keys should match";
currentEntries.add(metadata);
}

/**
* Adds a metadata entry to the update metadata entries.
*
* @param metadata
* metadata to add
*/
void addToUpdateEntries(Metadata metadata) {
assert Objects.equals(metadata.getKey(), key) : "keys should match";
updateEntries.add(metadata);
}

/**
* Merges the metadata of this key in the given behavior, respecting the
* maximum number.
*
* @return the metadata remaining after the repeated import
*/
@Override
public Collection<Metadata> get() {
if (!ArrayUtils.contains(Reimport.values(), reimport)) {
throw new IllegalStateException("Used not supported reimport case ".concat(Objects.toString(reimport)));
}
Collection<Metadata> result = reimport.equals(Reimport.REPLACE) && !updateEntries.isEmpty() ? new ArrayList<>()
: new ArrayList<>(currentEntries);
if (!reimport.equals(Reimport.KEEP) || result.isEmpty()) {
for (Metadata metadata : updateEntries) {
if (!result.contains(metadata) && result.size() < maxOccurs) {
result.add(metadata);
}
}
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,15 @@ private static Collection<RestrictivePermit> getConditionalPermits(RestrictivePe
return result;
}

private static void getConditionalPermits(ConditionsMapInterface conditionsMapInterface,
private static void getConditionalPermits(ConditionsMapInterface conditionsMap,
Map<String, Optional<MetadataEntry>> metadataCache, List<Map<MetadataEntry, Boolean>> metadata,
Collection<RestrictivePermit> result) {

for (String conditionKey : conditionsMapInterface.getConditionKeys()) {
for (String conditionKey : conditionsMap.getConditionKeys()) {
Optional<MetadataEntry> possibleMetadata = metadataCache.computeIfAbsent(conditionKey,
key -> getMetadataEntryForKey(key, metadata));
if (possibleMetadata.isPresent()) {
Condition condition = conditionsMapInterface.getCondition(conditionKey, possibleMetadata.get().getValue());
Condition condition = conditionsMap.getCondition(conditionKey, possibleMetadata.get().getValue());
if (Objects.nonNull(condition)) {
result.addAll(condition.getPermits());
getConditionalPermits(condition, metadataCache, metadata, result); // recursive
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Locale.LanguageRange;
import java.util.Map;
Expand All @@ -27,8 +28,10 @@

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.kitodo.api.Metadata;
import org.kitodo.api.dataeditor.rulesetmanagement.FunctionalDivision;
import org.kitodo.api.dataeditor.rulesetmanagement.FunctionalMetadata;
import org.kitodo.api.dataeditor.rulesetmanagement.MetadataViewInterface;
import org.kitodo.api.dataeditor.rulesetmanagement.RulesetManagementInterface;
import org.kitodo.api.dataeditor.rulesetmanagement.StructuralElementViewInterface;
import org.kitodo.dataeditor.ruleset.xml.AcquisitionStage;
Expand All @@ -48,6 +51,14 @@ public class RulesetManagement implements RulesetManagementInterface {
*/
private static final Logger logger = LogManager.getLogger(RulesetManagement.class);

/**
* English, the only language understood by the System user. This value is
* passed when a method requests a language of the user in order to display
* labels in this language, but the labels are not required from the result
* and the language passed is therefore irrelevant at this point.
*/
private static final List<LanguageRange> ENGLISH = LanguageRange.parse("en");

/**
* The ruleset.
*/
Expand Down Expand Up @@ -321,5 +332,43 @@ public boolean isAlwaysShowingForKey(String keyId) {
return false;
}

@Override
public int updateMetadata(String division, Collection<Metadata> currentMetadata, String acquisitionStage,
Collection<Metadata> updateMetadata) {

Settings settings = ruleset.getSettings(acquisitionStage);
Collection<MetadataViewInterface> allowedMetadata = getStructuralElementView(division, acquisitionStage,
ENGLISH).getAllowedMetadata();
Collection<ReimportMetadata> metadataForReimport = createListOfMetadataToMerge(currentMetadata, settings,
allowedMetadata, updateMetadata);

int sizeBefore = currentMetadata.size();
currentMetadata.clear();
for (ReimportMetadata metadataInReimport : metadataForReimport) {
currentMetadata.addAll(metadataInReimport.get());
}
return currentMetadata.size() - sizeBefore;
}

private Collection<ReimportMetadata> createListOfMetadataToMerge(Collection<Metadata> currentMetadata,
Settings settings, Collection<MetadataViewInterface> allowedMetadata, Collection<Metadata> updateMetadata) {
HashMap<String, ReimportMetadata> unifying = new HashMap<>();
for (Metadata metadata : currentMetadata) {
unifying.computeIfAbsent(metadata.getKey(), ReimportMetadata::new).addToCurrentEntries(metadata);
}
for (Metadata metadata : updateMetadata) {
unifying.computeIfAbsent(metadata.getKey(), ReimportMetadata::new).addToUpdateEntries(metadata);
}
Collection<ReimportMetadata> result = unifying.values();
for (ReimportMetadata entry : result) {
entry.setReimport(settings.getReimport(entry.getKey()));
}
for (MetadataViewInterface metadataView : allowedMetadata) {
ReimportMetadata metadataToMerge = unifying.get(metadataView.getId());
if (Objects.nonNull(metadataToMerge)) {
metadataToMerge.setMaxOccurs(metadataView.getMaxOccurs());
}
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.kitodo.dataeditor.ruleset.xml.Reimport;
import org.kitodo.dataeditor.ruleset.xml.Setting;

/**
Expand All @@ -44,6 +46,24 @@ public Settings(Collection<Setting> baseSettings) {
.collect(Collectors.toMap(Setting::getKey, Function.identity()));
}

/**
* Returns the reimport setting for a key.
*
* @param keyId
* key for which the query is
* @return reimport setting
*/
Reimport getReimport(String keyId) {
Setting settingForKey = currentSettings.get(keyId);
if (Objects.nonNull(settingForKey)) {
Reimport reimport = settingForKey.getReimport();
if (Objects.nonNull(reimport)) {
return reimport;
}
}
return Reimport.REPLACE;
}

/**
* Returns the settings for a key.
*
Expand Down Expand Up @@ -157,6 +177,7 @@ private List<Setting> merge(Collection<Setting> currentSettings, Collection<Sett
merged.setEditable(other.getEditable() != null ? other.getEditable() : current.getEditable());
merged.setExcluded(other.getExcluded() != null ? other.getExcluded() : current.getExcluded());
merged.setMultiline(other.getMultiline() != null ? other.getMultiline() : current.getMultiline());
merged.setReimport(other.getReimport() != null ? other.getReimport() : current.getReimport());
merged.setSettings(merge(current.getSettings(), other.getSettings()));
mergedSettings.add(merged);
} else {
Expand Down
Loading

0 comments on commit a046c8d

Please sign in to comment.