/**
  * Add a stream definition using the Event stream publisher.
  *
  * @param streamDefinitionBean The stream definition bean class.
  */
 public String addStreamDefinition(StreamDefinitionBean streamDefinitionBean)
     throws AnalyticsWebServiceException, MalformedStreamDefinitionException {
   StreamDefinition streamDefinition = Utils.getStreamDefinition(streamDefinitionBean);
   try {
     eventStreamService.addEventStreamDefinition(streamDefinition);
     return streamDefinition.getStreamId();
   } catch (Exception e) {
     logger.error(
         "Unable to set the stream definition: ["
             + streamDefinition.getName()
             + ":"
             + streamDefinition.getVersion()
             + "]"
             + e.getMessage(),
         e);
     throw new AnalyticsWebServiceException(
         "Unable to set the stream definition: ["
             + streamDefinition.getName()
             + ":"
             + streamDefinition.getVersion()
             + "], "
             + e.getMessage(),
         e);
   }
 }
 private StreamDefinition validateAndGetStreamDefinition(String name, String version)
     throws AnalyticsWebServiceException {
   StreamDefinition streamDefinition;
   try {
     if (name != null && version != null) {
       streamDefinition = eventStreamService.getStreamDefinition(name, version);
     } else if (name != null) {
       streamDefinition = eventStreamService.getStreamDefinition(name);
     } else {
       throw new AnalyticsWebServiceException("The stream name is not provided");
     }
   } catch (Exception e) {
     logger.error("Unable to get the stream definition: " + e.getMessage(), e);
     throw new AnalyticsWebServiceException(
         "Unable to get the stream definition: " + e.getMessage(), e);
   }
   return streamDefinition;
 }
  /**
   * Publishes events to a given stream represented by stream id.
   *
   * @param eventBean The event bean representing the event data.
   * @throws AnalyticsWebServiceException
   */
  public void publishEvent(EventBean eventBean) throws AnalyticsWebServiceException {
    try {
      StreamDefinition streamDefinition =
          validateAndGetStreamDefinition(eventBean.getStreamName(), eventBean.getStreamVersion());
      Event event = Utils.getEvent(eventBean, streamDefinition);

      eventStreamService.publish(event);
    } catch (Exception e) {
      logger.error("unable to publish event: " + e.getMessage(), e);
      throw new AnalyticsWebServiceException("unable to publish event: " + e.getMessage(), e);
    }
  }
 private static boolean validateSiddhiStreamWithDatabridgeStream(
     String streamName,
     String streamVersion,
     org.wso2.siddhi.query.api.definition.StreamDefinition siddhiStreamDefinition)
     throws ExecutionPlanConfigurationException, ExecutionPlanDependencyValidationException {
   if (siddhiStreamDefinition == null) {
     throw new ExecutionPlanDependencyValidationException(
         streamName + EventProcessorConstants.STREAM_SEPARATOR + streamVersion,
         "Cannot validate null Siddhi stream for the stream: "
             + streamName
             + EventProcessorConstants.STREAM_SEPARATOR
             + streamVersion
             + " ");
   }
   EventStreamService eventStreamService = EventProcessorValueHolder.getEventStreamService();
   try {
     StreamDefinition streamDefinition =
         eventStreamService.getStreamDefinition(streamName, streamVersion);
     if (streamDefinition != null) {
       String siddhiAttributeName;
       int attributeCount = 0;
       int streamSize =
           (streamDefinition.getMetaData() == null ? 0 : streamDefinition.getMetaData().size())
               + (streamDefinition.getCorrelationData() == null
                   ? 0
                   : streamDefinition.getCorrelationData().size())
               + (streamDefinition.getPayloadData() == null
                   ? 0
                   : streamDefinition.getPayloadData().size());
       if (siddhiStreamDefinition.getAttributeList().size() != streamSize) {
         throw new ExecutionPlanDependencyValidationException(
             streamName + EventProcessorConstants.STREAM_SEPARATOR + streamVersion,
             "No of attributes in stream "
                 + streamName
                 + EventProcessorConstants.STREAM_SEPARATOR
                 + streamVersion
                 + " do not match the no of attributes in Siddhi stream");
       }
       if (streamDefinition.getMetaData() != null) {
         for (Attribute attribute : streamDefinition.getMetaData()) {
           siddhiAttributeName = EventProcessorConstants.META_PREFIX + attribute.getName();
           org.wso2.siddhi.query.api.definition.Attribute.Type type =
               siddhiStreamDefinition.getAttributeType(siddhiAttributeName);
           // null check for type not required since an exception is thrown by Siddhi
           // StreamDefinition.getAttributeType() method for non-existent attributes
           if (siddhiStreamDefinition.getAttributePosition(siddhiAttributeName)
               != attributeCount++) {
             throw new ExecutionPlanDependencyValidationException(
                 streamName + EventProcessorConstants.STREAM_SEPARATOR + streamVersion,
                 "Stream "
                     + streamName
                     + EventProcessorConstants.STREAM_SEPARATOR
                     + streamVersion
                     + "; Attribute positions do not match for attribute: "
                     + attribute.getName());
           }
           if (!isMatchingType(type, attribute.getType())) {
             throw new ExecutionPlanDependencyValidationException(
                 streamName + EventProcessorConstants.STREAM_SEPARATOR + streamVersion,
                 "Stream "
                     + streamName
                     + EventProcessorConstants.STREAM_SEPARATOR
                     + streamVersion
                     + "; Type mismatch for attribute: "
                     + attribute.getName());
           }
         }
       }
       if (streamDefinition.getCorrelationData() != null) {
         for (Attribute attribute : streamDefinition.getCorrelationData()) {
           siddhiAttributeName = EventProcessorConstants.CORRELATION_PREFIX + attribute.getName();
           org.wso2.siddhi.query.api.definition.Attribute.Type type =
               siddhiStreamDefinition.getAttributeType(siddhiAttributeName);
           // null check for type not required since an exception is thrown by Siddhi
           // StreamDefinition.getAttributeType() method for non-existent attributes
           if (siddhiStreamDefinition.getAttributePosition(siddhiAttributeName)
               != attributeCount++) {
             throw new ExecutionPlanDependencyValidationException(
                 streamName + EventProcessorConstants.STREAM_SEPARATOR + streamVersion,
                 "Stream "
                     + streamName
                     + EventProcessorConstants.STREAM_SEPARATOR
                     + streamVersion
                     + "; Attribute positions do not match for attribute: "
                     + attribute.getName());
           }
           if (!isMatchingType(type, attribute.getType())) {
             throw new ExecutionPlanDependencyValidationException(
                 streamName + EventProcessorConstants.STREAM_SEPARATOR + streamVersion,
                 "Stream "
                     + streamName
                     + EventProcessorConstants.STREAM_SEPARATOR
                     + streamVersion
                     + "; Type mismatch for attribute: "
                     + attribute.getName());
           }
         }
       }
       if (streamDefinition.getPayloadData() != null) {
         for (Attribute attribute : streamDefinition.getPayloadData()) {
           siddhiAttributeName = attribute.getName();
           org.wso2.siddhi.query.api.definition.Attribute.Type type =
               siddhiStreamDefinition.getAttributeType(siddhiAttributeName);
           // null check for type not required since an exception is thrown by Siddhi
           // StreamDefinition.getAttributeType() method for non-existent attributes
           if (siddhiStreamDefinition.getAttributePosition(siddhiAttributeName)
               != attributeCount++) {
             throw new ExecutionPlanDependencyValidationException(
                 streamName + EventProcessorConstants.STREAM_SEPARATOR + streamVersion,
                 "Stream "
                     + streamName
                     + EventProcessorConstants.STREAM_SEPARATOR
                     + streamVersion
                     + "; Attribute positions do not match for attribute: "
                     + attribute.getName());
           }
           if (!isMatchingType(type, attribute.getType())) {
             throw new ExecutionPlanDependencyValidationException(
                 streamName + EventProcessorConstants.STREAM_SEPARATOR + streamVersion,
                 "Stream "
                     + streamName
                     + EventProcessorConstants.STREAM_SEPARATOR
                     + streamVersion
                     + "; Type mismatch for attribute: "
                     + attribute.getName());
           }
         }
       }
       return true;
     }
   } catch (EventStreamConfigurationException e) {
     throw new ExecutionPlanConfigurationException(
         "Error while validating stream definition with store : " + e.getMessage(), e);
   } catch (AttributeNotExistException e) {
     throw new ExecutionPlanDependencyValidationException(
         streamName + EventProcessorConstants.STREAM_SEPARATOR + streamVersion, e.getMessage());
   }
   throw new ExecutionPlanDependencyValidationException(
       streamName + EventProcessorConstants.STREAM_SEPARATOR + streamVersion,
       "Stream "
           + streamName
           + EventProcessorConstants.STREAM_SEPARATOR
           + streamVersion
           + " does not exist");
 }