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.List;
19  import java.util.Map;
20  
21  import net.sf.oval.constraint.NotEmpty;
22  import net.sf.oval.constraint.NotNull;
23  import net.sf.oval.guard.Guarded;
24  import net.sf.oxclient.communication.ICommunicationClient;
25  import net.sf.oxclient.communication.CommunicationQuery;
26  
27  /**
28   * @author Björn Voß
29   *
30   */
31  @Guarded
32  public class JSONQuery implements CommunicationQuery {
33  
34  	private ICommunicationClient client;
35  	private final IJSONMapper jsonMapper;
36  	private final IParameterGenerator parameterGenerator;
37  
38  	private String module = "unset";
39  	private Map<String, String> params;
40  	private Map<Integer, String> propertyColumns;
41  
42  	public JSONQuery(
43  			final ICommunicationClient client,
44  			final String module,
45  			final IJSONMapper jsonMapper,
46  			final IParameterGenerator parameterGenerator) {
47  		this(jsonMapper, parameterGenerator);
48  		this.client = client;
49  		this.module = module;
50  	}
51  
52  	public JSONQuery(
53  			final IJSONMapper jsonMapper,
54  			final IParameterGenerator parameterGenerator) {
55  		super();
56  		this.jsonMapper = jsonMapper;
57  		this.parameterGenerator = parameterGenerator;
58  	}
59  
60  	public <E> List<E> queryForList(final Class<E> elementClass) {
61  		final Map<String, String> myParams = getParamsWithColumns();
62  		final String response = this.client.query(myParams, this.module);
63  		final List<String> columns = this.parameterGenerator
64  				.generateColumnList(myParams, this.propertyColumns);
65  		final List<E> result = this.jsonMapper.convertList(elementClass,
66  				response, columns);
67  		return result;
68  	}
69  
70  	public <E> E queryForObject(final Class<E> elementClass) {
71  		final String response = queryForResponseString();
72  		final E result = this.jsonMapper.convert(elementClass, response,
73  				this.propertyColumns);
74  		return result;
75  	}
76  
77  	/**
78  	 * @return
79  	 */
80  	private Map<String, String> getParamsWithColumns() {
81  		final Map<String, String> myParams = this.parameterGenerator
82  				.generateParameter(this.params, this.propertyColumns);
83  		return myParams;
84  	}
85  
86  	public void setParameters(final Map<String, String> params) {
87  		this.params = params;
88  	}
89  
90  	public void setPropertyColumns(final Map<Integer, String> propertyColumns) {
91  		this.propertyColumns = propertyColumns;
92  	}
93  
94  	/**
95  	 * @param module
96  	 *            the module to set
97  	 */
98  	public void setModule(@NotNull
99  	@NotEmpty
100 	final String module) {
101 		this.module = module;
102 	}
103 
104 	/**
105 	 * @param client
106 	 *            the client to set
107 	 */
108 	public void setClient(@NotNull
109 	final ICommunicationClient client) {
110 		this.client = client;
111 	}
112 
113 	/*
114 	 * (non-Javadoc)
115 	 *
116 	 * @see net.sf.oxclient.IQuery#queryForResponseString()
117 	 */
118 	public String queryForResponseString() {
119 		final Map<String, String> myParams = getParamsWithColumns();
120 		return this.client.query(myParams, this.module);
121 	}
122 }