Created
October 27, 2010 14:34
-
-
Save lincolnthree/649131 to your computer and use it in GitHub Desktop.
UserService
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This file is part of SocialPM: Agile Project Management Tools (SocialPM) | |
* | |
* Copyright (c)2010 Lincoln Baxter, III <lincoln@ocpsoft.com> (OcpSoft) | |
* | |
* If you are developing and distributing open source applications under | |
* the GPL Licence, then you are free to use SocialPM under the GPL | |
* License: | |
* | |
* SocialPM is free software: you can redistribute it and/or modify | |
* it under the terms of the GNU General Public License as published by | |
* the Free Software Foundation, either version 3 of the License, or | |
* (at your option) any later version. | |
* | |
* SocialPM 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 General Public License for more details. | |
* | |
* You should have received a copy of the GNU General Public License | |
* along with SocialPM. If not, see <http://www.gnu.org/licenses/>. | |
* | |
* For OEMs, ISVs, and VARs who distribute SocialPM with their products, | |
* host their product online, OcpSoft provides flexible OEM commercial | |
* Licences. | |
* | |
* Optionally, customers may choose a Commercial License. For additional | |
* details, contact OcpSoft (http://ocpsoft.com) | |
*/ | |
package com.ocpsoft.socialpm.model; | |
import java.io.Serializable; | |
import java.util.Date; | |
import java.util.List; | |
import javax.ejb.Stateful; | |
import javax.persistence.EntityManager; | |
import javax.persistence.PersistenceContext; | |
import javax.persistence.PersistenceContextType; | |
import javax.persistence.TypedQuery; | |
import com.ocpsoft.socialpm.domain.PersistenceUtil; | |
import com.ocpsoft.socialpm.domain.feed.FeedEvent; | |
import com.ocpsoft.socialpm.domain.user.User; | |
@Stateful | |
public class FeedService extends PersistenceUtil implements Serializable | |
{ | |
private static final long serialVersionUID = 5716926734835352145L; | |
@PersistenceContext(type = PersistenceContextType.EXTENDED) | |
private EntityManager entityManager; | |
@Override | |
protected EntityManager getEntityManager() | |
{ | |
return entityManager; | |
} | |
public void addEvent(final FeedEvent event) | |
{ | |
event.setCreatedOn(new Date()); | |
create(event); | |
} | |
public List<FeedEvent> list(final int limit, final int offset) | |
{ | |
return this.findAll(FeedEvent.class); | |
} | |
public List<FeedEvent> listByUser(final User user, final int limit, final int offset) | |
{ | |
TypedQuery<FeedEvent> query = entityManager.createNamedQuery("feedEvent.byUser", FeedEvent.class) | |
.setParameter(1, user) | |
.setFirstResult(offset); | |
if (limit > 0) | |
{ | |
query.setMaxResults(limit); | |
} | |
return query.getResultList(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This file is part of SocialPM: Agile Project Management Tools (SocialPM) | |
* Copyright (c)2010 Lincoln Baxter, III <lincoln@ocpsoft.com> (OcpSoft) If you | |
* are developing and distributing open source applications under the GPL | |
* Licence, then you are free to use SocialPM under the GPL License: SocialPM is | |
* free software: you can redistribute it and/or modify it under the terms of | |
* the GNU General Public License as published by the Free Software Foundation, | |
* either version 3 of the License, or (at your option) any later version. | |
* SocialPM 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 General Public License for more details. | |
* You should have received a copy of the GNU General Public License along with | |
* SocialPM. If not, see <http://www.gnu.org/licenses/>. For OEMs, ISVs, and | |
* VARs who distribute SocialPM with their products, host their product online, | |
* OcpSoft provides flexible OEM commercial Licences. Optionally, customers may | |
* choose a Commercial License. For additional details, contact OcpSoft | |
* (http://ocpsoft.com) | |
*/ | |
package com.ocpsoft.socialpm.model; | |
import java.io.Serializable; | |
import java.util.List; | |
import javax.ejb.Stateful; | |
import javax.inject.Inject; | |
import javax.persistence.EntityManager; | |
import javax.persistence.PersistenceContext; | |
import javax.persistence.PersistenceContextType; | |
import com.ocpsoft.socialpm.domain.PersistenceUtil; | |
import com.ocpsoft.socialpm.domain.feed.UserRegistered; | |
import com.ocpsoft.socialpm.domain.user.Authority; | |
import com.ocpsoft.socialpm.domain.user.User; | |
import com.ocpsoft.socialpm.domain.user.UserPasswordMatchTester; | |
import com.ocpsoft.socialpm.domain.user.UserProfile; | |
import com.ocpsoft.socialpm.domain.user.UserSetCredentialsVisitor; | |
import com.ocpsoft.socialpm.domain.user.auth.Role; | |
import com.ocpsoft.socialpm.domain.user.auth.UserEnabled; | |
import com.ocpsoft.socialpm.domain.user.auth.UserRole; | |
import com.ocpsoft.socialpm.domain.user.auth.UserVerified; | |
import com.ocpsoft.socialpm.util.Assert; | |
import com.ocpsoft.socialpm.util.RandomGenerator; | |
import com.ocpsoft.socialpm.util.StringValidations; | |
@Stateful | |
public class UserService extends PersistenceUtil implements Serializable | |
{ | |
private static final long serialVersionUID = 2988513095024795683L; | |
@PersistenceContext(type = PersistenceContextType.EXTENDED) | |
private EntityManager em; | |
@Inject | |
private FeedService fs; | |
@Override | |
protected EntityManager getEntityManager() | |
{ | |
return em; | |
} | |
public List<User> getUsers(final int limit, final int offset) | |
{ | |
return findAll(User.class); | |
} | |
public void save(final User user) | |
{ | |
super.save(user); | |
} | |
public User getUserById(final long id) | |
{ | |
return findById(User.class, id); | |
} | |
public User getUserByName(final String name) | |
{ | |
return findUniqueByNamedQuery("user.byName", name); | |
} | |
public User getUserByEmail(final String email) | |
{ | |
return findUniqueByNamedQuery("user.byEmail", email); | |
} | |
/** | |
* Take a user object with populated username and plaintext password. Register that user, and return the pending | |
* registration key with which the user must be verified. | |
*/ | |
public String registerUser(final User user) | |
{ | |
Assert.isTrue(StringValidations.isAlphanumeric(user.getUsername()) | |
&& StringValidations.minLength(4, user.getUsername()) | |
&& StringValidations.maxLength(15, user.getUsername())); | |
Assert.isTrue(StringValidations.isPassword(user.getPassword())); | |
Assert.isTrue(StringValidations.isEmailAddress(user.getEmail())); | |
user.accept(new UserSetCredentialsVisitor(user.getUsername(), user.getPassword())); | |
user.getAuthorities().add(new UserRole(Role.GUEST)); | |
user.getAuthorities().add(new UserEnabled()); | |
UserProfile profile = new UserProfile(); | |
user.setProfile(profile); | |
profile.setUser(user); | |
user.setRegistrationKey(RandomGenerator.makeString()); | |
create(user); | |
fs.addEvent(new UserRegistered(user)); | |
return user.getRegistrationKey(); | |
} | |
public void enableAccount(final User user, final String password) | |
{ | |
if (!user.isEnabled() && passwordIs(user, password)) | |
{ | |
user.getAuthorities().add(new UserEnabled()); | |
} | |
save(user); | |
} | |
public User verifyUser(final String key) | |
{ | |
User user = findUniqueByNamedQuery("user.byRegKey", key); | |
user.getAuthorities().add(new UserVerified()); | |
save(user); | |
return user; | |
} | |
public void disableAccount(final User user) | |
{ | |
user.getAuthorities().remove(new UserEnabled()); | |
save(user); | |
} | |
public void addAuthority(final User user, final Authority authority) | |
{ | |
user.getAuthorities().add(authority); | |
save(user); | |
} | |
public boolean passwordIs(final User user, final String password) | |
{ | |
refresh(user); | |
return new UserPasswordMatchTester().passwordMatches(user, password); | |
} | |
public boolean updatePassword(final User user, final String oldPassword, final String newPassword) | |
{ | |
if (new UserPasswordMatchTester().passwordMatches(user, oldPassword)) | |
{ | |
user.accept(new UserSetCredentialsVisitor(user.getUsername(), newPassword)); | |
save(user); | |
return true; | |
} | |
return false; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2010 - Lincoln Baxter, III (lincoln@ocpsoft.com) - Licensed under | |
* the Apache License, Version 2.0 (the "License"); you may not use this file | |
* except in compliance with the License. You may obtain a copy of the License | |
* at http://www.apache.org/licenses/LICENSE-2.0 - Unless required by applicable | |
* law or agreed to in writing, software distributed under the License is | |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
* KIND, either express or implied. See the License for the specific language | |
* governing permissions and limitations under the License. | |
*/ | |
package com.ocpsoft.socialpm.domain; | |
import java.io.Serializable; | |
import java.util.List; | |
import javax.persistence.EntityManager; | |
import javax.persistence.NoResultException; | |
import javax.persistence.Query; | |
import javax.persistence.criteria.CriteriaQuery; | |
public abstract class PersistenceUtil implements Serializable | |
{ | |
private static final long serialVersionUID = -276417828563635020L; | |
protected abstract EntityManager getEntityManager(); | |
protected <T> void create(final T entity) | |
{ | |
getEntityManager().persist(entity); | |
} | |
protected <T> void delete(final T entity) throws NoSuchObjectException | |
{ | |
try | |
{ | |
getEntityManager().remove(entity); | |
} | |
catch (NoResultException e) | |
{ | |
throw new NoSuchObjectException(); | |
} | |
} | |
protected <T> T deleteById(final Class<T> type, final Long id) throws NoSuchObjectException | |
{ | |
T object = findById(type, id); | |
delete(object); | |
return object; | |
} | |
@SuppressWarnings("unchecked") | |
protected <T> T findById(final Class<T> type, final Long id) throws NoSuchObjectException | |
{ | |
Class<?> clazz = getObjectClass(type); | |
T result = (T) getEntityManager().find(clazz, id); | |
if (result == null) | |
{ | |
throw new NoSuchObjectException("No object of type: " + type + " with ID: " + id); | |
} | |
return result; | |
} | |
protected <T> void save(final T entity) | |
{ | |
getEntityManager().merge(entity); | |
} | |
protected <T> void refresh(final T entity) | |
{ | |
getEntityManager().refresh(entity); | |
} | |
protected Class<?> getObjectClass(final Object type) throws IllegalArgumentException | |
{ | |
Class<?> clazz = null; | |
if (type == null) | |
{ | |
throw new IllegalArgumentException("Null has no type. You must pass an Object"); | |
} | |
else if (type instanceof Class<?>) | |
{ | |
clazz = (Class<?>) type; | |
} | |
else | |
{ | |
clazz = type.getClass(); | |
} | |
return clazz; | |
} | |
@SuppressWarnings("unchecked") | |
protected <T> List<T> findByNamedQuery(final String namedQueryName) | |
{ | |
return getEntityManager().createNamedQuery(namedQueryName).getResultList(); | |
} | |
@SuppressWarnings("unchecked") | |
protected <T> List<T> findByNamedQuery(final String namedQueryName, final Object... params) | |
{ | |
Query query = getEntityManager().createNamedQuery(namedQueryName); | |
int i = 1; | |
for (Object p : params) | |
{ | |
query.setParameter(i++, p); | |
} | |
return query.getResultList(); | |
} | |
protected <T> List<T> findAll(final Class<T> type) | |
{ | |
CriteriaQuery<T> query = getEntityManager().getCriteriaBuilder().createQuery(type); | |
query.from(type); | |
return getEntityManager().createQuery(query).getResultList(); | |
} | |
@SuppressWarnings("unchecked") | |
protected <T> T findUniqueByNamedQuery(final String namedQueryName) throws NoSuchObjectException | |
{ | |
try | |
{ | |
return (T) getEntityManager().createNamedQuery(namedQueryName).getSingleResult(); | |
} | |
catch (NoResultException e) | |
{ | |
throw new NoSuchObjectException(e); | |
} | |
} | |
@SuppressWarnings("unchecked") | |
protected <T> T findUniqueByNamedQuery(final String namedQueryName, final Object... params) throws NoSuchObjectException | |
{ | |
Query query = getEntityManager().createNamedQuery(namedQueryName); | |
int i = 1; | |
for (Object p : params) | |
{ | |
query.setParameter(i++, p); | |
} | |
try | |
{ | |
return (T) query.getSingleResult(); | |
} | |
catch (NoResultException e) | |
{ | |
throw new NoSuchObjectException(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment