use of org.collectiveone.common.dto.PostResult 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);
}
}
}
}
use of org.collectiveone.common.dto.PostResult in project CollectiveOneWebapp by CollectiveOne.
the class AssignationService method deleteAssignation.
@Transactional
public PostResult deleteAssignation(UUID assignationId, UUID userId) {
Assignation assignation = assignationRepository.findById(assignationId);
if (assignation.getState() == AssignationState.OPEN) {
assignation.setState(AssignationState.DELETED);
assignationRepository.save(assignation);
activityService.assignationDeleted(assignation, appUserRepository.findByC1Id(userId));
return new PostResult("success", "assignation deleted", "");
} else {
return new PostResult("error", "assignation not open, cannot be deleted", "");
}
}
use of org.collectiveone.common.dto.PostResult in project CollectiveOneWebapp by CollectiveOne.
the class AssignationService method createAssignation.
public PostResult createAssignation(UUID initiativeId, AssignationDto assignationDto, UUID creatorId) {
Initiative initiative = initiativeRepository.findById(initiativeId);
Assignation assignation = new Assignation();
assignation.setType(AssignationType.valueOf(assignationDto.getType()));
assignation.setMotive(assignationDto.getMotive());
assignation.setNotes(assignationDto.getNotes());
assignation.setCreator(appUserRepository.findByC1Id(creatorId));
assignation.setCreationDate(new Timestamp(System.currentTimeMillis()));
assignation.setState(AssignationState.valueOf(assignationDto.getConfig().getStartState()));
assignation.setInitiative(initiative);
assignation.getAlsoInInitiatives().addAll(initiativeService.getParentGenealogyInitiatives(initiative.getId()));
AssignationConfig config = new AssignationConfig();
config.setSelfBiasVisible(Boolean.valueOf(assignationDto.getConfig().getSelfBiasVisible()));
config.setEvaluationsVisible(Boolean.valueOf(assignationDto.getConfig().getEvaluationsVisible()));
config.setDurationDays(assignationDto.getConfig().getMaxDuration());
if (assignation.getState() == AssignationState.OPEN) {
config.setMinClosureDate(new Timestamp(System.currentTimeMillis()));
config.setMaxClosureDate(new Timestamp(System.currentTimeMillis() + assignationDto.getConfig().getMaxDuration() * DAYS_TO_MS));
}
config = assignationConfigRepository.save(config);
assignation.setConfig(config);
assignation = assignationRepository.save(assignation);
for (ReceiverDto receiverDto : assignationDto.getReceivers()) {
Receiver receiver = new Receiver();
receiver.setUser(appUserRepository.findByC1Id(UUID.fromString(receiverDto.getUser().getC1Id())));
receiver.setAssignation(assignation);
receiver.setAssignedPercent(receiverDto.getPercent());
receiver.setState(ReceiverState.PENDING);
if (receiverDto.getIsDonor()) {
receiver.setType(ReceiverType.DONOR);
} else {
receiver.setType(ReceiverType.NORMAL);
}
receiver = receiverRepository.save(receiver);
assignation.getReceivers().add(receiver);
}
for (BillDto billDto : assignationDto.getAssets()) {
TokenType tokenType = tokenService.getTokenType(UUID.fromString(billDto.getAssetId()));
Bill bill = new Bill();
bill.setAssignation(assignation);
bill.setTokenType(tokenType);
bill.setValue(billDto.getValue());
bill = billRepository.save(bill);
assignation.getBills().add(bill);
}
switch(assignation.getType()) {
case DIRECT:
for (Bill bill : assignation.getBills()) {
for (Receiver receiver : assignation.getReceivers()) {
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);
}
}
}
assignation.setState(AssignationState.DONE);
assignationRepository.save(assignation);
activityService.directAssignationCreated(assignation, appUserRepository.findByC1Id(creatorId));
break;
case PEER_REVIEWED:
for (EvaluatorDto evaluatorDto : assignationDto.getEvaluators()) {
Evaluator evaluator = new Evaluator();
evaluator.setUser(appUserRepository.findByC1Id(UUID.fromString(evaluatorDto.getUser().getC1Id())));
evaluator.setAssignation(assignation);
evaluator.setState(EvaluatorState.PENDING);
/* Weight logic TBD */
evaluator.setWeight(1.0);
evaluator = evaluatorRepository.save(evaluator);
/* Create the grades of all evaluators already */
for (Receiver receiver : assignation.getReceivers()) {
EvaluationGrade grade = new EvaluationGrade();
grade.setAssignation(assignation);
grade.setEvaluator(evaluator);
grade.setReceiver(receiver);
grade.setPercent(0.0);
grade.setType(EvaluationGradeType.SET);
grade.setState(EvaluationGradeState.PENDING);
grade = evaluationGradeRepository.save(grade);
evaluator.getGrades().add(grade);
}
assignation.getEvaluators().add(evaluator);
}
activityService.peerReviewedAssignationCreated(assignation, appUserRepository.findByC1Id(creatorId));
break;
}
return new PostResult("success", "success", assignation.getId().toString());
}
use of org.collectiveone.common.dto.PostResult in project CollectiveOneWebapp by CollectiveOne.
the class AssignationService method revertAssignation.
@Transactional
public PostResult revertAssignation(UUID assignationId, UUID userId) {
Assignation assignation = assignationRepository.findById(assignationId);
if (assignation.getState() == AssignationState.DONE) {
for (Receiver receiver : assignation.getReceivers()) {
/* reset approvals */
receiver.setRevertApproval(false);
receiverRepository.save(receiver);
}
assignation.setState(AssignationState.REVERT_ORDERED);
assignationRepository.save(assignation);
activityService.assignationRevertOrdered(assignation, appUserRepository.findByC1Id(userId));
return new PostResult("success", "revert of assignation ordered", "");
} else {
return new PostResult("error", "assignation not done, cannot be reverted", "");
}
}
use of org.collectiveone.common.dto.PostResult in project CollectiveOneWebapp by CollectiveOne.
the class ModelService method deleteView.
@Transactional
public PostResult deleteView(UUID viewId, UUID creatorId) {
ModelView view = modelViewRepository.findById(viewId);
Initiative initiative = view.getInitiative();
initiative.getModelViews().remove(view);
initiative.getModelViewsTrash().add(view);
initiativeRepository.save(initiative);
view = modelViewRepository.save(view);
activityService.modelViewDeleted(view, appUserRepository.findByC1Id(creatorId));
return new PostResult("success", "view deleted", view.getId().toString());
}
Aggregations