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 38124546fb0..adb63be3cec 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 @@ -14,6 +14,7 @@ import static org.kitodo.production.helper.metadata.MediaPartialHelper.addMediaPartialToMediaSelection; import static org.kitodo.production.helper.metadata.MediaPartialHelper.calculateExtentAndSortMediaPartials; import static org.kitodo.production.helper.metadata.MediaPartialHelper.convertFormattedTimeToMilliseconds; +import static org.kitodo.production.helper.metadata.MediaPartialHelper.convertTimeToFormattedTime; import java.io.Serializable; import java.util.Map; @@ -166,6 +167,7 @@ protected boolean valid() { validationError = "mediaPartialFormStartEmpty"; return false; } + setBegin(convertTimeToFormattedTime(getBegin())); if (!MediaPartialHelper.FORMATTED_TIME_PATTERN.matcher(getBegin()).matches()) { validationError = "mediaPartialFormStartWrongTimeFormat"; return false; 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 87c35a9d7e5..7dcc967029b 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 @@ -12,8 +12,11 @@ package org.kitodo.production.helper.metadata; import java.net.URI; +import java.text.ParseException; +import java.text.SimpleDateFormat; import java.time.Duration; import java.time.LocalTime; +import java.util.Date; import java.util.LinkedList; import java.util.List; import java.util.ListIterator; @@ -21,6 +24,8 @@ import java.util.Objects; import java.util.regex.Pattern; +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.time.DateUtils; import org.apache.commons.lang3.tuple.Pair; import org.kitodo.api.dataformat.LogicalDivision; import org.kitodo.api.dataformat.MediaPartial; @@ -47,6 +52,34 @@ public static Long convertFormattedTimeToMilliseconds(String formattedTime) { return Duration.between(LocalTime.MIN, LocalTime.parse(formattedTime)).toMillis(); } + /** + * Convert various time input formats to formatted time. + * + * @param time + * The time input. + * @return The formatted time + */ + public static String convertTimeToFormattedTime(String time) { + if (FORMATTED_TIME_PATTERN.matcher(time).matches()) { + return time; + } + + String[] separatedMilliseconds = time.split("\\."); + Date date; + try { + date = DateUtils.parseDate(separatedMilliseconds[0], "HH:mm:ss", "mm:ss", "ss"); + } catch (ParseException e) { + return time; + } + + if (separatedMilliseconds.length == 2) { + String filledMilliseconds = StringUtils.rightPad(separatedMilliseconds[1], 3, "0"); + date = new Date(date.getTime() + Integer.parseInt(filledMilliseconds)); + } + + return new SimpleDateFormat("HH:mm:ss.SSS").format(date); + } + /** * Convert milliseconds to formatted time. * diff --git a/Kitodo/src/test/java/org/kitodo/production/forms/dataeditor/MediaPartialFormTest.java b/Kitodo/src/test/java/org/kitodo/production/forms/dataeditor/MediaPartialFormTest.java index 5ff8880ff14..f1f85880892 100644 --- a/Kitodo/src/test/java/org/kitodo/production/forms/dataeditor/MediaPartialFormTest.java +++ b/Kitodo/src/test/java/org/kitodo/production/forms/dataeditor/MediaPartialFormTest.java @@ -31,6 +31,7 @@ import org.kitodo.api.dataformat.PhysicalDivision; import org.kitodo.api.dataformat.View; import org.kitodo.api.dataformat.Workpiece; +import org.kitodo.production.helper.metadata.MediaPartialHelper; import org.kitodo.production.metadata.MetadataEditor; import org.mockito.MockedStatic; import org.mockito.Mockito; @@ -179,6 +180,19 @@ public void testValidation() throws NoSuchFieldException, IllegalAccessException assertTrue(mediaPartialForm.valid()); } + /** + * Test time to formatted time conversion. + */ + @Test + public void testTimeToFormattedTimeConversion() { + assertEquals("00:00:01.000", MediaPartialHelper.convertTimeToFormattedTime("1")); + assertEquals("00:01:01.000", MediaPartialHelper.convertTimeToFormattedTime("1:1")); + assertEquals("01:01:01.000", MediaPartialHelper.convertTimeToFormattedTime("1:1:1")); + assertEquals("00:00:01.100", MediaPartialHelper.convertTimeToFormattedTime("1.1")); + assertEquals("00:01:01.110", MediaPartialHelper.convertTimeToFormattedTime("1:1.11")); + assertEquals("01:01:01.111", MediaPartialHelper.convertTimeToFormattedTime("1:1:1.111")); + } + private static String getValidationError(MediaPartialForm mediaPartialForm) throws NoSuchFieldException, IllegalAccessException { Field validationErrorField = mediaPartialForm.getClass().getDeclaredField("validationError");