PL-9992 TimeZone and @dateEquals macro in filter condition

This commit is contained in:
Andrey Subbotin 2017-11-24 17:58:32 +04:00
parent 900cf3bd67
commit efd4602aab
9 changed files with 94 additions and 25 deletions

View File

@ -27,6 +27,7 @@ import org.apache.commons.lang.ObjectUtils;
import javax.swing.*;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.function.Supplier;
public class DesktopListEditor extends DesktopAbstractField<JPanel> implements ListEditor {
@ -228,4 +229,14 @@ public class DesktopListEditor extends DesktopAbstractField<JPanel> implements L
public Supplier<Map<String, Object>> getEditorParamsSupplier() {
return null;
}
@Override
public void setTimeZone(TimeZone timeZone) {
delegate.setTimeZone(timeZone);
}
@Override
public TimeZone getTimeZone() {
return delegate.getTimeZone();
}
}

View File

@ -121,7 +121,7 @@ public class LocalizedEnumerationWindow extends AbstractWindow implements ListEd
}
valuesMap = values.stream()
.collect(Collectors.toMap(Function.identity(), o -> ListEditorHelper.getValueCaption(o, ListEditor.ItemType.STRING)));
.collect(Collectors.toMap(Function.identity(), o -> ListEditorHelper.getValueCaption(o, ListEditor.ItemType.STRING, null)));
if (!valuesMap.isEmpty()) {
Map<String, String> localizedValues = LocaleHelper.getLocalizedValuesMap(enumerationLocales);
@ -180,7 +180,7 @@ public class LocalizedEnumerationWindow extends AbstractWindow implements ListEd
if (!valueExists(value)) {
valueField.setValue(null);
addValueToDatasource(value, null);
valuesMap.put(value, ListEditorHelper.getValueCaption(value, ListEditor.ItemType.STRING));
valuesMap.put(value, ListEditorHelper.getValueCaption(value, ListEditor.ItemType.STRING, null));
}
}
}

View File

