Mercurial > lada > lada-server
comparison src/main/java/de/intevation/lada/util/data/JsonObjectType.java @ 1299:0eece233cbae
Added JSON object type to database dialect.
author | Raimund Renkert <raimund.renkert@intevation.de> |
---|---|
date | Fri, 24 Feb 2017 14:28:19 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1298:ba47994c1665 | 1299:0eece233cbae |
---|---|
1 /* Copyright (C) 2013 by Bundesamt fuer Strahlenschutz | |
2 * Software engineering by Intevation GmbH | |
3 * | |
4 * This file is Free Software under the GNU GPL (v>=3) | |
5 * and comes with ABSOLUTELY NO WARRANTY! Check out | |
6 * the documentation coming with IMIS-Labordaten-Application for details. | |
7 */ | |
8 package de.intevation.lada.util.data; | |
9 | |
10 import java.io.IOException; | |
11 import java.io.Serializable; | |
12 import java.sql.PreparedStatement; | |
13 import java.sql.ResultSet; | |
14 import java.sql.SQLException; | |
15 import java.sql.Types; | |
16 | |
17 import org.hibernate.HibernateException; | |
18 import org.hibernate.engine.spi.SessionImplementor; | |
19 import org.hibernate.usertype.UserType; | |
20 | |
21 import com.fasterxml.jackson.databind.JsonNode; | |
22 import com.fasterxml.jackson.databind.ObjectMapper; | |
23 | |
24 /** | |
25 * Implementation for a new data type in the postgresql/postgis jdbc driver. | |
26 * | |
27 * @author <a href="mailto:rrenkert@intevation.de">Raimund Renkert</a> | |
28 */ | |
29 public class JsonObjectType implements UserType { | |
30 /** | |
31 * Reconstruct an object from the cacheable representation. At the very | |
32 * least this method should perform a deep copy if the type is mutable. | |
33 * (optional <span id="IL_AD5" class="IL_AD">operation</span>) | |
34 * | |
35 * @param cached | |
36 * the object to be cached | |
37 * @param owner | |
38 * the owner of the cached object | |
39 * @return a reconstructed object from the cachable representation | |
40 * @throws HibernateException | |
41 */ | |
42 @Override | |
43 public Object assemble(Serializable cached, Object owner) throws HibernateException { | |
44 return this.deepCopy(cached); | |
45 } | |
46 | |
47 /** | |
48 * Return a deep copy of the persistent state, stopping at entities and st | |
49 * collections. It is not necessary to copy immutable objects, or null | |
50 * values, in which case it is safe to simple return the argument. | |
51 * | |
52 * @param value | |
53 * the object to be cloned, which may be null | |
54 * | |
55 * @return object a copy | |
56 * @throws HibernateException | |
57 */ | |
58 @Override | |
59 public Object deepCopy(Object value) throws HibernateException { | |
60 return value; | |
61 } | |
62 | |
63 /** | |
64 * Transform the object into its cacheable representation. At the very least | |
65 * this method should perform a deep copy if the type is mutable. That may | |
66 * not be enough for some implementations, however; for example, | |
67 * <span id="IL_AD11" class="IL_AD">associations</span> must be cached as <span id="IL_AD9" class="IL_AD">identifier</span> values. (optional operation) | |
68 * | |
69 * @param value | |
70 * the object to be cached | |
71 * @return a cachable representation of the object | |
72 * @throws HibernateException | |
73 */ | |
74 @Override | |
75 public Serializable disassemble(Object value) throws HibernateException { | |
76 return (String) this.deepCopy(value); | |
77 } | |
78 | |
79 /** | |
80 * Compare two instances of the class mapped by this type for persistence | |
81 * "equality". Equality of the persistence state. | |
82 * | |
83 * @param x | |
84 * @param y | |
85 * @return <span id="IL_AD12" class="IL_AD">boolean</span> | |
86 * @throws HibernateException | |
87 */ | |
88 @Override | |
89 public boolean equals(Object x, Object y) throws HibernateException { | |
90 if (x == null) { | |
91 return y == null; | |
92 } | |
93 return x.equals(y); | |
94 } | |
95 | |
96 /** | |
97 * Get a hashcode for the instance, consistent with persistence "equality". | |
98 */ | |
99 @Override | |
100 public int hashCode(Object x) throws HibernateException { | |
101 return x.hashCode(); | |
102 } | |
103 | |
104 /** | |
105 * Are objects of this type mutable? | |
106 * | |
107 * @return boolean | |
108 */ | |
109 @Override | |
110 public boolean isMutable() { | |
111 return true; | |
112 } | |
113 | |
114 /** | |
115 * Retrieve an instance of the mapped class from a JDBC resultset. | |
116 * Implementors should handle possibility of null values. | |
117 * | |
118 * @param rs | |
119 * a JDBC result set | |
120 * @param names | |
121 * the column names | |
122 * @param session | |
123 * @param owner | |
124 * the containing entity | |
125 * @return | |
126 * @throws HibernateException | |
127 * @throws SQLException | |
128 */ | |
129 @Override | |
130 public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException { | |
131 ObjectMapper mapper = new ObjectMapper(); | |
132 JsonNode node = null; | |
133 try { | |
134 node = mapper.readTree(rs.getString(names[0])); | |
135 return node; | |
136 } catch (IOException e) { | |
137 e.printStackTrace(); | |
138 } | |
139 return mapper.createObjectNode(); | |
140 } | |
141 | |
142 /** | |
143 * Write an instance of the mapped class to a prepared statement. | |
144 * Implementors should handle possibility of null values. A multi-column | |
145 * type should be written to parameters starting from <tt>index</tt> | |
146 * | |
147 * @param st | |
148 * a JDBC prepared statement | |
149 * @param value | |
150 * the object to write | |
151 * @param index | |
152 * statement parameter index | |
153 * @param session | |
154 * @throws HibernateException | |
155 * @throws SQLException | |
156 */ | |
157 @Override | |
158 public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException { | |
159 if (value == null) { | |
160 st.setNull(index, Types.OTHER); | |
161 return; | |
162 } | |
163 st.setObject(index, value, Types.OTHER); | |
164 } | |
165 | |
166 /** | |
167 * During merge, <span id="IL_AD7" class="IL_AD">replace</span> the existing (target) values in the entity we are | |
168 * merging to with a new (original) value from the detched entity we are | |
169 * merging. For immutable objects, or null values, it is safe to return a | |
170 * copy of the first parameter. For the objects with component values, it | |
171 * might make sense to recursively replace component values | |
172 * | |
173 * @param original | |
174 * the value from the detched entity being merged | |
175 * @param target | |
176 * the value in the managed entity | |
177 * @param owner | |
178 * @return the value to be merged | |
179 * @throws HibernateException | |
180 */ | |
181 @Override | |
182 public Object replace(Object original, Object target, Object owner) throws HibernateException { | |
183 return original; | |
184 } | |
185 | |
186 /** | |
187 * The class returned by <tt>nullSafeGet()</tt> | |
188 * | |
189 * @return Class | |
190 */ | |
191 @Override | |
192 public Class returnedClass() { | |
193 return String.class; | |
194 } | |
195 | |
196 /** | |
197 * Returns the SQL type <span id="IL_AD3" class="IL_AD">codes</span> for the columns mapped by this type. The codes | |
198 * are defined on <tt>java.sql.Types</tt> | |
199 * | |
200 * @return int[] the typecodes | |
201 * @see java.sql.Types | |
202 */ | |
203 @Override | |
204 public int[] sqlTypes() { | |
205 return new int[] { Types.JAVA_OBJECT }; | |
206 } | |
207 } |