Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2010-2011. Axon Framework
   *
   * 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 org.axonframework.repository;
 
 
Abstract implementation of the Repository that takes care of the dispatching of events when an aggregate is persisted. All uncommitted events on an aggregate are dispatched when the aggregate is saved.

Note that this repository implementation does not take care of any locking. The underlying persistence is expected to deal with concurrency. Alternatively, consider using the LockingRepository.

Parameters:
<T> The type of aggregate this repository stores
Author(s):
Allard Buijze
Since:
0.1
See also:
setEventBus(org.axonframework.eventhandling.EventBus)
LockingRepository
 
 public abstract class AbstractRepository<T extends AggregateRootimplements Repository<T> {
 
     private EventBus eventBus;
     private final Logger logger = LoggerFactory.getLogger(getClass());
 
     @Override
     public void add(T aggregate) {
         if (aggregate.getVersion() != null) {
             throw new IllegalArgumentException("Only newly created (unpersisted) aggregates may be added.");
         }
         CurrentUnitOfWork.get().registerAggregate(aggregate);
     }

    

Throws:
AggregateNotFoundException if aggregate with given id cannot be found
java.lang.RuntimeException any exception thrown by implementing classes
 
     @Override
     public T load(AggregateIdentifier aggregateIdentifierLong expectedVersion) {
         T aggregate = doLoad(aggregateIdentifierexpectedVersion);
         validateOnLoad(aggregateexpectedVersion);
         return CurrentUnitOfWork.get().registerAggregate(aggregate);
     }

    
 
     @Override
     public T load(AggregateIdentifier aggregateIdentifier) {
         return load(aggregateIdentifiernull);
     }

    
Checks the aggregate for concurrent changes. Throws a ConflictingModificationException when conflicting changes have been detected.

This implementation throws a ConflictingAggregateVersionException if the expected version is not null and the version number of the aggregate does not match the expected version

Parameters:
aggregate The loaded aggregate
expectedVersion The expected version of the aggregate
Throws:
ConflictingModificationException
ConflictingAggregateVersionException
 
     protected void validateOnLoad(T aggregateLong expectedVersion) {
         if (expectedVersion != null && aggregate.getVersion() != null && aggregate.getVersion() > expectedVersion) {
            throw new ConflictingAggregateVersionException(
                    String.format("Aggregate with identifier [%s] contains conflicting changes. "
                                          + "Expected version [%s], but was [%s]",
                                  aggregate.getIdentifier(),
                                  expectedVersion,
                                  aggregate.getVersion()));
        }
    }

    
Performs the actual saving of the aggregate.

Parameters:
aggregate the aggregate to store
    protected abstract void doSave(T aggregate);

    
Loads and initialized the aggregate with the given aggregateIdentifier.

Parameters:
aggregateIdentifier the identifier of the aggregate to load
expectedVersion The expected version of the aggregate to load
Returns:
a fully initialized aggregate
Throws:
AggregateNotFoundException if the aggregate with given identifier does not exist
    protected abstract T doLoad(AggregateIdentifier aggregateIdentifierLong expectedVersion);

    
Removes the aggregate from the repository. Typically, the repository should ensure that any calls to doLoad(org.axonframework.domain.AggregateIdentifier,java.lang.Long) throw a AggregateNotFoundException when loading a deleted aggregate.

Parameters:
aggregate the aggregate to delete
    protected abstract void doDelete(T aggregate);

    
Sets the event bus to which newly stored events should be published. Optional. By default, the repository tries to autowire the event bus.

Parameters:
eventBus the event bus to publish events to
    @Resource
    public void setEventBus(EventBus eventBus) {
        this. = eventBus;
    }
    private class SimpleSaveAggregateCallback implements SaveAggregateCallback<T> {
        @Override
        public void save(final T aggregate) {
            DomainEventStream uncommittedEvents = aggregate.getUncommittedEvents();
            if (aggregate.isDeleted()) {
                doDelete(aggregate);
            } else {
                doSave(aggregate);
            }
            aggregate.commitEvents();
            dispatchUncommittedEvents(uncommittedEvents);
        }
        private void dispatchUncommittedEvents(DomainEventStream uncommittedEvents) {
            while (uncommittedEvents.hasNext()) {
                DomainEvent event = uncommittedEvents.next();
                if (.isDebugEnabled()) {
                    .debug("Publishing event [{}] to the UnitOfWork"event.getClass().getSimpleName());
                }
                CurrentUnitOfWork.get().publishEvent(event);
            }
        }
    }
New to GrepCode? Check out our FAQ X