CCM NG: Replaced usage of Java 8 method List#sort(Comparator) with Collections.sort(List, Comparator) which is available in Java 7.
git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@3538 8810af33-2d31-482b-a856-94f89814c4dfpull/2/head
parent
f3a443a6ab
commit
96091995d0
|
|
@ -28,16 +28,35 @@ import javax.persistence.Entity;
|
||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.GenerationType;
|
import javax.persistence.GenerationType;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.NamedQueries;
|
||||||
|
import javax.persistence.NamedQuery;
|
||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
import javax.xml.bind.annotation.XmlElement;
|
import javax.xml.bind.annotation.XmlElement;
|
||||||
import javax.xml.bind.annotation.XmlRootElement;
|
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 <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
|
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
|
||||||
*/
|
*/
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "ccm_privileges")
|
@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)
|
@XmlRootElement(name = "privilege", namespace = CORE_XML_NS)
|
||||||
public class Privilege implements Serializable {
|
public class Privilege implements Serializable {
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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 <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
|
||||||
|
*/
|
||||||
|
@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<Privilege> 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<Integer> query = entityManager.createNamedQuery(
|
||||||
|
"isPrivilegeInUse", Integer.class);
|
||||||
|
query.setParameter("name", privilegeName);
|
||||||
|
|
||||||
|
final Integer result = query.getSingleResult();
|
||||||
|
|
||||||
|
return result > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -53,9 +53,12 @@ import java.util.Set;
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
import javax.persistence.TypedQuery;
|
import javax.persistence.TypedQuery;
|
||||||
|
|
||||||
import org.jboss.arquillian.container.test.api.ShouldThrowException;
|
import org.jboss.arquillian.container.test.api.ShouldThrowException;
|
||||||
import org.jboss.arquillian.persistence.ShouldMatchDataSet;
|
import org.jboss.arquillian.persistence.ShouldMatchDataSet;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
|
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
|
||||||
|
|
@ -219,7 +222,7 @@ public class PermissionRepositoryTest {
|
||||||
final List<Permission> jdoePermissions = permissionRepository
|
final List<Permission> jdoePermissions = permissionRepository
|
||||||
.findPermissionsForUser(jdoe);
|
.findPermissionsForUser(jdoe);
|
||||||
assertThat(jdoePermissions.size(), is(4));
|
assertThat(jdoePermissions.size(), is(4));
|
||||||
jdoePermissions.sort(new Comparator<Permission>() {
|
Collections.sort(jdoePermissions, new Comparator<Permission>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compare(final Permission permission1,
|
public int compare(final Permission permission1,
|
||||||
|
|
@ -294,7 +297,7 @@ public class PermissionRepositoryTest {
|
||||||
final List<Permission> object1Permissions = permissionRepository
|
final List<Permission> object1Permissions = permissionRepository
|
||||||
.findPermissionsForCcmObject(object1);
|
.findPermissionsForCcmObject(object1);
|
||||||
assertThat(object1Permissions.size(), is(3));
|
assertThat(object1Permissions.size(), is(3));
|
||||||
object1Permissions.sort(new Comparator<Permission>() {
|
Collections.sort(object1Permissions, new Comparator<Permission>() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int compare(final Permission permission1,
|
public int compare(final Permission permission1,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue