001: /*
002: * $Id: Users.java,v 1.5 2007/09/18 11:27:16 agoubard Exp $
003: */
004: package com.mycompany.rest.api;
005:
006: import java.util.Map;
007: import java.util.TreeMap;
008: import org.xins.server.API;
009:
010: import org.xins.common.manageable.Manageable;
011:
012: /**
013: * Common object used to manage the users.
014: *
015: * @version $Revision: 1.5 $ $Date: 2007/09/18 11:27:16 $
016: * @author <a href="mailto:anthony.goubard@japplis.com">Anthony Goubard</a>
017: */
018: public class Users extends Manageable {
019:
020: /**
021: * The users are stored in a map where the key is the account number and the
022: * value a String[] with the first name and the surname.
023: */
024: private Map _usersMap = new TreeMap();
025:
026: /**
027: * The last account number created.
028: */
029: private int _lastAccountNumber = 1000;
030:
031: /**
032: * Constructs a new <code>Users</code> instance.
033: *
034: * @param api
035: * the API to which this function belongs, guaranteed to be not
036: * <code>null</code>.
037: */
038: public Users(API api) {
039: }
040:
041: /**
042: * Creates a new user.
043: *
044: * @param firstName
045: * the first name of the user.
046: * @param surname
047: * the surname of the user.
048: *
049: * @return
050: * the account number associated with this user.
051: */
052: public final synchronized int addUser(String firstName,
053: String surname) {
054: String[] name = { firstName, surname };
055: _lastAccountNumber++;
056: _usersMap.put(new Integer(_lastAccountNumber), name);
057: return _lastAccountNumber;
058: }
059:
060: /**
061: * Updates the information of a user.
062: *
063: * @param accountNumber
064: * the account number of the user.
065: * @param firstName
066: * the first name of the user.
067: * @param surname
068: * the surname of the user.
069: *
070: * @return
071: * true if the user has been found and has been updated, false otherwise.
072: */
073: public final synchronized boolean updateUser(int accountNumber,
074: String firstName, String surname) {
075: if (!_usersMap.containsKey(new Integer(accountNumber))) {
076: return false;
077: }
078: String[] name = { firstName, surname };
079: _usersMap.put(new Integer(accountNumber), name);
080: return true;
081: }
082:
083: /**
084: * Deletes the information of a user.
085: *
086: * @param accountNumber
087: * the account number of the user.
088: *
089: * @return
090: * true if the user has been found and has been deleted, false otherwise.
091: */
092: public final synchronized boolean deleteUser(int accountNumber) {
093: if (!_usersMap.containsKey(new Integer(accountNumber))) {
094: return false;
095: }
096: _usersMap.remove(new Integer(accountNumber));
097: return true;
098: }
099:
100: /**
101: * Update the information of a user.
102: *
103: * @param accountNumber
104: * the account number of the user.
105: *
106: * @return
107: * a String[] containing the first name and the surname of the user or
108: * <code>null</code> if the user cannot be found.
109: */
110: public final String[] getUser(int accountNumber) {
111: return (String[]) _usersMap.get(new Integer(accountNumber));
112: }
113: }
|