From fb7cc7a1e0b446eca2a3654496d37748b4ec1ba1 Mon Sep 17 00:00:00 2001 From: Teo Sarca Date: Mon, 27 Feb 2017 14:48:29 +0200 Subject: [PATCH] #181 LookupDescriptor convenient implementations --- .../descriptor/ListLookupDescriptor.java | 167 ++++++++++++++++++ .../window/descriptor/LookupDescriptor.java | 4 +- .../SimpleLookupDescriptorTemplate.java | 120 +++++++++++++ 3 files changed, 289 insertions(+), 2 deletions(-) create mode 100644 metasfresh-webui-api/src/main/java/de/metas/ui/web/window/descriptor/ListLookupDescriptor.java create mode 100644 metasfresh-webui-api/src/main/java/de/metas/ui/web/window/descriptor/SimpleLookupDescriptorTemplate.java diff --git a/metasfresh-webui-api/src/main/java/de/metas/ui/web/window/descriptor/ListLookupDescriptor.java b/metasfresh-webui-api/src/main/java/de/metas/ui/web/window/descriptor/ListLookupDescriptor.java new file mode 100644 index 000000000..20f1ba903 --- /dev/null +++ b/metasfresh-webui-api/src/main/java/de/metas/ui/web/window/descriptor/ListLookupDescriptor.java @@ -0,0 +1,167 @@ +package de.metas.ui.web.window.descriptor; + +import java.util.Set; +import java.util.function.Function; + +import org.adempiere.util.Check; + +import com.google.common.base.MoreObjects; +import com.google.common.collect.ImmutableSet; + +import de.metas.ui.web.window.datatypes.LookupValue; +import de.metas.ui.web.window.datatypes.LookupValuesList; +import de.metas.ui.web.window.model.lookup.LookupDataSourceContext; +import de.metas.ui.web.window.model.lookup.LookupDataSourceFetcher; + +/* + * #%L + * metasfresh-webui-api + * %% + * Copyright (C) 2017 metas GmbH + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * . + * #L% + */ + +/** + * {@link LookupDescriptor} and {@link LookupDataSourceFetcher} implementation which is backed by a given {@link LookupValuesList} supplier. + * + * @author metas-dev + * + */ +public final class ListLookupDescriptor extends SimpleLookupDescriptorTemplate +{ + public static final Builder builder() + { + return new Builder(); + } + + private final Function lookupValues; + private final boolean numericKey; + + private final Set dependsOnFieldNames; + private final Function filteredLookupValues; + + private ListLookupDescriptor(final Builder builder) + { + Check.assumeNotNull(builder.lookupValues, "Parameter builder.lookupValues is not null"); + + lookupValues = builder.lookupValues; + numericKey = builder.numericKey; + + dependsOnFieldNames = builder.dependsOnFieldNames == null ? ImmutableSet.of() : ImmutableSet.copyOf(builder.dependsOnFieldNames); + + if (builder.filteredLookupValues != null) + { + filteredLookupValues = builder.filteredLookupValues; + } + else + { + filteredLookupValues = evalCtx -> { + final LookupValuesList list = lookupValues.apply(evalCtx); + final LookupValue lookupValue = list.getById(evalCtx.getIdToFilter()); + return lookupValue; + }; + } + } + + @Override + public String toString() + { + return MoreObjects.toStringHelper(this) + .add("lookupValues", lookupValues) + .toString(); + } + + @Override + public boolean isNumericKey() + { + return numericKey; + } + + @Override + public Set getDependsOnFieldNames() + { + return dependsOnFieldNames; + } + + @Override + public LookupValue retrieveLookupValueById(final LookupDataSourceContext evalCtx) + { + return filteredLookupValues.apply(evalCtx); + } + + @Override + public LookupValuesList retrieveEntities(final LookupDataSourceContext evalCtx) + { + return lookupValues.apply(evalCtx); + } + + public static class Builder + { + private Function lookupValues; + private boolean numericKey; + + private Function filteredLookupValues; + private Set dependsOnFieldNames; + + private Builder() + { + } + + public ListLookupDescriptor build() + { + return new ListLookupDescriptor(this); + } + + public Builder setLookupValues(final boolean numericKey, final Function lookupValues) + { + this.numericKey = numericKey; + this.lookupValues = lookupValues; + return this; + } + + public Builder setIntegerLookupValues(final Function lookupValues) + { + setLookupValues(true, lookupValues); + return this; + } + + public Builder setStringLookupValues(final Function lookupValues) + { + setLookupValues(false, lookupValues); + return this; + } + + public Builder setDependsOnFieldNames(final Set dependsOnFieldNames) + { + this.dependsOnFieldNames = dependsOnFieldNames; + return this; + } + + public Builder setDependsOnFieldNames(final String[] dependsOnFieldNames) + { + this.dependsOnFieldNames = ImmutableSet.copyOf(dependsOnFieldNames); + return this; + } + + public Builder setFilteredLookupValues(final Function filteredLookupValues) + { + this.filteredLookupValues = filteredLookupValues; + return this; + } + + } +} diff --git a/metasfresh-webui-api/src/main/java/de/metas/ui/web/window/descriptor/LookupDescriptor.java b/metasfresh-webui-api/src/main/java/de/metas/ui/web/window/descriptor/LookupDescriptor.java index 6000a3098..e8031e5ad 100644 --- a/metasfresh-webui-api/src/main/java/de/metas/ui/web/window/descriptor/LookupDescriptor.java +++ b/metasfresh-webui-api/src/main/java/de/metas/ui/web/window/descriptor/LookupDescriptor.java @@ -31,6 +31,8 @@ public interface LookupDescriptor { + LookupDataSourceFetcher getLookupDataSourceFetcher(); + boolean isHighVolume(); LookupSource getLookupSourceType(); @@ -46,8 +48,6 @@ default Class getValueClass() return isNumericKey() ? IntegerLookupValue.class : StringLookupValue.class; } - LookupDataSourceFetcher getLookupDataSourceFetcher(); - default T cast(final Class lookupDescriptorClass) { @SuppressWarnings("unchecked") diff --git a/metasfresh-webui-api/src/main/java/de/metas/ui/web/window/descriptor/SimpleLookupDescriptorTemplate.java b/metasfresh-webui-api/src/main/java/de/metas/ui/web/window/descriptor/SimpleLookupDescriptorTemplate.java new file mode 100644 index 000000000..f2d520117 --- /dev/null +++ b/metasfresh-webui-api/src/main/java/de/metas/ui/web/window/descriptor/SimpleLookupDescriptorTemplate.java @@ -0,0 +1,120 @@ +package de.metas.ui.web.window.descriptor; + +import java.util.List; +import java.util.Set; + +import org.compiere.util.CCache.CCacheStats; + +import com.google.common.collect.ImmutableList; + +import de.metas.ui.web.window.datatypes.LookupValue; +import de.metas.ui.web.window.datatypes.LookupValuesList; +import de.metas.ui.web.window.descriptor.DocumentLayoutElementFieldDescriptor.LookupSource; +import de.metas.ui.web.window.model.lookup.LookupDataSourceContext; +import de.metas.ui.web.window.model.lookup.LookupDataSourceFetcher; + +/* + * #%L + * metasfresh-webui-api + * %% + * Copyright (C) 2017 metas GmbH + * %% + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as + * published by the Free Software Foundation, either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program. If not, see + * . + * #L% + */ + +/** + * Simple template implementation of {@link LookupDescriptor} and {@link LookupDataSourceFetcher}. + * + * @author metas-dev + * + */ +public abstract class SimpleLookupDescriptorTemplate implements LookupDescriptor, LookupDataSourceFetcher +{ + @Override + public final LookupDataSourceFetcher getLookupDataSourceFetcher() + { + return this; + } + + @Override + public final boolean isHighVolume() + { + // NOTE: method will never be called because isCached() == true + return false; + } + + @Override + public LookupSource getLookupSourceType() + { + return LookupSource.list; + } + + @Override + public boolean hasParameters() + { + return !getDependsOnFieldNames().isEmpty(); + } + + @Override + public abstract boolean isNumericKey(); + + @Override + public abstract Set getDependsOnFieldNames(); + + // + // + // + // ----------------------- + // + // + + @Override + public LookupDataSourceContext.Builder newContextForFetchingById(final Object id) + { + return LookupDataSourceContext.builderWithoutTableName(); + } + + @Override + public abstract LookupValue retrieveLookupValueById(LookupDataSourceContext evalCtx); + + @Override + public LookupDataSourceContext.Builder newContextForFetchingList() + { + return LookupDataSourceContext.builderWithoutTableName(); + } + + @Override + public abstract LookupValuesList retrieveEntities(LookupDataSourceContext evalCtx); + + @Override + public final String getCachePrefix() + { + // NOTE: method will never be called because isCached() == true + return null; + } + + @Override + public final boolean isCached() + { + return true; + } + + @Override + public List getCacheStats() + { + return ImmutableList.of(); + } +}