From 0207423731a791655187cda52836fc5367b6fa46 Mon Sep 17 00:00:00 2001 From: Fortunato Maldonado Date: Wed, 16 Aug 2023 15:57:37 -0600 Subject: [PATCH] LPS-190101 Create Date filters from FDSView --- .../renderer/FDSViewFragmentRenderer.java | 136 ++++++++++++++++++ .../resources/js/fds_view/Filters.tsx | 5 +- 2 files changed, 140 insertions(+), 1 deletion(-) diff --git a/modules/apps/frontend-data-set/frontend-data-set-views-web/src/main/java/com/liferay/frontend/data/set/views/web/internal/fragment/renderer/FDSViewFragmentRenderer.java b/modules/apps/frontend-data-set/frontend-data-set-views-web/src/main/java/com/liferay/frontend/data/set/views/web/internal/fragment/renderer/FDSViewFragmentRenderer.java index 942010950a558c..050e0409c6630f 100644 --- a/modules/apps/frontend-data-set/frontend-data-set-views-web/src/main/java/com/liferay/frontend/data/set/views/web/internal/fragment/renderer/FDSViewFragmentRenderer.java +++ b/modules/apps/frontend-data-set/frontend-data-set-views-web/src/main/java/com/liferay/frontend/data/set/views/web/internal/fragment/renderer/FDSViewFragmentRenderer.java @@ -22,6 +22,7 @@ import com.liferay.portal.kernel.dao.orm.QueryUtil; import com.liferay.portal.kernel.feature.flag.FeatureFlagManagerUtil; import com.liferay.portal.kernel.json.JSONArray; +import com.liferay.portal.kernel.json.JSONFactory; import com.liferay.portal.kernel.json.JSONObject; import com.liferay.portal.kernel.json.JSONUtil; import com.liferay.portal.kernel.language.Language; @@ -47,11 +48,15 @@ import java.io.PrintWriter; import java.io.Writer; +import java.sql.Timestamp; + import java.util.ArrayList; import java.util.Arrays; +import java.util.Calendar; import java.util.Collection; import java.util.Collections; import java.util.Comparator; +import java.util.Date; import java.util.List; import java.util.Locale; import java.util.Map; @@ -225,6 +230,10 @@ private String _buildFragmentHTML( componentDescriptor, HashMapBuilder.put( "apiURL", _getAPIURL(fdsEntryObjectEntry, httpServletRequest) + ).put( + "filters", + _getFiltersJSONArray( + fdsViewObjectDefinition, fdsViewObjectEntry) ).put( "id", "FDS_" + fragmentRendererContext.getFragmentElementId() ).put( @@ -276,6 +285,50 @@ private String _getAPIURL( return _interpolateURL(sb.toString(), httpServletRequest); } + private JSONObject _getDateJSONObject(Object fdsDateFilterMax) { + JSONObject jsonObject = _jsonFactory.createJSONObject(); + + Timestamp fdsDateFilterMaxTimestamp = (Timestamp)fdsDateFilterMax; + + Date fdsDateFilterMaxDate = new Date( + fdsDateFilterMaxTimestamp.getTime()); + + Calendar calendar = Calendar.getInstance(); + + calendar.setTime(fdsDateFilterMaxDate); + + jsonObject.put( + "day", calendar.get(Calendar.DATE) + ).put( + "month", calendar.get(Calendar.MONTH) + 1 + ).put( + "year", calendar.get(Calendar.YEAR) + ); + + return jsonObject; + } + + private JSONObject _getFDSDateFilterJSONObject( + ObjectEntry fdsDateFilterObjectEntry) { + + Map fdsDateFilterProperties = + fdsDateFilterObjectEntry.getProperties(); + + return JSONUtil.put( + "entityFieldType", fdsDateFilterProperties.get("type") + ).put( + "id", fdsDateFilterProperties.get("fieldName") + ).put( + "label", fdsDateFilterProperties.get("name") + ).put( + "max", _getDateJSONObject(fdsDateFilterProperties.get("to")) + ).put( + "min", _getDateJSONObject(fdsDateFilterProperties.get("from")) + ).put( + "type", "dateRange" + ); + } + private JSONArray _getFieldsJSONArray( FragmentEntryLink fragmentEntryLink, ObjectDefinition objectDefinition, ObjectEntry objectEntry) @@ -338,6 +391,68 @@ private JSONArray _getFieldsJSONArray( }); } + private JSONArray _getFiltersJSONArray( + ObjectDefinition fdsViewObjectDefinition, + ObjectEntry fdsViewObjectEntry) + throws Exception { + + Collection fdsFilters = new ArrayList<>(); + + Map fdsViewProperties = + fdsViewObjectEntry.getProperties(); + + String fdsFiltersOrder = (String)fdsViewProperties.get( + "fdsFiltersOrder"); + + fdsFilters.addAll( + _getRelatedObjectEntries( + fdsViewObjectDefinition, fdsViewObjectEntry, + "fdsViewFDSDateFilterRelationship")); + + fdsFilters.addAll( + _getRelatedObjectEntries( + fdsViewObjectDefinition, fdsViewObjectEntry, + "fdsViewFDSDynamicFilterRelationship")); + + Collection fdsFiltersObjectEntries = + _sortObjectEntriesByIdsList(fdsFilters, fdsFiltersOrder); + + if ((fdsFiltersObjectEntries == null) || + fdsFiltersObjectEntries.isEmpty()) { + + return _jsonFactory.createJSONArray(); + } + + try { + return JSONUtil.toJSONArray( + fdsFiltersObjectEntries, + (ObjectEntry fdsFilterObjectEntry) -> { + Map fdsFiltersProperties = + fdsFilterObjectEntry.getProperties(); + + if (fdsFiltersProperties.get( + "type" + ).equals( + "date" + )) { + + return _getFDSDateFilterJSONObject( + fdsFilterObjectEntry); + } + + return null; + }); + } + catch (Exception exception) { + if (_log.isWarnEnabled()) { + _log.warn( + "Unable to generate FDS filters from FDSView", exception); + } + + return _jsonFactory.createJSONArray(); + } + } + private ObjectEntry _getObjectEntry( long companyId, String externalReferenceCode, ObjectDefinition objectDefinition) @@ -423,6 +538,24 @@ private String _interpolateURL( return apiUrl; } + private Collection _sortObjectEntriesByIdsList( + Collection objectEntries, String idsOrderString) { + + List idsOrder = ListUtil.toList( + Arrays.asList(StringUtil.split(idsOrderString, StringPool.COMMA)), + Long::parseLong); + + List objectEntriesList = new ArrayList<>(objectEntries); + + Collections.sort( + objectEntriesList, + Comparator.comparing( + ObjectEntry::getId, + Comparator.comparingInt(idsOrder::indexOf))); + + return objectEntriesList; + } + private static final Log _log = LogFactoryUtil.getLog( FDSViewFragmentRenderer.class); @@ -432,6 +565,9 @@ private String _interpolateURL( @Reference private FragmentEntryConfigurationParser _fragmentEntryConfigurationParser; + @Reference + private JSONFactory _jsonFactory; + @Reference private Language _language; diff --git a/modules/apps/frontend-data-set/frontend-data-set-views-web/src/main/resources/META-INF/resources/js/fds_view/Filters.tsx b/modules/apps/frontend-data-set/frontend-data-set-views-web/src/main/resources/META-INF/resources/js/fds_view/Filters.tsx index eee62d9d21ef11..890f4ee890b19d 100644 --- a/modules/apps/frontend-data-set/frontend-data-set-views-web/src/main/resources/META-INF/resources/js/fds_view/Filters.tsx +++ b/modules/apps/frontend-data-set/frontend-data-set-views-web/src/main/resources/META-INF/resources/js/fds_view/Filters.tsx @@ -167,7 +167,10 @@ function AddFDSFilterModalContent({ let displayType: string = ''; let url: string = ''; - if (selectedField.format === 'date-time') { + if ( + selectedField.format === 'date-time' || + selectedField.format === 'date' + ) { url = API_URL.FDS_DATE_FILTERS; body = {