001    /**
002     * Copyright (c) 2000-present 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.license.messaging;
016    
017    import com.liferay.portal.kernel.json.JSONFactoryUtil;
018    import com.liferay.portal.kernel.json.JSONObject;
019    import com.liferay.portal.kernel.messaging.Message;
020    
021    /**
022     * @author Igor Beslic
023     */
024    public enum LicenseManagerMessageType {
025    
026            LCS_AVAILABLE, SUBSCRIPTION_VALID, VALIDATE_LCS, VALIDATE_SUBSCRIPTION;
027    
028            public static String MESSAGE_BUS_DESTINATION_REQUEST =
029                    "liferay/lcs_request";
030    
031            public static String MESSAGE_BUS_DESTINATION_STATUS = "liferay/lcs_status";
032    
033            public static JSONObject getMessagePayload(Message message) {
034                    return getMessagePayload(message.getPayload());
035            }
036    
037            public static JSONObject getMessagePayload(Object object) {
038                    if (object instanceof String) {
039                            return getMessagePayload((String)object);
040                    }
041    
042                    return null;
043            }
044    
045            public static JSONObject getMessagePayload(String json) {
046                    try {
047                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject(json);
048    
049                            valueOf(jsonObject);
050    
051                            return jsonObject;
052                    }
053                    catch (Exception e) {
054                            return null;
055                    }
056            }
057    
058            public static LicenseManagerMessageType valueOf(JSONObject jsonObject) {
059                    String type = jsonObject.getString("type");
060    
061                    return valueOf(type);
062            }
063    
064            public Message createMessage() {
065                    Message message = new Message();
066    
067                    message.setDestinationName(getDestinationName());
068    
069                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
070    
071                    jsonObject.put("type", name());
072    
073                    message.setPayload(jsonObject.toString());
074    
075                    return message;
076            }
077    
078            public Message createMessage(LCSPortletState lcsPortletState) {
079                    Message message = new Message();
080    
081                    message.setDestinationName(getDestinationName());
082    
083                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
084    
085                    jsonObject.put("state", lcsPortletState.intValue());
086                    jsonObject.put("type", name());
087    
088                    message.setPayload(jsonObject.toString());
089    
090                    return message;
091            }
092    
093            public String getDestinationName() {
094                    if ((this == LCS_AVAILABLE) || (this == SUBSCRIPTION_VALID)) {
095                            return MESSAGE_BUS_DESTINATION_STATUS;
096                    }
097                    else if ((this == VALIDATE_LCS) || (this == VALIDATE_SUBSCRIPTION)) {
098                            return MESSAGE_BUS_DESTINATION_REQUEST;
099                    }
100    
101                    return null;
102            }
103    
104    }