@Override
 public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse)
     throws IOException, PortletException {
   String id = resourceRequest.getResourceID();
   if (id.equalsIgnoreCase("getBrokerTopics")) {
     JSONArray topics = JSONFactoryUtil.createJSONArray();
     long brokerId = ParamUtil.getLong(resourceRequest, "brokerId");
     long brokerMessageListenerId = ParamUtil.getLong(resourceRequest, "brokerMessageListenerId");
     try {
       Broker b = BrokerLocalServiceUtil.fetchBroker(brokerId);
       BrokerMessageListener bml =
           (brokerMessageListenerId > 0)
               ? BrokerMessageListenerLocalServiceUtil.fetchBrokerMessageListener(
                   brokerMessageListenerId)
               : null;
       String[] topicsArr = b.getTopics().split(";");
       for (int i = 0; i < topicsArr.length; i++) {
         JSONObject obj = JSONFactoryUtil.createJSONObject();
         obj.put("topic", topicsArr[i]);
         boolean checked = false;
         if (bml != null) {
           String[] messageListenerTopcs = bml.getTopics().split(";");
           for (int j = 0; j < messageListenerTopcs.length && !checked; j++) {
             if (messageListenerTopcs[j].equalsIgnoreCase(topicsArr[i])) checked = true;
           }
         }
         obj.put("checked", checked);
         topics.put(obj);
       }
     } catch (Exception e) {
       logger.error(e);
     }
     resourceResponse.getWriter().println(topics.toString());
   }
 }
 public void deleteMessageListener(ActionRequest request, ActionResponse response)
     throws Exception {
   long brokerMessageListenerId = ParamUtil.getLong(request, "brokerMessageListenerId");
   BrokerMessageListener bml =
       BrokerMessageListenerLocalServiceUtil.deleteBrokerMessageListener(brokerMessageListenerId);
   String[] topic = bml.getTopics().split(";");
   for (int i = 0; i < topic.length; i++) {
     // unregistering the service using the entity id
     BrokersServicesManager.getInstance()
         .unregister(
             topic[i], bml.getBrokerId(), String.valueOf(bml.getBrokerMessageListenerId()));
   }
 }
  public void saveOrUpdateBrokerMessageListener(ActionRequest request, ActionResponse response)
      throws Exception {
    ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY);
    PortletConfig portletConfig =
        (PortletConfig) request.getAttribute(JavaConstants.JAVAX_PORTLET_CONFIG);

    long brokerMessageListenerId = ParamUtil.getLong(request, "brokerMessageListenerId");
    boolean isNew = brokerMessageListenerId == 0;
    BrokerMessageListener bml = null;

    if (!isNew) {
      bml =
          BrokerMessageListenerLocalServiceUtil.fetchBrokerMessageListener(brokerMessageListenerId);

    } else {
      bml =
          BrokerMessageListenerLocalServiceUtil.createBrokerMessageListener(
              CounterLocalServiceUtil.increment(BrokerMessageListener.class.getName()));
      bml.setCreateDate(new Date());
    }

    if (isNew
            && BrokerModelPermission.contains(
                themeDisplay.getPermissionChecker(),
                themeDisplay.getScopeGroupId(),
                ActionKeys.ADD_ENTRY)
        || !isNew
            && BrokerMessageListenerPermission.contains(
                themeDisplay.getPermissionChecker(), bml, ActionKeys.UPDATE)) {
      try {
        String brokerMessageListenerName = ParamUtil.getString(request, "name");
        String brokerMessageListenerDescription = ParamUtil.getString(request, "description");
        long brokerId = ParamUtil.getLong(request, "brokerId");
        String[] topicsArr = ParamUtil.getParameterValues(request, "topic");
        StringBuilder topics = new StringBuilder();
        for (int i = 0; i < topicsArr.length; i++) {
          topics.append(topicsArr[i] + ";");
        }
        String typeStr = ParamUtil.getString(request, "type");
        BrokerMessageListenerType type = BrokerMessageListenerType.valueOf(typeStr);

        bml.setCompanyId(themeDisplay.getCompanyId());
        bml.setGroupId(themeDisplay.getCompanyGroupId());
        bml.setUserName(themeDisplay.getUser().getScreenName());
        bml.setUserId(themeDisplay.getUserId());
        bml.setModifiedDate(new Date());
        bml.setName(brokerMessageListenerName);
        bml.setDescription(brokerMessageListenerDescription);
        bml.setBrokerId(brokerId);
        bml.setTopics(topics.toString());
        bml.setType(typeStr);
        if (type.equals(BrokerMessageListenerType.MESSAGE_BUS)) {
          String messageBusDestination = ParamUtil.getString(request, "messageBusDestination");
          bml.setMessageBusDestination(messageBusDestination);
        }

        BrokerMessageListenerLocalServiceUtil.saveOrUpdateBrokerMessageListener(
            bml, themeDisplay.getLocale(), portletConfig);
      } catch (Exception e) {
        response.setRenderParameter("mvcPath", "/html/brokermessagelistener/edit.jsp");
        response.setRenderParameter("errorMessage", e.getMessage());
        request.setAttribute("brokerMessageListener", bml);
        SessionErrors.add(request, "it.acsoftware.brokerportlet.error");
      }
    }
  }