Newer
Older
CactusServer / src / main / java / cactusServer / models / Accounts.java
y-ota on 31 May 2018 3 KB SessionをURI...に置き換え
package cactusServer.models;

import javax.inject.Singleton;

import cactusServer.entities.*;
import cactusServer.utils.RandomStringGenerator;

import java.util.*;

@Singleton
public class Accounts {
	private static Accounts theInstance = null;
	private ArrayList<Account> accounts = new ArrayList<>(10000);
	private HashSet<String> userIDSet = new HashSet<>(10000);
	private HashSet<String> uniqueIDSet = new HashSet<>(10000);
	private URIAddressedEntity session;

	private Accounts() {

	}

	public static Accounts getInstance() {
		if (theInstance == null) {
			theInstance = new Accounts();
		}
		return theInstance;
	}

	public URIAddressedEntity createAcount(String userID, String userName, String userPass) {
		if (!userIDSet.add(userID))
			return null;
		String uniqueID = RandomStringGenerator.generateUniqueString(12, RandomStringGenerator.ALPHA_NUMERIC,
				uniqueIDSet);
		uniqueIDSet.add(uniqueID);
		Account newAccount = new Account(userID, userName, userPass, uniqueID);
		accounts.add(newAccount);
		session = new URIAddressedEntity("/CactusServer/rest/accounts/" + uniqueID, newAccount);

		return session;
	}

	public Account getAccountToken(String token) {
		Account editAccount;
		for (int i = 0; i < accounts.size(); i++) {
			editAccount = accounts.get(i);
			if (editAccount.getToken().equals(token)) {
				return editAccount;
			}
		}
		return null;
	}

	public Account getAccountByID(String userID) {
		Account editAccount;
		for (int i = 0; i < accounts.size(); i++) {
			editAccount = accounts.get(i);
			if (editAccount.getId().equals(userID)) {
				return editAccount;
			}
		}
		return null;
	}

	public Account getAccountByName(String userName) {
		Account editAccount;
		for (int i = 0; i < accounts.size(); i++) {
			editAccount = accounts.get(i);
			if (editAccount.getName().equals(userName)) {
				return editAccount;
			}
		}
		return null;
	}

	public Account getAccountByuniqueID(String uniqueID) {
		Account editAccount;
		for (int i = 0; i < accounts.size(); i++) {
			editAccount = accounts.get(i);
			if (editAccount.getUniqueID().equals(uniqueID)) {
				return editAccount;
			}
		}
		return null;
	}

	public URIAddressedEntity loginAccount(String userID, String userPass) {
		if (getAccountByID(userID).getPass().equals(userPass)) {
			Accounts.getInstance().getAccountByID(userID).setLogin(true);
			Accounts.getInstance().getAccountByID(userID).formToken();
			session = new URIAddressedEntity(
					"/CactusServer/rest/accounts/" + Accounts.getInstance().getAccountByID(userID).getUniqueID(),
					Accounts.getInstance().getAccountByID(userID));
			return session;
		} else {
			return null;
		}
	}

	public Account logoutAccount(String token) {
		Accounts.getInstance().getAccountToken(token).setLogin(false);
		return Accounts.getInstance().getAccountToken(token);
	}

	public Account deleteAccount(String uniqueID) {
		Account edit = Accounts.getInstance().getAccountToken(uniqueID);
		userIDSet.remove(edit.getId());
		uniqueIDSet.remove(edit.getUniqueID());
		accounts.remove(edit);
		return edit;
	}

	public ArrayList<Account> getAccounts() {
		return accounts;
	}
}