diff --git a/ccm-core/src/main/java/org/libreccm/core/Privilege.java b/ccm-core/src/main/java/org/libreccm/core/Privilege.java
index 756194b6f..d55559318 100644
--- a/ccm-core/src/main/java/org/libreccm/core/Privilege.java
+++ b/ccm-core/src/main/java/org/libreccm/core/Privilege.java
@@ -28,16 +28,35 @@ import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
+import javax.persistence.NamedQueries;
+import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
/**
+ * Privileges are used the create {@link Permission}s for specific operations.
+ * Modules can create {@code Privilege}s by using the
+ * {@link PrivilegeRepository}.
+ *
+ * This class is an JPA implementation of the PDL object type
+ * {@code com.arsdigita.kernel.permissions.Privilege} which has been implemented
+ * as an JPA entity. In future releases this class will may refactored to an
+ * {@code enum}. After the class has been refactored to an {@code enum} it is
+ * not longer necessary to store the available privileges in the database.
*
* @author Jens Pelzetter
*/
@Entity
@Table(name = "ccm_privileges")
+@NamedQueries({
+ @NamedQuery(name = "findPrivilegeByName",
+ query = "SELECT p FROM Privilege p "
+ + "WHERE p.privilege = :name"),
+ @NamedQuery(name = "isPrivilegeInUse",
+ query = "SELECT COUNT(p) FROM Permission p JOIN Privilege r "
+ + "WHERE r.privilege = :name")
+})
@XmlRootElement(name = "privilege", namespace = CORE_XML_NS)
public class Privilege implements Serializable {
diff --git a/ccm-core/src/main/java/org/libreccm/core/PrivilegeRepository.java b/ccm-core/src/main/java/org/libreccm/core/PrivilegeRepository.java
new file mode 100644
index 000000000..2646c04c5
--- /dev/null
+++ b/ccm-core/src/main/java/org/libreccm/core/PrivilegeRepository.java
@@ -0,0 +1,105 @@
+/*
+ * Copyright (C) 2015 LibreCCM Foundation.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301 USA
+ */
+package org.libreccm.core;
+
+import javax.enterprise.context.RequestScoped;
+import javax.inject.Inject;
+import javax.persistence.EntityManager;
+import javax.persistence.TypedQuery;
+
+/**
+ * Repository class for retrieving and creating new {@link Privilege}s.
+ *
+ * When the {@link Privilege} class is refactored to an enum this class will be
+ * removed.
+ *
+ * @author Jens Pelzetter
+ */
+@RequestScoped
+public class PrivilegeRepository {
+
+ @Inject
+ private transient EntityManager entityManager;
+
+ /**
+ * Finds the {@link Privilege} identified by {@code name}.
+ *
+ * @param privilege The name of the privilege to return.
+ *
+ * @return
+ */
+ public Privilege retrievePrivilege(final String privilege) {
+ final TypedQuery query = entityManager.createNamedQuery(
+ "findPrivilegeByName", Privilege.class);
+ query.setParameter("name", privilege);
+
+ return query.getSingleResult();
+ }
+
+ /**
+ * Creates a new {@link Privilege}.
+ *
+ * This method can only be invoked by the system user.
+ *
+ * ToDo: Check if current user is system user.
+ *
+ * @param privilegeName The privilege to create.
+ *
+ * @return The new privilege.
+ */
+ public Privilege createPrivilege(final String privilegeName) {
+ final Privilege privilege = new Privilege();
+ privilege.setPrivilege(privilegeName);
+
+ entityManager.persist(privilege);
+
+ return privilege;
+ }
+
+ /**
+ * Deletes a {@link Privilege}.
+ *
+ * ToDo: Check if current user is system user.
+ *
+ * @param privilegeName The privilege to delete.
+ */
+ public void deletePrivilege(final String privilegeName) {
+ final Privilege privilege = retrievePrivilege(privilegeName);
+
+ if (isPrivilegeInUse(privilegeName)) {
+ throw new IllegalArgumentException(
+ "Provided privilage can't be removed because its still in use");
+ }
+
+ if (privilege != null) {
+ entityManager.remove(privilege);
+ }
+ }
+
+ public boolean isPrivilegeInUse(final String privilegeName) {
+ final TypedQuery query = entityManager.createNamedQuery(
+ "isPrivilegeInUse", Integer.class);
+ query.setParameter("name", privilegeName);
+
+ final Integer result = query.getSingleResult();
+
+ return result > 0;
+ }
+
+}
diff --git a/ccm-core/src/test/java/org/libreccm/core/PermissionRepositoryTest.java b/ccm-core/src/test/java/org/libreccm/core/PermissionRepositoryTest.java
index b4467bdf8..985fa4066 100644
--- a/ccm-core/src/test/java/org/libreccm/core/PermissionRepositoryTest.java
+++ b/ccm-core/src/test/java/org/libreccm/core/PermissionRepositoryTest.java
@@ -53,9 +53,12 @@ import java.util.Set;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
+
import org.jboss.arquillian.container.test.api.ShouldThrowException;
import org.jboss.arquillian.persistence.ShouldMatchDataSet;
+import java.util.Collections;
+
/**
*
* @author Jens Pelzetter
@@ -219,7 +222,7 @@ public class PermissionRepositoryTest {
final List jdoePermissions = permissionRepository
.findPermissionsForUser(jdoe);
assertThat(jdoePermissions.size(), is(4));
- jdoePermissions.sort(new Comparator() {
+ Collections.sort(jdoePermissions, new Comparator() {
@Override
public int compare(final Permission permission1,
@@ -294,7 +297,7 @@ public class PermissionRepositoryTest {
final List object1Permissions = permissionRepository
.findPermissionsForCcmObject(object1);
assertThat(object1Permissions.size(), is(3));
- object1Permissions.sort(new Comparator() {
+ Collections.sort(object1Permissions, new Comparator() {
@Override
public int compare(final Permission permission1,