38 changed files with 478 additions and 339 deletions
@ -0,0 +1,60 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2017 The Thingsboard Authors |
||||
|
* |
||||
|
* 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.thingsboard.server.dao.util; |
||||
|
|
||||
|
import com.fasterxml.jackson.core.JsonProcessingException; |
||||
|
import com.fasterxml.jackson.databind.JsonNode; |
||||
|
import com.fasterxml.jackson.databind.ObjectMapper; |
||||
|
|
||||
|
import java.io.IOException; |
||||
|
|
||||
|
/** |
||||
|
* Created by Valerii Sosliuk on 5/12/2017. |
||||
|
*/ |
||||
|
public class JacksonUtil { |
||||
|
|
||||
|
public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); |
||||
|
|
||||
|
public static <T> T fromString(String string, Class<T> clazz) { |
||||
|
try { |
||||
|
return OBJECT_MAPPER.readValue(string, clazz); |
||||
|
} catch (IOException e) { |
||||
|
throw new IllegalArgumentException("The given string value: " |
||||
|
+ string + " cannot be transformed to Json object"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public static String toString(Object value) { |
||||
|
try { |
||||
|
return OBJECT_MAPPER.writeValueAsString(value); |
||||
|
} catch (JsonProcessingException e) { |
||||
|
throw new IllegalArgumentException("The given Json object value: " |
||||
|
+ value + " cannot be transformed to a String"); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public static JsonNode toJsonNode(String value) { |
||||
|
try { |
||||
|
return OBJECT_MAPPER.readTree(value); |
||||
|
} catch (IOException e) { |
||||
|
throw new IllegalArgumentException(e); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
public static <T> T clone(T value) { |
||||
|
return fromString(toString(value), (Class<T>) value.getClass()); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,114 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2017 The Thingsboard Authors |
||||
|
* |
||||
|
* 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.thingsboard.server.dao.util; |
||||
|
|
||||
|
import com.fasterxml.jackson.databind.JsonNode; |
||||
|
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; |
||||
|
|
||||
|
import java.sql.*; |
||||
|
|
||||
|
/** |
||||
|
* Created by Valerii Sosliuk on 5/12/2017. |
||||
|
*/ |
||||
|
public class JsonBinarySqlTypeDescriptor |
||||
|
implements SqlTypeDescriptor { |
||||
|
|
||||
|
public static final JsonBinarySqlTypeDescriptor INSTANCE = |
||||
|
new JsonBinarySqlTypeDescriptor(); |
||||
|
|
||||
|
@Override |
||||
|
public int getSqlType() { |
||||
|
return Types.OTHER; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public boolean canBeRemapped() { |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public <X> ValueExtractor<X> getExtractor( |
||||
|
final JavaTypeDescriptor<X> javaTypeDescriptor) { |
||||
|
return new BasicExtractor<X>(javaTypeDescriptor, this) { |
||||
|
@Override |
||||
|
protected X doExtract( |
||||
|
ResultSet rs, |
||||
|
String name, |
||||
|
WrapperOptions options) throws SQLException { |
||||
|
return javaTypeDescriptor.wrap( |
||||
|
rs.getObject(name), options |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected X doExtract( |
||||
|
CallableStatement statement, |
||||
|
int index, |
||||
|
WrapperOptions options) throws SQLException { |
||||
|
return javaTypeDescriptor.wrap( |
||||
|
statement.getObject(index), options |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected X doExtract( |
||||
|
CallableStatement statement, |
||||
|
String name, |
||||
|
WrapperOptions options) throws SQLException { |
||||
|
return javaTypeDescriptor.wrap( |
||||
|
statement.getObject(name), options |
||||
|
); |
||||
|
} |
||||
|
}; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public <X> ValueBinder<X> getBinder( |
||||
|
final JavaTypeDescriptor<X> javaTypeDescriptor) { |
||||
|
return new BasicBinder<X>(javaTypeDescriptor, this) { |
||||
|
@Override |
||||
|
protected void doBind( |
||||
|
PreparedStatement st, |
||||
|
X value, |
||||
|
int index, |
||||
|
WrapperOptions options) throws SQLException { |
||||
|
st.setObject(index, |
||||
|
javaTypeDescriptor.unwrap( |
||||
|
value, JsonNode.class, options), getSqlType() |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
protected void doBind( |
||||
|
CallableStatement st, |
||||
|
X value, |
||||
|
String name, |
||||
|
WrapperOptions options) |
||||
|
throws SQLException { |
||||
|
st.setObject(name, |
||||
|
javaTypeDescriptor.unwrap( |
||||
|
value, JsonNode.class, options), getSqlType() |
||||
|
); |
||||
|
} |
||||
|
}; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,44 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2017 The Thingsboard Authors |
||||
|
* |
||||
|
* 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.thingsboard.server.dao.util; |
||||
|
|
||||
|
import org.hibernate.type.AbstractSingleColumnStandardBasicType; |
||||
|
import org.hibernate.usertype.DynamicParameterizedType; |
||||
|
|
||||
|
import java.util.Properties; |
||||
|
|
||||
|
/** |
||||
|
* Created by Valerii Sosliuk on 5/12/2017. |
||||
|
*/ |
||||
|
public class JsonBinaryType extends AbstractSingleColumnStandardBasicType<Object> |
||||
|
implements DynamicParameterizedType { |
||||
|
|
||||
|
public JsonBinaryType() { |
||||
|
super( |
||||
|
JsonBinarySqlTypeDescriptor.INSTANCE, |
||||
|
new JsonTypeDescriptor() |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
public String getName() { |
||||
|
return "jsonb"; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public void setParameterValues(Properties parameters) { |
||||
|
((JsonTypeDescriptor) getJavaTypeDescriptor()) |
||||
|
.setParameterValues(parameters); |
||||
|
}} |
||||
@ -0,0 +1,95 @@ |
|||||
|
/** |
||||
|
* Copyright © 2016-2017 The Thingsboard Authors |
||||
|
* |
||||
|
* 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.thingsboard.server.dao.util; |
||||
|
|
||||
|
import org.hibernate.type.descriptor.WrapperOptions; |
||||
|
import org.hibernate.type.descriptor.java.AbstractTypeDescriptor; |
||||
|
import org.hibernate.type.descriptor.java.MutableMutabilityPlan; |
||||
|
import org.hibernate.usertype.DynamicParameterizedType; |
||||
|
|
||||
|
import java.util.Properties; |
||||
|
|
||||
|
/** |
||||
|
* Created by Valerii Sosliuk on 5/12/2017. |
||||
|
*/ |
||||
|
public class JsonTypeDescriptor |
||||
|
extends AbstractTypeDescriptor<Object> |
||||
|
implements DynamicParameterizedType { |
||||
|
|
||||
|
private Class<?> jsonObjectClass; |
||||
|
|
||||
|
@Override |
||||
|
public void setParameterValues(Properties parameters) { |
||||
|
jsonObjectClass = ( (ParameterType) parameters.get( PARAMETER_TYPE ) ) |
||||
|
.getReturnedClass(); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public JsonTypeDescriptor() { |
||||
|
super( Object.class, new MutableMutabilityPlan<Object>() { |
||||
|
@Override |
||||
|
protected Object deepCopyNotNull(Object value) { |
||||
|
return JacksonUtil.clone(value); |
||||
|
} |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public boolean areEqual(Object one, Object another) { |
||||
|
if ( one == another ) { |
||||
|
return true; |
||||
|
} |
||||
|
if ( one == null || another == null ) { |
||||
|
return false; |
||||
|
} |
||||
|
return JacksonUtil.toJsonNode(JacksonUtil.toString(one)).equals( |
||||
|
JacksonUtil.toJsonNode(JacksonUtil.toString(another))); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public String toString(Object value) { |
||||
|
return JacksonUtil.toString(value); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Object fromString(String string) { |
||||
|
return JacksonUtil.fromString(string, jsonObjectClass); |
||||
|
} |
||||
|
|
||||
|
@SuppressWarnings({ "unchecked" }) |
||||
|
@Override |
||||
|
public <X> X unwrap(Object value, Class<X> type, WrapperOptions options) { |
||||
|
if ( value == null ) { |
||||
|
return null; |
||||
|
} |
||||
|
if ( String.class.isAssignableFrom( type ) ) { |
||||
|
return (X) toString(value); |
||||
|
} |
||||
|
if ( Object.class.isAssignableFrom( type ) ) { |
||||
|
return (X) JacksonUtil.toJsonNode(toString(value)); |
||||
|
} |
||||
|
throw unknownUnwrap( type ); |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public <X> Object wrap(X value, WrapperOptions options) { |
||||
|
if ( value == null ) { |
||||
|
return null; |
||||
|
} |
||||
|
return fromString(value.toString()); |
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue