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

Class Class, % Method, % Line, %
ResourceClassPersister 100% (1/ 1) 100% (6/ 6) 80.6% (50/ 62)


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.ResourceClassInternalInfo; 23 import com.acciente.oacc.sql.internal.persister.id.Id; 24 import com.acciente.oacc.sql.internal.persister.id.ResourceClassId; 25  26 import java.io.Serializable; 27 import java.sql.SQLException; 28 import java.util.LinkedList; 29 import java.util.List; 30  31 public class ResourceClassPersister extends Persister implements Serializable { 32  private static final long serialVersionUID = 1L; 33  34  protected final SQLProfile sqlProfile; 35  private final SQLStrings sqlStrings; 36  37  public ResourceClassPersister(SQLProfile sqlProfile, 38  SQLStrings sqlStrings) { 39  this.sqlProfile = sqlProfile; 40  this.sqlStrings = sqlStrings; 41  } 42  43  public Id<ResourceClassId> getResourceClassId(SQLConnection connection, 44  String resourceClassName) { 45  SQLStatement statement = null; 46  47  try { 48  Id<ResourceClassId> resourceClassId = null; 49  50  statement = connection.prepareStatement(sqlStrings.SQL_findInResourceClass_ResourceClassID_BY_ResourceClassName); 51  statement.setString(1, resourceClassName); 52  SQLResult resultSet = statement.executeQuery(); 53  54  if (resultSet.next()) { 55  resourceClassId = resultSet.getResourceClassId("ResourceClassId"); 56  } 57  58  return resourceClassId; 59  } 60  catch (SQLException e) { 61  throw new RuntimeException(e); 62  } 63  finally { 64  closeStatement(statement); 65  } 66  } 67  68  public ResourceClassInternalInfo getResourceClassInfo(SQLConnection connection, 69  String resourceClassName) { 70  SQLStatement statement = null; 71  72  if (resourceClassName == null) { 73  throw new IllegalArgumentException("Resource class name cannot be null"); 74  } 75  76  try { 77  ResourceClassInternalInfo resourceClassInternalInfo = null; 78  79  statement = connection.prepareStatement(sqlStrings.SQL_findInResourceClass_ResourceClassID_ResourceClassName_IsAuthenticatable_IsUnauthenticatedCreateAllowed_BY_ResourceClassName); 80  statement.setString(1, resourceClassName.trim()); 81  SQLResult resultSet = statement.executeQuery(); 82  83  if (resultSet.next()) { 84  resourceClassInternalInfo = new ResourceClassInternalInfo(resultSet.getResourceClassId("ResourceClassId"), 85  resultSet.getString("ResourceClassName"), 86  resultSet.getBoolean("IsAuthenticatable"), 87  resultSet.getBoolean("IsUnauthenticatedCreateAllowed")); 88  } 89  90  return resourceClassInternalInfo; 91  } 92  catch (SQLException e) { 93  throw new RuntimeException(e); 94  } 95  finally { 96  closeStatement(statement); 97  } 98  } 99  100  public ResourceClassInternalInfo getResourceClassInfoByResourceId(SQLConnection connection, 101  Resource resource) { 102  SQLStatement statement = null; 103  104  try { 105  ResourceClassInternalInfo resourceClassInternalInfo = null; 106  107  statement = connection.prepareStatement(sqlStrings.SQL_findInResourceClass_ResourceClassID_ResourceClassName_IsAuthenticatable_IsUnauthenticatedCreateAllowed_BY_ResourceID); 108  statement.setResourceId(1, resource); 109  SQLResult resultSet = statement.executeQuery(); 110  111  if (resultSet.next()) { 112  resourceClassInternalInfo = new ResourceClassInternalInfo(resultSet.getResourceClassId("ResourceClassId"), 113  resultSet.getString("ResourceClassName"), 114  resultSet.getBoolean("IsAuthenticatable"), 115  resultSet.getBoolean("IsUnauthenticatedCreateAllowed")); 116  } 117  118  if (resourceClassInternalInfo == null) { 119  throw new IllegalArgumentException("Could not determine resource class for resource: " + resource); 120  } 121  122  return resourceClassInternalInfo; 123  } 124  catch (SQLException e) { 125  throw new RuntimeException(e); 126  } 127  finally { 128  closeStatement(statement); 129  } 130  } 131  132  public List<String> getResourceClassNames(SQLConnection connection) { 133  SQLStatement statement = null; 134  135  try { 136  List<String> resourceClassNames = new LinkedList<>(); 137  138  statement = connection.prepareStatement(sqlStrings.SQL_findInResourceClass_ResourceClassName_BY_ALL); 139  SQLResult resultSet = statement.executeQuery(); 140  141  while (resultSet.next()) { 142  resourceClassNames.add(resultSet.getString("ResourceClassName")); 143  } 144  145  return resourceClassNames; 146  } 147  catch (SQLException e) { 148  throw new RuntimeException(e); 149  } 150  finally { 151  closeStatement(statement); 152  } 153  } 154  155  public void addResourceClass(SQLConnection connection, 156  String resourceClassName, 157  boolean authenticatable, 158  boolean nonAuthenticatedCreateAllowed) { 159  SQLStatement statement = null; 160  161  try { 162  // create the new resource class 163  statement = connection.prepareStatement(sqlStrings.SQL_createInResourceClass_WITH_ResourceClassName_IsAuthenticatable_IsUnauthenticatedCreateAllowed); 164  statement.setString(1, resourceClassName); 165  statement.setBoolean(2, authenticatable); 166  statement.setBoolean(3, nonAuthenticatedCreateAllowed); 167  assertOneRowInserted(statement.executeUpdate()); 168  } 169  catch (SQLException e) { 170  throw new RuntimeException(e); 171  } 172  finally { 173  closeStatement(statement); 174  } 175  } 176 }