/*
- * Copyright 2005 the original author or authors.
+ * Copyright 2005-2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
package org.wamblee.usermgt.hibernate;
-import java.util.List;
-import java.util.Set;
-import java.util.TreeSet;
-
import org.wamblee.persistence.hibernate.HibernateSupport;
+
import org.wamblee.usermgt.Group;
import org.wamblee.usermgt.GroupSet;
+import java.util.List;
+import java.util.Set;
+import java.util.TreeSet;
+
/**
- * Set of groups backed by the database.
- *
+ * Set of groups backed by the database.
+ *
* @author Erik Brakkee
*/
public class HibernateGroupSet extends HibernateSupport implements GroupSet {
-
-
private static final String QUERY_FIND_BY_NAME = "findGroupByName";
-
+
private static final String PARAM_NAME = "name";
-
+
private static final String QUERY_COUNT_GROUPS = "countGroups";
-
+
+ /**
+ * Creates a new HibernateGroupSet object.
+ */
public HibernateGroupSet() {
// Empty
}
-
- /* (non-Javadoc)
- * @see org.wamblee.usermgt.GroupSet#groupModified(org.wamblee.usermgt.Group)
+
+ /*
+ * (non-Javadoc)
+ *
+ * @see
+ * org.wamblee.usermgt.GroupSet#groupModified(org.wamblee.usermgt.Group)
*/
public void groupModified(Group aGroup) {
- assert aGroup.getPrimaryKey() != null;
- super.merge(aGroup);
+ assert aGroup.getPrimaryKey() != null;
+ super.merge(aGroup);
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see org.wamblee.usermgt.GroupSet#find(java.lang.String)
*/
public Group find(String aName) {
- List list = getHibernateTemplate().findByNamedQueryAndNamedParam(QUERY_FIND_BY_NAME, PARAM_NAME, aName);
- if ( list.size() > 1 ) {
- throw new RuntimeException("More than one group with the same name '" + aName + "'");
+ List list = getHibernateTemplate().findByNamedQueryAndNamedParam(
+ QUERY_FIND_BY_NAME, PARAM_NAME, aName);
+
+ if (list.size() > 1) {
+ throw new RuntimeException(
+ "More than one group with the same name '" + aName + "'");
}
- if ( list.size() == 0 ) {
- return null;
+
+ if (list.size() == 0) {
+ return null;
}
- return new Group((Group)list.get(0));
+
+ return new Group((Group) list.get(0));
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see org.wamblee.usermgt.GroupSet#contains(org.wamblee.usermgt.Group)
*/
public boolean contains(Group aGroup) {
- return find(aGroup.getName()) != null;
+ return find(aGroup.getName()) != null;
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see org.wamblee.usermgt.GroupSet#add(org.wamblee.usermgt.Group)
*/
public boolean add(Group aGroup) {
- assert aGroup.getPrimaryKey() == null;
- if ( contains(aGroup) ) {
- return false;
+ assert aGroup.getPrimaryKey() == null;
+
+ if (contains(aGroup)) {
+ return false;
}
+
super.merge(aGroup);
- return true;
+
+ return true;
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see org.wamblee.usermgt.GroupSet#remove(org.wamblee.usermgt.Group)
*/
public boolean remove(Group aGroup) {
- assert aGroup.getPrimaryKey() != null;
- if ( !contains(aGroup)) {
- return false;
+ assert aGroup.getPrimaryKey() != null;
+
+ if (!contains(aGroup)) {
+ return false;
}
- Group group = (Group) getHibernateTemplate().merge(aGroup);
+
+ Group group = (Group) getHibernateTemplate().merge(aGroup);
getHibernateTemplate().delete(group);
- aGroup.setPrimaryKey(null);
+ aGroup.setPrimaryKey(null);
aGroup.setPersistedVersion(-1);
- return true;
+
+ return true;
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see org.wamblee.usermgt.GroupSet#list()
*/
public Set<Group> list() {
- Set<Group> groups = new TreeSet<Group>();
+ Set<Group> groups = new TreeSet<Group>();
List<Group> list = getHibernateTemplate().loadAll(Group.class);
- for (Group group: list) {
+
+ for (Group group : list) {
groups.add(new Group(group));
}
- return groups;
+
+ return groups;
}
- /* (non-Javadoc)
+ /*
+ * (non-Javadoc)
+ *
* @see org.wamblee.usermgt.GroupSet#size()
*/
public int size() {
- Long result = (Long) getHibernateTemplate().findByNamedQuery(QUERY_COUNT_GROUPS).get(0);
- return result.intValue();
+ Long result = (Long) getHibernateTemplate().findByNamedQuery(
+ QUERY_COUNT_GROUPS).get(0);
+
+ return result.intValue();
}
}