001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.poller;
016    
017    import com.liferay.portal.kernel.json.JSONArray;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.util.Validator;
021    
022    import java.util.HashMap;
023    import java.util.Iterator;
024    import java.util.Map;
025    
026    /**
027     * @author Brian Wing Shun Chan
028     */
029    public class PollerResponse {
030    
031            public static final String POLLER_HINT_HIGH_CONNECTIVITY =
032                    "pollerHintHighConnectivity";
033    
034            public PollerResponse(String portletId, String chunkId) {
035                    _portletId = portletId;
036                    _chunkId = chunkId;
037            }
038    
039            public void setParameter(String name, JSONArray value) {
040                    _parameterMap.put(name, value);
041            }
042    
043            public void setParameter(String name, JSONObject value) {
044                    _parameterMap.put(name, value);
045            }
046    
047            public void setParameter(String name, String value) {
048                    _parameterMap.put(name, value);
049            }
050    
051            public JSONObject toJSONObject() {
052                    JSONObject pollerResponseJSON = JSONFactoryUtil.createJSONObject();
053    
054                    pollerResponseJSON.put("portletId", _portletId);
055    
056                    if (Validator.isNotNull(_chunkId)) {
057                            pollerResponseJSON.put("chunkId", _chunkId);
058                    }
059    
060                    JSONObject dataJSON = JSONFactoryUtil.createJSONObject();
061    
062                    Iterator<Map.Entry<String, Object>> itr =
063                            _parameterMap.entrySet().iterator();
064    
065                    while (itr.hasNext()) {
066                            Map.Entry<String, Object> entry = itr.next();
067    
068                            String name = entry.getKey();
069                            Object value = entry.getValue();
070    
071                            if (value instanceof JSONArray) {
072                                    dataJSON.put(name, (JSONArray)value);
073                            }
074                            else if (value instanceof JSONObject) {
075                                    dataJSON.put(name, (JSONObject)value);
076                            }
077                            else {
078                                    dataJSON.put(name, String.valueOf(value));
079                            }
080                    }
081    
082                    pollerResponseJSON.put("data", dataJSON);
083    
084                    return pollerResponseJSON;
085            }
086    
087            private String _chunkId;
088            private Map<String, Object> _parameterMap = new HashMap<String, Object>();
089            private String _portletId;
090    
091    }