Skip to content

Commit

Permalink
Fix checkstyle problems
Browse files Browse the repository at this point in the history
  • Loading branch information
markusweigelt committed Nov 20, 2023
1 parent 5b259d5 commit 56e25e1
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@

package org.kitodo.production.forms.dataeditor;

import static org.kitodo.production.forms.dataeditor.MediaPartialsPanel.convertFormattedTimeToMilliseconds;
import static org.kitodo.production.forms.dataeditor.MediaPartialsPanel.generateExtentAndSortMediaPartials;

import java.io.Serializable;
import java.util.LinkedList;
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;
import org.kitodo.api.dataformat.LogicalDivision;
Expand All @@ -22,15 +31,6 @@
import org.omnifaces.util.Ajax;
import org.primefaces.PrimeFaces;

import java.io.Serializable;
import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Pattern;

import static org.kitodo.production.forms.dataeditor.MediaPartialsPanel.generateExtentAndSortMediaPartials;
import static org.kitodo.production.forms.dataeditor.MediaPartialsPanel.getMillisecondsOfFormattedTime;

public class MediaPartialForm implements Serializable {

private final DataEditorForm dataEditor;
Expand All @@ -39,12 +39,17 @@ public class MediaPartialForm implements Serializable {
private String title;
private String begin;
private String type;
private String validationError;
private String validationErrorMessage;

MediaPartialForm(DataEditorForm dataEditor) {
this.dataEditor = dataEditor;
}

/**
* Check if form is in edit mode.
*
* @return True if form is in edit mode.
*/
public boolean isEdit() {
return Objects.nonNull(mediaPartialDivision);
}
Expand All @@ -56,50 +61,26 @@ public void clean() {
mediaPartialDivision = null;
title = "";
begin = null;
validationError = "";
validationErrorMessage = "";
Ajax.update("mediaPartialForm");
}

public boolean valid() {
validationError = "";
if (Objects.isNull(getMediaSelection())) {
validationError = Helper.getTranslation("mediaPartialFormNoMedium");
return false;
}
validationError = dataEditor.getGalleryPanel().getMediaPartialsPanel().validateDuration();
if (Objects.nonNull(validationError)) {
return false;
}
if (StringUtils.isEmpty(getBegin())) {
validationError = Helper.getTranslation("mediaPartialFormStartEmpty");
return false;
}
if (!Pattern.compile(MediaPartialsPanel.FORMATTED_TIME_REGEX).matcher(getBegin()).matches()) {
validationError = Helper.getTranslation("mediaPartialFormStartWrongTimeFormat");
return false;
}
if (getMillisecondsOfFormattedTime(getBegin()) >= getMillisecondsOfFormattedTime(getDuration())) {
validationError = Helper.getTranslation("mediaPartialFormStartLessThanMediaDuration");
return false;
}
if (!isEdit() || (isEdit() && !mediaPartialDivision.getValue().getBegin().equals(getBegin()))) {
boolean exists = getMediaSelection().getValue().getChildren().stream().anyMatch(
logicalDivision -> logicalDivision.getViews().getFirst().getPhysicalDivision().getMediaPartialView()
.getBegin().equals(getBegin()));
if (exists) {
validationError = Helper.getTranslation("mediaPartialFormStartExists");
return false;
}
}
return true;
}

public String getValidationError() {
return validationError;
/**
* Get the validation error message.
*
* @return The validation error message.
*/
public String getValidationErrorMessage() {
return validationErrorMessage;
}

public boolean hasValidationError() {
return StringUtils.isNotEmpty(validationError);
/**
* Check if form has a validation error message.
*
* @return True if validation error is not empty.
*/
public boolean hasValidationErrorMessage() {
return StringUtils.isNotEmpty(validationErrorMessage);
}

/**
Expand Down Expand Up @@ -138,7 +119,7 @@ public void save() {
}

generateExtentAndSortMediaPartials(getMediaSelection().getValue().getChildren(),
getMillisecondsOfFormattedTime(getDuration()));
convertFormattedTimeToMilliseconds(getMediaDuration()));

try {
dataEditor.refreshStructurePanel();
Expand Down Expand Up @@ -179,11 +160,46 @@ public void setType(String type) {
this.type = type;
}

private String getDuration() {
return dataEditor.getGalleryPanel().getMediaPartialsPanel().getDuration();
private String getMediaDuration() {
return dataEditor.getGalleryPanel().getMediaPartialsPanel().getMediaDuration();
}

private Pair<PhysicalDivision, LogicalDivision> getMediaSelection() {
return dataEditor.getGalleryPanel().getMediaPartialsPanel().getMediaSelection();
}

private boolean valid() {
validationErrorMessage = "";
if (Objects.isNull(getMediaSelection())) {
validationErrorMessage = Helper.getTranslation("mediaPartialFormNoMedium");
return false;
}
validationErrorMessage = dataEditor.getGalleryPanel().getMediaPartialsPanel().validateDuration();
if (Objects.nonNull(validationErrorMessage)) {
return false;
}
if (StringUtils.isEmpty(getBegin())) {
validationErrorMessage = Helper.getTranslation("mediaPartialFormStartEmpty");
return false;
}
if (!Pattern.compile(MediaPartialsPanel.FORMATTED_TIME_REGEX).matcher(getBegin()).matches()) {
validationErrorMessage = Helper.getTranslation("mediaPartialFormStartWrongTimeFormat");
return false;
}
if (convertFormattedTimeToMilliseconds(getBegin()) >= convertFormattedTimeToMilliseconds(getMediaDuration())) {
validationErrorMessage = Helper.getTranslation("mediaPartialFormStartLessThanMediaDuration");
return false;
}
if (!isEdit() || (isEdit() && !mediaPartialDivision.getValue().getBegin().equals(getBegin()))) {
boolean exists = getMediaSelection().getValue().getChildren().stream().anyMatch(
logicalDivision -> logicalDivision.getViews().getFirst().getPhysicalDivision().getMediaPartialView()
.getBegin().equals(getBegin()));
if (exists) {
validationErrorMessage = Helper.getTranslation("mediaPartialFormStartExists");
return false;
}
}
return true;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@

public class MediaPartialsPanel implements Serializable {

public static final String FORMATTED_TIME_REGEX = "(([0-1][0-9])|([2][0-3])):([0-5][0-9]):([0-5][0-9])";
public static final String REQUEST_PARAMETER_DURATION = "duration";
public static final String FORMATTED_TIME_REGEX = "([0-1]\\d|2[0-3]):[0-5]\\d:[0-5]\\d";
public static final String REQUEST_PARAMETER_MEDIA_DURATION = "mediaDuration";
private MediaPartialForm mediaPartialForm;
private DataEditorForm dataEditor;
private String duration;
private String mediaDuration;
private Pair<PhysicalDivision, LogicalDivision> mediaSelection;

MediaPartialsPanel(DataEditorForm dataEditor) {
Expand Down Expand Up @@ -93,9 +93,9 @@ private static void getMediaPartialViewDivisions(Map<LogicalDivision, MediaParti
*/
public String validateDuration() {
String errorMessage = null;
if (StringUtils.isEmpty(getDuration())) {
if (StringUtils.isEmpty(getMediaDuration())) {
errorMessage = Helper.getTranslation("mediaPartialFormMediaDurationEmpty");
} else if (!Pattern.compile(MediaPartialsPanel.FORMATTED_TIME_REGEX).matcher(getDuration()).matches()) {
} else if (!Pattern.compile(MediaPartialsPanel.FORMATTED_TIME_REGEX).matcher(getMediaDuration()).matches()) {
errorMessage = Helper.getTranslation("mediaPartialFormMediaDurationWrongTimeFormat");
}
return errorMessage;
Expand All @@ -119,7 +119,7 @@ public void deleteMediaViewDivision(Map.Entry<LogicalDivision, MediaPartialView>
logicalDivision.getViews().remove();
dataEditor.getStructurePanel().deleteLogicalDivision(logicalDivision);
generateExtentAndSortMediaPartials(getMediaSelection().getValue().getChildren(),
getMillisecondsOfFormattedTime(getDuration()));
convertFormattedTimeToMilliseconds(getMediaDuration()));
}
}

Expand Down Expand Up @@ -177,6 +177,11 @@ public MediaPartialForm getMediaPartialForm() {
return mediaPartialForm;
}

/**
* Get the media selection.
*
* @return The media selection
*/
public Pair<PhysicalDivision, LogicalDivision> getMediaSelection() {
return mediaSelection;
}
Expand All @@ -186,16 +191,24 @@ public Pair<PhysicalDivision, LogicalDivision> getMediaSelection() {
*/
public void setMembersByRequestParameter() {
Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
if (params.containsKey(REQUEST_PARAMETER_DURATION)) {
duration = params.get(REQUEST_PARAMETER_DURATION);
if (params.containsKey(REQUEST_PARAMETER_MEDIA_DURATION)) {
mediaDuration = params.get(REQUEST_PARAMETER_MEDIA_DURATION);
}
}

public static void generateExtentAndSortMediaPartials(List<LogicalDivision> logicalDivisions, Long duration) {
/**
* Generate the extent field of every media partial and sort media partials by begin.
*
* @param logicalDivisions
* The logical divisions of media partials.
* @param mediaDuration
* The media duration.
*/
public static void generateExtentAndSortMediaPartials(List<LogicalDivision> logicalDivisions, Long mediaDuration) {
// sorting reverse to set extent starting from the last entry
Collections.sort(logicalDivisions, getLogicalDivisionComparator().reversed());

generateExtentForMediaPartials(logicalDivisions, duration);
generateExtentForMediaPartials(logicalDivisions, mediaDuration);

Collections.sort(logicalDivisions, getLogicalDivisionComparator());
}
Expand All @@ -211,19 +224,27 @@ private static void generateExtentForMediaPartials(List<LogicalDivision> logical
PhysicalDivision previousPhysicalDivision = previousLogicalDivision.getViews().getFirst()
.getPhysicalDivision();
if (previousPhysicalDivision.hasMediaPartialView()) {
mediaPartialView.setExtent(getFormattedTimeOfMilliseconds(getMillisecondsOfFormattedTime(
previousPhysicalDivision.getMediaPartialView().getBegin()) - getMillisecondsOfFormattedTime(
mediaPartialView.setExtent(convertMillisecondsToFormattedTime(convertFormattedTimeToMilliseconds(
previousPhysicalDivision.getMediaPartialView()
.getBegin()) - convertFormattedTimeToMilliseconds(
mediaPartialView.getBegin())));
}
} else {
mediaPartialView.setExtent(getFormattedTimeOfMilliseconds(
duration - getMillisecondsOfFormattedTime(mediaPartialView.getBegin())));
mediaPartialView.setExtent(convertMillisecondsToFormattedTime(
duration - convertFormattedTimeToMilliseconds(mediaPartialView.getBegin())));
}
previousLogicalDivision = logicalDivision;
}
}

public static Long getMillisecondsOfFormattedTime(String formattedTime) {
/**
* Convert formatted time to milliseconds.
*
* @param formattedTime
* The formatted time in form of {@value #FORMATTED_TIME_REGEX}
* @return The milliseconds
*/
public static Long convertFormattedTimeToMilliseconds(String formattedTime) {
if (formattedTime.contains(".")) {
formattedTime = formattedTime.split(".")[0];
}
Expand All @@ -232,28 +253,33 @@ public static Long getMillisecondsOfFormattedTime(String formattedTime) {
Integer.valueOf(time[0]) * 3600 + Integer.valueOf(time[1]) * 60 + Integer.valueOf(time[2])) * 1000;
}

private static String getFormattedTimeOfMilliseconds(Long milliseconds) {
private static String convertMillisecondsToFormattedTime(Long milliseconds) {
return String.format("%02d:%02d:%02d", TimeUnit.MILLISECONDS.toHours(milliseconds),
TimeUnit.MILLISECONDS.toMinutes(milliseconds) % 60, TimeUnit.MILLISECONDS.toSeconds(milliseconds) % 60);
}

private static Comparator<LogicalDivision> getLogicalDivisionComparator() {
return (logicalDivision1, logicalDivision2) -> {
View view1 = logicalDivision1.getViews().getFirst();
View view2 = logicalDivision2.getViews().getFirst();
if (Objects.nonNull(view1) && Objects.nonNull(view2)) {
PhysicalDivision physicalDivision1 = view1.getPhysicalDivision();
PhysicalDivision physicalDivision2 = view2.getPhysicalDivision();
if (physicalDivision1.hasMediaPartialView() && physicalDivision2.hasMediaPartialView()) {
return physicalDivision1.getMediaPartialView().getBegin()
.compareTo(physicalDivision2.getMediaPartialView().getBegin());
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(logicalDivision1.getOrder(), logicalDivision2.getOrder());
return Integer.compare(logicalDivisionA.getOrder(), logicalDivisionB.getOrder());
};
}

public String getDuration() {
return duration;
/**
* Get the media duration.
*
* @return The media duration
*/
public String getMediaDuration() {
return mediaDuration;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ metadataEditor.gallery = {
}
},
setDuration() {
let duration = this.formatTime(document.querySelector('#imagePreviewForm\\:mediaDetailMediaContainer video, #imagePreviewForm\\:mediaDetailMediaContainer audio').duration)
setMediaPartialsViewsPanelMembers([{name: "duration", value: duration}]);
let mediaDuration = this.formatTime(document.querySelector('#imagePreviewForm\\:mediaDetailMediaContainer video, #imagePreviewForm\\:mediaDetailMediaContainer audio').duration)
remoteCommandSetMembersByRequestParameter([{name: "mediaDuration", value: mediaDuration}]);
},
stopPlayEvent: new CustomEvent("mediaPartialStopPlay"),
togglePlay(button, formattedTimeBegin, formattedTimeExtent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
<p:row>
<h3>#{DataEditorForm.galleryPanel.mediaPartialsPanel.mediaPartialForm.isEdit() ? msgs.editMediaPartial : msgs.addMediaPartial}</h3>
<h:panelGroup id="errorWrapper"
rendered="#{DataEditorForm.galleryPanel.mediaPartialsPanel.mediaPartialForm.hasValidationError()}">
rendered="#{DataEditorForm.galleryPanel.mediaPartialsPanel.mediaPartialForm.hasValidationErrorMessage()}">
<h:panelGroup layout="block"
styleClass="select-note ui-messages-error">
<h:outputText
value="#{DataEditorForm.galleryPanel.mediaPartialsPanel.mediaPartialForm.validationError}"/>
value="#{DataEditorForm.galleryPanel.mediaPartialsPanel.mediaPartialForm.validationErrorMessage}"/>
</h:panelGroup>
</h:panelGroup>
<p:outputPanel styleClass="dialogFieldWrapper">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
rendered="#{DataEditorForm.galleryPanel.mediaPartialsPanel.enabled}">

<ui:fragment>
<p:remoteCommand name="setMediaPartialsViewsPanelMembers"
<p:remoteCommand name="remoteCommandSetMembersByRequestParameter"
action="#{DataEditorForm.galleryPanel.mediaPartialsPanel.setMembersByRequestParameter()}"/>

<p:outputPanel id="mediaPartialList"
Expand Down

0 comments on commit 56e25e1

Please sign in to comment.