📄 userbean.java
字号:
/* * UserEJB - CyberDemia's User management library implemented using EJBs. * Copyright (C) 2004 CyberDemia Research and Services * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. * * See the COPYING file located in the top-level-directory of * the archive of this library for complete text of license. */package com.cyberdemia.user;import java.util.*;import java.util.logging.*;import java.security.*;import javax.ejb.CreateException;import com.cyberdemia.util.HexString;import com.cyberdemia.ejb.SimpleEntityBean;/** * UserBean stores the primary data about a user such as email, password and name. * These primary data may be accessed or set via the UserData data transfer object. * Additional data or resources may be associated with a User using the * "resource reference" mechanism. * * @ejb.bean name="UserBean" * type="CMP" * view-type="local" * schema="User" * cmp-version="2.x" * primkey-field="id" * local-jndi-name="ejb/LocalUser" * * @ejb.util generate="physical" * @ejb.interface local-class="com.cyberdemia.user.LocalUser" local-extends="javax.ejb.EJBLocalObject" * @ejb.home local-class="com.cyberdemia.user.LocalUserHome" local-extends="javax.ejb.EJBLocalHome" * @ejb.persistence table-name="CDUser" * * @jboss.method-attributes pattern="getGroups" read-only="false" * @jboss.method-attributes pattern="get*" read-only="true" * @jboss.method-attributes pattern="is*" read-only="true" * * @ejb.finder signature="java.util.Collection findAll()" * query="SELECT DISTINCT OBJECT(u) FROM User u" * * @ejb.finder signature="java.util.Collection findByLogin(java.lang.String name)" * query="SELECT DISTINCT OBJECT(u) FROM User u WHERE u.name=?1" * * @ejb.finder signature="java.util.Collection findByRoleName(java.lang.String roleName, int limit)" * query="SELECT DISTINCT OBJECT(u) FROM User u, UserRole r WHERE r.roleName=?1 AND u.name=r.name" * @jboss.query signature="java.util.Collection findByRoleName(java.lang.String roleName, int limit)" * query="SELECT DISTINCT OBJECT(u) FROM User u, UserRole r WHERE r.roleName=?1 AND u.name=r.name ORDER BY u.name LIMIT ?2" * * @ejb.finder signature="java.util.Collection findByLoginSubString(java.lang.String login, int limit)" * query="SELECT DISTINCT OBJECT(u) FROM User u WHERE locate( ?1, u.name ) > 0" * @jboss.query signature="java.util.Collection findByLoginSubString(java.lang.String login, int limit)" * query="SELECT DISTINCT OBJECT(u) FROM User u WHERE locate( ?1, u.name ) > 0 ORDER BY u.name LIMIT ?2" * * @ejb.finder signature="java.util.Collection findByLastNameSubString(java.lang.String lastName, int limit)" * query="SELECT DISTINCT OBJECT(u) FROM User u WHERE locate( ?1, u.lastName ) > 0" * @jboss.query signature="java.util.Collection findByLastNameSubString(java.lang.String lastName, int limit)" * query="SELECT DISTINCT OBJECT(u) FROM User u WHERE locate( ?1, u.lastName ) > 0 ORDER BY u.lastName LIMIT ?2" * * @jboss.container-configuration name="CyberDemia Optimized Standalone CMP" * * @author Alexander Yap * @version $Revision: 1.10 $ at $Date: 2004/06/24 16:25:57 $ by $Author: alexycyap $ */public abstract class UserBean extends SimpleEntityBean{ // NOTE: email should have a UNIQUE index, but XDoclet's dbindex atribute // doesn't seem to support unique index yet. So I just leave this // unindexed by XDoclet, then rely on SQL initialization script to // add a unique index. /** * Gets the email. * * @ejb.interface-method * @ejb.persistence * @jboss.persistence not-null="true" * * @return The email address. */ public abstract String getEmail(); /** * Sets the email. * * @ejb.interface-method * * @param email The email address. */ public abstract void setEmail(String email); /** * Sets the hashed password encoded in hexadecimal format. * * @ejb.interface-method * * @param password The hashed password. */ public abstract void setHashedPassword(String password); /** * Gets the hashed password encoded in hexadecimal format. * * @ejb.interface-method * @ejb.persistence * * @return The hashed password. */ public abstract String getHashedPassword(); /** * Gets the first name. * * @ejb.interface-method * @ejb.persistence * * @return The first name. */ public abstract String getFirstName(); /** * Sets the first name. * * @ejb.interface-method * * @param firstName The first name. */ public abstract void setFirstName(String firstName); /** * Sets the last name. * * @ejb.interface-method * * @param lastName The last name. */ public abstract void setLastName(String lastName); /** * Gets the last name. * * @ejb.interface-method * @ejb.persistence * @jboss.persistence not-null="true" dbindex="true" * * @return The last name. */ public abstract String getLastName(); /** * Sets the title. * * @ejb.interface-method * * @param title The title. */ public abstract void setTitle(String title); /** * Gets the title. * * @ejb.interface-method * @ejb.persistence * * @return The title. */ public abstract String getTitle(); /** * Sets last modified timestamp in milliseconds. * * @ejb.interface-method * * @param lastModified Last modified timestamp. */ public abstract void setLastModifiedMillis(long lastModified); /** * Gets last modified timestamp in milliseconds. * * @ejb.interface-method * @ejb.persistence * * @return Last modified timestamp. */ public abstract long getLastModifiedMillis(); /** * Sets the password expiry time in milliseconds. * * @ejb.interface-method * * @param passwordExpiry The password expiry time. */ public abstract void setPasswordExpiryMillis(long passwordExpiry); /** * Gets the password expiry time in milliseconds. * * @ejb.interface-method * @ejb.persistence * * @return The password expiry time. */ public abstract long getPasswordExpiryMillis(); /** * Sets LocalResourceReferences associated with this user. * * @ejb.interface-method * * @param resRefs Set of ResourceReferences. */ public abstract void setResourceReferences(Set resRefs); /** * Gets ResourceReferences associated with this user. * * @ejb.interface-method * @ejb.relation name="user-resourceReferences" role-name="user-has-resourceReferences" target-ejb="ResourceReferenceBean" target-role-name="resourceReference-belongs-to-user" * * @return Set of ResourceReferences. */ public abstract Set getResourceReferences(); /** * Gets Collection of all groups (LocalGroup interfaces) that this user belongs to. * * @ejb.interface-method * @ejb.relation name="groups-users" role-name="user-belongs-to-group" target-ejb="GroupBean" target-role-name="group-has-users" target-multiple="yes" cascade-delete="no" * @jboss.relation fk-constraint="yes" related-pk-field="id" fk-column="groupId" * @jboss.relation-table table-name="Group_User" create-table="true" remove-table="false" * * @return Collection of LocalGroup interfaces. */ public abstract Collection getGroups(); /** * Sets Collection of all groups (LocalGroup interfaces) that this user belongs to. * * @ejb.interface-method * * @param groups Collection of LocalGroup interfaces. */ public abstract void setGroups(Collection groups); /** * Gets resources within the specified category that are * associated with this user. * * @ejb.interface-method * * @param catKey Key to identify the category of resource to extract. * @return Array of LocalResourceReference objects, or empty array if there is no matching resource. */ public LocalResourceReference[] getResources( String catKey ) { Iterator resIter = getResourceReferences().iterator(); List foundList = new ArrayList(); while (resIter.hasNext()) { LocalResourceReference res = (LocalResourceReference)resIter.next(); if (catKey!=null) { if ( res.getCategoryKey().equals( catKey )) { foundList.add( res ); } } else { foundList.add(res); } } return (LocalResourceReference[])foundList.toArray( new LocalResourceReference[0] ); } /** * Associates a resource with this user. * * @ejb.interface-method * * @param res The LocalResourceReference to add. * @return true if resource is added successfully, otherwise false (it already exists). */ public boolean addResource( LocalResourceReference res ) { return getResourceReferences().add(res); } /** * Removes a resource from this user. * * @ejb.interface-method * * @param res The LocalResourceReference to remove. * @return true if resource is removed successfully, otherwise false (not found). */ public boolean removeResource( LocalResourceReference res ) { return getResourceReferences().remove(res); } /** * Removes all resources from this user. * * @ejb.interface-method * */ public void removeAllResources() { getResourceReferences().clear(); } //----------------------------- // Create methods. //----------------------------- /** * Creates a UserBean. * * @ejb.create-method view-type="local" */ public Integer ejbCreate(String loginName, String password, String email, String title, String firstName, String lastName ) throws CreateException { // Note : Unique ID is assigned by database auto-increment try { byte[] hashed = MessageDigest.getInstance("MD5").digest( password.getBytes() ); setHashedPassword( HexString.toString(hashed) ); } catch (NoSuchAlgorithmException ex) { s_logger.log(Level.SEVERE, "Cannot create MD5 MessageDigest",ex); throw new CreateException("Cannot create MD5 MessageDigest"); } setName(loginName); setEmail(email); setTitle(title); setFirstName(firstName); setLastName(lastName); long nowMillis = System.currentTimeMillis(); setCreatedMillis( nowMillis ); setLastModifiedMillis(nowMillis); setPasswordExpiryMillis(0); return null; } public void ejbPostCreate(String loginName, String password, String email, String title, String firstName, String lastName) { // Nothing here } private static Logger s_logger = Logger.getLogger( UserBean.class.getName());}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -