From 86cf9ece99fdc169427426010d07d59b3d0d1d91 Mon Sep 17 00:00:00 2001
From: sebdet <sebastien.determe@intl.att.com>
Date: Tue, 5 Mar 2019 05:17:11 +0100
Subject: Rework the hibernate adapter

This was using the Jackson library so a new handmade hibernate adapter
has been developed.

Issue-ID: CLAMP-300
Change-Id: Ia1c1e8791370c139127e6fa4bd2b96c02f632fde
Signed-off-by: sebdet <sebastien.determe@intl.att.com>
---
 src/main/java/org/onap/clamp/dao/model/Loop.java   |  26 +++--
 .../onap/clamp/dao/model/MicroServicePolicy.java   |  19 ++--
 .../onap/clamp/dao/model/OperationalPolicy.java    |  13 +--
 .../jsontype/JsonStringSqlTypeDescriptor.java      | 109 +++++++++++++++++++++
 .../dao/model/jsontype/JsonTypeDescriptor.java     | 101 +++++++++++++++++++
 .../dao/model/jsontype/StringJsonUserType.java     |  52 ++++++++++
 6 files changed, 298 insertions(+), 22 deletions(-)
 create mode 100644 src/main/java/org/onap/clamp/dao/model/jsontype/JsonStringSqlTypeDescriptor.java
 create mode 100644 src/main/java/org/onap/clamp/dao/model/jsontype/JsonTypeDescriptor.java
 create mode 100644 src/main/java/org/onap/clamp/dao/model/jsontype/StringJsonUserType.java

(limited to 'src/main/java')

diff --git a/src/main/java/org/onap/clamp/dao/model/Loop.java b/src/main/java/org/onap/clamp/dao/model/Loop.java
index c5946c9d..78175cc5 100644
--- a/src/main/java/org/onap/clamp/dao/model/Loop.java
+++ b/src/main/java/org/onap/clamp/dao/model/Loop.java
@@ -23,12 +23,11 @@
 
 package org.onap.clamp.dao.model;
 
+import com.google.gson.JsonObject;
 import com.google.gson.annotations.Expose;
-import com.vladmihalcea.hibernate.type.json.JsonStringType;
 
 import java.io.Serializable;
 import java.util.HashSet;
-import java.util.Map;
 import java.util.Set;
 
 import javax.persistence.CascadeType;
@@ -46,11 +45,12 @@ import javax.persistence.Table;
 
 import org.hibernate.annotations.Type;
 import org.hibernate.annotations.TypeDef;
+import org.hibernate.annotations.TypeDefs;
+import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
 
 @Entity
 @Table(name = "loops")
