Search in sources :

Example 1 with ElectionStatus

use of org.broadinstitute.consent.http.enumeration.ElectionStatus in project consent by DataBiosphere.

the class DarCollectionService method createElectionsForDarCollection.

/**
 * DarCollections with no elections, or with previously canceled elections, are valid
 * for initiating a new set of elections. Elections in open, closed, pending, or final
 * states are not valid.
 *
 * @param user The User initiating new elections for a collection
 * @param collection The DarCollection
 * @return The updated DarCollection
 */
public DarCollection createElectionsForDarCollection(User user, DarCollection collection) {
    final List<String> invalidStatuses = Stream.of(ElectionStatus.CLOSED, ElectionStatus.OPEN, ElectionStatus.FINAL, ElectionStatus.PENDING_APPROVAL).map(ElectionStatus::getValue).collect(Collectors.toList());
    List<String> referenceIds = collection.getDars().values().stream().map(DataAccessRequest::getReferenceId).collect(Collectors.toList());
    if (!referenceIds.isEmpty()) {
        List<Election> nonCanceledElections = electionDAO.findLastElectionsByReferenceIds(referenceIds).stream().filter(e -> invalidStatuses.contains(e.getStatus())).collect(Collectors.toList());
        if (!nonCanceledElections.isEmpty()) {
            logger.error("Non-canceled elections exist for collection: " + collection.getDarCollectionId());
            throw new IllegalArgumentException("Non-canceled elections exist for this collection.");
        }
    }
    try {
        collectionServiceDAO.createElectionsForDarCollection(collection);
        collection.getDars().values().forEach(dar -> {
            Election accessElection = electionDAO.findLastElectionByReferenceIdAndType(dar.getReferenceId(), ElectionType.DATA_ACCESS.getValue());
            if (Objects.nonNull(accessElection)) {
                List<Vote> votes = voteDAO.findVotesByElectionId(accessElection.getElectionId());
                try {
                    emailNotifierService.sendNewCaseMessageToList(votes, accessElection);
                } catch (Exception e) {
                    logger.error("Unable to send new case message to DAC members for DAR: " + dar.getReferenceId());
                }
            } else {
                logger.error("Did not find a created access election for DAR: " + dar.getReferenceId());
            }
        });
    } catch (Exception e) {
        logger.error("Exception creating elections and votes for collection: " + collection.getDarCollectionId());
    }
    return darCollectionDAO.findDARCollectionByCollectionId(collection.getDarCollectionId());
}
Also used : ElectionStatus(org.broadinstitute.consent.http.enumeration.ElectionStatus) DataAccessRequest(org.broadinstitute.consent.http.models.DataAccessRequest) Resource(org.broadinstitute.consent.http.resources.Resource) Date(java.util.Date) Inject(com.google.inject.Inject) LoggerFactory(org.slf4j.LoggerFactory) Vote(org.broadinstitute.consent.http.models.Vote) UserRoles(org.broadinstitute.consent.http.enumeration.UserRoles) DarCollection(org.broadinstitute.consent.http.models.DarCollection) Function(java.util.function.Function) ArrayList(java.util.ArrayList) DatasetDAO(org.broadinstitute.consent.http.db.DatasetDAO) Map(java.util.Map) ElectionType(org.broadinstitute.consent.http.enumeration.ElectionType) DarCollectionServiceDAO(org.broadinstitute.consent.http.service.dao.DarCollectionServiceDAO) BadRequestException(javax.ws.rs.BadRequestException) DataAccessRequestData(org.broadinstitute.consent.http.models.DataAccessRequestData) DataSet(org.broadinstitute.consent.http.models.DataSet) Logger(org.slf4j.Logger) DataAccessRequestDAO(org.broadinstitute.consent.http.db.DataAccessRequestDAO) Collection(java.util.Collection) UserRole(org.broadinstitute.consent.http.models.UserRole) Set(java.util.Set) User(org.broadinstitute.consent.http.models.User) Collectors(java.util.stream.Collectors) NotFoundException(javax.ws.rs.NotFoundException) ElectionDAO(org.broadinstitute.consent.http.db.ElectionDAO) Objects(java.util.Objects) List(java.util.List) DarCollectionDAO(org.broadinstitute.consent.http.db.DarCollectionDAO) Stream(java.util.stream.Stream) Election(org.broadinstitute.consent.http.models.Election) PaginationToken(org.broadinstitute.consent.http.models.PaginationToken) PaginationResponse(org.broadinstitute.consent.http.models.PaginationResponse) Collections(java.util.Collections) VoteDAO(org.broadinstitute.consent.http.db.VoteDAO) Vote(org.broadinstitute.consent.http.models.Vote) Election(org.broadinstitute.consent.http.models.Election) BadRequestException(javax.ws.rs.BadRequestException) NotFoundException(javax.ws.rs.NotFoundException)

Aggregations

Inject (com.google.inject.Inject)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Date (java.util.Date)1 List (java.util.List)1 Map (java.util.Map)1 Objects (java.util.Objects)1 Set (java.util.Set)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 BadRequestException (javax.ws.rs.BadRequestException)1 NotFoundException (javax.ws.rs.NotFoundException)1 DarCollectionDAO (org.broadinstitute.consent.http.db.DarCollectionDAO)1 DataAccessRequestDAO (org.broadinstitute.consent.http.db.DataAccessRequestDAO)1 DatasetDAO (org.broadinstitute.consent.http.db.DatasetDAO)1 ElectionDAO (org.broadinstitute.consent.http.db.ElectionDAO)1 VoteDAO (org.broadinstitute.consent.http.db.VoteDAO)1 ElectionStatus (org.broadinstitute.consent.http.enumeration.ElectionStatus)1