From 5b8ed809973928108e821989f1b7856e8bfde16d Mon Sep 17 00:00:00 2001 From: Markus Weigelt Date: Tue, 2 Jan 2024 13:33:52 +0100 Subject: [PATCH] Improvements regarding the review --- .../access/FileXmlElementAccess.java | 4 +- .../forms/dataeditor/MediaPartialForm.java | 3 +- .../forms/dataeditor/MediaPartialsPanel.java | 3 +- .../helper/metadata/MediaPartialHelper.java | 58 ++++++------------- ...MediaPartialLogicalDivisionComparator.java | 38 ++++++++++++ 5 files changed, 60 insertions(+), 46 deletions(-) create mode 100644 Kitodo/src/main/java/org/kitodo/production/helper/metadata/MediaPartialLogicalDivisionComparator.java diff --git a/Kitodo-DataFormat/src/main/java/org/kitodo/dataformat/access/FileXmlElementAccess.java b/Kitodo-DataFormat/src/main/java/org/kitodo/dataformat/access/FileXmlElementAccess.java index 5fec27cfbc4..bb6f3e10535 100644 --- a/Kitodo-DataFormat/src/main/java/org/kitodo/dataformat/access/FileXmlElementAccess.java +++ b/Kitodo-DataFormat/src/main/java/org/kitodo/dataformat/access/FileXmlElementAccess.java @@ -154,7 +154,7 @@ DivType toDiv(Map mediaFilesToIDFiles, div.setTYPE(physicalDivision.getType()); for (Entry use : physicalDivision.getMediaFiles().entrySet()) { Fptr fptr = new Fptr(); - Object fileId = mediaFilesToIDFiles.get(use.getValue()); + FileType fileId = mediaFilesToIDFiles.get(use.getValue()); if (PhysicalDivision.TYPE_TRACK.equals(physicalDivision.getType()) && MediaUtil.isAudioOrVideo( use.getKey().getMimeType()) && physicalDivision.hasMediaPartialView()) { fptr.setArea(getAreaType(fileId)); @@ -181,7 +181,7 @@ DivType toDiv(Map mediaFilesToIDFiles, return div; } - private AreaType getAreaType(Object fileId) { + private AreaType getAreaType(FileType fileId) { MediaPartialView mediaPartialView = physicalDivision.getMediaPartialView(); AreaType areaType = new AreaType(); areaType.setFILEID(fileId); diff --git a/Kitodo/src/main/java/org/kitodo/production/forms/dataeditor/MediaPartialForm.java b/Kitodo/src/main/java/org/kitodo/production/forms/dataeditor/MediaPartialForm.java index d8464faaca8..a9e57aa9152 100644 --- a/Kitodo/src/main/java/org/kitodo/production/forms/dataeditor/MediaPartialForm.java +++ b/Kitodo/src/main/java/org/kitodo/production/forms/dataeditor/MediaPartialForm.java @@ -18,7 +18,6 @@ import java.io.Serializable; import java.util.Map; import java.util.Objects; -import java.util.regex.Pattern; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.Pair; @@ -167,7 +166,7 @@ protected boolean valid() { validationError = "mediaPartialFormStartEmpty"; return false; } - if (!Pattern.compile(MediaPartialHelper.FORMATTED_TIME_REGEX).matcher(getBegin()).matches()) { + if (!MediaPartialHelper.FORMATTED_TIME_PATTERN.matcher(getBegin()).matches()) { validationError = "mediaPartialFormStartWrongTimeFormat"; return false; } diff --git a/Kitodo/src/main/java/org/kitodo/production/forms/dataeditor/MediaPartialsPanel.java b/Kitodo/src/main/java/org/kitodo/production/forms/dataeditor/MediaPartialsPanel.java index dfe48de5788..b4fe64d3377 100644 --- a/Kitodo/src/main/java/org/kitodo/production/forms/dataeditor/MediaPartialsPanel.java +++ b/Kitodo/src/main/java/org/kitodo/production/forms/dataeditor/MediaPartialsPanel.java @@ -22,7 +22,6 @@ import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.regex.Pattern; import java.util.stream.Collectors; import javax.faces.context.FacesContext; @@ -96,7 +95,7 @@ public String validateMediaDuration() { String error = null; if (StringUtils.isEmpty(getMediaDuration())) { error = "mediaPartialFormMediaDurationEmpty"; - } else if (!Pattern.compile(MediaPartialHelper.FORMATTED_TIME_REGEX).matcher(getMediaDuration()).matches()) { + } else if (!MediaPartialHelper.FORMATTED_TIME_PATTERN.matcher(getMediaDuration()).matches()) { error = "mediaPartialFormMediaDurationWrongTimeFormat"; } return error; diff --git a/Kitodo/src/main/java/org/kitodo/production/helper/metadata/MediaPartialHelper.java b/Kitodo/src/main/java/org/kitodo/production/helper/metadata/MediaPartialHelper.java index 6e94528fdde..95b41e6da5f 100644 --- a/Kitodo/src/main/java/org/kitodo/production/helper/metadata/MediaPartialHelper.java +++ b/Kitodo/src/main/java/org/kitodo/production/helper/metadata/MediaPartialHelper.java @@ -11,41 +11,37 @@ package org.kitodo.production.helper.metadata; -import java.text.SimpleDateFormat; -import java.util.Comparator; -import java.util.Date; -import java.util.List; -import java.util.ListIterator; -import java.util.Objects; -import java.util.concurrent.TimeUnit; - import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.Pair; import org.kitodo.api.dataformat.LogicalDivision; import org.kitodo.api.dataformat.MediaPartialView; import org.kitodo.api.dataformat.PhysicalDivision; -import org.kitodo.api.dataformat.View; import org.kitodo.api.dataformat.Workpiece; import org.kitodo.production.metadata.MetadataEditor; +import java.time.Duration; +import java.time.LocalTime; +import java.util.List; +import java.util.ListIterator; +import java.util.Objects; +import java.util.concurrent.TimeUnit; +import java.util.regex.Pattern; + public class MediaPartialHelper { - public static final String FORMATTED_TIME_REGEX = "([0-1]\\d|2[0-3]):[0-5]\\d:[0-5]\\d\\.\\d{3}"; + public static final Pattern FORMATTED_TIME_PATTERN = Pattern.compile("([0-1]\\d|2[0-3]):[0-5]\\d:[0-5]\\d\\.\\d{3}"); + + private static final MediaPartialLogicalDivisionComparator mediaPartialLogicalDivisionComparator = new MediaPartialLogicalDivisionComparator(); /** * Convert formatted time to milliseconds. * * @param formattedTime - * The formatted time in form of {@value #FORMATTED_TIME_REGEX} + * The formatted time in form of {@value #FORMATTED_TIME_PATTERN} * @return The milliseconds */ public static Long convertFormattedTimeToMilliseconds(String formattedTime) { - int milliseconds = 0; - String[] splittedFormattedTime = formattedTime.split("\\."); - formattedTime = splittedFormattedTime[0]; - milliseconds = Integer.parseInt(splittedFormattedTime[1]); - String[] time = formattedTime.split(":"); - return (Integer.parseInt(time[0]) * 3600L + Integer.parseInt(time[1]) * 60L + Integer.parseInt(time[2])) * 1000 + milliseconds; + return Duration.between(LocalTime.MIN, LocalTime.parse(formattedTime)).toMillis(); } /** @@ -56,11 +52,9 @@ public static Long convertFormattedTimeToMilliseconds(String formattedTime) { * @return The formatted time */ public static String convertMillisecondsToFormattedTime(Long milliseconds) { - String formattedMilliseconds = StringUtils.leftPad( - String.valueOf(TimeUnit.MILLISECONDS.toMillis(milliseconds) % 1000), 3, "0"); - return String.format("%02d:%02d:%02d.%s", TimeUnit.MILLISECONDS.toHours(milliseconds), - TimeUnit.MILLISECONDS.toMinutes(milliseconds) % 60, TimeUnit.MILLISECONDS.toSeconds(milliseconds) % 60, - formattedMilliseconds); + Duration duration = Duration.ofMillis(milliseconds); + return String.format("%02d:%02d:%02d.%03d", duration.toHours(), duration.toMinutesPart(), + duration.toSecondsPart(), duration.toMillisPart()); } /** @@ -73,7 +67,7 @@ public static String convertMillisecondsToFormattedTime(Long milliseconds) { */ public static void calculateExtentAndSortMediaPartials(List logicalDivisions, Long mediaDuration) { calculateExtentForMediaPartials(logicalDivisions, mediaDuration); - logicalDivisions.sort(getLogicalDivisionComparator()); + logicalDivisions.sort(mediaPartialLogicalDivisionComparator); } /** @@ -89,7 +83,7 @@ public static void calculateExtentAndSortMediaPartials(List log */ private static void calculateExtentForMediaPartials(List logicalDivisions, Long mediaDuration) { // sorting reverse by begin - logicalDivisions.sort(getLogicalDivisionComparator().reversed()); + logicalDivisions.sort(mediaPartialLogicalDivisionComparator.reversed()); ListIterator iterator = logicalDivisions.listIterator(); LogicalDivision previousLogicalDivision = null; @@ -151,20 +145,4 @@ public static void addMediaPartialToMediaSelection(String type, String title, St mediaSelection.getValue().getChildren().add(logicalDivision); } - private static Comparator getLogicalDivisionComparator() { - return (logicalDivisionA, logicalDivisionB) -> { - View viewA = logicalDivisionA.getViews().getFirst(); - View viewB = logicalDivisionB.getViews().getFirst(); - if (Objects.nonNull(viewA) && Objects.nonNull(viewB)) { - PhysicalDivision physicalDivisionA = viewA.getPhysicalDivision(); - PhysicalDivision physicalDivisionB = viewB.getPhysicalDivision(); - if (physicalDivisionA.hasMediaPartialView() && physicalDivisionB.hasMediaPartialView()) { - return physicalDivisionA.getMediaPartialView().getBegin() - .compareTo(physicalDivisionB.getMediaPartialView().getBegin()); - } - } - return Integer.compare(logicalDivisionA.getOrder(), logicalDivisionB.getOrder()); - }; - } - } diff --git a/Kitodo/src/main/java/org/kitodo/production/helper/metadata/MediaPartialLogicalDivisionComparator.java b/Kitodo/src/main/java/org/kitodo/production/helper/metadata/MediaPartialLogicalDivisionComparator.java new file mode 100644 index 00000000000..07a73601571 --- /dev/null +++ b/Kitodo/src/main/java/org/kitodo/production/helper/metadata/MediaPartialLogicalDivisionComparator.java @@ -0,0 +1,38 @@ +/* + * (c) Kitodo. Key to digital objects e. V. + * + * 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.production.helper.metadata; + +import org.kitodo.api.dataformat.LogicalDivision; +import org.kitodo.api.dataformat.PhysicalDivision; +import org.kitodo.api.dataformat.View; + +import java.util.Comparator; +import java.util.Objects; + +public class MediaPartialLogicalDivisionComparator implements Comparator { + + @Override + public int compare(LogicalDivision logicalDivisionA, LogicalDivision logicalDivisionB) { + View viewA = logicalDivisionA.getViews().getFirst(); + View viewB = logicalDivisionB.getViews().getFirst(); + if (Objects.nonNull(viewA) && Objects.nonNull(viewB)) { + PhysicalDivision physicalDivisionA = viewA.getPhysicalDivision(); + PhysicalDivision physicalDivisionB = viewB.getPhysicalDivision(); + if (physicalDivisionA.hasMediaPartialView() && physicalDivisionB.hasMediaPartialView()) { + return physicalDivisionA.getMediaPartialView().getBegin() + .compareTo(physicalDivisionB.getMediaPartialView().getBegin()); + } + } + return Integer.compare(logicalDivisionA.getOrder(), logicalDivisionB.getOrder()); + } + +}