Search in sources :

Example 1 with PeerReviewedAssignation

use of org.collectiveone.modules.assignations.evaluationlogic.PeerReviewedAssignation in project CollectiveOneWebapp by CollectiveOne.

the class AssignationService method updateAssignationState.

@Transactional
public void updateAssignationState(UUID assignationId) {
    Assignation assignation = assignationRepository.findById(assignationId);
    if (assignation.getType() == AssignationType.PEER_REVIEWED) {
        PeerReviewedAssignation peerReviewedAssignation = new PeerReviewedAssignation();
        peerReviewedAssignation.setAssignation(assignation);
        peerReviewedAssignation.updateState();
        if (peerReviewedAssignation.getState() == PeerReviewedAssignationState.CLOSED) {
            /* percents already updated by peerReviewAssignation,
				 * so just change state, transfer funds and save */
            /* transfer the assets to the receivers */
            /* normalize once again just to be sure...*/
            boolean valid = true;
            double sumOfPercents = 0.0;
            for (Receiver receiver : assignation.getReceivers()) {
                sumOfPercents += receiver.getAssignedPercent();
                if (Double.isNaN(receiver.getAssignedPercent())) {
                    valid = false;
                }
            }
            if (sumOfPercents == 0) {
                valid = false;
            }
            if (Math.abs(100.0 - sumOfPercents) >= 1E-6) {
                /* must sum 100 */
                valid = false;
            }
            if (valid) {
                boolean errorSending = false;
                for (Bill bill : assignation.getBills()) {
                    for (Receiver receiver : assignation.getReceivers()) {
                        if (receiver.getState().equals(ReceiverState.PENDING)) {
                            double value = bill.getValue() * receiver.getAssignedPercent() / 100.0;
                            PostResult result = tokenTransferService.transferFromInitiativeToUser(assignation.getInitiative().getId(), receiver.getUser().getC1Id(), bill.getTokenType().getId(), value);
                            if (result.getResult().equals("success")) {
                                receiver.setState(ReceiverState.RECEIVED);
                                receiver.setTransfer(memberTransferRepository.findById(UUID.fromString(result.getElementId())));
                                receiverRepository.save(receiver);
                            } else {
                                errorSending = true;
                            }
                        }
                    }
                }
                if (!errorSending) {
                    assignation.setState(AssignationState.DONE);
                    activityService.peerReviewedAssignationDone(assignation);
                } else {
                    assignation.setState(AssignationState.ERROR);
                }
                assignationRepository.save(assignation);
            }
        }
    }
}
Also used : PostResult(org.collectiveone.common.dto.PostResult) PeerReviewedAssignation(org.collectiveone.modules.assignations.evaluationlogic.PeerReviewedAssignation) PeerReviewedAssignation(org.collectiveone.modules.assignations.evaluationlogic.PeerReviewedAssignation) Transactional(javax.transaction.Transactional)

Aggregations

Transactional (javax.transaction.Transactional)1 PostResult (org.collectiveone.common.dto.PostResult)1 PeerReviewedAssignation (org.collectiveone.modules.assignations.evaluationlogic.PeerReviewedAssignation)1