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 static final String POLLER_HINT_HIGH_CONNECTIVITY =
43          "pollerHintHighConnectivity";
44  
45      public PollerResponse(String portletId, String chunkId) {
46          _portletId = portletId;
47          _chunkId = chunkId;
48      }
49  
50      public void setParameter(String name, JSONArray value) {
51          _parameterMap.put(name, value);
52      }
53  
54      public void setParameter(String name, JSONObject value) {
55          _parameterMap.put(name, value);
56      }
57  
58      public void setParameter(String name, String value) {
59          _parameterMap.put(name, value);
60      }
61  
62      public JSONObject toJSONObject() {
63          JSONObject pollerResponseJSON = JSONFactoryUtil.createJSONObject();
64  
65          pollerResponseJSON.put("portletId", _portletId);
66  
67          if (Validator.isNotNull(_chunkId)) {
68              pollerResponseJSON.put("chunkId", _chunkId);
69          }
70  
71          JSONObject dataJSON = JSONFactoryUtil.createJSONObject();
72  
73          Iterator<Map.Entry<String, Object>> itr =
74              _parameterMap.entrySet().iterator();
75  
76          while (itr.hasNext()) {
77              Map.Entry<String, Object> entry = itr.next();
78  
79              String name = entry.getKey();
80              Object value = entry.getValue();
81  
82              if (value instanceof JSONArray) {
83                  dataJSON.put(name, (JSONArray)value);
84              }
85              else if (value instanceof JSONObject) {
86                  dataJSON.put(name, (JSONObject)value);
87              }
88              else {
89                  dataJSON.put(name, String.valueOf(value));
90              }
91          }
92  
93          pollerResponseJSON.put("data", dataJSON);
94  
95          return pollerResponseJSON;
96      }
97  
98      private String _chunkId;
99      private Map<String, Object> _parameterMap = new HashMap<String, Object>();
100     private String _portletId;
101 
102 }