Skip to content

Commit

Permalink
Improvements regarding the review
Browse files Browse the repository at this point in the history
  • Loading branch information
markusweigelt committed Jan 2, 2024
1 parent e906c52 commit 5b8ed80
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ DivType toDiv(Map<URI, FileType> mediaFilesToIDFiles,
div.setTYPE(physicalDivision.getType());
for (Entry<MediaVariant, URI> 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));
Expand All @@ -181,7 +181,7 @@ DivType toDiv(Map<URI, FileType> mediaFilesToIDFiles,
return div;
}

private AreaType getAreaType(Object fileId) {
private AreaType getAreaType(FileType fileId) {
MediaPartialView mediaPartialView = physicalDivision.getMediaPartialView();
AreaType areaType = new AreaType();
areaType.setFILEID(fileId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -73,7 +67,7 @@ public static String convertMillisecondsToFormattedTime(Long milliseconds) {
*/
public static void calculateExtentAndSortMediaPartials(List<LogicalDivision> logicalDivisions, Long mediaDuration) {
calculateExtentForMediaPartials(logicalDivisions, mediaDuration);
logicalDivisions.sort(getLogicalDivisionComparator());
logicalDivisions.sort(mediaPartialLogicalDivisionComparator);
}

/**
Expand All @@ -89,7 +83,7 @@ public static void calculateExtentAndSortMediaPartials(List<LogicalDivision> log
*/
private static void calculateExtentForMediaPartials(List<LogicalDivision> logicalDivisions, Long mediaDuration) {
// sorting reverse by begin
logicalDivisions.sort(getLogicalDivisionComparator().reversed());
logicalDivisions.sort(mediaPartialLogicalDivisionComparator.reversed());

ListIterator<LogicalDivision> iterator = logicalDivisions.listIterator();
LogicalDivision previousLogicalDivision = null;
Expand Down Expand Up @@ -151,20 +145,4 @@ public static void addMediaPartialToMediaSelection(String type, String title, St
mediaSelection.getValue().getChildren().add(logicalDivision);
}

private static Comparator<LogicalDivision> 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());
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* (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.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<LogicalDivision> {

@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());
}

}

0 comments on commit 5b8ed80

Please sign in to comment.