Coverage Summary for Class: CommonDomainPersister (com.acciente.oacc.sql.internal.persister)
Class | Class, % | Method, % | Line, % |
---|---|---|---|
CommonDomainPersister | 100% (1/ 1) | 100% (5/ 5) | 79.6% (39/ 49) |
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.SQLProfile;
22 import com.acciente.oacc.sql.internal.persister.id.DomainId;
23 import com.acciente.oacc.sql.internal.persister.id.Id;
24
25 import java.io.Serializable;
26 import java.sql.SQLException;
27 import java.util.Set;
28
29 public abstract class CommonDomainPersister extends Persister implements DomainPersister, Serializable {
30 private static final long serialVersionUID = 1L;
31
32 protected final SQLProfile sqlProfile;
33 protected final SQLStrings sqlStrings;
34
35 public CommonDomainPersister(SQLProfile sqlProfile,
36 SQLStrings sqlStrings) {
37 this.sqlProfile = sqlProfile;
38 this.sqlStrings = sqlStrings;
39 }
40
41 @Override
42 public Id<DomainId> getResourceDomainId(SQLConnection connection,
43 String resourceDomainName) {
44 SQLStatement statement = null;
45
46 if (resourceDomainName == null) {
47 throw new IllegalArgumentException("Domain name must not be null");
48 }
49
50 try {
51 Id<DomainId> resourceDomainId = null;
52
53 statement = connection.prepareStatement(sqlStrings.SQL_findInDomain_DomainID_BY_ResourceDomainName);
54 statement.setString(1, resourceDomainName.trim());
55 SQLResult resultSet = statement.executeQuery();
56
57 if (resultSet.next()) {
58 resourceDomainId = resultSet.getResourceDomainId("DomainId");
59 }
60
61 return resourceDomainId;
62 }
63 catch (SQLException e) {
64 throw new RuntimeException(e);
65 }
66 finally {
67 closeStatement(statement);
68 }
69 }
70
71 @Override
72 public String getResourceDomainNameByResourceId(SQLConnection connection,
73 Resource resource) {
74 SQLStatement statement = null;
75
76 try {
77 String resourceDomainName = null;
78
79 statement = connection.prepareStatement(sqlStrings.SQL_findInDomain_ResourceDomainName_BY_ResourceID);
80 statement.setResourceId(1, resource);
81 SQLResult resultSet = statement.executeQuery();
82
83 if (resultSet.next()) {
84 resourceDomainName = resultSet.getString("DomainName");
85 }
86
87 if (resourceDomainName == null) {
88 throw new IllegalArgumentException("Could not determine domain for resource: " + resource);
89 }
90
91 return resourceDomainName;
92 }
93 catch (SQLException e) {
94 throw new RuntimeException(e);
95 }
96 finally {
97 closeStatement(statement);
98 }
99 }
100
101 @Override
102 public abstract Set<String> getResourceDomainNameDescendants(SQLConnection connection,
103 String resourceDomainName);
104
105 @Override
106 public void addResourceDomain(SQLConnection connection,
107 String resourceDomainName) {
108 SQLStatement statement = null;
109
110 try {
111 // create the new root domain
112 statement = connection.prepareStatement(sqlStrings.SQL_createInDomain_WITH_ResourceDomainName);
113 statement.setString(1, resourceDomainName);
114 assertOneRowInserted(statement.executeUpdate());
115 }
116 catch (SQLException e) {
117 throw new RuntimeException(e);
118 }
119 finally {
120 closeStatement(statement);
121 }
122 }
123
124 @Override
125 public void addResourceDomain(SQLConnection connection,
126 String resourceDomainName,
127 Id<DomainId> parentResourceDomainId) {
128 SQLStatement statement = null;
129
130 try {
131 // create the new child domain
132 statement = connection.prepareStatement(sqlStrings.SQL_createInDomain_WITH_ResourceDomainName_ParentDomainID);
133 statement.setString(1, resourceDomainName);
134 statement.setResourceDomainId(2, parentResourceDomainId);
135 assertOneRowInserted(statement.executeUpdate());
136 }
137 catch (SQLException e) {
138 throw new RuntimeException(e);
139 }
140 finally {
141 closeStatement(statement);
142 }
143 }
144
145 @Override
146 public abstract void deleteDomain(SQLConnection connection, Id<DomainId> domainId);
147 }