Java Source Code: net.nplus1.anduril.server.services.ClientManagerBean


   1: package net.nplus1.anduril.server.services;
   2: 
   3: import javax.ejb.SessionBean;
   4: import javax.ejb.CreateException;
   5: import javax.ejb.FinderException;
   6: import javax.ejb.RemoveException;
   7: import org.apache.log4j.Logger;
   8: import java.util.Collection;
   9: import java.util.Collections;
  10: import java.util.Comparator;
  11: import java.util.LinkedList;
  12: import java.util.Iterator;
  13: import net.nplus1.anduril.server.common.dto.ClientDTO;
  14: import net.nplus1.anduril.server.util.EJBLocalHomeFactory;
  15: import net.nplus1.anduril.server.domain.ClientLocal;
  16: import net.nplus1.anduril.server.domain.ClientHomeLocal;
  17: import net.nplus1.anduril.server.common.exceptions.HomeFactoryException;
  18: import net.nplus1.anduril.server.common.exceptions.UpdateException;
  19: 
  20: /*
  21:    The contents of this file are subject to the Mozilla Public License
  22:    Version 1.1 (the "License"); you may not use this file except in
  23:    compliance with the License. You may obtain a copy of the License at
  24:    http://www.mozilla.org/MPL/
  25: 
  26:    Software distributed under the License is distributed on an "AS IS"
  27:    basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
  28:    License for the specific language governing rights and limitations
  29:    under the License.
  30: 
  31:    The Original Code is anduril.nplus1.net code.
  32:       
  33:    The Initial Developer of the Original Code is n + 1, Inc.  
  34:    Portions created by n + 1, Inc. are Copyright (C) 2003. 
  35:    All Rights Reserved.
  36:       
  37:    Contributor(s): Brian Schenkenfelder and Craig McDaniel.
  38: */
  39: 
  40: /**
  41:  * @ejb.bean name="ClientManager"
  42:  *         type="Stateless"
  43:  *         jndi-name="anduril/ejb/remote/ClientManager"
  44:  *         description="Manages client information"
  45:  *         view-type="remote"
  46:  * @ejb.interface remote-class="net.nplus1.anduril.server.common.services.ClientManagerRemote"
  47:  * @ejb.home remote-class="net.nplus1.anduril.server.common.services.ClientManagerHomeRemote" 
  48:  * @ejb.ejb-ref ejb-name="Client"
  49:  *        view-type="local"
  50:  */
  51: public abstract class ClientManagerBean implements SessionBean
  52:	  {
  53:    private Logger log = Logger.getLogger(getClass());
  54:
  55:    /**
  56:     * @ejb.create-method view-type="remote"
  57:     */
  58:    public void ejbCreate() throws CreateException { }
  59:
  60:
  61:    /**
  62:     * @ejb.interface-method view-type="remote"
  63:     */
  64:    public ClientDTO addClient(String name) throws CreateException
  65:	      {
  66:        ClientDTO client = null;
  67:        
  68:        try 
  69:	          {
  70:            ClientHomeLocal home = this.getHome();
  71:            ClientLocal local = home.create(name);
  72:            client = local.getClientDTO();
  73:        } 
  74:        catch (Exception ex) 
  75:	          {
  76:            log.error("Can not add client", ex);
  77:            throw new CreateException();
  78:        }
  79:
  80:        return client;
  81:    }
  82:
  83:    /**
  84:     * @ejb.interface-method view-type="remote"
  85:     */
  86:    public void updateClient(ClientDTO client) throws UpdateException
  87:	      {
  88:        try 
  89:	          {
  90:            ClientHomeLocal home = this.getHome();
  91:            ClientLocal local = home.findByPrimaryKey(client.getClientId());
  92:            local.setClientName(client.getClientName());
  93:        } 
  94:        catch (FinderException ex) 
  95:	          {
  96:            log.error("Could not find client", ex);
  97:            throw new UpdateException("Could not find this client for updating.");
  98:        } 
  99:        catch (Exception ex) 
 100:	          {
 101:            log.error("An unexpected error occurred", ex);
 102:            throw new UpdateException(ex);
 103:        }
 104:    }
 105:
 106:
 107:    /**
 108:     * @ejb.interface-method view-type="remote"
 109:     */
 110:    public void removeClient(Integer id) throws RemoveException
 111:	      {
 112:        try 
 113:	          {
 114:            ClientHomeLocal home = this.getHome();
 115:            home.remove(id);
 116:        } 
 117:        catch (Exception ex) 
 118:	          {
 119:            log.error("Can not remove client", ex);
 120:            throw new RemoveException(ex.getMessage());
 121:        }
 122:    }
 123:
 124:    
 125:    /**
 126:     * @ejb.interface-method view-type="remote"
 127:     */
 128:    public ClientDTO getClient(Integer id) throws FinderException
 129:	      {
 130:        ClientDTO client = null;
 131:        
 132:        try 
 133:	          {
 134:            ClientHomeLocal home = this.getHome();
 135:            ClientLocal local = home.findByPrimaryKey(id);
 136:            client = local.getClientDTO();
 137:        } 
 138:        catch (Exception ex) 
 139:	          {
 140:            log.error("Can not find client", ex);
 141:            throw new FinderException();
 142:        }
 143:        
 144:        return client;
 145:    }
 146:
 147:
 148:    /**
 149:     * Service method for the List Clients use case.
 150:     * @ejb.interface-method view-type="remote"
 151:     */
 152:    public Collection getClients() throws FinderException
 153:	      {
 154:        LinkedList c = new LinkedList();
 155:
 156:        try 
 157:	          {
 158:            ClientHomeLocal home = this.getHome();
 159:        
 160:            Iterator clients = home.findAll().iterator();
 161:            while(clients.hasNext())
 162:	              {
 163:                ClientLocal local = (ClientLocal)clients.next();
 164:                c.add(local.getClientDTO());
 165:            }
 166:
 167:            Collections.sort(c, new CompareClients());
 168:        } 
 169:        catch (Exception ex) 
 170:	          {
 171:            log.error("Can not get client list", ex);
 172:            throw new FinderException();
 173:        }
 174:
 175:        return c;
 176:    }
 177:
 178:    
 179:    /**
 180:     * Utility method for getting a ClientHomeLocal reference.
 181:     */
 182:    private ClientHomeLocal getHome() throws HomeFactoryException
 183:	      {
 184:        EJBLocalHomeFactory factory = EJBLocalHomeFactory.getFactory();
 185:        ClientHomeLocal home = (ClientHomeLocal)factory.lookupHome(ClientHomeLocal.class);
 186:
 187:        return home;
 188:    }
 189:
 190:    
 191:    /**
 192:     * comparator class to use in ordering clients.
 193:     */
 194:    private class CompareClients implements Comparator
 195:	      {
 196:        public int compare(Object o1, Object o2)
 197:	          {
 198:            ClientDTO client1 = (ClientDTO) o1;
 199:            ClientDTO client2 = (ClientDTO) o2;
 200:
 201:            return client1.getClientName().compareToIgnoreCase(client2.getClientName());
 202:        }
 203:        
 204:        public boolean equals(Object obj)
 205:	          {
 206:            return this.equals(obj);
 207:        }
 208:    }
 209:}