-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#181 LookupDescriptor convenient implementations
- Loading branch information
Showing
3 changed files
with
289 additions
and
2 deletions.
There are no files selected for viewing
167 changes: 167 additions & 0 deletions
167
...fresh-webui-api/src/main/java/de/metas/ui/web/window/descriptor/ListLookupDescriptor.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
* <http://www.gnu.org/licenses/gpl-2.0.html>. | ||
* #L% | ||
*/ | ||
|
||
/** | ||
* {@link LookupDescriptor} and {@link LookupDataSourceFetcher} implementation which is backed by a given {@link LookupValuesList} supplier. | ||
* | ||
* @author metas-dev <dev@metasfresh.com> | ||
* | ||
*/ | ||
public final class ListLookupDescriptor extends SimpleLookupDescriptorTemplate | ||
{ | ||
public static final Builder builder() | ||
{ | ||
return new Builder(); | ||
} | ||
|
||
private final Function<LookupDataSourceContext, LookupValuesList> lookupValues; | ||
private final boolean numericKey; | ||
|
||
private final Set<String> dependsOnFieldNames; | ||
private final Function<LookupDataSourceContext, LookupValue> 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<String> 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<LookupDataSourceContext, LookupValuesList> lookupValues; | ||
private boolean numericKey; | ||
|
||
private Function<LookupDataSourceContext, LookupValue> filteredLookupValues; | ||
private Set<String> dependsOnFieldNames; | ||
|
||
private Builder() | ||
{ | ||
} | ||
|
||
public ListLookupDescriptor build() | ||
{ | ||
return new ListLookupDescriptor(this); | ||
} | ||
|
||
public Builder setLookupValues(final boolean numericKey, final Function<LookupDataSourceContext, LookupValuesList> lookupValues) | ||
{ | ||
this.numericKey = numericKey; | ||
this.lookupValues = lookupValues; | ||
return this; | ||
} | ||
|
||
public Builder setIntegerLookupValues(final Function<LookupDataSourceContext, LookupValuesList> lookupValues) | ||
{ | ||
setLookupValues(true, lookupValues); | ||
return this; | ||
} | ||
|
||
public Builder setStringLookupValues(final Function<LookupDataSourceContext, LookupValuesList> lookupValues) | ||
{ | ||
setLookupValues(false, lookupValues); | ||
return this; | ||
} | ||
|
||
public Builder setDependsOnFieldNames(final Set<String> dependsOnFieldNames) | ||
{ | ||
this.dependsOnFieldNames = dependsOnFieldNames; | ||
return this; | ||
} | ||
|
||
public Builder setDependsOnFieldNames(final String[] dependsOnFieldNames) | ||
{ | ||
this.dependsOnFieldNames = ImmutableSet.copyOf(dependsOnFieldNames); | ||
return this; | ||
} | ||
|
||
public Builder setFilteredLookupValues(final Function<LookupDataSourceContext, LookupValue> filteredLookupValues) | ||
{ | ||
this.filteredLookupValues = filteredLookupValues; | ||
return this; | ||
} | ||
|
||
} | ||
} |
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
120 changes: 120 additions & 0 deletions
120
...i-api/src/main/java/de/metas/ui/web/window/descriptor/SimpleLookupDescriptorTemplate.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
* <http://www.gnu.org/licenses/gpl-2.0.html>. | ||
* #L% | ||
*/ | ||
|
||
/** | ||
* Simple template implementation of {@link LookupDescriptor} and {@link LookupDataSourceFetcher}. | ||
* | ||
* @author metas-dev <dev@metasfresh.com> | ||
* | ||
*/ | ||
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<String> 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<CCacheStats> getCacheStats() | ||
{ | ||
return ImmutableList.of(); | ||
} | ||
} |