| PollerResponse.java |
1 /**
2 * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 * SOFTWARE.
21 */
22
23 package com.liferay.portal.kernel.poller;
24
25 import com.liferay.portal.kernel.json.JSONArray;
26 import com.liferay.portal.kernel.json.JSONFactoryUtil;
27 import com.liferay.portal.kernel.json.JSONObject;
28 import com.liferay.portal.kernel.util.Validator;
29
30 import java.util.HashMap;
31 import java.util.Iterator;
32 import java.util.Map;
33
34 /**
35 * <a href="PollerResponse.java.html"><b><i>View Source</i></b></a>
36 *
37 * @author Brian Wing Shun Chan
38 *
39 */
40 public class PollerResponse {
41
42 public PollerResponse(String portletId, String chunkId) {
43 _portletId = portletId;
44 _chunkId = chunkId;
45 }
46
47 public void setParameter(String name, JSONArray value) {
48 _parameterMap.put(name, value);
49 }
50
51 public void setParameter(String name, JSONObject value) {
52 _parameterMap.put(name, value);
53 }
54
55 public void setParameter(String name, String value) {
56 _parameterMap.put(name, value);
57 }
58
59 public JSONObject toJSONObject() {
60 JSONObject pollerResponseJSON = JSONFactoryUtil.createJSONObject();
61
62 pollerResponseJSON.put("portletId", _portletId);
63
64 if (Validator.isNotNull(_chunkId)) {
65 pollerResponseJSON.put("chunkId", _chunkId);
66 }
67
68 JSONObject dataJSON = JSONFactoryUtil.createJSONObject();
69
70 Iterator<Map.Entry<String, Object>> itr =
71 _parameterMap.entrySet().iterator();
72
73 while (itr.hasNext()) {
74 Map.Entry<String, Object> entry = itr.next();
75
76 String name = entry.getKey();
77 Object value = entry.getValue();
78
79 if (value instanceof JSONArray) {
80 dataJSON.put(name, (JSONArray)value);
81 }
82 else if (value instanceof JSONObject) {
83 dataJSON.put(name, (JSONObject)value);
84 }
85 else {
86 dataJSON.put(name, String.valueOf(value));
87 }
88 }
89
90 pollerResponseJSON.put("data", dataJSON);
91
92 return pollerResponseJSON;
93 }
94
95 private String _chunkId;
96 private Map<String, Object> _parameterMap = new HashMap<String, Object>();
97 private String _portletId;
98
99 }