mirror of
https://gitee.com/jmix/cuba.git
synced 2024-12-03 11:47:38 +08:00
Verbose usage of DataManager.commit in case we want to obtain committed instance by id #469
This commit is contained in:
parent
4414c56945
commit
275b5e939f
@ -22,8 +22,8 @@ import com.haulmont.cuba.core.app.DataService;
|
||||
import com.haulmont.cuba.core.entity.Entity;
|
||||
import com.haulmont.cuba.core.entity.KeyValueEntity;
|
||||
import com.haulmont.cuba.core.global.*;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import javax.inject.Inject;
|
||||
@ -92,8 +92,8 @@ public class DataManagerClientImpl implements DataManager {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Entity> commit(CommitContext context) {
|
||||
return dataService.commit(context);
|
||||
public EntitySet commit(CommitContext context) {
|
||||
return EntitySet.of(dataService.commit(context));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -133,7 +133,7 @@ public class DataManagerBean implements DataManager {
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public Set<Entity> commit(CommitContext context) {
|
||||
public EntitySet commit(CommitContext context) {
|
||||
Map<String, CommitContext> storeToContextMap = new TreeMap<>();
|
||||
Set<Entity> toRepeat = new HashSet<>();
|
||||
for (Entity entity : context.getCommitInstances()) {
|
||||
@ -206,7 +206,7 @@ public class DataManagerBean implements DataManager {
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
return EntitySet.of(result);
|
||||
}
|
||||
|
||||
protected void adjustState(Entity committedEntity) {
|
||||
|
@ -0,0 +1,61 @@
|
||||
/*
|
||||
* Copyright (c) 2008-2018 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 spec.cuba.core.data_manager
|
||||
|
||||
import com.haulmont.cuba.core.global.AppBeans
|
||||
import com.haulmont.cuba.core.global.CommitContext
|
||||
import com.haulmont.cuba.core.global.DataManager
|
||||
import com.haulmont.cuba.core.global.EntitySet
|
||||
import com.haulmont.cuba.testmodel.sales.Customer
|
||||
import com.haulmont.cuba.testmodel.sales.Order
|
||||
import com.haulmont.cuba.testsupport.TestContainer
|
||||
import org.junit.ClassRule
|
||||
import spock.lang.Shared
|
||||
import spock.lang.Specification
|
||||
|
||||
class DataManagerCommitTest extends Specification {
|
||||
|
||||
@Shared @ClassRule
|
||||
public TestContainer cont = TestContainer.Common.INSTANCE
|
||||
|
||||
private DataManager dataManager
|
||||
|
||||
void setup() {
|
||||
dataManager = AppBeans.get(DataManager)
|
||||
}
|
||||
|
||||
def "usage"() {
|
||||
|
||||
Customer customer = new Customer(name: 'Smith')
|
||||
Order order = new Order(number: '111', customer: customer)
|
||||
|
||||
def cc = new CommitContext().addInstanceToCommit(customer).addInstanceToCommit(order)
|
||||
expect:
|
||||
|
||||
EntitySet committed = dataManager.commit(cc)
|
||||
|
||||
def customer1 = committed.get(Customer, customer.id)
|
||||
customer1 == customer
|
||||
|
||||
def customer2 = dataManager.commit(cc).get(customer)
|
||||
customer2 == customer
|
||||
|
||||
cleanup:
|
||||
|
||||
cont.deleteRecord(order, customer)
|
||||
}
|
||||
}
|
@ -112,7 +112,7 @@ public interface DataManager {
|
||||
* @param context {@link com.haulmont.cuba.core.global.CommitContext} object, containing committing entities and other information
|
||||
* @return set of committed instances
|
||||
*/
|
||||
Set<Entity> commit(CommitContext context);
|
||||
EntitySet commit(CommitContext context);
|
||||
|
||||
/**
|
||||
* Commits the entity to the data store.
|
||||
|
117
modules/global/src/com/haulmont/cuba/core/global/EntitySet.java
Normal file
117
modules/global/src/com/haulmont/cuba/core/global/EntitySet.java
Normal file
@ -0,0 +1,117 @@
|
||||
/*
|
||||
* Copyright (c) 2008-2018 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.core.global;
|
||||
|
||||
import com.google.common.collect.ForwardingSet;
|
||||
import com.haulmont.bali.util.Preconditions;
|
||||
import com.haulmont.cuba.core.entity.Entity;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* Implementation of {@code Set<Entity>} with convenient methods for getting entities by a prototype instance
|
||||
* or by a class and id.
|
||||
*
|
||||
* @see #get(Entity)
|
||||
* @see #get(Class, Object)
|
||||
* @see #optional(Entity)
|
||||
* @see #optional(Class, Object)
|
||||
*/
|
||||
public class EntitySet extends ForwardingSet implements Serializable {
|
||||
|
||||
private Set<? extends Entity> entities;
|
||||
|
||||
public EntitySet(Set<? extends Entity> entities) {
|
||||
this.entities = entities;
|
||||
}
|
||||
|
||||
public EntitySet(Collection<? extends Entity> entities) {
|
||||
this.entities = new HashSet<>(entities);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the {@code EntitySet} wrapping an existing set.
|
||||
*/
|
||||
public static EntitySet of(Set<? extends Entity> entities) {
|
||||
return new EntitySet(entities);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the {@code EntitySet} by copying the given collection to the internal set.
|
||||
*/
|
||||
public static EntitySet of(Collection<? extends Entity> entities) {
|
||||
return new EntitySet(entities);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the entity wrapped in {@code Optional} if it exists in the set.
|
||||
*
|
||||
* @param entityClass class of entity
|
||||
* @param entityId entity id
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends Entity<K>, K> Optional<T> optional(Class<T> entityClass, K entityId) {
|
||||
Preconditions.checkNotNullArgument(entityClass, "entityClass is null");
|
||||
Preconditions.checkNotNullArgument(entityId, "entityId is null");
|
||||
return (Optional<T>) entities.stream()
|
||||
.filter(entity -> entityClass.equals(entity.getClass()) && entity.getId().equals(entityId))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the entity wrapped in {@code Optional} if it exists in the set.
|
||||
*
|
||||
* @param prototype a prototype instance whose class and id are used to look up an entity in the set.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends Entity> Optional<T> optional(T prototype) {
|
||||
Preconditions.checkNotNullArgument(prototype, "prototype entity is null");
|
||||
return (Optional<T>) optional(prototype.getClass(), prototype.getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the entity if it exists in the set.
|
||||
*
|
||||
* @param entityClass class of entity
|
||||
* @param entityId entity id
|
||||
* @throws IllegalArgumentException if the entity not found
|
||||
*/
|
||||
public <T extends Entity<K>, K> T get(Class<T> entityClass, K entityId) {
|
||||
return optional(entityClass, entityId).orElseThrow(() -> new IllegalArgumentException("Entity not found"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the entity if it exists in the set.
|
||||
*
|
||||
* @param prototype a prototype instance whose class and id are used to look up an entity in the set.
|
||||
* @throws IllegalArgumentException if the entity not found
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T extends Entity> T get(T prototype) {
|
||||
Preconditions.checkNotNullArgument(prototype, "prototype entity is null");
|
||||
return (T) get(prototype.getClass(), prototype.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Set delegate() {
|
||||
return entities;
|
||||
}
|
||||
}
|
@ -0,0 +1,62 @@
|
||||
/*
|
||||
* Copyright (c) 2008-2018 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.core.global;
|
||||
|
||||
import com.haulmont.cuba.core.entity.Server;
|
||||
import com.haulmont.cuba.security.entity.User;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class EntitySetTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
Server server1 = new Server(); Server server2 = new Server();
|
||||
Set<Server> set = new HashSet<>();
|
||||
set.add(server1); set.add(server2);
|
||||
|
||||
EntitySet entitySet = EntitySet.of(set);
|
||||
|
||||
assertSame(server1, entitySet.optional(Server.class, server1.getId()).orElse(null));
|
||||
assertFalse(entitySet.optional(Server.class, UUID.randomUUID()).isPresent());
|
||||
assertFalse(entitySet.optional(User.class, server1.getId()).isPresent());
|
||||
|
||||
assertSame(server1, entitySet.get(Server.class, server1.getId()));
|
||||
|
||||
try {
|
||||
entitySet.get(Server.class, UUID.randomUUID());
|
||||
fail();
|
||||
} catch (IllegalArgumentException e) {
|
||||
// ok
|
||||
}
|
||||
|
||||
assertSame(server1, entitySet.optional(server1).orElse(null));
|
||||
assertSame(server1, entitySet.get(server1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOfCollection() {
|
||||
Server server1 = new Server(); Server server2 = new Server();
|
||||
List<Server> list = Arrays.asList(server1, server2);
|
||||
|
||||
EntitySet entitySet = EntitySet.of(list);
|
||||
assertSame(server1, entitySet.optional(Server.class, server1.getId()).orElse(null));
|
||||
}
|
||||
}
|
@ -25,7 +25,6 @@ import com.haulmont.cuba.gui.data.DataSupplier;
|
||||
import javax.annotation.Nonnull;
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
public class GenericDataSupplier implements DataSupplier {
|
||||
|
||||
@ -86,7 +85,7 @@ public class GenericDataSupplier implements DataSupplier {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Entity> commit(CommitContext context) {
|
||||
public EntitySet commit(CommitContext context) {
|
||||
return getDataManager().commit(context);
|
||||
}
|
||||
|
||||
|
@ -41,7 +41,7 @@ public class TestDataSupplier implements DataSupplier {
|
||||
CommitValidator commitValidator;
|
||||
|
||||
@Override
|
||||
public Set<Entity> commit(CommitContext context) {
|
||||
public EntitySet commit(CommitContext context) {
|
||||
commitCount++;
|
||||
|
||||
if (commitValidator != null)
|
||||
@ -51,7 +51,7 @@ public class TestDataSupplier implements DataSupplier {
|
||||
for (Entity entity : context.getCommitInstances()) {
|
||||
result.add(entity);
|
||||
}
|
||||
return result;
|
||||
return EntitySet.of(result);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
Loading…
Reference in New Issue
Block a user