Coverage Summary for Class: CommonGrantDomainCreatePermissionSysPersister (com.acciente.oacc.sql.internal.persister)

Class Class, % Method, % Line, %
CommonGrantDomainCreatePermissionSysPersister 100% (1/ 1) 100% (7/ 7) 86.1% (62/ 72)


1 /* 2  * Copyright 2009-2018, Acciente LLC 3  * 4  * Acciente LLC licenses this file to you under the 5  * Apache License, Version 2.0 (the "License"); you 6  * may not use this file except in compliance with the 7  * License. You may obtain a copy of the License at 8  * 9  * http://www.apache.org/licenses/LICENSE-2.0 10  * 11  * Unless required by applicable law or agreed to in 12  * writing, software distributed under the License is 13  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES 14  * OR CONDITIONS OF ANY KIND, either express or implied. 15  * See the License for the specific language governing 16  * permissions and limitations under the License. 17  */ 18 package com.acciente.oacc.sql.internal.persister; 19  20 import com.acciente.oacc.DomainCreatePermission; 21 import com.acciente.oacc.DomainCreatePermissions; 22 import com.acciente.oacc.Resource; 23 import com.acciente.oacc.sql.SQLProfile; 24  25 import java.io.Serializable; 26 import java.sql.SQLException; 27 import java.util.HashSet; 28 import java.util.Set; 29  30 public abstract class CommonGrantDomainCreatePermissionSysPersister extends Persister implements GrantDomainCreatePermissionSysPersister, Serializable { 31  private static final long serialVersionUID = 1L; 32  33  protected final SQLProfile sqlProfile; 34  protected final SQLStrings sqlStrings; 35  36  public CommonGrantDomainCreatePermissionSysPersister(SQLProfile sqlProfile, 37  SQLStrings sqlStrings) { 38  this.sqlProfile = sqlProfile; 39  this.sqlStrings = sqlStrings; 40  } 41  42  @Override 43  public abstract Set<DomainCreatePermission> getDomainCreateSysPermissionsIncludeInherited(SQLConnection connection, 44  Resource accessorResource); 45  46  @Override 47  public Set<DomainCreatePermission> getDomainCreateSysPermissions(SQLConnection connection, 48  Resource accessorResource) { 49  SQLStatement statement = null; 50  51  try { 52  statement = connection 53  .prepareStatement(sqlStrings.SQL_findInGrantDomainCreatePermissionSys_withoutInheritance_SysPermissionID_BY_AccessorID); 54  statement.setResourceId(1, accessorResource); 55  SQLResult resultSet = statement.executeQuery(); 56  57  // first collect the create permissions that this resource has to domains directly 58  Set<DomainCreatePermission> domainCreatePermissions = new HashSet<>(); 59  while (resultSet.next()) { 60  domainCreatePermissions.add(getDomainCreateSysPermission(resultSet)); 61  } 62  resultSet.close(); 63  64  return domainCreatePermissions; 65  } 66  catch (SQLException e) { 67  throw new RuntimeException(e); 68  } 69  finally { 70  closeStatement(statement); 71  } 72  } 73  74  protected static DomainCreatePermission getDomainCreateSysPermission(SQLResult resultSet) throws SQLException { 75  final String sysPermissionName = resultSet.getDomainCreateSysPermissionName("SysPermissionId"); 76  77  if (resultSet.getBoolean("IsWithGrant")) { 78  return DomainCreatePermissions.getInstanceWithGrantOption(sysPermissionName); 79  } 80  else { 81  return DomainCreatePermissions.getInstance(sysPermissionName); 82  } 83  } 84  85  @Override 86  public void addDomainCreateSysPermissions(SQLConnection connection, 87  Resource accessorResource, 88  Resource grantorResource, 89  Set<DomainCreatePermission> domainCreatePermissions) { 90  SQLStatement statement = null; 91  92  try { 93  statement = connection.prepareStatement(sqlStrings.SQL_createInGrantDomainCreatePermissionSys_WITH_AccessorID_GrantorID_IsWithGrant_SysPermissionID); 94  for (DomainCreatePermission domainCreatePermission : domainCreatePermissions) { 95  if (domainCreatePermission.isSystemPermission()) { 96  statement.setResourceId(1, accessorResource); 97  statement.setResourceId(2, grantorResource); 98  statement.setBoolean(3, domainCreatePermission.isWithGrantOption()); 99  statement.setDomainCreateSystemPermissionId(4, domainCreatePermission.getSystemPermissionId()); 100  101  assertOneRowInserted(statement.executeUpdate()); 102  } 103  } 104  } 105  catch (SQLException e) { 106  throw new RuntimeException(e); 107  } 108  finally { 109  closeStatement(statement); 110  } 111  } 112  113  @Override 114  public void updateDomainCreateSysPermissions(SQLConnection connection, 115  Resource accessorResource, 116  Resource grantorResource, 117  Set<DomainCreatePermission> domainCreatePermissions) { 118  SQLStatement statement = null; 119  120  try { 121  statement = connection.prepareStatement(sqlStrings.SQL_updateInGrantDomainCreatePermissionSys_SET_GrantorID_IsWithGrant_BY_AccessorID_SysPermissionID); 122  for (DomainCreatePermission domainCreatePermission : domainCreatePermissions) { 123  if (domainCreatePermission.isSystemPermission()) { 124  statement.setResourceId(1, grantorResource); 125  statement.setBoolean(2, domainCreatePermission.isWithGrantOption()); 126  statement.setResourceId(3, accessorResource); 127  statement.setDomainCreateSystemPermissionId(4, domainCreatePermission.getSystemPermissionId()); 128  129  assertOneRowUpdated(statement.executeUpdate()); 130  } 131  } 132  } 133  catch (SQLException e) { 134  throw new RuntimeException(e); 135  } 136  finally { 137  closeStatement(statement); 138  } 139  } 140  141  @Override 142  public void removeDomainCreateSysPermissions(SQLConnection connection, 143  Resource accessorResource) { 144  SQLStatement statement = null; 145  146  try { 147  statement = connection.prepareStatement(sqlStrings.SQL_removeInGrantDomainCreatePermissionSys_BY_AccessorID); 148  statement.setResourceId(1, accessorResource); 149  statement.executeUpdate(); 150  } 151  catch (SQLException e) { 152  throw new RuntimeException(e); 153  } 154  finally { 155  closeStatement(statement); 156  } 157  } 158  159  @Override 160  public void removeDomainCreateSysPermissions(SQLConnection connection, 161  Resource accessorResource, 162  Set<DomainCreatePermission> domainCreatePermissions) { 163  SQLStatement statement = null; 164  165  try { 166  statement = connection.prepareStatement(sqlStrings.SQL_removeInGrantDomainCreatePermissionSys_BY_AccessorID_SysPermissionID); 167  for (DomainCreatePermission domainCreatePermission : domainCreatePermissions) { 168  if (domainCreatePermission.isSystemPermission()) { 169  statement.setResourceId(1, accessorResource); 170  statement.setDomainCreateSystemPermissionId(2, domainCreatePermission.getSystemPermissionId()); 171  172  assertOneRowUpdated(statement.executeUpdate()); 173  } 174  } 175  } 176  catch (SQLException e) { 177  throw new RuntimeException(e); 178  } 179  finally { 180  closeStatement(statement); 181  } 182  } 183 }