package io.github.saidie.plantuml_api;

import java.util.ArrayList;
import java.util.List;
import javax.inject.Inject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.social.connect.ConnectionRepository;
import org.springframework.social.twitter.api.Twitter;
import org.springframework.stereotype.Service;

@Service
public class AccountService {
    @Inject
    private Twitter twitter;

    @Inject
    private ConnectionRepository connectionRepository;

    @Autowired
    private FederationRepository federationRepository;

    @Autowired
    private AccountRepository accountRepository;

    public boolean isIdpAuthenticated() {
        return isTwitterAuthenticated();
    }

    protected boolean isTwitterAuthenticated() {
        return connectionRepository.findPrimaryConnection(Twitter.class) != null;
    }

    public Account getCurrentAccount() {
        // TODO: check cookie

        if (isTwitterAuthenticated()) {
            long id = twitter.userOperations().getUserProfile().getId();
            IdpId idpId = new IdpId(IdpId.FederationType.TWITTER, Long.toString(id));
            Federation federation = federationRepository.findOne(idpId);
            if (federation != null)
                return federation.getAccount();
        }
        return null;
    }

    public Account createAccount(String username) {
        List<Federation> list = new ArrayList<Federation>();

        IdpId idpId = null;
        if (isTwitterAuthenticated()) {
            long id = twitter.userOperations().getUserProfile().getId();
            idpId = new IdpId(IdpId.FederationType.TWITTER, Long.toString(id));
        }
        Account account = new Account();
        Federation federation = new Federation(idpId, account);
        list.add(federation);

        account.setUsername(username);
        account.setFederations(list);

        return accountRepository.save(account);
    }
}

@@ -37,87 +37,8 @@
 i;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.List;%0Aimport javax.inject.Inject;%0A
 impo
@@ -128,1684 +128,235 @@
 rk.s
-ocial.connect.ConnectionRepository;%0Aimport org.springframework.social.twitter.api.Twitter;%0Aimport org.springframework.stereotype.Service;%0A%0A@Service%0Apublic class AccountService %7B%0A    @Inject%0A    private Twitter twitter;%0A%0A    @Inject%0A    private ConnectionRepository connectionRepository;%0A%0A    @Autowired%0A    private FederationRepository federationRepository;%0A%0A    @Autowired%0A    private AccountRepository accountRepository;%0A%0A    public boolean isIdpAuthenticated() %7B%0A        return isTwitterAuthenticated();%0A    %7D%0A%0A    protected boolean isTwitterAuthenticated() %7B%0A        return connectionRepository.findPrimaryConnection(Twitter.class) != null;%0A    %7D%0A%0A    public Account getCurrentAccount() %7B%0A        // TODO: check cookie%0A%0A        if (isTwitterAuthenticated()) %7B%0A            long id = twitter.userOperations().getUserProfile().getId();%0A            IdpId idpId = new IdpId(IdpId.FederationType.TWITTER, Long.toString(id));%0A            Federation federation = federationRepository.findOne(idpId);%0A            if (federation != null)%0A                return federation.getAccount();%0A        %7D%0A        return null;%0A    %7D%0A%0A    public Account createAccount(String username) %7B%0A        List%3CFederation%3E list = new ArrayList%3CFederation%3E();%0A%0A        IdpId idpId = null;%0A        if (isTwitterAuthenticated()) %7B%0A            long id = twitter.userOperations().getUserProfile().getId();%0A            idpId = new IdpId(IdpId.FederationType.TWITTER, Long.toString(id));%0A        %7D%0A        Account account = new Account();%0A        Federation federation = new Federation(idpId, account);%0A        list.add(federation);%0A%0A        account.setUsername(username);%0A        account.setFederations(list);%0A
+tereotype.Service;%0A%0A@Service%0Apublic class AccountService %7B%0A    @Autowired%0A    private AccountRepository accountRepository;%0A%0A    public Account createAccount(String username) %7B%0A        Account account = new Account(0, username);
 %0A   

Clean AccountService