Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#302 refactorized repository component #345

Merged
merged 6 commits into from
Oct 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ configure(subprojects) {

configure([compileJava, compileTestJava]) {
options.encoding = 'UTF-8'
options.warnings = false
}

configurations {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ protected static String relationIdsToString(final KasperID sourceId, final Kaspe
return String.format("%s%s%s", sourceId.toString(), SEPARATOR, targetId.toString());
}

@SuppressWarnings("deprecation")
protected static KasperID stringToKasperId(final String id) {
try {
final UUID uuid = UUID.fromString(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public void testFixedId_shouldDeserializeItsOwnSerializedId() {
assertEquals(id2, id.getTargetId());
}

@SuppressWarnings("deprecation")
@Test
public void testFixedStringId_shouldDeserializeItsOwnSerializedId() {
// Given
Expand All @@ -74,6 +75,7 @@ public void testFixedStringId_shouldDeserializeItsOwnSerializedId() {
assertEquals(id2.getClass(), id.getTargetId().getClass());
}

@SuppressWarnings("deprecation")
@Test
public void testFixedIntegerId_shouldDeserializeItsOwnSerializedId() {
// Given
Expand All @@ -99,6 +101,7 @@ public void testFixedIntegerId_shouldDeserializeItsOwnSerializedId() {
assertEquals(id2.getClass(), id.getTargetId().getClass());
}

@SuppressWarnings("deprecation")
@Test
public void testFixedMixedId_shouldDeserializeItsOwnSerializedId() {
// Given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public void setup() throws IOException {

// ------------------------------------------------------------------------

@SuppressWarnings("deprecation")
@Test
public void testSendEvent() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,10 @@ public Optional<TypeAdapter<Object>> create(
final Class rawClass = typeToken.getRawType();

if (rawClass.isArray()) {
final TypeAdapter elementAdapter = adapterFactory
@SuppressWarnings({ "unchecked" })
final TypeAdapter elementAdapter = adapterFactory
.create(
TypeToken.of(rawClass.getComponentType())
TypeToken.of(rawClass.getComponentType())
);

@SuppressWarnings({ "unchecked" })
Expand Down Expand Up @@ -203,6 +204,8 @@ public Optional<TypeAdapter<Collection>> create(
typeToken.getType(),
Collection.class, 0
).get();

@SuppressWarnings({ "unchecked" })
final TypeAdapter elementAdapter = adapterFactory.create(TypeToken.of(elementType));

@SuppressWarnings({ "unchecked", "rawtypes" })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,7 @@ private TypeAdapter<? extends Query> provideBeanQueryMapper(final TypeToken<Clas

// ------------------------------------------------------------------------

@SuppressWarnings("unchecked")
private PropertyAdapter createPropertyAdapter(final Method mutator, final Method accessor,
final String name, final TypeToken<Object> propertyType) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public String[] lookupParameterNames(
*
* @param declaringClass the declaring class of the object
*/
@SuppressWarnings("unchecked")
protected void checkClassDefinition(final Class<?> declaringClass) {
final Constructor[] declaredConstructors = declaringClass.getDeclaredConstructors();

Expand All @@ -119,6 +120,7 @@ protected void checkClassDefinition(final Class<?> declaringClass) {
}

Constructor annotatedConstructor = null;

for (final Constructor constructor : declaredConstructors) {
if (null != constructor.getAnnotation(JsonCreator.class)) {
if (null != annotatedConstructor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* @author Mglcel &lt;[email protected]&gt;
*
*/
@SuppressWarnings("unchecked")
public final class ReflectionGenericsResolver {

private ReflectionGenericsResolver() { /* singleton */ }
Expand Down Expand Up @@ -63,7 +64,7 @@ public static Optional<? extends Class> getParameterTypeFromClass(final Type run
*
* @return the (optional) type of the resolved parameter at specific position
*/
@SuppressWarnings("rawtypes")
@SuppressWarnings({"rawtypes", "unchecked"})
public static Optional<? extends Class> getParameterTypeFromClass(final Field runtimeField,
final Type targetType,
final Integer nbParameter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public void deserialize_toDefaultKasperId_isOk() throws IOException {
assertEquals(expectedKasperId, actualKasperId);
}

@SuppressWarnings("deprecation")
@Test
public void serialize_fromIntegerKasperId_isOk() throws IOException {
// Given
Expand All @@ -103,6 +104,7 @@ public void serialize_fromIntegerKasperId_isOk() throws IOException {
assertEquals("{\"id\":" + id + "}", json);
}

@SuppressWarnings("deprecation")
@Test
public void deserialize_toIntegerKasperId_isOk() throws IOException {
// Given
Expand All @@ -117,6 +119,7 @@ public void deserialize_toIntegerKasperId_isOk() throws IOException {
assertEquals(expectedKasperId, actualKasperId);
}

@SuppressWarnings("deprecation")
@Test
public void serialize_fromLongKasperId_isOk() throws IOException {
// Given
Expand All @@ -131,6 +134,7 @@ public void serialize_fromLongKasperId_isOk() throws IOException {
assertEquals("{\"id\":" + id + "}", json);
}

@SuppressWarnings("deprecation")
@Test
public void deserialize_toLongKasperId_isOk() throws IOException {
// Given
Expand All @@ -145,6 +149,7 @@ public void deserialize_toLongKasperId_isOk() throws IOException {
assertEquals(expectedKasperId, actualKasperId);
}

@SuppressWarnings("deprecation")
@Test
public void serialize_fromStringKasperId_isOk() throws IOException {
// Given
Expand All @@ -159,6 +164,7 @@ public void serialize_fromStringKasperId_isOk() throws IOException {
assertEquals("{\"id\":\"" + id + "\"}", json);
}

@SuppressWarnings("deprecation")
@Test
public void deserialize_toStringKasperId_isOk() throws IOException {
// Given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ public Class<INPUT> getInputClass() {
return handler.getInputClass();
}

@SuppressWarnings("unchecked")
@Override
public Class<HANDLER> getHandlerClass() {
return (Class<HANDLER>) handler.getHandlerClass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
// ============================================================================
package com.viadeo.kasper.core.component.command;

import com.google.common.base.Optional;
import com.viadeo.kasper.api.component.command.Command;
import com.viadeo.kasper.api.component.event.Event;
import com.viadeo.kasper.api.context.Context;
import com.viadeo.kasper.api.exception.KasperCommandException;
import com.viadeo.kasper.core.component.command.aggregate.ddd.AggregateRoot;
import com.viadeo.kasper.core.component.command.gateway.CommandGateway;
import com.viadeo.kasper.core.component.command.repository.Repository;
import com.viadeo.kasper.core.context.CurrentContext;
import org.axonframework.domain.EventMessage;
import org.axonframework.domain.GenericEventMessage;
Expand Down Expand Up @@ -80,4 +83,19 @@ public void setCommandGateway(final CommandGateway commandGateway) {
this.commandGateway = checkNotNull(commandGateway);
}

/**
* Get the related repository of the specified entity class
*
* @param entityClass the class of the entity
* @param <REPO> the type of repository
* @return the entity repository
*/
public <REPO extends Repository> Optional<REPO> getRepositoryOf(final Class<? extends AggregateRoot> entityClass) {
if (null == repositoryManager) {
throw new KasperCommandException("Unable to resolve repository, no repository manager was provided");
}

return repositoryManager.getEntityRepository(entityClass);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import com.google.common.base.Optional;
import com.viadeo.kasper.api.component.command.Command;
import com.viadeo.kasper.api.exception.KasperCommandException;
import com.viadeo.kasper.api.id.KasperID;
import com.viadeo.kasper.common.tools.ReflectionGenericsResolver;
import com.viadeo.kasper.core.component.command.aggregate.ddd.AggregateRoot;
import com.viadeo.kasper.core.component.command.aggregate.ddd.IRepository;
import com.viadeo.kasper.core.component.command.repository.ClientRepository;
import com.viadeo.kasper.core.component.command.repository.Repository;

import static com.google.common.base.Preconditions.checkNotNull;

Expand All @@ -33,17 +33,16 @@ public abstract class AutowiredEntityCommandHandler<C extends Command, AGR exten
implements EntityCommandHandler<C,AGR>, WirableCommandHandler<C>
{


protected final transient BaseEntityCommandHandler.ConsistentRepositoryEntity<AGR> consistentRepositoryEntity;
protected final Class<AGR> aggregateClass;
protected Repository<? extends KasperID, AGR> repository;

// ------------------------------------------------------------------------

public AutowiredEntityCommandHandler() {
super();
consistentRepositoryEntity = new BaseEntityCommandHandler.ConsistentRepositoryEntity<>();

@SuppressWarnings("unchecked")
final Optional<Class<? extends AggregateRoot>> entityAssignClass = (Optional<Class<? extends AggregateRoot>>) ReflectionGenericsResolver
final Optional<Class<AGR>> entityAssignClass = (Optional<Class<AGR>>) ReflectionGenericsResolver
.getParameterTypeFromClass(
this.getClass(),
AutowiredEntityCommandHandler.class,
Expand All @@ -57,26 +56,24 @@ public AutowiredEntityCommandHandler() {
);
}

this.consistentRepositoryEntity.setEntityClass(entityAssignClass.get());
this.aggregateClass = entityAssignClass.get();
}

// ------------------------------------------------------------------------

@Override
public Class<AGR> getAggregateClass() {
return consistentRepositoryEntity.getEntityClass();
return aggregateClass;
}

// ------------------------------------------------------------------------

/**
* @param repository the repository related to the aggregate handled by this instance
* @see AutowiredEntityCommandHandler#setRepository(com.viadeo.kasper.core.component.command.aggregate.ddd.IRepository)
* @see AutowiredEntityCommandHandler#setRepository(com.viadeo.kasper.core.component.command.repository.Repository)
*/
public void setRepository(final IRepository<AGR> repository) {
this.consistentRepositoryEntity.setRepository(
new ClientRepository<>(checkNotNull(repository))
);
public <ID extends KasperID> void setRepository(final Repository<ID,AGR> repository) {
this.repository = checkNotNull(repository);
}

/**
Expand All @@ -85,42 +82,25 @@ public void setRepository(final IRepository<AGR> repository) {
* @return the repository
*/
@SuppressWarnings("unchecked")
public ClientRepository<AGR> getRepository() {
if (null == this.consistentRepositoryEntity.getRepository()) {
public <REPO extends Repository> REPO getRepository() {
if (null == this.repository) {

if (null == repositoryManager) {
throw new KasperCommandException("Unable to resolve repository, no repository manager was provided");
}

final Optional<ClientRepository<AGR>> optRepo =
repositoryManager.getEntityRepository(this.consistentRepositoryEntity.getEntityClass());
final Optional<Repository<KasperID,AGR>> optRepo = repositoryManager.getEntityRepository(getAggregateClass());

if ( ! optRepo.isPresent()) {
throw new KasperCommandException(String.format(
"The entity %s has not been recorded on any domain",
this.consistentRepositoryEntity.getEntityClass().getSimpleName())
"The entity %s has not been recorded on any domain", getAggregateClass().getSimpleName())
);
}

this.consistentRepositoryEntity.setRepository(optRepo.get());
}

return this.consistentRepositoryEntity.getRepository();
}

/**
* Get the related repository of the specified entity class
*
* @param entityClass the class of the entity
* @param <E> the type of the entity
* @return the entity repository
*/
public <E extends AggregateRoot> Optional<ClientRepository<E>> getRepositoryOf(final Class<E> entityClass) {
if (null == repositoryManager) {
throw new KasperCommandException("Unable to resolve repository, no repository manager was provided");
this.repository = optRepo.get();
}

return repositoryManager.getEntityRepository(entityClass);
return (REPO) repository;
}

// ------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public abstract class BaseCommandHandler<COMMAND extends Command> implements Com

private final Class<COMMAND> commandClass;

// ------------------------------------------------------------------------

protected BaseCommandHandler() {
@SuppressWarnings("unchecked")
final Optional<Class<COMMAND>> commandClass =
Expand All @@ -36,6 +38,8 @@ protected BaseCommandHandler() {
this.commandClass = commandClass.get();
}

// ------------------------------------------------------------------------

@Override
public CommandResponse handle(final CommandMessage<COMMAND> message) {
try {
Expand Down Expand Up @@ -79,4 +83,5 @@ public Class<COMMAND> getInputClass() {
public Class<? extends CommandHandler> getHandlerClass() {
return this.getClass();
}

}
Loading