001package org.apache.fulcrum.security.hibernate; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021import java.util.List; 022 023import org.apache.fulcrum.security.entity.Group; 024import org.apache.fulcrum.security.spi.AbstractGroupManager; 025import org.apache.fulcrum.security.util.DataBackendException; 026import org.apache.fulcrum.security.util.EntityExistsException; 027import org.apache.fulcrum.security.util.GroupSet; 028import org.apache.fulcrum.security.util.UnknownEntityException; 029import org.hibernate.HibernateException; 030 031/** 032 * This implementation persists to a database via Hibernate. 033 * 034 * @author <a href="mailto:epugh@upstate.com">Eric Pugh</a> 035 * @version $Id: HibernateGroupManagerImpl.java 1374014 2012-08-16 19:47:27Z tv 036 * $ 037 */ 038@SuppressWarnings("unchecked") 039public class HibernateGroupManagerImpl extends AbstractGroupManager 040{ 041 /** 042 * 043 */ 044 private static final long serialVersionUID = 1L; 045 private PersistenceHelper persistenceHelper; 046 047 /** 048 * Retrieve a Group object with specified name. 049 * 050 * @param name 051 * the name of the Group. 052 * @return an object representing the Group with specified name. 053 * @throws DataBackendException 054 * if there was an error accessing the data backend. 055 * @throws UnknownEntityException 056 * if the group does not exist. 057 */ 058 @Override 059 public Group getGroupByName(String name) throws DataBackendException, UnknownEntityException 060 { 061 Group group = null; 062 try 063 { 064 List<Group> groups = getPersistenceHelper().retrieveSession() 065 .createQuery("from " + Group.class.getName() + " g where g.name=:name").setString("name", name.toLowerCase()).list(); 066 if (groups.size() == 0) 067 { 068 throw new UnknownEntityException("Could not find group" + name); 069 } 070 group = groups.get(0); 071 } 072 catch (HibernateException e) 073 { 074 throw new DataBackendException("Error retrieving group information", e); 075 } 076 return group; 077 } 078 079 /** 080 * Retrieves all groups defined in the system. 081 * 082 * @return the names of all groups defined in the system. 083 * @throws DataBackendException 084 * if there was an error accessing the data backend. 085 */ 086 public GroupSet getAllGroups() throws DataBackendException 087 { 088 GroupSet groupSet = new GroupSet(); 089 try 090 { 091 092 List<Group> groups = getPersistenceHelper().retrieveSession().createQuery("from " + Group.class.getName()).list(); 093 groupSet.add(groups); 094 } 095 catch (HibernateException e) 096 { 097 throw new DataBackendException("Error retrieving group information", e); 098 } 099 return groupSet; 100 } 101 102 /** 103 * Removes a Group from the system. 104 * 105 * @param group 106 * The object describing the group to be removed. 107 * @throws DataBackendException 108 * if there was an error accessing the data backend. 109 * @throws UnknownEntityException 110 * if the group does not exist. 111 */ 112 public synchronized void removeGroup(Group group) throws DataBackendException, UnknownEntityException 113 { 114 getPersistenceHelper().removeEntity(group); 115 } 116 117 /** 118 * Renames an existing Group. 119 * 120 * @param group 121 * The object describing the group to be renamed. 122 * @param name 123 * the new name for the group. 124 * @throws DataBackendException 125 * if there was an error accessing the data backend. 126 * @throws UnknownEntityException 127 * if the group does not exist. 128 */ 129 public synchronized void renameGroup(Group group, String name) throws DataBackendException, UnknownEntityException 130 { 131 boolean groupExists = false; 132 groupExists = checkExists(group); 133 if (groupExists) 134 { 135 group.setName(name); 136 getPersistenceHelper().updateEntity(group); 137 } 138 else 139 { 140 throw new UnknownEntityException("Unknown group '" + group + "'"); 141 } 142 } 143 144 /** 145 * Determines if the <code>Group</code> exists in the security system. 146 * 147 * @param groupName 148 * a <code>Group</code> value 149 * @return true if the group name exists in the system, false otherwise 150 * @throws DataBackendException 151 * when more than one Group with the same name exists. 152 */ 153 public boolean checkExists(String groupName) throws DataBackendException 154 { 155 List<Group> groups; 156 try 157 { 158 groups = getPersistenceHelper().retrieveSession().createQuery("from " + Group.class.getName() + " sg where sg.name=:name") 159 .setString("name", groupName).list(); 160 } 161 catch (HibernateException e) 162 { 163 throw new DataBackendException("Error retrieving user information", e); 164 } 165 if (groups.size() > 1) 166 { 167 throw new DataBackendException("Multiple groups with same name '" + groupName + "'"); 168 } 169 return (groups.size() == 1); 170 } 171 172 /** 173 * Creates a new group with specified attributes. 174 * 175 * @param group 176 * the object describing the group to be created. 177 * @return a new Group object that has id set up properly. 178 * @throws DataBackendException 179 * if there was an error accessing the data backend. 180 */ 181 @Override 182 protected synchronized Group persistNewGroup(Group group) throws DataBackendException 183 { 184 getPersistenceHelper().addEntity(group); 185 return group; 186 } 187 188 /** 189 * @return Returns the persistenceHelper. 190 */ 191 public PersistenceHelper getPersistenceHelper() 192 { 193 if (persistenceHelper == null) 194 { 195 persistenceHelper = (PersistenceHelper) resolve(PersistenceHelper.ROLE); 196 } 197 return persistenceHelper; 198 } 199 200 /** 201 * Retrieve a Group object with specified id. 202 * 203 * @param id 204 * the id of the Group. 205 * @return an object representing the Group with specified id. 206 * @throws DataBackendException 207 * if there was an error accessing the data backend. 208 * @throws UnknownEntityException 209 * if the group does not exist. 210 */ 211 @Override 212 public Group getGroupById(Object id) throws DataBackendException, UnknownEntityException 213 { 214 215 Group group = null; 216 217 if (id != null) 218 { 219 try 220 { 221 List<Group> groups = getPersistenceHelper().retrieveSession() 222 .createQuery("from " + Group.class.getName() + " sr where sr.id=:id").setLong("id", ((Long) id).longValue()).list(); 223 if (groups.size() == 0) 224 { 225 throw new UnknownEntityException("Could not find group by id " + id); 226 } 227 group = groups.get(0); 228 229 } 230 catch (HibernateException e) 231 { 232 throw new DataBackendException("Error retrieving group information", e); 233 } 234 } 235 236 return group; 237 } 238}