Coverage Summary for Class: NonRecursiveDomainPersister (com.acciente.oacc.sql.internal.persister)
Class | Class, % | Method, % | Line, % |
---|---|---|---|
NonRecursiveDomainPersister | 100% (1/ 1) | 100% (3/ 3) | 91.1% (41/ 45) |
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.Collections;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Set;
31
32 public class NonRecursiveDomainPersister extends CommonDomainPersister implements Serializable {
33 private static final long serialVersionUID = 1L;
34
35 public NonRecursiveDomainPersister(SQLProfile sqlProfile,
36 SQLStrings sqlStrings) {
37 super(sqlProfile, sqlStrings);
38 }
39
40 @Override
41 public Set<String> getResourceDomainNameDescendants(SQLConnection connection,
42 String resourceDomainName) {
43 SQLStatement statement = null;
44
45 try {
46 // verify the domain
47 if (getResourceDomainId(connection, resourceDomainName) == null) {
48 return Collections.emptySet();
49 }
50
51 Set<String> allDescendantDomainNames = new HashSet<>();
52 Set<Id<DomainId>> previousDescendantDomainIds = new HashSet<>();
53
54 allDescendantDomainNames.add(resourceDomainName);
55
56 // find first-level descendants (excluding the requested root domain)
57 statement = connection
58 .prepareStatement(sqlStrings.SQL_findInDomain_DirectDescendantResourceDomainName_BY_ResourceDomainName);
59 statement.setString(1, resourceDomainName);
60 SQLResult resultSet = statement.executeQuery();
61
62 while (resultSet.next()) {
63 allDescendantDomainNames.add(resultSet.getString("DomainName"));
64 previousDescendantDomainIds.add(resultSet.getResourceDomainId("DomainId"));
65 }
66 statement.close();
67
68 // find second-level and higher descendants, if necessary
69 if (allDescendantDomainNames.size() > 1) {
70 Set<Id<DomainId>> newestDescendantDomainIds = new HashSet<>();
71 int prevTotalOfDescendantDomains = 1;
72 while (allDescendantDomainNames.size() > prevTotalOfDescendantDomains) {
73 prevTotalOfDescendantDomains = allDescendantDomainNames.size();
74
75 for (Id<DomainId> descendantDomainId : previousDescendantDomainIds) {
76 statement = connection
77 .prepareStatement(sqlStrings.SQL_findInDomain_DirectDescendantResourceDomainName_BY_DomainID);
78 statement.setResourceDomainId(1, descendantDomainId);
79 resultSet = statement.executeQuery();
80
81 while (resultSet.next()) {
82 allDescendantDomainNames.add(resultSet.getString("DomainName"));
83 newestDescendantDomainIds.add(resultSet.getResourceDomainId("DomainId"));
84 }
85 statement.close();
86 }
87 previousDescendantDomainIds = newestDescendantDomainIds;
88 }
89 }
90
91 return allDescendantDomainNames;
92 }
93 catch (SQLException e) {
94 throw new RuntimeException(e);
95 }
96 finally {
97 closeStatement(statement);
98 }
99 }
100
101 @Override
102 public void deleteDomain(SQLConnection connection,
103 Id<DomainId> domainId) {
104 SQLStatement statement = null;
105
106 try {
107 // get descendant domain Ids
108 List<Id<DomainId>> descendantDomainIds
109 = new ArrayList<>(NonRecursivePersisterHelper.getDescendantDomainIdsOrderedByAscendingLevel(sqlStrings,
110 connection,
111 domainId));
112
113 // delete descendant domains one at a time, in reverse order of domainLevel, to preserve FK constraints
114 statement = connection.prepareStatement(sqlStrings.SQL_removeInDomain_BY_DomainID);
115
116 for (int i = descendantDomainIds.size() - 1; i >= 0; i--) {
117 statement.setResourceDomainId(1, descendantDomainIds.get(i));
118 assertOneRowUpdated(statement.executeUpdate());
119 }
120 }
121 catch (SQLException e) {
122 throw new RuntimeException(e);
123 }
124 finally {
125 closeStatement(statement);
126 }
127 }
128
129 }