Search in sources :

Example 6 with TransferDto

use of org.collectiveone.modules.tokens.dto.TransferDto in project CollectiveOneWebapp by CollectiveOne.

the class InitiativeService method transferAssets.

@Transactional
private PostResult transferAssets(UUID initiativeId, NewInitiativeDto initiativeDto, UUID creatorId) {
    Initiative initiative = initiativeRepository.findById(initiativeId);
    if (!initiativeDto.getAsSubinitiative()) {
        /* if is not a sub-initiative, then create a token for this initiative */
        if (initiativeDto.getCreateToken()) {
            TokenType token = tokenService.create(initiativeDto.getOwnTokens().getAssetName(), "T");
            initiative.getTokenTypes().add(token);
            initiative = initiativeRepository.save(initiative);
            tokenService.mintToHolder(token.getId(), initiative.getId(), initiativeDto.getOwnTokens().getOwnedByThisHolder(), TokenHolderType.INITIATIVE);
            TokenMint mint = new TokenMint();
            mint.setToken(token);
            mint.setOrderedBy(appUserRepository.findByC1Id(creatorId));
            mint.setToHolder(initiativeId);
            mint.setMotive("Initiative creation.");
            mint.setNotes("");
            mint.setValue(initiativeDto.getOwnTokens().getOwnedByThisHolder());
            mint = tokenMintRespository.save(mint);
        }
        return new PostResult("success", "initiative tokens created", initiative.getId().toString());
    } else {
        Initiative parent = initiativeRepository.findById(UUID.fromString(initiativeDto.getParentInitiativeId()));
        /* if it is a sub-initiative, then link to parent initiative */
        InitiativeRelationship relationship = new InitiativeRelationship();
        relationship.setInitiative(initiative);
        relationship.setType(InitiativeRelationshipType.IS_ATTACHED_SUB);
        relationship.setOfInitiative(parent);
        relationship = initiativeRelationshipRepository.save(relationship);
        List<InitiativeTransfer> transfers = new ArrayList<InitiativeTransfer>();
        /* and transfer parent assets to child */
        for (TransferDto thisTransfer : initiativeDto.getAssetsTransfers()) {
            /* ignore zero token transfers */
            if (thisTransfer.getValue() > 0) {
                TokenType token = tokenService.getTokenType(UUID.fromString(thisTransfer.getAssetId()));
                tokenService.transfer(token.getId(), parent.getId(), initiative.getId(), thisTransfer.getValue(), TokenHolderType.INITIATIVE);
                /* upper layer keeping track of who transfered what to whom */
                InitiativeTransfer transfer = new InitiativeTransfer();
                transfer.setFrom(parent);
                transfer.setTo(initiative);
                transfer.setTokenType(token);
                transfer.setValue(thisTransfer.getValue());
                transfer.setMotive("sub-initiative creation");
                transfer.setNotes("");
                transfer.setOrderDate(new Timestamp(System.currentTimeMillis()));
                transfer = initiativeTransferRepository.save(transfer);
                transfers.add(transfer);
            }
        }
        initiative.getRelationships().add(relationship);
        initiativeRepository.save(initiative);
        activityService.newSubinitiativeCreated(parent, initiative.getCreator(), initiative, transfers);
    }
    return new PostResult("success", "sub initiative created and tokens transferred", initiative.getId().toString());
}
Also used : PostResult(org.collectiveone.common.dto.PostResult) TokenType(org.collectiveone.modules.tokens.TokenType) TokenMint(org.collectiveone.modules.tokens.TokenMint) ArrayList(java.util.ArrayList) TransferDto(org.collectiveone.modules.tokens.dto.TransferDto) InitiativeTransfer(org.collectiveone.modules.tokens.InitiativeTransfer) Timestamp(java.sql.Timestamp) Transactional(javax.transaction.Transactional)

Example 7 with TransferDto

use of org.collectiveone.modules.tokens.dto.TransferDto in project CollectiveOneWebapp by CollectiveOne.

the class InitiativeTransfer method toDto.

public TransferDto toDto() {
    TransferDto dto = new TransferDto();
    dto.setAssetId(tokenType.getId().toString());
    dto.setAssetName(tokenType.getName());
    dto.setSenderId(from.getId().toString());
    dto.setSenderName(from.getMeta().getName());
    dto.setReceiverId(to.getId().toString());
    dto.setReceiverName(to.getMeta().getName());
    if (orderDate != null)
        dto.setOrderDate(orderDate.getTime());
    if (orderedBy != null)
        dto.setOrderedBy(orderedBy.toDtoLight());
    dto.setMotive(motive);
    dto.setNotes(notes);
    dto.setValue(value);
    return dto;
}
Also used : TransferDto(org.collectiveone.modules.tokens.dto.TransferDto)

Example 8 with TransferDto

use of org.collectiveone.modules.tokens.dto.TransferDto in project CollectiveOneWebapp by CollectiveOne.

the class TokenTransferService method getTransfersPending.

@Transactional
public List<TransferDto> getTransfersPending(UUID initiativeId) {
    Initiative initiative = initiativeRepository.findById(initiativeId);
    List<TransferDto> transfersPending = new ArrayList<TransferDto>();
    List<Assignation> assignations = assignationService.getOpenAssignations(initiativeId);
    for (Assignation assignation : assignations) {
        for (Bill bill : assignation.getBills()) {
            TransferDto dto = new TransferDto();
            dto.setAssetId(bill.getTokenType().getId().toString());
            dto.setAssetName(bill.getTokenType().getName());
            dto.setSenderId(initiative.getId().toString());
            dto.setSenderName(initiative.getMeta().getName());
            dto.setValue(bill.getValue());
            transfersPending.add(dto);
        }
    }
    return transfersPending;
}
Also used : ArrayList(java.util.ArrayList) Bill(org.collectiveone.modules.assignations.Bill) TransferDto(org.collectiveone.modules.tokens.dto.TransferDto) Assignation(org.collectiveone.modules.assignations.Assignation) Initiative(org.collectiveone.modules.initiatives.Initiative) Transactional(javax.transaction.Transactional)

Aggregations

TransferDto (org.collectiveone.modules.tokens.dto.TransferDto)8 Transactional (javax.transaction.Transactional)5 ArrayList (java.util.ArrayList)4 Initiative (org.collectiveone.modules.initiatives.Initiative)4 UUID (java.util.UUID)2 GetResult (org.collectiveone.common.dto.GetResult)2 PageRequest (org.springframework.data.domain.PageRequest)2 Sort (org.springframework.data.domain.Sort)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 Timestamp (java.sql.Timestamp)1 PostResult (org.collectiveone.common.dto.PostResult)1 Assignation (org.collectiveone.modules.assignations.Assignation)1 Bill (org.collectiveone.modules.assignations.Bill)1 InitiativeRelationship (org.collectiveone.modules.initiatives.InitiativeRelationship)1 Member (org.collectiveone.modules.initiatives.Member)1 InitiativeTransfer (org.collectiveone.modules.tokens.InitiativeTransfer)1 TokenMint (org.collectiveone.modules.tokens.TokenMint)1 TokenType (org.collectiveone.modules.tokens.TokenType)1 AssetsDto (org.collectiveone.modules.tokens.dto.AssetsDto)1