Last active
January 26, 2019 20:25
-
-
Save vegegoku/1de9eb474bd5fb4e866b36614477e709 to your computer and use it in GitHub Desktop.
Implementing a remote data loader in domino-ui
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface CanLoadItems<T> { | |
void load(LoadContext context, Consumer<IsSummaries<T>> responseConsumer); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LoadContext { | |
private String sortProperty; | |
private String sortDirection; | |
private int activePage; | |
private Map<String, String> searchProperties; | |
public LoadContext(String sortProperty, String sortDirection, int activePage, Map<String, String> searchProperties) { | |
this.sortProperty = sortProperty; | |
this.sortDirection = sortDirection; | |
this.activePage = activePage; | |
this.searchProperties = searchProperties; | |
} | |
public String getSortProperty() { | |
return sortProperty; | |
} | |
public Map<String, String> getSearchProperties() { | |
return searchProperties; | |
} | |
public String getSortDirection() { | |
return sortDirection; | |
} | |
public int getActivePage() { | |
return activePage; | |
} | |
public void setSortProperty(String sortProperty) { | |
this.sortProperty = sortProperty; | |
} | |
public void setSortDirection(String sortDirection) { | |
this.sortDirection = sortDirection; | |
} | |
public void setActivePage(int activePage) { | |
this.activePage = activePage; | |
} | |
public void setSearchProperties(Map<String, String> searchProperties) { | |
this.searchProperties = searchProperties; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RemoteDataStore<T> implements DataStore<T> { | |
private List<StoreDataChangeListener<T>> listeners = new ArrayList<>(); | |
private HasPagination pagination; | |
private CanLoadItems<T> loader; | |
private String propertyName; | |
private String sortDirection; | |
private int activePage = 1; | |
private Map<String, String> searchProperties = new HashMap<>(); | |
private List<T> items; | |
public RemoteDataStore(CanLoadItems<T> loader) { | |
this.loader = loader; | |
} | |
@Override | |
public void onDataChanged(StoreDataChangeListener<T> dataChangeListener) { | |
listeners.add(dataChangeListener); | |
} | |
@Override | |
public void removeDataChangeListener(StoreDataChangeListener<T> dataChangeListener) { | |
listeners.remove(dataChangeListener); | |
} | |
@Override | |
public void load() { | |
loader.load(getLoadContext(), summaries -> { | |
this.items = summaries.get_embedded().getResourceList(); | |
if (nonNull(summaries.getPage())) { | |
pagination.updatePagesByTotalCount(summaries.getPage().getTotalElements(), summaries.getPage().getSize()); | |
pagination.gotoPage(summaries.getPage().getNumber() + 1, true); | |
} | |
fireUpdate(); | |
}); | |
} | |
private LoadContext getLoadContext() { | |
return new LoadContext(propertyName, sortDirection, activePage, searchProperties); | |
} | |
@Override | |
public void handleEvent(TableEvent event) { | |
switch (event.getType()) { | |
case TablePageChangeEvent.PAGINATION_EVENT: | |
activePage = pagination.activePage(); | |
load(); | |
break; | |
case SORT_EVENT: | |
propertyName = ((SortEvent<?>) event).getColumnConfig().getName(); | |
sortDirection = ((SortEvent<?>) event).getSortDirection().toString(); | |
load(); | |
break; | |
case SEARCH_EVENT: | |
searchProperties = new HashMap<>(); | |
List<Filter> filters = ((SearchEvent) event).getFilters(); | |
this.activePage = 1; | |
for (Filter filter : filters) { | |
if (!filter.getValues().isEmpty()) { | |
searchProperties.put(filter.getFieldName(), filter.getValues().get(0)); | |
} | |
} | |
load(); | |
break; | |
} | |
} | |
private void fireUpdate() { | |
listeners.forEach(dataChangeListener -> dataChangeListener.onDataChanged(new DataChangedEvent<>(items, items.size()))); | |
} | |
public HasPagination getPagination() { | |
return pagination; | |
} | |
public void setPagination(HasPagination pagination) { | |
this.pagination = pagination; | |
} | |
public void load(int pageNumber) { | |
this.activePage = pageNumber; | |
load(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment