Source Code Cross Referenced for UserDao.java in  » Project-Management » EmForce » ru » emdev » EmForge » security » dao » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Project Management » EmForce » ru.emdev.EmForge.security.dao 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package ru.emdev.EmForge.security.dao;
002:
003:        import java.util.Collection;
004:        import java.util.Date;
005:        import java.util.List;
006:
007:        import org.acegisecurity.userdetails.UserDetails;
008:        import org.acegisecurity.userdetails.UsernameNotFoundException;
009:        import org.emforge.projectmanager.base.ProjectRole;
010:        import org.springframework.dao.DataAccessException;
011:        import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
012:
013:        import ru.emdev.EmForge.security.EmForgeUserDetailsService;
014:        import ru.emdev.EmForge.security.web.SiteRole;
015:
016:        /**
017:         *
018:         */
019:        public class UserDao extends HibernateDaoSupport implements 
020:                EmForgeUserDetailsService {
021:
022:            /**
023:             * @see org.springframework.dao.support.DaoSupport#initDao()
024:             */
025:            @Override
026:            protected void initDao() throws Exception {
027:
028:                super .initDao();
029:
030:                // initialize default site roles
031:                for (SiteRole role : SiteRole.values()) {
032:                    initializeRole(role.getId(), role.getName());
033:                }
034:            }
035:
036:            /**
037:             * @return all existing roles
038:             */
039:            @SuppressWarnings("unchecked")
040:            public Collection<Role> getAllRoles() {
041:
042:                Collection roles = getHibernateTemplate().loadAll(Role.class);
043:
044:                return roles;
045:            }
046:
047:            /**
048:             * @see ru.emdev.EmForge.security.EmForgeUserDetailsService#getAllUsers()
049:             */
050:            @SuppressWarnings("unchecked")
051:            public Collection<UserDetails> getAllUsers() {
052:
053:                Collection users = getHibernateTemplate().loadAll(User.class);
054:
055:                return users;
056:            }
057:
058:            /**
059:             * @see org.acegisecurity.userdetails.UserDetailsService#loadUserByUsername(java.lang.String)
060:             */
061:            public UserDetails loadUserByUsername(String username)
062:                    throws UsernameNotFoundException, DataAccessException {
063:
064:                List users = getHibernateTemplate().findByNamedQuery(
065:                        "User.loadUserByName", username);
066:
067:                // get firstelement
068:                if (users.size() > 0) {
069:                    return (UserDetails) users.get(0);
070:                } else {
071:                    throw new UsernameNotFoundException("Username: " + username
072:                            + " not found in database");
073:                }
074:            }
075:
076:            /**
077:             * @param i_user
078:             */
079:            public void saveUser(User i_user) {
080:
081:                if (i_user.getId() == null) {
082:                    i_user.setRegisteredAt(new Date());
083:                }
084:                getHibernateTemplate().saveOrUpdate(i_user);
085:                getHibernateTemplate().flush();
086:            }
087:
088:            /**
089:             * @param i_role
090:             */
091:            public void saveRole(Role i_role) {
092:
093:                getHibernateTemplate().saveOrUpdate(i_role);
094:            }
095:
096:            /**
097:             * Seems like we should do it manually, not delegate this to hibernate or db
098:             * 
099:             * @param i_role
100:             */
101:            public void deleteRole(Role i_role) {
102:
103:                getHibernateTemplate().deleteAll(
104:                        getHibernateTemplate().findByNamedQuery(
105:                                "ProjectRole.findByRole", i_role.getId()));
106:                getHibernateTemplate().deleteAll(
107:                        getHibernateTemplate().findByNamedQuery(
108:                                "ProjectUserRole.findByRole", i_role.getId()));
109:                getHibernateTemplate().delete(i_role);
110:                getHibernateTemplate().flush();
111:            }
112:
113:            /**
114:             * @param projectRole
115:             */
116:            public void saveProjectRole(ProjectRole projectRole) {
117:
118:                getHibernateTemplate().saveOrUpdate(projectRole);
119:            }
120:
121:            /**
122:             * @param i_roleName is role name to search
123:             * @return <code>Role</code> if specified role exists, otherwise <code>null</code>
124:             */
125:            public Role getRoleByName(String i_roleName) {
126:
127:                List<?> roles = getHibernateTemplate().findByNamedQuery(
128:                        "Role.loadRoleByName", i_roleName);
129:
130:                // get first element
131:                if (roles.size() > 0) {
132:                    return (Role) roles.get(0);
133:                } else {
134:                    return null;
135:                }
136:            }
137:
138:            /**
139:             * Creates required role if it does not exist and return it
140:             * 
141:             * @param i_roleName is role name to get
142:             * @return Required <code>Role</code>
143:             */
144:            public Role ensureRole(String i_roleName) {
145:
146:                Role role = getRoleByName(i_roleName);
147:                if (role == null) {
148:                    role = new Role();
149:                    role.setName(i_roleName);
150:                    saveRole(role);
151:                }
152:                return role;
153:            }
154:
155:            /**
156:             * @param rolename
157:             * @param projectId
158:             * @return
159:             */
160:            public ProjectRole getEmailByRoleNameAndProjectId(String rolename,
161:                    Long projectId) {
162:
163:                List projectRoles = getHibernateTemplate().findByNamedQuery(
164:                        "ProjectRole.loadEmailByRoleNameAndProjectId",
165:                        new Object[] { rolename, projectId });
166:                // get first element
167:                if (projectRoles.size() > 0) {
168:                    return (ProjectRole) projectRoles.get(0);
169:                } else {
170:                    return null;
171:                }
172:            }
173:
174:            /**
175:             * Activate User by Activation Code
176:             * 
177:             * @returns true if user with specified code was found and activated, false - if not
178:             */
179:            public boolean activateUser(String activationCode) {
180:
181:                List users = getHibernateTemplate().findByNamedQuery(
182:                        "User.loadUserByCode", activationCode);
183:
184:                // get firstelement
185:                if (users.size() == 1) {
186:                    User user = (User) users.get(0);
187:                    user.setActive(true);
188:                    saveUser(user);
189:
190:                    return true;
191:                } else {
192:                    return false;
193:                }
194:            }
195:
196:            /**
197:             * @param i_roleName
198:             * @param i_roleDisplayName
199:             */
200:            protected void initializeRole(String i_roleName,
201:                    String i_roleDisplayName) {
202:
203:                if (getRoleByName(i_roleName) == null) {
204:                    Role role = new Role();
205:                    role.setName(i_roleName);
206:                    role.setDisplayName(i_roleDisplayName);
207:                    role.setRoleType(Role.ROLE_TYPE_SITE);
208:
209:                    saveRole(role);
210:                }
211:            }
212:
213:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.