-//@JsonAdapter(JsonLoopAdapter.class)
-@TypeDef(name = "json", typeClass = JsonStringType.class)
+@TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
 public class Loop implements Serializable {
 
     /**
@@ -71,6 +71,10 @@ public class Loop implements Serializable {
     @Column(name = "dcae_deployment_status_url")
     private String dcaeDeploymentStatusUrl;
 
+    @Expose
+    @Column(name = "dcae_blueprint_id")
+    private String dcaeBlueprintId;
+
     @Expose
     @Column(name = "svg_representation")
     private String svgRepresentation;
@@ -78,7 +82,7 @@ public class Loop implements Serializable {
     @Expose
     @Type(type = "json")
     @Column(columnDefinition = "json", name = "global_properties_json")
-    private Map<String, Object> globalPropertiesJson;
+    private JsonObject globalPropertiesJson;
 
     @Expose
     @Column(nullable = false, name = "blueprint_yaml")
@@ -166,11 +170,11 @@ public class Loop implements Serializable {
         this.microServicePolicies = microServicePolicies;
     }
 
-    public Map<String, Object> getGlobalPropertiesJson() {
+    public JsonObject getGlobalPropertiesJson() {
         return globalPropertiesJson;
     }
 
-    public void setGlobalPropertiesJson(Map<String, Object> globalPropertiesJson) {
+    public void setGlobalPropertiesJson(JsonObject globalPropertiesJson) {
         this.globalPropertiesJson = globalPropertiesJson;
     }
 
@@ -197,6 +201,14 @@ public class Loop implements Serializable {
         log.setLoop(this);
     }
 
+    public String getDcaeBlueprintId() {
+        return dcaeBlueprintId;
+    }
+
+    public void setDcaeBlueprintId(String dcaeBlueprintId) {
+        this.dcaeBlueprintId = dcaeBlueprintId;
+    }
+
     @Override
     public int hashCode() {
         final int prime = 31;
diff --git a/src/main/java/org/onap/clamp/dao/model/MicroServicePolicy.java b/src/main/java/org/onap/clamp/dao/model/MicroServicePolicy.java
index 6014d8d5..8ea6d265 100644
--- a/src/main/java/org/onap/clamp/dao/model/MicroServicePolicy.java
+++ b/src/main/java/org/onap/clamp/dao/model/MicroServicePolicy.java
@@ -23,12 +23,11 @@
 
 package org.onap.clamp.dao.model;
 
+import com.google.gson.JsonObject;
 import com.google.gson.annotations.Expose;
-import com.vladmihalcea.hibernate.type.json.JsonStringType;
 
 import java.io.Serializable;
 import java.util.HashSet;
-import java.util.Map;
 import java.util.Set;
 
 import javax.persistence.Column;
@@ -39,10 +38,12 @@ import javax.persistence.Table;
 
 import org.hibernate.annotations.Type;
 import org.hibernate.annotations.TypeDef;
+import org.hibernate.annotations.TypeDefs;
+import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
 
 @Entity
 @Table(name = "micro_service_policies")
-@TypeDef(name = "json", typeClass = JsonStringType.class)
+@TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
 public class MicroServicePolicy implements Serializable {
     /**
      *
@@ -57,7 +58,7 @@ public class MicroServicePolicy implements Serializable {
     @Expose
     @Type(type = "json")
     @Column(columnDefinition = "json", name = "properties")
-    private Map<String, Object> properties;
+    private JsonObject properties;
 
     @Expose
     @Column(name = "shared", nullable = false)
@@ -70,7 +71,7 @@ public class MicroServicePolicy implements Serializable {
     @Expose
     @Type(type = "json")
     @Column(columnDefinition = "json", name = "json_representation", nullable = false)
-    private Map<String, Object> jsonRepresentation;
+    private JsonObject jsonRepresentation;
 
     @ManyToMany(mappedBy = "microServicePolicies")
     private Set<Loop> usedByLoops = new HashSet<>();
@@ -83,11 +84,11 @@ public class MicroServicePolicy implements Serializable {
         this.name = name;
     }
 
-    public Map<String, Object> getProperties() {
+    public JsonObject getProperties() {
         return properties;
     }
 
-    public void setProperties(Map<String, Object> properties) {
+    public void setProperties(JsonObject properties) {
         this.properties = properties;
     }
 
@@ -107,11 +108,11 @@ public class MicroServicePolicy implements Serializable {
         this.policyTosca = policyTosca;
     }
 
-    public Map<String, Object> getJsonRepresentation() {
+    public JsonObject getJsonRepresentation() {
         return jsonRepresentation;
     }
 
-    public void setJsonRepresentation(Map<String, Object> jsonRepresentation) {
+    public void setJsonRepresentation(JsonObject jsonRepresentation) {
         this.jsonRepresentation = jsonRepresentation;
     }
 
diff --git a/src/main/java/org/onap/clamp/dao/model/OperationalPolicy.java b/src/main/java/org/onap/clamp/dao/model/OperationalPolicy.java
index 23f75741..ba2c442c 100644
--- a/src/main/java/org/onap/clamp/dao/model/OperationalPolicy.java
+++ b/src/main/java/org/onap/clamp/dao/model/OperationalPolicy.java
@@ -23,11 +23,10 @@
 
 package org.onap.clamp.dao.model;
 
+import com.google.gson.JsonObject;
 import com.google.gson.annotations.Expose;
-import com.vladmihalcea.hibernate.type.json.JsonStringType;
 
 import java.io.Serializable;
-import java.util.Map;
 
 import javax.persistence.Column;
 import javax.persistence.Entity;
@@ -39,10 +38,12 @@ import javax.persistence.Table;
 
 import org.hibernate.annotations.Type;
 import org.hibernate.annotations.TypeDef;
+import org.hibernate.annotations.TypeDefs;
+import org.onap.clamp.dao.model.jsontype.StringJsonUserType;
 
 @Entity
 @Table(name = "operational_policies")
-@TypeDef(name = "json", typeClass = JsonStringType.class)
+@TypeDefs({ @TypeDef(name = "json", typeClass = StringJsonUserType.class) })
 public class OperationalPolicy implements Serializable {
     /**
      *
@@ -57,7 +58,7 @@ public class OperationalPolicy implements Serializable {
     @Expose
     @Type(type = "json")
     @Column(columnDefinition = "json", name = "configurations_json")
-    private Map<String, Object> configurationsJson;
+    private JsonObject configurationsJson;
 
     @ManyToOne(fetch = FetchType.LAZY)
     @JoinColumn(name = "loop_id", nullable = false)
@@ -79,11 +80,11 @@ public class OperationalPolicy implements Serializable {
         this.name = name;
     }
 
-    public Map<String, Object> getConfigurationsJson() {
+    public JsonObject getConfigurationsJson() {
         return configurationsJson;
     }
 
-    public void setConfigurationsJson(Map<String, Object> configurationsJson) {
+    public void setConfigurationsJson(JsonObject configurationsJson) {
         this.configurationsJson = configurationsJson;
     }
 
diff --git a/src/main/java/org/onap/clamp/dao/model/jsontype/JsonStringSqlTypeDescriptor.java b/src/main/java/org/onap/clamp/dao/model/jsontype/JsonStringSqlTypeDescriptor.java
new file mode 100644
index 00000000..611f5ec2
--- /dev/null
+++ b/src/main/java/org/onap/clamp/dao/model/jsontype/JsonStringSqlTypeDescriptor.java
@@ -0,0 +1,109 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP CLAMP
+ * ================================================================================
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights
+ *                             reserved.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END============================================
+ * ===================================================================
+ *
+ */
+
+package org.onap.clamp.dao.model.jsontype;
+
+import java.sql.CallableStatement;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Types;
+
+import org.hibernate.type.descriptor.ValueBinder;
+import org.hibernate.type.descriptor.ValueExtractor;
+import org.hibernate.type.descriptor.WrapperOptions;
+import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
+import org.hibernate.type.descriptor.sql.BasicBinder;
+import org.hibernate.type.descriptor.sql.BasicExtractor;
+import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
+
+public class JsonStringSqlTypeDescriptor implements SqlTypeDescriptor {
+
+    /**
+     *
+     */
+    private static final long serialVersionUID = 1103168570216921981L;
+
+    public static final JsonStringSqlTypeDescriptor INSTANCE = new JsonStringSqlTypeDescriptor();
+
+    @Override
+    public int getSqlType() {
+        return Types.OTHER;
+    }
+
+    @Override
+    public boolean canBeRemapped() {
+        return true;
+    }
+
+    @Override
+    public <X> ValueBinder<X> getBinder(JavaTypeDescriptor<X> javaTypeDescriptor) {
+        return new BasicBinder<X>(javaTypeDescriptor, this) {
+            @Override
+            protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options)
+                throws SQLException {
+                st.setString(index, javaTypeDescriptor.unwrap(value, String.class, options));
+            }
+
+            @Override
+            protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
+                throws SQLException {
+                st.setString(name, javaTypeDescriptor.unwrap(value, String.class, options));
+            }
+        };
+    }
+
+    @Override
+    public <X> ValueExtractor<X> getExtractor(JavaTypeDescriptor<X> javaTypeDescriptor) {
+        return new BasicExtractor<X>(javaTypeDescriptor, this) {
+            @Override
+            protected X doExtract(ResultSet rs, String name, WrapperOptions options) throws SQLException {
+                return javaTypeDescriptor.wrap(extractJson(rs, name), options);
+            }
+
+            @Override
+            protected X doExtract(CallableStatement statement, int index, WrapperOptions options) throws SQLException {
+                return javaTypeDescriptor.wrap(extractJson(statement, index), options);
+            }
+
+            @Override
+            protected X doExtract(CallableStatement statement, String name, WrapperOptions options)
+                throws SQLException {
+                return javaTypeDescriptor.wrap(extractJson(statement, name), options);
+            }
+        };
+    }
+
+    protected Object extractJson(ResultSet rs, String name) throws SQLException {
+        return rs.getObject(name);
+    }
+
+    protected Object extractJson(CallableStatement statement, int index) throws SQLException {
+        return statement.getObject(index);
+    }
+
+    protected Object extractJson(CallableStatement statement, String name) throws SQLException {
+        return statement.getObject(name);
+    }
+
+}
diff --git a/src/main/java/org/onap/clamp/dao/model/jsontype/JsonTypeDescriptor.java b/src/main/java/org/onap/clamp/dao/model/jsontype/JsonTypeDescriptor.java
new file mode 100644
index 00000000..155758e1
--- /dev/null
+++ b/src/main/java/org/onap/clamp/dao/model/jsontype/JsonTypeDescriptor.java
@@ -0,0 +1,101 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP CLAMP
+ * ================================================================================
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights
+ *                             reserved.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END============================================
+ * ===================================================================
+ *
+ */
+
+package org.onap.clamp.dao.model.jsontype;
+
+import com.google.gson.JsonObject;
+
+import java.io.Serializable;
+
+import org.hibernate.type.descriptor.WrapperOptions;
+import org.hibernate.type.descriptor.java.AbstractTypeDescriptor;
+import org.hibernate.type.descriptor.java.ImmutableMutabilityPlan;
+import org.onap.clamp.clds.util.JsonUtils;
+
+public class JsonTypeDescriptor extends AbstractTypeDescriptor<JsonObject> {
+
+    /**
+     *
+     */
+    private static final long serialVersionUID = -3439698221196089003L;
+
+    public static final JsonTypeDescriptor INSTANCE = new JsonTypeDescriptor();
+
+    public JsonTypeDescriptor() {
+        super(JsonObject.class, new ImmutableMutabilityPlan<JsonObject>() {
+
+            /**
+             *
+             */
+            private static final long serialVersionUID = 1169396106518110214L;
+
+            @Override
+            public Serializable disassemble(JsonObject value) {
+                return JsonUtils.GSON_JPA_MODEL.toJson(value);
+            }
+
+            @Override
+            public JsonObject assemble(Serializable cached) {
+                return JsonUtils.GSON_JPA_MODEL.fromJson((String) cached, JsonObject.class);
+            }
+
+        });
+    }
+
+    @Override
+    public String toString(JsonObject value) {
+        return JsonUtils.GSON_JPA_MODEL.toJson(value);
+    }
+
+    @Override
+    public JsonObject fromString(String string) {
+        return JsonUtils.GSON_JPA_MODEL.fromJson(string, JsonObject.class);
+    }
+
+    @Override
+    public <X> X unwrap(JsonObject value, Class<X> type, WrapperOptions options) {
+        if (value == null)
+            return null;
+
+        if (String.class.isAssignableFrom(type)) {
+            return (X) toString(value);
+        }
+
+        if (JsonObject.class.isAssignableFrom(type)) {
+            return (X) JsonUtils.GSON_JPA_MODEL.toJson(toString(value));
+        }
+        throw unknownUnwrap(type);
+    }
+
+    @Override
+    public <X> JsonObject wrap(X value, WrapperOptions options) {
+        if (value == null)
+            return null;
+
+        if (String.class.isInstance(value))
+            return JsonUtils.GSON_JPA_MODEL.fromJson((String) value, JsonObject.class);
+
+        throw unknownWrap(value.getClass());
+    }
+
+}
diff --git a/src/main/java/org/onap/clamp/dao/model/jsontype/StringJsonUserType.java b/src/main/java/org/onap/clamp/dao/model/jsontype/StringJsonUserType.java
new file mode 100644
index 00000000..1831c174
--- /dev/null
+++ b/src/main/java/org/onap/clamp/dao/model/jsontype/StringJsonUserType.java
@@ -0,0 +1,52 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP CLAMP
+ * ================================================================================
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights
+ *                             reserved.
+ * ================================================================================
+ * 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.
+ * ============LICENSE_END============================================
+ * ===================================================================
+ *
+ */
+
+package org.onap.clamp.dao.model.jsontype;
+
+import com.google.gson.JsonObject;
+
+import org.hibernate.type.AbstractSingleColumnStandardBasicType;
+import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
+import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
+
+public class StringJsonUserType extends AbstractSingleColumnStandardBasicType<JsonObject> {
+
+    /**
+     *
+     */
+    private static final long serialVersionUID = -7929809808079327767L;
+
+    public StringJsonUserType() {
+        super(JsonStringSqlTypeDescriptor.INSTANCE, JsonTypeDescriptor.INSTANCE);
+    }
+
+    public StringJsonUserType(SqlTypeDescriptor sqlTypeDescriptor, JavaTypeDescriptor<JsonObject> javaTypeDescriptor) {
+        super(sqlTypeDescriptor, javaTypeDescriptor);
+    }
+
+    @Override
+    public String getName() {
+        return "json";
+    }
+
+}
-- 
cgit