Search in sources :

Example 11 with TransactionStatus

use of org.skife.jdbi.v2.TransactionStatus in project killbill by killbill.

the class DefaultUserDao method updateRoleDefinition.

@Override
public void updateRoleDefinition(final String role, final List<String> permissions, final String createdBy) throws SecurityApiException {
    final DateTime createdDate = clock.getUTCNow();
    inTransactionWithExceptionHandling(new TransactionCallback<Void>() {

        @Override
        public Void inTransaction(final Handle handle, final TransactionStatus status) throws Exception {
            final RolesPermissionsSqlDao rolesPermissionsSqlDao = handle.attach(RolesPermissionsSqlDao.class);
            final List<RolesPermissionsModelDao> existingPermissions = rolesPermissionsSqlDao.getByRoleName(role);
            // A empty list of permissions means we should remove all current permissions
            final Iterable<RolesPermissionsModelDao> toBeDeleted = existingPermissions.isEmpty() ? existingPermissions : Iterables.filter(existingPermissions, new Predicate<RolesPermissionsModelDao>() {

                @Override
                public boolean apply(final RolesPermissionsModelDao input) {
                    return !permissions.contains(input.getPermission());
                }
            });
            final Iterable<String> toBeAdded = Iterables.filter(permissions, new Predicate<String>() {

                @Override
                public boolean apply(final String input) {
                    for (RolesPermissionsModelDao e : existingPermissions) {
                        if (e.getPermission().equals(input)) {
                            return false;
                        }
                    }
                    return true;
                }
            });
            for (RolesPermissionsModelDao d : toBeDeleted) {
                rolesPermissionsSqlDao.unactiveEvent(d.getRecordId(), createdDate, createdBy);
            }
            for (final String permission : toBeAdded) {
                rolesPermissionsSqlDao.create(new RolesPermissionsModelDao(role, permission, createdDate, createdBy));
            }
            return null;
        }
    });
}
Also used : TransactionStatus(org.skife.jdbi.v2.TransactionStatus) DateTime(org.joda.time.DateTime) SecurityApiException(org.killbill.billing.security.SecurityApiException) Handle(org.skife.jdbi.v2.Handle) Predicate(com.google.common.base.Predicate) List(java.util.List)

Aggregations

Handle (org.skife.jdbi.v2.Handle)10 TransactionStatus (org.skife.jdbi.v2.TransactionStatus)10 DateTime (org.joda.time.DateTime)7 IOException (java.io.IOException)6 DataSegment (io.druid.timeline.DataSegment)4 SQLException (java.sql.SQLException)4 List (java.util.List)4 SecurityApiException (org.killbill.billing.security.SecurityApiException)4 CallbackFailedException (org.skife.jdbi.v2.exceptions.CallbackFailedException)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 ResultSet (java.sql.ResultSet)2 ArrayList (java.util.ArrayList)2 Set (java.util.Set)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 DataSegment (org.apache.druid.timeline.DataSegment)2 TimelineObjectHolder (org.apache.druid.timeline.TimelineObjectHolder)2 SimpleHash (org.apache.shiro.crypto.hash.SimpleHash)2 ByteSource (org.apache.shiro.util.ByteSource)2 Interval (org.joda.time.Interval)2 StatementContext (org.skife.jdbi.v2.StatementContext)2