1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portal.kernel.poller;
16  
17  import com.liferay.portal.kernel.json.JSONArray;
18  import com.liferay.portal.kernel.json.JSONFactoryUtil;
19  import com.liferay.portal.kernel.json.JSONObject;
20  import com.liferay.portal.kernel.util.Validator;
21  
22  import java.util.HashMap;
23  import java.util.Iterator;
24  import java.util.Map;
25  
26  /**
27   * <a href="PollerResponse.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   */
31  public class PollerResponse {
32  
33      public static final String POLLER_HINT_HIGH_CONNECTIVITY =
34          "pollerHintHighConnectivity";
35  
36      public PollerResponse(String portletId, String chunkId) {
37          _portletId = portletId;
38          _chunkId = chunkId;
39      }
40  
41      public void setParameter(String name, JSONArray value) {
42          _parameterMap.put(name, value);
43      }
44  
45      public void setParameter(String name, JSONObject value) {
46          _parameterMap.put(name, value);
47      }
48  
49      public void setParameter(String name, String value) {
50          _parameterMap.put(name, value);
51      }
52  
53      public JSONObject toJSONObject() {
54          JSONObject pollerResponseJSON = JSONFactoryUtil.createJSONObject();
55  
56          pollerResponseJSON.put("portletId", _portletId);
57  
58          if (Validator.isNotNull(_chunkId)) {
59              pollerResponseJSON.put("chunkId", _chunkId);
60          }
61  
62          JSONObject dataJSON = JSONFactoryUtil.createJSONObject();
63  
64          Iterator<Map.Entry<String, Object>> itr =
65              _parameterMap.entrySet().iterator();
66  
67          while (itr.hasNext()) {
68              Map.Entry<String, Object> entry = itr.next();
69  
70              String name = entry.getKey();
71              Object value = entry.getValue();
72  
73              if (value instanceof JSONArray) {
74                  dataJSON.put(name, (JSONArray)value);
75              }
76              else if (value instanceof JSONObject) {
77                  dataJSON.put(name, (JSONObject)value);
78              }
79              else {
80                  dataJSON.put(name, String.valueOf(value));
81              }
82          }
83  
84          pollerResponseJSON.put("data", dataJSON);
85  
86          return pollerResponseJSON;
87      }
88  
89      private String _chunkId;
90      private Map<String, Object> _parameterMap = new HashMap<String, Object>();
91      private String _portletId;
92  
93  }