Add JSON and collection handler 98/130498/1
authorChuanyuChen <chenchuanyu@huawei.com>
Tue, 30 Aug 2022 02:15:49 +0000 (10:15 +0800)
committerChuanyuChen <chenchuanyu@huawei.com>
Tue, 30 Aug 2022 02:15:49 +0000 (10:15 +0800)
Add handlers to save an object or a collection as String in DB

Issue-ID: USECASEUI-710

Signed-off-by: ChuanyuChen <chenchuanyu@huawei.com>
Change-Id: Id7f9df35dc3ccad23bf784197671b39e043010c9

intentanalysis/pom.xml
intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/bean/handler/JsonCollectionTypeHandler.java [new file with mode: 0644]
intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/bean/handler/JsonTypeHandler.java [new file with mode: 0644]

index 0c34071..fc3e624 100644 (file)
             <artifactId>commons-lang</artifactId>
             <version>2.6</version>
         </dependency>
+        <dependency>
+            <groupId>com.google.code.gson</groupId>
+            <artifactId>gson</artifactId>
+            <version>2.8.6</version>
+        </dependency>
+        <dependency>
+            <groupId>com.mikesamuel</groupId>
+            <artifactId>json-sanitizer</artifactId>
+            <version>1.2.2</version>
+        </dependency>
     </dependencies>
     <build>
         <plugins>
diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/bean/handler/JsonCollectionTypeHandler.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/bean/handler/JsonCollectionTypeHandler.java
new file mode 100644 (file)
index 0000000..55796d2
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ *    Copyright 2020 Huawei Technologies Co., Ltd.
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+package org.onap.usecaseui.intentanalysis.bean.handler;
+
+import com.google.gson.Gson;
+import com.google.gson.reflect.TypeToken;
+import com.google.json.JsonSanitizer;
+import java.lang.reflect.Type;
+import java.sql.CallableStatement;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.util.Collection;
+import org.apache.ibatis.type.BaseTypeHandler;
+import org.apache.ibatis.type.JdbcType;
+
+public class JsonCollectionTypeHandler<T extends Collection<?>> extends BaseTypeHandler<T> {
+
+    Gson gson = new Gson();
+
+    Type collectionType = new TypeToken<T>() { }.getType();
+
+    @Override
+    public void setNonNullParameter(PreparedStatement preparedStatement, int i, T o, JdbcType jdbcType)
+        throws SQLException {
+        preparedStatement.setString(i, gson.toJson(o, collectionType));
+    }
+
+    @Override
+    public T getNullableResult(ResultSet resultSet, String s) throws SQLException {
+        String text = resultSet.getString(s);
+        return gson.fromJson(text, collectionType);
+    }
+
+    @Override
+    public T getNullableResult(ResultSet resultSet, int i) throws SQLException {
+        String text = resultSet.getString(i);
+        return gson.fromJson(text, collectionType);
+    }
+
+    @Override
+    public T getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
+        String text = callableStatement.getString(i);
+        text = JsonSanitizer.sanitize(text);
+        return gson.fromJson(text, collectionType);
+    }
+}
diff --git a/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/bean/handler/JsonTypeHandler.java b/intentanalysis/src/main/java/org/onap/usecaseui/intentanalysis/bean/handler/JsonTypeHandler.java
new file mode 100644 (file)
index 0000000..dc30684
--- /dev/null
@@ -0,0 +1,68 @@
+/*
+ *    Copyright 2020 Huawei Technologies Co., Ltd.
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+
+package org.onap.usecaseui.intentanalysis.bean.handler;
+
+import com.google.gson.Gson;
+import com.google.json.JsonSanitizer;
+import java.sql.CallableStatement;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import org.apache.ibatis.type.BaseTypeHandler;
+import org.apache.ibatis.type.JdbcType;
+
+public class JsonTypeHandler<T extends Object> extends BaseTypeHandler<T> {
+
+    Gson gson = new Gson();
+
+    private Class<T> clazz;
+
+    /**
+     * handle json.
+     */
+    public JsonTypeHandler(Class<T> clazz) {
+        if (clazz == null) {
+            throw new IllegalArgumentException("Type argument cannot be null");
+        }
+        this.clazz = clazz;
+    }
+
+    @Override
+    public void setNonNullParameter(PreparedStatement preparedStatement, int i, Object o, JdbcType jdbcType)
+        throws SQLException {
+        preparedStatement.setString(i, gson.toJson(o));
+    }
+
+    @Override
+    public T getNullableResult(ResultSet resultSet, String s) throws SQLException {
+        String text = resultSet.getString(s);
+        return gson.fromJson(text, clazz);
+    }
+
+    @Override
+    public T getNullableResult(ResultSet resultSet, int i) throws SQLException {
+        String text = resultSet.getString(i);
+        return gson.fromJson(text, clazz);
+    }
+
+    @Override
+    public T getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
+        String text = callableStatement.getString(i);
+        text = JsonSanitizer.sanitize(text);
+        return gson.fromJson(text, clazz);
+    }
+}