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

Class Class, % Method, % Line, %
ResourceClassPermissionPersister 100% (1/ 1) 100% (4/ 4) 84.2% (32/ 38)


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.sql.SQLProfile; 21 import com.acciente.oacc.sql.internal.persister.id.Id; 22 import com.acciente.oacc.sql.internal.persister.id.ResourceClassId; 23 import com.acciente.oacc.sql.internal.persister.id.ResourcePermissionId; 24  25 import java.io.Serializable; 26 import java.sql.SQLException; 27 import java.util.LinkedList; 28 import java.util.List; 29  30 public class ResourceClassPermissionPersister extends Persister implements Serializable { 31  private static final long serialVersionUID = 1L; 32  33  protected final SQLProfile sqlProfile; 34  private final SQLStrings sqlStrings; 35  36  public ResourceClassPermissionPersister(SQLProfile sqlProfile, 37  SQLStrings sqlStrings) { 38  this.sqlProfile = sqlProfile; 39  this.sqlStrings = sqlStrings; 40  } 41  42  public Id<ResourcePermissionId> getResourceClassPermissionId(SQLConnection connection, 43  Id<ResourceClassId> resourceClassId, 44  String permissionName) { 45  SQLStatement statement = null; 46  try { 47  Id<ResourcePermissionId> permissionId = null; 48  49  // check if the permission name is already defined! 50  statement = connection.prepareStatement(sqlStrings.SQL_findInResourceClassPermission_PermissionID_BY_ResourceClassID_PermissionName); 51  statement.setResourceClassId(1, resourceClassId); 52  statement.setString(2, permissionName); 53  SQLResult resultSet = statement.executeQuery(); 54  55  if (resultSet.next()) { 56  permissionId = resultSet.getResourcePermissionId("PermissionId"); 57  } 58  resultSet.close(); 59  60  return permissionId; 61  } 62  catch (SQLException e) { 63  throw new RuntimeException(e); 64  } 65  finally { 66  closeStatement(statement); 67  } 68  } 69  70  public List<String> getPermissionNames(SQLConnection connection, String resourceClassName) { 71  SQLStatement statement = null; 72  73  try { 74  List<String> resourceClassNames = new LinkedList<>(); 75  76  statement = connection.prepareStatement(sqlStrings.SQL_findInResourceClassPermission_PermissionName_BY_ResourceClassName); 77  statement.setString(1, resourceClassName); 78  SQLResult resultSet = statement.executeQuery(); 79  80  while (resultSet.next()) { 81  resourceClassNames.add(resultSet.getString("PermissionName")); 82  } 83  84  return resourceClassNames; 85  } 86  catch (SQLException e) { 87  throw new RuntimeException(e); 88  } 89  finally { 90  closeStatement(statement); 91  } 92  } 93  94  public void addResourceClassPermission(SQLConnection connection, 95  Id<ResourceClassId> resourceClassId, 96  String permissionName) { 97  SQLStatement statement = null; 98  99  try { 100  // finally add the new permission 101  statement = connection.prepareStatement(sqlStrings.SQL_createInResourceClassPermission_WITH_ResourceClassID_PermissionName); 102  statement.setResourceClassId(1, resourceClassId); 103  statement.setString(2, permissionName); 104  assertOneRowInserted(statement.executeUpdate()); 105  } 106  catch (SQLException e) { 107  throw new RuntimeException(e); 108  } 109  finally { 110  closeStatement(statement); 111  } 112  } 113 }