View Javadoc

1   /**
2    * Copyright 2007 Björn Voß
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5    * use this file except in compliance with the License. You may obtain a copy of
6    * 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, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.oxclient.communication.json;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Properties;
24  import java.util.Map.Entry;
25  
26  import net.sf.json.JSONArray;
27  import net.sf.json.JSONObject;
28  import net.sf.oval.constraint.NotEmpty;
29  import net.sf.oval.constraint.NotNull;
30  import net.sf.oval.guard.Guarded;
31  import net.sf.oxclient.factory.IBeanFactory;
32  import net.sf.oxclient.factory.IBeanHelper;
33  
34  import org.apache.commons.collections.MapUtils;
35  
36  @Guarded
37  public class SimpleJSONMapper implements IJSONMapper {
38  
39  	private final IBeanFactory beanFactory;
40  	private final IBeanHelper beanHelper;
41  	private final Map<Integer, String> oxColumns;
42  
43  	@SuppressWarnings("unchecked")
44  	public SimpleJSONMapper(
45  			final IBeanFactory beanFactory,
46  			final Map<Integer, String> oxColumns,
47  			final IBeanHelper beanHelper) {
48  		super();
49  		this.beanFactory = beanFactory;
50  		this.beanHelper = beanHelper;
51  		this.oxColumns = MapUtils.unmodifiableMap(oxColumns);
52  	}
53  
54  	public SimpleJSONMapper(
55  			final IBeanFactory beanFactory,
56  			final IBeanHelper beanHelper,
57  			final Properties oxColumns
58  			) {
59  		super();
60  		this.beanFactory = beanFactory;
61  		this.beanHelper = beanHelper;
62  		this.oxColumns = generateMap(oxColumns);
63  	}
64  
65  
66  	/**
67  	 * @param oxColumns2
68  	 * @return
69  	 */
70  	private Map<Integer, String> generateMap(final Properties properties) {
71  		final Map<Integer, String> result = new HashMap<Integer, String>();
72  		for (final Entry<Object, Object> entry : properties.entrySet()) {
73  			final Integer key =  Integer.valueOf(entry.getKey().toString());
74  			final String value = entry.getValue().toString();
75  			result.put(key, value);
76  		}
77  		return result;
78  	}
79  
80  	public <T> T convert(
81  			@NotNull final Class<T> element,
82  			@NotNull @NotEmpty final String s,
83  			@NotNull @NotEmpty final Map<Integer, String> columns) {
84  		final JSONObject jsonObject = parseResponse(s);
85  		final T result = this.beanFactory.createNewInstance(element);
86  		for (final Map.Entry<Integer, String> entry : columns.entrySet()) {
87  			final String oxProperty = this.oxColumns.get(entry.getKey());
88  			final Object value = jsonObject.opt(oxProperty);
89  			final String elementProperty = entry.getValue();
90  			this.beanHelper.setProperty(result, elementProperty, value);
91  		}
92  		return result;
93  	}
94  
95  	private JSONObject parseResponse(final String response) {
96  		final JSONObject jsonObject = JSONObject.fromObject(response);
97  		final JSONObject result = jsonObject.optJSONObject("data");
98  		if (result == null) {
99  			System.out.println("response:\n" + response);
100 			throw createErrorException(jsonObject);
101 		}
102 		return result;
103 	}
104 
105 	@SuppressWarnings("unchecked")
106 	private JSONException createErrorException(final JSONObject response) {
107 		final JSONException result = new JSONException(response.optString("error"));
108 		result.setCode(response.optString("code"));
109 		result.setErrorId(response.optString("error_id"));
110 		final JSONArray jsonarray = response.optJSONArray("error_params");
111 		if (jsonarray != null) {
112 			result.setErrorParams(JSONArray.toList(jsonarray, Integer.class));
113 		}
114 		return result;
115 	}
116 
117 	@SuppressWarnings("unchecked")
118 	public <T> List<T> convertList(
119 			@NotNull final Class<T> elementClazz,
120 			@NotNull @NotEmpty final String s,
121 			@NotNull @NotEmpty final List<String> columns) {
122 		final JSONArray response = parseResponseArray(s);
123 		final List<T> result = new ArrayList<T>();
124 		final Iterator<JSONArray> it = response.iterator();
125 		while (it.hasNext()) {
126 			final T element = this.beanFactory.createNewInstance(elementClazz);
127 			final JSONArray jsonElem = it.next();
128 			for (int i = 0; i < columns.size(); i++) {
129 				final Object value = jsonElem.opt(i);
130 				this.beanHelper.setProperty(element, columns.get(i), value);
131 			}
132 			result.add(element);
133 		}
134 		return result;
135 	}
136 
137 	private JSONArray parseResponseArray(final String response) {
138 		final JSONObject jsonObject = JSONObject.fromObject(response);
139 		final JSONArray result = jsonObject.optJSONArray("data");
140 		if (result == null) {
141 			throw createErrorException(jsonObject);
142 		}
143 		return result;
144 	}
145 
146 }