SciProjectManager for managing contacts and members of an SciProject

pull/1/head
Jens Pelzetter 2019-08-03 20:04:54 +02:00
parent 20252b16a0
commit 8efe522fe3
1 changed files with 98 additions and 11 deletions

View File

@ -10,6 +10,7 @@ import org.librecms.assets.Person;
import java.io.Serializable;
import java.util.Objects;
import java.util.Optional;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
@ -28,12 +29,12 @@ public class SciProjectMananger implements Serializable {
private SciProjectRepository sciProjectRepository;
@Transactional(Transactional.TxType.REQUIRED)
public void addcontact(final ContactableEntity contactable,
public void addContact(final ContactableEntity contactable,
final SciProject toProject,
final String withType) {
Objects.requireNonNull(contactable,
"Can't null as Contact to a SciProject.");
"Can't add null as Contact to a SciProject.");
Objects.requireNonNull(toProject,
"Can't add a Contact to a project null.");
@ -48,13 +49,99 @@ public class SciProjectMananger implements Serializable {
sciProjectRepository.save(toProject);
}
@Transactional(Transactional.TxType.REQUIRED)
public void removeContact(final ContactableEntity contactable,
final SciProject fromProject) {
Objects.requireNonNull(
contactable,
"Can't remove null as Contact from a SciProject."
);
Objects.requireNonNull(fromProject,
"Can't remove a Contact from project null.");
final Optional<Contact> result = fromProject
.getContacts()
.stream()
.filter(contact -> filterContact(contact, contactable, fromProject))
.findFirst();
if (result.isPresent()) {
final Contact remove = result.get();
fromProject.removeContact(remove);
sciProjectRepository.save(fromProject);
}
}
@Transactional(Transactional.TxType.REQUIRED)
public void addMember(final Person person,
final SciProject toProject,
final String withRole,
final MembershipStatus withStatus) {
throw new UnsupportedOperationException("ToDo");
Objects.requireNonNull(
person,
"Can't add null as a member null to a SciProject."
);
Objects.requireNonNull(
toProject,
"Can't a member to a SciProject null."
);
final Membership membership = new Membership();
membership.setProject(toProject);
membership.setMember(person);
membership.setRole(withRole);
membership.setStatus(withStatus);
toProject.addMember(membership);
sciProjectRepository.save(toProject);
}
@Transactional(Transactional.TxType.REQUIRED)
public void removeMember(final Person person,
final SciProject fromProject) {
Objects.requireNonNull(
person,
"Can't remove null as a member null from a SciProject."
);
Objects.requireNonNull(
fromProject,
"Can't a member from a SciProject null."
);
final Optional<Membership> result = fromProject
.getMembers()
.stream()
.filter(membership -> filterMembership(membership,
person,
fromProject))
.findFirst();
if (result.isPresent()) {
final Membership remove = result.get();
fromProject.removeMembership(remove);
sciProjectRepository.save(fromProject);
}
}
private boolean filterContact(final Contact contact,
final ContactableEntity byContactable,
final SciProject byProject) {
return contact.getContactable().equals(byContactable)
&& contact.getProject().equals(byProject);
}
private boolean filterMembership(final Membership membership,
final Person byPerson,
final SciProject byProject) {
return membership.getMember().equals(byPerson)
&& membership.getProject().equals(byProject);
}
}