Skip to content

Commit

Permalink
MID-9043: removing of dependency for Wicket DateTime 8.00
Browse files Browse the repository at this point in the history
  • Loading branch information
skublik committed Apr 30, 2024
1 parent 966733c commit 0243ea4
Show file tree
Hide file tree
Showing 11 changed files with 205 additions and 96 deletions.
4 changes: 0 additions & 4 deletions gui/admin-gui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -412,10 +412,6 @@
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-extensions</artifactId>
</dependency>
<dependency>
<groupId>org.apache.wicket</groupId>
<artifactId>wicket-datetime</artifactId>
</dependency>
<dependency>
<groupId>org.wicketstuff</groupId>
<artifactId>wicketstuff-select2</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName;

import com.evolveum.midpoint.gui.impl.component.input.converter.DateConverter;
import com.evolveum.midpoint.web.page.admin.server.dto.ApprovalOutcomeIcon;

import org.apache.commons.collections4.CollectionUtils;
Expand All @@ -40,7 +41,6 @@
import org.apache.wicket.behavior.AttributeAppender;
import org.apache.wicket.behavior.Behavior;
import org.apache.wicket.core.request.handler.RenderPageRequestHandler;
import org.apache.wicket.datetime.PatternDateConverter;
import org.apache.wicket.extensions.markup.html.repeater.util.SortParam;
import org.apache.wicket.extensions.markup.html.tabs.ITab;
import org.apache.wicket.feedback.IFeedback;
Expand Down Expand Up @@ -1526,7 +1526,7 @@ public static String getLocalizedDate(Date date, String style) {
if (date == null) {
return null;
}
PatternDateConverter converter = new PatternDateConverter(getLocalizedDatePattern(style), true);
DateConverter converter = new DateConverter(getLocalizedDatePattern(style), true);
return converter.convertToString(date, getCurrentLocale());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,28 @@
import org.apache.wicket.Session;
import org.jetbrains.annotations.NotNull;

import java.io.Serial;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

public class DateTimePickerOptions {
/**
* Options that will be used for date time picker in js.
* We can generate options as string for js script by {@link #toJsConfiguration()}.
*/
public class DateTimePickerOptions implements Serializable {

@Serial private static final long serialVersionUID = 1L;

private final static List<String> LIST_OF_LOCALIZATION_KEYS;

public enum Theme {LIGHT, DARK, AUTO}

private Theme theme;

// List of key that contains translation values. Key for localization is DateTimePickerOptions.'key'.
static {
LIST_OF_LOCALIZATION_KEYS = List.of(
"today",
Expand Down Expand Up @@ -57,6 +67,7 @@ public enum Theme {LIGHT, DARK, AUTO}
"selectDate"
);
}

private DateTimePickerOptions() {
SessionStorage.Mode mode = ((MidPointAuthWebSession) Session.get()).getSessionStorage().getMode();
this.theme = Theme.LIGHT;
Expand All @@ -65,7 +76,7 @@ private DateTimePickerOptions() {
}
}

public static DateTimePickerOptions of(){
public static DateTimePickerOptions of() {
return new DateTimePickerOptions();
}

Expand All @@ -74,6 +85,9 @@ public DateTimePickerOptions theme(Theme theme) {
return this;
}

/**
* Produce string that represent options for date time picker which can be used for js script.
*/
public String toJsConfiguration() {
StringBuilder sb = new StringBuilder("{");

Expand All @@ -83,15 +97,15 @@ public String toJsConfiguration() {
LIST_OF_LOCALIZATION_KEYS.forEach(
key -> sb.append(key)
.append(": '")
.append(LocalizationUtil.translate("DateTimePickerPanel." + key))
.append(LocalizationUtil.translate("DateTimePickerOptions." + key))
.append("', "));
sb.append("dayViewHeaderFormat: { month: 'long', year: 'numeric' }, ");

@NotNull Locale locale = LocalizationUtil.findLocale();
sb.append("locale: '").append(locale.toLanguageTag()).append("', ");

sb.append("format: '")
.append(getDateTimeFormatForLocale(locale))
.append(getDateTimeFormatForOption(locale))
.append("' ");

sb.append("}");
Expand All @@ -104,11 +118,11 @@ public String getDateTimeFormat() {
@NotNull Locale locale = LocalizationUtil.findLocale();
String localizedDatePattern = getDateTimeFormat(locale);

if (localizedDatePattern.contains("MMMM")){
if (localizedDatePattern.contains("MMMM")) {
localizedDatePattern = localizedDatePattern.replaceAll("MMMM", "LLLL");
}

if (localizedDatePattern != null && !localizedDatePattern.contains("yyyy")){
if (!localizedDatePattern.contains("yyyy")) {
localizedDatePattern = localizedDatePattern.replaceAll("yy", "yyyy");
}

Expand All @@ -121,7 +135,7 @@ private String getDateTimeFormat(@NotNull Locale locale) {
return dateFormat + " " + timeFormat;
}

private String getDateTimeFormatForLocale(@NotNull Locale locale) {
private String getDateTimeFormatForOption(@NotNull Locale locale) {
return replacingSingleQuotationMark(getDateTimeFormat(locale));
}

Expand All @@ -131,7 +145,7 @@ private String replacingSingleQuotationMark(String dateTimeFormat) {
}

String replacingChar = "[";
while (dateTimeFormat.indexOf("'") != -1) {
while (dateTimeFormat.contains("'")) {
dateTimeFormat = dateTimeFormat.replaceFirst("'", replacingChar);
if (replacingChar.equals("[")) {
replacingChar = "]";
Expand All @@ -152,7 +166,7 @@ private String replacingCharForAmOrPm(String dateTimeFormat) {

boolean isInBrackets = false;
char[] chars = dateTimeFormat.toCharArray();
for (int i = 0; i < chars.length; i++){
for (int i = 0; i < chars.length; i++) {
char currentChar = chars[i];

if (!isInBrackets && currentChar == 'a') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@
import javax.xml.datatype.XMLGregorianCalendar;
import java.util.Date;

/**
* Panel for Date. Panel use date time picker. We can use Model of types Date or XMLGregorianCalendar.
*/
public class DateTimePickerPanel extends InputPanel {

private static final String ID_CONTAINER = "container";
private static final String ID_INPUT = "input";
private static final String ID_ICON_CONTAINER = "iconContainer";

private final DateTimePickerOptions dateTimePickerOptions = DateTimePickerOptions.of();

public static DateTimePickerPanel createByDateModel(String id, IModel<Date> model) {
return new DateTimePickerPanel(id, model);
}
Expand All @@ -53,7 +58,7 @@ public void renderHead(IHeaderResponse response) {
OnDomReadyHeaderItem.forScript(
"MidPointTheme.initDateTimePicker("
+ getMarkupId() + ", "
+ DateTimePickerOptions.of().toJsConfiguration() + ");"));
+ dateTimePickerOptions.toJsConfiguration() + ");"));
}
};
container.setOutputMarkupId(true);
Expand All @@ -65,7 +70,7 @@ protected IConverter<?> createConverter(Class<?> clazz)
{
if (Date.class.isAssignableFrom(clazz))
{
return new DateConverter();
return new DateConverter(dateTimePickerOptions.getDateTimeFormat());
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

import com.evolveum.midpoint.gui.api.util.LocalizationUtil;
import com.evolveum.midpoint.gui.api.util.WebModelServiceUtils;
import com.evolveum.midpoint.gui.impl.component.input.DateTimePickerOptions;

import org.apache.wicket.Session;
import org.apache.wicket.core.request.ClientInfo;
import org.apache.wicket.protocol.http.request.WebClientInfo;
import org.apache.wicket.util.convert.ConversionException;
import org.apache.wicket.util.convert.IConverter;
import org.apache.wicket.util.string.Strings;
Expand All @@ -20,47 +22,71 @@
import java.util.Locale;
import java.util.TimeZone;

/**
* Converter for Date to String. Converter need pattern. We can define a boolean whether the time zone is used.
*/
public class DateConverter implements IConverter<Date> {

private final boolean useTimeZone;
private final String dateTimePattern;

public DateConverter(String dateTimePattern, boolean useTimeZone) {
this.useTimeZone = useTimeZone;
this.dateTimePattern = dateTimePattern;
}

public DateConverter(String dateTimePattern) {
this(dateTimePattern, false);
}

@Override
public Date convertToObject(String value, Locale locale) throws ConversionException {
if (Strings.isEmpty(value))
{
if (Strings.isEmpty(value)) {
return null;
}

SimpleDateFormat formatter = getFormatter();
try
{
return formatter.parse(value);
}
catch (Exception e)
{
throw newConversionException(e);
}
try {
return formatter.parse(value);
} catch (Exception e) {
throw newConversionException(e);
}
}

private ConversionException newConversionException(Exception cause)
{
private ConversionException newConversionException(Exception cause) {
return new ConversionException(cause)
.setVariable("formatter", getDatePattern());
}

private String getDatePattern() {
return DateTimePickerOptions.of().getDateTimeFormat();
.setVariable("formatter", dateTimePattern);
}

private SimpleDateFormat getFormatter() {
return new SimpleDateFormat(getDatePattern(), LocalizationUtil.findLocale());
SimpleDateFormat formatter = new SimpleDateFormat(dateTimePattern, LocalizationUtil.findLocale());

if (useTimeZone) {
TimeZone timezone = getTimeZone();
if (timezone != null) {
formatter.setTimeZone(timezone);
}
}

return formatter;
}

@Override
public String convertToString(Date date, Locale locale) {
SimpleDateFormat formatter = getFormatter();
return getFormatter().format(date);
}

private TimeZone getTimeZone() {
TimeZone timezone = WebModelServiceUtils.getTimezone();
if (timezone != null) {
formatter.setTimeZone(timezone);
return timezone;
}
return formatter.format(date);

ClientInfo info = Session.get().getClientInfo();
if (info instanceof WebClientInfo) {
return ((WebClientInfo) info).getProperties().getTimeZone();
}
return null;
}

}

0 comments on commit 0243ea4

Please sign in to comment.