1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
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  }