Skip to content

Commit

Permalink
LPS-190101 Create Date filters from FDSView
Browse files Browse the repository at this point in the history
  • Loading branch information
fortunatomaldonado committed Aug 23, 2023
1 parent 056330e commit 0207423
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -225,6 +230,10 @@ private String _buildFragmentHTML(
componentDescriptor,
HashMapBuilder.<String, Object>put(
"apiURL", _getAPIURL(fdsEntryObjectEntry, httpServletRequest)
).put(
"filters",
_getFiltersJSONArray(
fdsViewObjectDefinition, fdsViewObjectEntry)
).put(
"id", "FDS_" + fragmentRendererContext.getFragmentElementId()
).put(
Expand Down Expand Up @@ -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<String, Object> 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)
Expand Down Expand Up @@ -338,6 +391,68 @@ private JSONArray _getFieldsJSONArray(
});
}

private JSONArray _getFiltersJSONArray(
ObjectDefinition fdsViewObjectDefinition,
ObjectEntry fdsViewObjectEntry)
throws Exception {

Collection<ObjectEntry> fdsFilters = new ArrayList<>();

Map<String, Object> fdsViewProperties =
fdsViewObjectEntry.getProperties();

String fdsFiltersOrder = (String)fdsViewProperties.get(
"fdsFiltersOrder");

fdsFilters.addAll(
_getRelatedObjectEntries(
fdsViewObjectDefinition, fdsViewObjectEntry,
"fdsViewFDSDateFilterRelationship"));

fdsFilters.addAll(
_getRelatedObjectEntries(
fdsViewObjectDefinition, fdsViewObjectEntry,
"fdsViewFDSDynamicFilterRelationship"));

Collection<ObjectEntry> fdsFiltersObjectEntries =
_sortObjectEntriesByIdsList(fdsFilters, fdsFiltersOrder);

if ((fdsFiltersObjectEntries == null) ||
fdsFiltersObjectEntries.isEmpty()) {

return _jsonFactory.createJSONArray();
}

try {
return JSONUtil.toJSONArray(
fdsFiltersObjectEntries,
(ObjectEntry fdsFilterObjectEntry) -> {
Map<String, Object> 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)
Expand Down Expand Up @@ -423,6 +538,24 @@ private String _interpolateURL(
return apiUrl;
}

private Collection<ObjectEntry> _sortObjectEntriesByIdsList(
Collection<ObjectEntry> objectEntries, String idsOrderString) {

List<Long> idsOrder = ListUtil.toList(
Arrays.asList(StringUtil.split(idsOrderString, StringPool.COMMA)),
Long::parseLong);

List<ObjectEntry> 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);

Expand All @@ -432,6 +565,9 @@ private String _interpolateURL(
@Reference
private FragmentEntryConfigurationParser _fragmentEntryConfigurationParser;

@Reference
private JSONFactory _jsonFactory;

@Reference
private Language _language;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 0207423

Please sign in to comment.