Skip to content

Instantly share code, notes, and snippets.

@msulima
Created July 27, 2012 11:24
Show Gist options
  • Save msulima/3187477 to your computer and use it in GitHub Desktop.
Save msulima/3187477 to your computer and use it in GitHub Desktop.
EventResignServiceImpl
package com.futureprocessing.fpcommunity.core.services.events;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.neo4j.template.Neo4jOperations;
import org.springframework.stereotype.Service;
import org.springframework.validation.Validator;
import com.futureprocessing.fpcommunity.core.datetime.IDateTime;
import com.futureprocessing.fpcommunity.core.domain.Event;
import com.futureprocessing.fpcommunity.core.domain.User;
import com.futureprocessing.fpcommunity.core.exceptions.EventEndedException;
import com.futureprocessing.fpcommunity.core.exceptions.UserNotAttendingEventException;
import com.futureprocessing.fpcommunity.core.services.impl.EventSatifsiesSearchedTextCriteriaPredicate;
import com.futureprocessing.fpcommunity.core.validation.groups.FulfilsPredicate;
@Service
public class EventResignServiceImpl implements IEventResignService {
@Autowired
private Neo4jOperations neo4jOperations;
@Autowired
private IDateTime dateTimeProvider;
@Autowired
private Validator validator;
@Override
public void resignFromEvent(@FulfilsPredicate(values = { EventSatifsiesSearchedTextCriteriaPredicate.class }) User resigningUser,
Event event) {
fetchEventsAndParticipants(resigningUser, event);
throwExceptionIfUserIsNotParticipatingEvent(resigningUser, event);
throwExceptionIfEventIsInThePast(event);
event.removeParticipant(resigningUser);
resigningUser.removeAttendingEvent(event);
neo4jOperations.save(resigningUser);
neo4jOperations.save(event);
}
private void fetchEventsAndParticipants(User resigningUser, Event event) {
neo4jOperations.fetch(resigningUser.getAttendingEvents());
neo4jOperations.fetch(event.getParticipants());
}
private void throwExceptionIfUserIsNotParticipatingEvent(User resigningUser, Event event) {
if (userIsNotParticipatingEvent(resigningUser, event)) {
throw new UserNotAttendingEventException(resigningUser, event);
}
}
private boolean userIsNotParticipatingEvent(User resigningUser, Event event) {
return !(resigningUser.getAttendingEvents().contains(event) && event.getParticipants().contains(resigningUser));
}
private void throwExceptionIfEventIsInThePast(Event event) {
if (eventIsInThePast(event)) {
throw new EventEndedException(event);
}
}
private boolean eventIsInThePast(Event event) {
return event.getEndTime() < dateTimeProvider.getNow().getMillis();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment