View Javadoc
1   /*
2    *  Copyright 2010, 2011, 2012 Christopher Pheby
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.jadira.usertype.json.jackson;
17  
18  import java.io.IOException;
19  import java.lang.reflect.InvocationTargetException;
20  import java.sql.PreparedStatement;
21  import java.sql.ResultSet;
22  import java.sql.SQLException;
23  import java.util.Properties;
24  
25  import org.hibernate.HibernateException;
26  import org.hibernate.engine.spi.SharedSessionContractImplementor;
27  import org.hibernate.usertype.ParameterizedType;
28  import org.jadira.usertype.spi.shared.AbstractKnownClassHeuristicUserType;
29  
30  import com.fasterxml.jackson.core.JsonParseException;
31  import com.fasterxml.jackson.core.JsonProcessingException;
32  import com.fasterxml.jackson.databind.JsonMappingException;
33  import com.fasterxml.jackson.databind.ObjectMapper;
34  import com.fasterxml.jackson.databind.ObjectReader;
35  import com.fasterxml.jackson.databind.ObjectWriter;
36  
37  public class PersistentJsonObjectAsString<T> extends AbstractKnownClassHeuristicUserType<T> implements ParameterizedType {
38  
39  	private static final long serialVersionUID = 3094384329334123541L;
40  	
41  	private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
42  	
43  	private ObjectReader objectReader;
44  
45  	private ObjectWriter objectWriter;
46  	
47  	protected ObjectReader getObjectReader() {
48  		return objectReader;
49  	}
50  	
51  	protected ObjectWriter getObjectWriter() {
52  		return objectWriter;
53  	}
54  	
55  	protected void setMappedClass(Class<T> mappedClass) {
56      	super.setMappedClass(mappedClass);
57      	
58      	objectReader = OBJECT_MAPPER.readerFor(mappedClass);
59      	objectWriter = OBJECT_MAPPER.writerFor(mappedClass);
60      }
61  	
62  	@SuppressWarnings({ "unchecked" })
63  	public void setParameterValues(Properties parameters) {
64  
65  		if (parameters.containsKey("jsonClass")) {
66  			String jsonClassName = parameters.getProperty("jsonClass");
67  			try {
68  				Class<T> mappedClass = (Class<T>) Class.forName(jsonClassName);
69  				setMappedClass(mappedClass);
70  				setIdentifierType(mappedClass);
71  			} catch (ClassNotFoundException e) {
72  				throw new HibernateException("Specified class could not be found", e);
73  			}
74  		}
75  		
76  		super.setParameterValues(parameters);
77  	}
78  	
79  	 @Override
80  	 public Class<T> returnedClass() {
81  	    Class<T> mappedClass = getMappedClass();
82  	    if (mappedClass == null) {
83  	        throw new IllegalStateException("class was not defined for " + this.getClass().getName());
84  	    }
85  	    return mappedClass;
86  	 }
87  	 
88  	@Override
89  	public Object doNullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
90  
91  		String jsonText = (String) getType().get(rs, names[0], session);
92  		
93  		if (rs.wasNull()) {
94  			return null;
95  		}
96  
97  		Object obj;
98  		try {
99  			obj = getObjectReader().readValue(jsonText);
100 		} catch (JsonParseException e) {
101 			throw new HibernateException("Problem parsing retrieved JSON String: " + jsonText, e);
102 		} catch (JsonMappingException e) {
103 			throw new HibernateException("Problem mapping retrieved JSON String: " + jsonText, e);
104 		} catch (IOException e) {
105 			throw new HibernateException("Problem reading JSON String: " + jsonText, e);
106 		}
107 		
108 		return obj;
109 	}
110 
111     @Override
112     public void doNullSafeSet(PreparedStatement preparedStatement, Object value, int index, SharedSessionContractImplementor session) throws SQLException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
113 
114     	if (value == null) {
115 			preparedStatement.setNull(index, getType().sqlType());
116 		} else {				
117 			
118 			try {
119 				String identifier = objectWriter.writeValueAsString(value);
120 				getType().nullSafeSet(preparedStatement, identifier, index, session);
121 			} catch (JsonProcessingException e) {
122 				throw new HibernateException("Problem writing JSON String: " + e.getMessage(), e);
123 			}
124 		}
125     }
126     
127     @Override
128     public String objectToSQLString(final Object object) {
129     	
130     	if (object == null) {
131     		return null;
132     	}
133 
134     	try {
135 			return objectWriter.writeValueAsString(object);
136 		} catch (JsonProcessingException e) {
137 			throw new HibernateException("Cannot serialize JSON object: " + e.getMessage(), e);
138 		}
139     }
140     
141     @Override
142     public String toXMLString(Object object) {
143     	
144     	if (object == null) {
145     		return null;
146     	}
147 
148     	try {
149 			return objectWriter.writeValueAsString(object);
150 		} catch (JsonProcessingException e) {
151 			throw new HibernateException("Cannot serialize JSON object: " + e.getMessage(), e);
152 		}
153     }
154 }