Fix Checkstyle issues
[clamp.git] / src / main / java / org / onap / clamp / dao / model / jsontype / JsonStringSqlTypeDescriptor.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.dao.model.jsontype;
25
26 import java.sql.CallableStatement;
27 import java.sql.PreparedStatement;
28 import java.sql.ResultSet;
29 import java.sql.SQLException;
30 import java.sql.Types;
31
32 import org.hibernate.type.descriptor.ValueBinder;
33 import org.hibernate.type.descriptor.ValueExtractor;
34 import org.hibernate.type.descriptor.WrapperOptions;
35 import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
36 import org.hibernate.type.descriptor.sql.BasicBinder;
37 import org.hibernate.type.descriptor.sql.BasicExtractor;
38 import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
39
40 public class JsonStringSqlTypeDescriptor implements SqlTypeDescriptor {
41
42     /**
43      * The serial version ID.
44      */
45     private static final long serialVersionUID = 1103168570216921981L;
46
47     public static final JsonStringSqlTypeDescriptor INSTANCE = new JsonStringSqlTypeDescriptor();
48
49     @Override
50     public int getSqlType() {
51         return Types.OTHER;
52     }
53
54     @Override
55     public boolean canBeRemapped() {
56         return true;
57     }
58
59     @Override
60     public <X> ValueBinder<X> getBinder(JavaTypeDescriptor<X> javaTypeDescriptor) {
61         return new BasicBinder<X>(javaTypeDescriptor, this) {
62             @Override
63             protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options)
64                 throws SQLException {
65                 st.setString(index, javaTypeDescriptor.unwrap(value, String.class, options));
66             }
67
68             @Override
69             protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
70                 throws SQLException {
71                 st.setString(name, javaTypeDescriptor.unwrap(value, String.class, options));
72             }
73         };
74     }
75
76     @Override
77     public <X> ValueExtractor<X> getExtractor(JavaTypeDescriptor<X> javaTypeDescriptor) {
78         return new BasicExtractor<X>(javaTypeDescriptor, this) {
79             @Override
80             protected X doExtract(ResultSet rs, String name, WrapperOptions options) throws SQLException {
81                 return javaTypeDescriptor.wrap(extractJson(rs, name), options);
82             }
83
84             @Override
85             protected X doExtract(CallableStatement statement, int index, WrapperOptions options) throws SQLException {
86                 return javaTypeDescriptor.wrap(extractJson(statement, index), options);
87             }
88
89             @Override
90             protected X doExtract(CallableStatement statement, String name, WrapperOptions options)
91                 throws SQLException {
92                 return javaTypeDescriptor.wrap(extractJson(statement, name), options);
93             }
94         };
95     }
96
97     protected Object extractJson(ResultSet rs, String name) throws SQLException {
98         return rs.getObject(name);
99     }
100
101     protected Object extractJson(CallableStatement statement, int index) throws SQLException {
102         return statement.getObject(index);
103     }
104
105     protected Object extractJson(CallableStatement statement, String name) throws SQLException {
106         return statement.getObject(name);
107     }
108
109 }