1
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
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 }