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

Class Class, % Method, % Line, %
NonRecursivePersisterHelper 100% (1/ 1) 80% (4/ 5) 89.7% (78/ 87)


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.Resource; 21 import com.acciente.oacc.sql.internal.persister.id.DomainId; 22 import com.acciente.oacc.sql.internal.persister.id.Id; 23 import com.acciente.oacc.sql.internal.persister.id.ResourceId; 24  25 import java.sql.SQLException; 26 import java.util.HashSet; 27 import java.util.LinkedHashSet; 28 import java.util.Set; 29  30 public class NonRecursivePersisterHelper { 31  protected static Set<Id<ResourceId>> getInheritedAccessorResourceIds(SQLStrings sqlStrings, 32  SQLConnection connection, 33  Resource accessorResource) { 34  SQLStatement statement = null; 35  Set<Id<ResourceId>> allAccessorResourceIds = new HashSet<>(); 36  allAccessorResourceIds.add(Id.<ResourceId>from(accessorResource.getId())); 37  Set<Id<ResourceId>> previousAccessorResourceIds = new HashSet<>(allAccessorResourceIds); 38  39  try { 40  statement = connection.prepareStatement(sqlStrings.SQL_findInGrantResourcePermissionSys_directInheritance_ResourceID_BY_AccessorID); 41  42  while (!previousAccessorResourceIds.isEmpty()) { 43  Set<Id<ResourceId>> newestAccessorResourceIds = new HashSet<>(); 44  45  for (Id<ResourceId> accessorResourceId : previousAccessorResourceIds) { 46  statement.setResourceId(1, accessorResourceId); 47  SQLResult resultSet = statement.executeQuery(); 48  49  while (resultSet.next()) { 50  newestAccessorResourceIds.add(resultSet.getResourceId("ResourceId")); 51  } 52  resultSet.close(); 53  } 54  allAccessorResourceIds.addAll(newestAccessorResourceIds); 55  previousAccessorResourceIds = newestAccessorResourceIds; 56  } 57  } 58  catch (SQLException e) { 59  throw new RuntimeException(e); 60  } 61  finally { 62  Persister.closeStatement(statement); 63  } 64  65  return allAccessorResourceIds; 66  } 67  68  protected static Set<Id<DomainId>> getDescendantDomainIdsOrderedByAscendingLevel(SQLStrings sqlStrings, 69  SQLConnection connection, 70  Id<DomainId> parentDomainId) { 71  SQLStatement statement = null; 72  Set<Id<DomainId>> allDomainIds = new LinkedHashSet<>(); 73  allDomainIds.add(parentDomainId); 74  Set<Id<DomainId>> previousDomainIds = new HashSet<>(allDomainIds); 75  76  try { 77  statement = connection.prepareStatement(sqlStrings.SQL_findInDomain_DirectDescendantResourceDomainName_BY_DomainID); 78  79  while (!previousDomainIds.isEmpty()) { 80  Set<Id<DomainId>> newestDomainIds = new HashSet<>(); 81  82  for (Id<DomainId> domainId : previousDomainIds) { 83  statement.setResourceDomainId(1, domainId); 84  SQLResult resultSet = statement.executeQuery(); 85  86  while (resultSet.next()) { 87  newestDomainIds.add(resultSet.getResourceDomainId("DomainId")); 88  } 89  resultSet.close(); 90  } 91  allDomainIds.addAll(newestDomainIds); 92  previousDomainIds = newestDomainIds; 93  } 94  } 95  catch (SQLException e) { 96  throw new RuntimeException(e); 97  } 98  finally { 99  Persister.closeStatement(statement); 100  } 101  102  return allDomainIds; 103  } 104  105  protected static Set<String> getDescendantDomainNames(SQLStrings sqlStrings, 106  SQLConnection connection, 107  String parentDomainName) { 108  SQLStatement statement = null; 109  Set<String> allDomainNames = new HashSet<>(); 110  allDomainNames.add(parentDomainName); 111  Set<String> previousDomainNames = new HashSet<>(allDomainNames); 112  113  try { 114  statement = connection.prepareStatement(sqlStrings.SQL_findInDomain_DirectDescendantResourceDomainName_BY_ResourceDomainName); 115  116  while (!previousDomainNames.isEmpty()) { 117  Set<String> newestDomainNames = new HashSet<>(); 118  119  for (String domainName : previousDomainNames) { 120  statement.setString(1, domainName); 121  SQLResult resultSet = statement.executeQuery(); 122  123  while (resultSet.next()) { 124  newestDomainNames.add(resultSet.getString("DomainName")); 125  } 126  resultSet.close(); 127  } 128  allDomainNames.addAll(newestDomainNames); 129  previousDomainNames = newestDomainNames; 130  } 131  } 132  catch (SQLException e) { 133  throw new RuntimeException(e); 134  } 135  finally { 136  Persister.closeStatement(statement); 137  } 138  139  return allDomainNames; 140  } 141  142  protected static Set<Id<DomainId>> getAncestorDomainIds(SQLStrings sqlStrings, 143  SQLConnection connection, 144  Id<DomainId> domainId) { 145  SQLStatement statement = null; 146  Set<Id<DomainId>> ancestorDomainIds = new HashSet<>(); 147  ancestorDomainIds.add(domainId); 148  int previousSize = 0; 149  150  try { 151  statement = connection.prepareStatement(sqlStrings.SQL_findInDomain_ParentResourceDomainName_BY_DomainID); 152  Id<DomainId> parentDomainId = domainId; 153  154  while (previousSize < ancestorDomainIds.size()) { 155  previousSize = ancestorDomainIds.size(); 156  statement.setResourceDomainId(1, parentDomainId); 157  SQLResult resultSet = statement.executeQuery(); 158  159  if (resultSet.next()) { 160  parentDomainId = resultSet.getResourceDomainId("DomainId"); 161  ancestorDomainIds.add(parentDomainId); 162  } 163  resultSet.close(); 164  } 165  } 166  catch (SQLException e) { 167  throw new RuntimeException(e); 168  } 169  finally { 170  Persister.closeStatement(statement); 171  } 172  173  return ancestorDomainIds; 174  } 175 }