PL-8764 Create CustomValueCollectionDatasource, CustomValueGroupDatasource, CustomValueHierarchicalDatasource

This commit is contained in:
Konstantin Krivopustov 2017-03-31 17:45:10 +04:00
parent cc83291e07
commit bfb275d606
5 changed files with 248 additions and 23 deletions

View File

@ -527,34 +527,85 @@ public class DsBuilder {
return datasource;
}
/**
* Build a {@link ValueCollectionDatasourceImpl} with the standard implementation.
*/
public ValueCollectionDatasourceImpl buildValuesCollectionDatasource() {
ValueCollectionDatasourceImpl datasource = createValueCollectionDatasource();
datasource.setup(dsContext, dataSupplier, id, metaClass, null);
if (maxResults > 0)
datasource.setMaxResults(maxResults);
datasource.setSoftDeletion(softDeletion);
registerDatasource(datasource);
return datasource;
try {
ValueCollectionDatasourceImpl datasource = dsClass == null ?
createValueCollectionDatasource() : (ValueCollectionDatasourceImpl) dsClass.newInstance();
datasource.setup(dsContext, dataSupplier, id, metaClass, null);
if (maxResults > 0)
datasource.setMaxResults(maxResults);
datasource.setSoftDeletion(softDeletion);
registerDatasource(datasource);
return datasource;
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
/**
* Build a {@link ValueCollectionDatasourceImpl} with the specified implementation class.
*/
@SuppressWarnings("unchecked")
public <T> T buildValuesCollectionDatasource(Class<T> datasourceClass) {
setDsClass(datasourceClass);
return (T) buildValuesCollectionDatasource();
}
/**
* Build a {@link ValueGroupDatasourceImpl} with the standard implementation.
*/
public ValueGroupDatasourceImpl buildValuesGroupDatasource() {
ValueGroupDatasourceImpl datasource = createValueGroupDatasource();
datasource.setup(dsContext, dataSupplier, id, metaClass, null);
if (maxResults > 0)
datasource.setMaxResults(maxResults);
datasource.setSoftDeletion(softDeletion);
registerDatasource(datasource);
return datasource;
try {
ValueGroupDatasourceImpl datasource = dsClass == null ?
createValueGroupDatasource() : (ValueGroupDatasourceImpl) dsClass.newInstance();
datasource.setup(dsContext, dataSupplier, id, metaClass, null);
if (maxResults > 0)
datasource.setMaxResults(maxResults);
datasource.setSoftDeletion(softDeletion);
registerDatasource(datasource);
return datasource;
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public ValueHierarchicalDatasourceImpl buildValuesHierarchicalDatasourceImpl() {
ValueHierarchicalDatasourceImpl datasource = createValueHierarchicalDatasource();
datasource.setup(dsContext, dataSupplier, id, metaClass, null);
if (maxResults > 0)
datasource.setMaxResults(maxResults);
datasource.setSoftDeletion(softDeletion);
registerDatasource(datasource);
return datasource;
/**
* Build a {@link ValueGroupDatasourceImpl} with the specified implementation class.
*/
@SuppressWarnings("unchecked")
public <T> T buildValuesGroupDatasource(Class<T> datasourceClass) {
setDsClass(datasourceClass);
return (T) buildValuesGroupDatasource();
}
/**
* Build a {@link ValueHierarchicalDatasourceImpl} with the standard implementation.
*/
public ValueHierarchicalDatasourceImpl buildValuesHierarchicalDatasource() {
try {
ValueHierarchicalDatasourceImpl datasource = dsClass == null ?
createValueHierarchicalDatasource() : (ValueHierarchicalDatasourceImpl) dsClass.newInstance();
datasource.setup(dsContext, dataSupplier, id, metaClass, null);
if (maxResults > 0)
datasource.setMaxResults(maxResults);
datasource.setSoftDeletion(softDeletion);
registerDatasource(datasource);
return datasource;
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
/**
* Build a {@link ValueHierarchicalDatasourceImpl} with the specified implementation class.
*/
@SuppressWarnings("unchecked")
public <T> T buildValuesHierarchicalDatasource(Class<T> datasourceClass) {
setDsClass(datasourceClass);
return (T) buildValuesHierarchicalDatasource();
}
private void registerDatasource(Datasource datasource) {

View File

@ -0,0 +1,58 @@
/*
* Copyright (c) 2008-2017 Haulmont.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.haulmont.cuba.gui.data.impl;
import com.haulmont.cuba.core.entity.KeyValueEntity;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import java.util.Collection;
import java.util.Map;
/**
* Base class for custom implementations of {@link ValueDatasource} containing a plain collection.
* <ul>
* <li>In a subclass, implement the {@link #getEntities(Map)} method and return a collection of {@link KeyValueEntity} from it.
* <li>Register your subclass in the {@code datasourceClass} attribute of the datasource XML element.
* </ul>
* @see CustomValueGroupDatasource
* @see CustomValueHierarchicalDatasource
*/
public abstract class CustomValueCollectionDatasource extends ValueCollectionDatasourceImpl {
/**
* Callback method to be implemented in subclasses.
* @param params datasource parameters, as described in {@link CollectionDatasource#refresh(java.util.Map)}
* @return collection of entities to populate the datasource
*/
protected abstract Collection<KeyValueEntity> getEntities(Map<String, Object> params);
@Override
protected void loadData(Map<String, Object> params) {
Collection<KeyValueEntity> entities = getEntities(params);
detachListener(data.values());
data.clear();
if (entities != null) {
for (KeyValueEntity entity : entities) {
data.put(entity.getId(), entity);
attachListener(entity);
entity.setMetaClass(metaClass);
}
}
}
}

View File

@ -0,0 +1,58 @@
/*
* Copyright (c) 2008-2017 Haulmont.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.haulmont.cuba.gui.data.impl;
import com.haulmont.cuba.core.entity.KeyValueEntity;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import java.util.Collection;
import java.util.Map;
/**
* Base class for custom implementations of {@link ValueDatasource} containing a grouped collection.
* <ul>
* <li>In a subclass, implement the {@link #getEntities(Map)} method and return a collection of {@link KeyValueEntity} from it.
* <li>Register your subclass in the {@code datasourceClass} attribute of the datasource XML element.
* </ul>
* @see CustomValueCollectionDatasource
* @see CustomValueHierarchicalDatasource
*/
public abstract class CustomValueGroupDatasource extends ValueGroupDatasourceImpl {
/**
* Callback method to be implemented in subclasses.
* @param params datasource parameters, as described in {@link CollectionDatasource#refresh(java.util.Map)}
* @return collection of entities to populate the datasource
*/
protected abstract Collection<KeyValueEntity> getEntities(Map<String, Object> params);
@Override
protected void loadData(Map<String, Object> params) {
Collection<KeyValueEntity> entities = getEntities(params);
detachListener(data.values());
data.clear();
if (entities != null) {
for (KeyValueEntity entity : entities) {
data.put(entity.getId(), entity);
attachListener(entity);
entity.setMetaClass(metaClass);
}
}
}
}

View File

@ -0,0 +1,58 @@
/*
* Copyright (c) 2008-2017 Haulmont.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.haulmont.cuba.gui.data.impl;
import com.haulmont.cuba.core.entity.KeyValueEntity;
import com.haulmont.cuba.gui.data.CollectionDatasource;
import java.util.Collection;
import java.util.Map;
/**
* Base class for custom implementations of {@link ValueDatasource} containing a hierarchical collection.
* <ul>
* <li>In a subclass, implement the {@link #getEntities(Map)} method and return a collection of {@link KeyValueEntity} from it.
* <li>Register your subclass in the {@code datasourceClass} attribute of the datasource XML element.
* </ul>
* @see CustomValueCollectionDatasource
* @see CustomValueGroupDatasource
*/
public abstract class CustomValueHierarchicalDatasource extends ValueHierarchicalDatasourceImpl {
/**
* Callback method to be implemented in subclasses.
* @param params datasource parameters, as described in {@link CollectionDatasource#refresh(java.util.Map)}
* @return collection of entities to populate the datasource
*/
protected abstract Collection<KeyValueEntity> getEntities(Map<String, Object> params);
@Override
protected void loadData(Map<String, Object> params) {
Collection<KeyValueEntity> entities = getEntities(params);
detachListener(data.values());
data.clear();
if (entities != null) {
for (KeyValueEntity entity : entities) {
data.put(entity.getId(), entity);
attachListener(entity);
entity.setMetaClass(metaClass);
}
}
}
}

View File

@ -506,7 +506,7 @@ public class DsContextLoader {
String id = getDatasourceId(element);
builder.reset().setMetaClass(metadata.getClassNN(KeyValueEntity.class)).setId(id);
ValueHierarchicalDatasourceImpl datasource = builder.buildValuesHierarchicalDatasourceImpl();
ValueHierarchicalDatasourceImpl datasource = builder.buildValuesHierarchicalDatasource();
String maxResults = element.attributeValue("maxResults");
if (!StringUtils.isEmpty(maxResults))