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

Class Class, % Method, % Line, %
RecursiveDomainPersister 100% (1/ 1) 100% (3/ 3) 56.8% (21/ 37)


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.DomainId; 22 import com.acciente.oacc.sql.internal.persister.id.Id; 23  24 import java.io.Serializable; 25 import java.sql.SQLException; 26 import java.util.ArrayList; 27 import java.util.HashSet; 28 import java.util.List; 29 import java.util.Set; 30  31 public class RecursiveDomainPersister extends CommonDomainPersister implements Serializable { 32  private static final long serialVersionUID = 1L; 33  34  public RecursiveDomainPersister(SQLProfile sqlProfile, 35  SQLStrings sqlStrings) { 36  super(sqlProfile, sqlStrings); 37  } 38  39  @Override 40  public Set<String> getResourceDomainNameDescendants(SQLConnection connection, 41  String resourceDomainName) { 42  SQLStatement statement = null; 43  44  try { 45  Set<String> resourceDomainNameDescendants; 46  47  statement = connection.prepareStatement(sqlStrings.SQL_findInDomain_DescendantResourceDomainName_BY_ResourceDomainName); 48  statement.setString(1, resourceDomainName); 49  SQLResult resultSet = statement.executeQuery(); 50  51  resourceDomainNameDescendants = new HashSet<>(); 52  53  while (resultSet.next()) { 54  resourceDomainNameDescendants.add(resultSet.getString("DomainName")); 55  } 56  57  return resourceDomainNameDescendants; 58  } 59  catch (SQLException e) { 60  throw new RuntimeException(e); 61  } 62  finally { 63  closeStatement(statement); 64  } 65  } 66  67  @Override 68  public void deleteDomain(SQLConnection connection, 69  Id<DomainId> domainId) { 70  SQLStatement statement = null; 71  72  try { 73  // chose strategy to perform recursive delete based on sql profile 74  if (sqlProfile.isRecursiveDeleteEnabled()) { 75  // prepare the standard recursive delete statement of domain and its children 76  statement = connection.prepareStatement(sqlStrings.SQL_removeInDomain_withDescendants_BY_DomainID); 77  statement.setResourceDomainId(1, domainId); 78  79  final int rowCount = statement.executeUpdate(); 80  81  if (rowCount < 1) { 82  throw new IllegalStateException("Security table data update, 1 or more rows expected, got: " + rowCount); 83  } 84  } 85  else { 86  // DBMS doesn't support recursive deletion, so we have to remove domain's children first 87  88  // get descendant domain Ids 89  statement = connection.prepareStatement(sqlStrings.SQL_findInDomain_DescendantResourceDomainID_BY_DomainID_ORDERBY_DomainLevel); 90  statement.setResourceDomainId(1, domainId); 91  SQLResult resultSet = statement.executeQuery(); 92  93  List<Id<DomainId>> descendantDomainIds = new ArrayList<>(); 94  95  while (resultSet.next()) { 96  descendantDomainIds.add(resultSet.getResourceDomainId("DomainId")); 97  } 98  closeStatement(statement); 99  100  // delete descendant domains one at a time, in reverse order of domainLevel, to preserve FK constraints 101  statement = connection.prepareStatement(sqlStrings.SQL_removeInDomain_BY_DomainID); 102  103  for (int i=descendantDomainIds.size()-1; i >= 0; i--) { 104  statement.setResourceDomainId(1, descendantDomainIds.get(i)); 105  assertOneRowUpdated(statement.executeUpdate()); 106  } 107  } 108  } 109  catch (SQLException e) { 110  throw new RuntimeException(e); 111  } 112  finally { 113  closeStatement(statement); 114  } 115  } 116  117 }