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                    if (!(message.getPayload() instanceof String)) {
035                            return null;
036                    }
037    
038                    try {
039                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject(
040                                    (String)message.getPayload());
041    
042                            valueOf(jsonObject);
043    
044                            return jsonObject;
045                    }
046                    catch (Exception e) {
047                            return null;
048                    }
049            }
050    
051            public static JSONObject getMessagePayload(Object object) {
052                    if (!(object instanceof Message)) {
053                            return null;
054                    }
055    
056                    return getMessagePayload((Message)object);
057            }
058    
059            public static LicenseManagerMessageType valueOf(JSONObject jsonObject) {
060                    String type = jsonObject.getString("type");
061    
062                    return valueOf(type);
063            }
064    
065            public Message createMessage() {
066                    Message message = new Message();
067    
068                    message.setDestinationName(getDestinationName());
069    
070                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
071    
072                    jsonObject.put("type", name());
073    
074                    message.setPayload(jsonObject.toString());
075    
076                    return message;
077            }
078    
079            public Message createMessage(LCSPortletState state) {
080                    Message message = new Message();
081    
082                    message.setDestinationName(getDestinationName());
083    
084                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
085    
086                    jsonObject.put("type", name());
087                    jsonObject.put("state", state.intValue());
088    
089                    message.setPayload(jsonObject.toString());
090    
091                    return message;
092            }
093    
094            public String getDestinationName() {
095                    if ((this == LCS_AVAILABLE) || (this == SUBSCRIPTION_VALID)) {
096                            return MESSAGE_BUS_DESTINATION_STATUS;
097                    }
098                    else if ((this == VALIDATE_LCS) || (this == VALIDATE_SUBSCRIPTION)) {
099                            return MESSAGE_BUS_DESTINATION_REQUEST;
100                    }
101    
102                    return null;
103            }
104    
105    }