1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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
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
96
97
98 public void setModule(@NotNull
99 @NotEmpty
100 final String module) {
101 this.module = module;
102 }
103
104
105
106
107
108 public void setClient(@NotNull
109 final ICommunicationClient client) {
110 this.client = client;
111 }
112
113
114
115
116
117
118 public String queryForResponseString() {
119 final Map<String, String> myParams = getParamsWithColumns();
120 return this.client.query(myParams, this.module);
121 }
122 }