@ -18,6 +18,7 @@ package com.haulmont.cuba.gui.components;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.function.Supplier;
/**
@ -109,6 +110,14 @@ public interface ListEditor extends Field, Component.Focusable {
Supplier<Map<String, Object>> getEditorParamsSupplier();
/**
*
* @param timeZone - for DateTime fields and date formatting
*/
void setTimeZone(TimeZone timeZone);
TimeZone getTimeZone();
enum ItemType {
STRING,
DATE,

View File

@ -29,6 +29,7 @@ import com.haulmont.cuba.core.app.dynamicattributes.DynamicAttributesUtils;
import com.haulmont.cuba.core.app.dynamicattributes.PropertyType;
import com.haulmont.cuba.core.entity.CategoryAttribute;
import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.core.entity.annotation.IgnoreUserTimeZone;
import com.haulmont.cuba.core.entity.annotation.Lookup;
import com.haulmont.cuba.core.entity.annotation.LookupType;
import com.haulmont.cuba.core.global.*;
@ -46,6 +47,7 @@ import com.haulmont.cuba.gui.data.impl.DatasourceImplementation;
import com.haulmont.cuba.gui.theme.ThemeConstants;
import com.haulmont.cuba.gui.theme.ThemeConstantsManager;
import com.haulmont.cuba.gui.xml.layout.ComponentsFactory;
import com.haulmont.cuba.security.global.UserSession;
import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
@ -573,17 +575,26 @@ public class Param {
}
protected Component createDateField(Class javaClass, final ValueProperty valueProperty) {
UserSession userSession = userSessionSource.getUserSession();
boolean supportTimezones = false;
boolean dateOnly = false;
if (property != null) {
TemporalType tt = (TemporalType) property.getAnnotations().get(MetadataTools.TEMPORAL_ANN_NAME);
dateOnly = (tt == TemporalType.DATE);
Object ignoreUserTimeZone = property.getAnnotations().get(IgnoreUserTimeZone.class.getName());
supportTimezones = !dateOnly && !Boolean.TRUE.equals(ignoreUserTimeZone);
} else if (javaClass.equals(java.sql.Date.class)) {
dateOnly = true;
} else {
supportTimezones = true;
}
if (inExpr) {
if (property != null) {
TemporalType tt = (TemporalType) property.getAnnotations().get(MetadataTools.TEMPORAL_ANN_NAME);
if (tt == TemporalType.DATE) {
javaClass = java.sql.Date.class;
}
}
ListEditor listEditor = componentsFactory.createComponent(ListEditor.class);
ListEditor.ItemType itemType = java.sql.Date.class.equals(javaClass) ? ListEditor.ItemType.DATE : ListEditor.ItemType.DATETIME;
ListEditor.ItemType itemType = dateOnly ? ListEditor.ItemType.DATE : ListEditor.ItemType.DATETIME;
listEditor.setItemType(itemType);
if (userSession.getTimeZone() != null && supportTimezones) {
listEditor.setTimeZone(userSession.getTimeZone());
}
initListEditor(listEditor, valueProperty);
return listEditor;
}
@ -592,15 +603,7 @@ public class Param {
DateField.Resolution resolution;
String formatStr;
boolean dateOnly = false;
if (property != null) {
TemporalType tt = (TemporalType) property.getAnnotations().get(MetadataTools.TEMPORAL_ANN_NAME);
dateOnly = (tt == TemporalType.DATE);
} else if (javaClass.equals(java.sql.Date.class)) {
dateOnly = true;
}
Messages messages = AppBeans.get(Messages.NAME);
if (dateOnly) {
resolution = com.haulmont.cuba.gui.components.DateField.Resolution.DAY;
formatStr = messages.getMainMessage("dateFormat");
@ -610,6 +613,9 @@ public class Param {
}
dateField.setResolution(resolution);
dateField.setDateFormat(formatStr);
if (userSession.getTimeZone() != null && supportTimezones) {
dateField.setTimeZone(userSession.getTimeZone());
}
dateField.addValueChangeListener(e -> _setValue(e.getValue(), valueProperty));

View File

@ -20,6 +20,7 @@ import com.haulmont.cuba.gui.components.*;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import java.util.function.Supplier;
/**
@ -82,4 +83,7 @@ public interface ListEditorDelegate {
void setEditorParamsSupplier(Supplier<Map<String, Object>> paramsSupplier);
Supplier<Map<String, Object>> getEditorParamsSupplier();
void setTimeZone(TimeZone timeZone);
TimeZone getTimeZone();
}

View File

@ -64,6 +64,7 @@ public class ListEditorDelegateImpl implements ListEditorDelegate {
protected String entityJoinClause;
protected String entityWhereClause;
protected Class<? extends Enum> enumClass;
protected TimeZone timeZone;
protected TextField displayValuesField;
protected HBoxLayout layout;
@ -106,6 +107,7 @@ public class ListEditorDelegateImpl implements ListEditorDelegate {
params.put("entityWhereClause", entityWhereClause);
params.put("values", getValue());
params.put("editable", editable);
params.put("timeZone", timeZone);
if (editorParamsSupplier != null) {
Map<String, Object> additionalParams = getEditorParamsSupplier().get();
@ -173,7 +175,7 @@ public class ListEditorDelegateImpl implements ListEditorDelegate {
}
} else {
captions = ((List<Object>) newValue).stream()
.map(o -> ListEditorHelper.getValueCaption(o, itemType))
.map(o -> ListEditorHelper.getValueCaption(o, itemType, timeZone))
.collect(Collectors.toList());
}
strValue = Joiner.on(", ").join(captions);
@ -359,4 +361,14 @@ public class ListEditorDelegateImpl implements ListEditorDelegate {
public Supplier<Map<String, Object>> getEditorParamsSupplier() {
return editorParamsSupplier;
}
@Override
public void setTimeZone(TimeZone timeZone) {
this.timeZone = timeZone;
}
@Override
public TimeZone getTimeZone() {
return timeZone;
}
}

View File

@ -18,13 +18,17 @@ package com.haulmont.cuba.gui.components.listeditor;
import com.haulmont.chile.core.datatypes.Datatype;
import com.haulmont.chile.core.datatypes.Datatypes;
import com.haulmont.chile.core.datatypes.TimeZoneAwareDatatype;
import com.haulmont.chile.core.model.Instance;
import com.haulmont.cuba.core.global.AppBeans;
import com.haulmont.cuba.core.global.Messages;
import com.haulmont.cuba.core.global.UserSessionSource;
import com.haulmont.cuba.gui.components.ListEditor;
import com.haulmont.cuba.security.global.UserSession;
import java.math.BigDecimal;
import java.util.Date;
import java.util.TimeZone;
import static com.haulmont.cuba.gui.components.ListEditor.ItemType.*;
@ -32,7 +36,7 @@ import static com.haulmont.cuba.gui.components.ListEditor.ItemType.*;
*/
public class ListEditorHelper {
public static String getValueCaption(Object v, ListEditor.ItemType itemType) {
public static String getValueCaption(Object v, ListEditor.ItemType itemType, TimeZone timeZone) {
if (v == null)
return null;
switch (itemType) {
@ -46,7 +50,13 @@ public class ListEditorHelper {
case DATE:
return Datatypes.getNN(java.sql.Date.class).format(v);
case DATETIME:
return Datatypes.getNN(Date.class).format(v);
UserSessionSource userSessionSource = AppBeans.get(UserSessionSource.NAME);
UserSession userSession = userSessionSource.getUserSession();
if (timeZone != null) {
return ((TimeZoneAwareDatatype)Datatypes.getNN(Date.class)).format(v, userSession.getLocale(), timeZone);
} else {
return Datatypes.getNN(Date.class).format(v, userSession.getLocale());
}
case INTEGER:
return Datatypes.getNN(Integer.class).format(v);
case LONG:

View File

@ -89,6 +89,9 @@ public class ListEditorPopupWindow extends AbstractWindow implements ListEditorW
@WindowParam
protected Boolean editable;
@WindowParam
protected TimeZone timeZone;
@Inject
protected Metadata metadata;
@ -138,7 +141,7 @@ public class ListEditorPopupWindow extends AbstractWindow implements ListEditorW
}
valuesMap = values.stream()
.collect(Collectors.toMap(Function.identity(), o -> ListEditorHelper.getValueCaption(o, itemType)));
.collect(Collectors.toMap(Function.identity(), o -> ListEditorHelper.getValueCaption(o, itemType, timeZone)));
for (Map.Entry<Object, String> entry : valuesMap.entrySet()) {
addValueToLayout(entry.getKey(), entry.getValue());
@ -224,7 +227,7 @@ public class ListEditorPopupWindow extends AbstractWindow implements ListEditorW
if (value != null) {
componentForAdding.setValue(null);
if (!valueExists(value)) {
addValueToLayout(value, ListEditorHelper.getValueCaption(value, itemType));
addValueToLayout(value, ListEditorHelper.getValueCaption(value, itemType, timeZone));
}
}
}
@ -284,7 +287,7 @@ public class ListEditorPopupWindow extends AbstractWindow implements ListEditorW
componentForEntity.addValueChangeListener(e -> {
Entity selectedEntity = (Entity) e.getValue();
if (selectedEntity != null && !valueExists(selectedEntity)) {
this.addValueToLayout(selectedEntity, ListEditorHelper.getValueCaption(selectedEntity, itemType));
this.addValueToLayout(selectedEntity, ListEditorHelper.getValueCaption(selectedEntity, itemType, timeZone));
}
componentForEntity.setValue(null);
});
@ -294,6 +297,9 @@ public class ListEditorPopupWindow extends AbstractWindow implements ListEditorW
protected DateField createComponentForDate(DateField.Resolution resolution) {
DateField dateField = componentsFactory.createComponent(DateField.class);
dateField.setResolution(resolution);
if (timeZone != null) {
dateField.setTimeZone(timeZone);
}
return dateField;
}
@ -302,7 +308,7 @@ public class ListEditorPopupWindow extends AbstractWindow implements ListEditorW
lookupField.addValueChangeListener(e -> {
Object selectedValue = e.getValue();
if (selectedValue != null) {
this.addValueToLayout(selectedValue, ListEditorHelper.getValueCaption(selectedValue, itemType));
this.addValueToLayout(selectedValue, ListEditorHelper.getValueCaption(selectedValue, itemType, timeZone));
}
lookupField.setValue(null);
});

View File

@ -28,6 +28,7 @@ import org.apache.commons.lang.StringUtils;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.TimeZone;
import java.util.function.Supplier;
public class WebListEditor extends WebAbstractField<WebListEditor.CubaListEditor> implements ListEditor {
@ -243,4 +244,14 @@ public class WebListEditor extends WebAbstractField<WebListEditor.CubaListEditor
public Supplier<Map<String, Object>> getEditorParamsSupplier() {
return delegate.getEditorParamsSupplier();
}
@Override
public void setTimeZone(TimeZone timeZone) {
delegate.setTimeZone(timeZone);
}
@Override
public TimeZone getTimeZone() {
return delegate.getTimeZone();
}
}