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

Class Class, % Method, % Line, %
CommonGrantDomainCreatePermissionPostCreateSysPersister 100% (1/ 1) 100% (7/ 7) 87% (67/ 77)


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.DomainPermission; 23 import com.acciente.oacc.DomainPermissions; 24 import com.acciente.oacc.Resource; 25 import com.acciente.oacc.sql.SQLProfile; 26  27 import java.io.Serializable; 28 import java.sql.SQLException; 29 import java.util.HashSet; 30 import java.util.Set; 31  32 public abstract class CommonGrantDomainCreatePermissionPostCreateSysPersister extends Persister implements GrantDomainCreatePermissionPostCreateSysPersister, Serializable { 33  private static final long serialVersionUID = 1L; 34  35  protected final SQLProfile sqlProfile; 36  protected final SQLStrings sqlStrings; 37  38  public CommonGrantDomainCreatePermissionPostCreateSysPersister(SQLProfile sqlProfile, 39  SQLStrings sqlStrings) { 40  this.sqlProfile = sqlProfile; 41  this.sqlStrings = sqlStrings; 42  } 43  44  @Override 45  public abstract Set<DomainCreatePermission> getDomainCreatePostCreateSysPermissionsIncludeInherited(SQLConnection connection, 46  Resource accessorResource); 47  48  @Override 49  public Set<DomainCreatePermission> getDomainCreatePostCreateSysPermissions(SQLConnection connection, 50  Resource accessorResource) { 51  SQLStatement statement = null; 52  53  try { 54  statement = connection 55  .prepareStatement(sqlStrings.SQL_findInGrantDomainCreatePermissionPostCreateSys_withoutInheritance_PostCreateSysPermissionID_PostCreateIsWithGrant_IsWithGrant_BY_AccessorID); 56  statement.setResourceId(1, accessorResource); 57  SQLResult resultSet = statement.executeQuery(); 58  59  // collect the create permissions that this resource has to domains directly 60  Set<DomainCreatePermission> domainCreatePermissions = new HashSet<>(); 61  while (resultSet.next()) { 62  domainCreatePermissions.add(getDomainCreatePostCreateSysPermission(resultSet)); 63  } 64  resultSet.close(); 65  66  return domainCreatePermissions; 67  } 68  catch (SQLException e) { 69  throw new RuntimeException(e); 70  } 71  finally { 72  closeStatement(statement); 73  } 74  } 75  76  protected static DomainCreatePermission getDomainCreatePostCreateSysPermission(SQLResult resultSet) throws SQLException { 77  final DomainPermission postCreatePermission; 78  final String postCreateSysPermissionName = resultSet.getDomainSysPermissionName("PostCreateSysPermissionId"); 79  80  if (resultSet.getBoolean("PostCreateIsWithGrant")) { 81  postCreatePermission = DomainPermissions.getInstanceWithGrantOption(postCreateSysPermissionName); 82  } 83  else { 84  postCreatePermission = DomainPermissions.getInstance(postCreateSysPermissionName); 85  } 86  87  if (resultSet.getBoolean("IsWithGrant")) { 88  return DomainCreatePermissions.getInstanceWithGrantOption(postCreatePermission); 89  } 90  else { 91  return DomainCreatePermissions.getInstance(postCreatePermission); 92  } 93  } 94  95  @Override 96  public void removeDomainCreatePostCreateSysPermissions(SQLConnection connection, 97  Resource accessorResource) { 98  SQLStatement statement = null; 99  100  try { 101  statement = connection.prepareStatement(sqlStrings.SQL_removeInGrantDomainCreatePermissionPostCreateSys_BY_AccessorID); 102  statement.setResourceId(1, accessorResource); 103  statement.executeUpdate(); 104  } 105  catch (SQLException e) { 106  throw new RuntimeException(e); 107  } 108  finally { 109  closeStatement(statement); 110  } 111  } 112  113  @Override 114  public void removeDomainCreatePostCreateSysPermissions(SQLConnection connection, 115  Resource accessorResource, 116  Set<DomainCreatePermission> domainCreatePermissions) { 117  SQLStatement statement = null; 118  119  try { 120  statement = connection.prepareStatement(sqlStrings.SQL_removeInGrantDomainCreatePermissionPostCreateSys_BY_AccessorID_PostCreateSysPermissionID); 121  for (DomainCreatePermission domainCreatePermission : domainCreatePermissions) { 122  if (!domainCreatePermission.isSystemPermission() 123  && domainCreatePermission.getPostCreateDomainPermission().isSystemPermission()) { 124  statement.setResourceId(1, accessorResource); 125  statement.setDomainSystemPermissionId(2, domainCreatePermission.getPostCreateDomainPermission().getSystemPermissionId()); 126  127  assertOneRowUpdated(statement.executeUpdate()); 128  } 129  } 130  } 131  catch (SQLException e) { 132  throw new RuntimeException(e); 133  } 134  finally { 135  closeStatement(statement); 136  } 137  } 138  139  @Override 140  public void addDomainCreatePostCreateSysPermissions(SQLConnection connection, 141  Resource accessorResource, 142  Resource grantorResource, 143  Set<DomainCreatePermission> domainCreatePermissions) { 144  SQLStatement statement = null; 145  146  try { 147  statement = connection.prepareStatement(sqlStrings.SQL_createInGrantDomainCreatePermissionPostCreateSys_WITH_AccessorID_GrantorID_IsWithGrant_PostCreateIsWithGrant_PostCreateSysPermissionID); 148  for (DomainCreatePermission domainCreatePermission : domainCreatePermissions) { 149  if (!domainCreatePermission.isSystemPermission() 150  && domainCreatePermission.getPostCreateDomainPermission().isSystemPermission()) { 151  statement.setResourceId(1, accessorResource); 152  statement.setResourceId(2, grantorResource); 153  statement.setBoolean(3, domainCreatePermission.isWithGrantOption()); 154  statement.setBoolean(4, domainCreatePermission.getPostCreateDomainPermission().isWithGrantOption()); 155  statement.setDomainSystemPermissionId(5, domainCreatePermission.getPostCreateDomainPermission().getSystemPermissionId()); 156  157  assertOneRowInserted(statement.executeUpdate()); 158  } 159  } 160  } 161  catch (SQLException e) { 162  throw new RuntimeException(e); 163  } 164  finally { 165  closeStatement(statement); 166  } 167  } 168  169  @Override 170  public void updateDomainCreatePostCreateSysPermissions(SQLConnection connection, 171  Resource accessorResource, 172  Resource grantorResource, 173  Set<DomainCreatePermission> domainCreatePermissions) { 174  SQLStatement statement = null; 175  176  try { 177  statement = connection.prepareStatement(sqlStrings.SQL_updateInGrantDomainCreatePermissionPostCreateSys_SET_GrantorID_IsWithGrant_PostCreateIsWithGrant_BY_AccessorID_PostCreateSysPermissionID); 178  for (DomainCreatePermission domainCreatePermission : domainCreatePermissions) { 179  if (!domainCreatePermission.isSystemPermission() 180  && domainCreatePermission.getPostCreateDomainPermission().isSystemPermission()) { 181  statement.setResourceId(1, grantorResource); 182  statement.setBoolean(2, domainCreatePermission.isWithGrantOption()); 183  statement.setBoolean(3, domainCreatePermission.getPostCreateDomainPermission().isWithGrantOption()); 184  statement.setResourceId(4, accessorResource); 185  statement.setDomainSystemPermissionId(5, domainCreatePermission.getPostCreateDomainPermission().getSystemPermissionId()); 186  187  assertOneRowUpdated(statement.executeUpdate()); 188  } 189  } 190  } 191  catch (SQLException e) { 192  throw new RuntimeException(e); 193  } 194  finally { 195  closeStatement(statement); 196  } 197  } 198 }