/*
 * Copyright (c) 2005 - 2014, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed
 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 */

package org.wso2.carbon.event.processor.manager.core;

import org.wso2.carbon.databridge.commons.Attribute;
import org.wso2.siddhi.query.api.definition.StreamDefinition;

import java.util.ArrayList;
import java.util.List;

/**
 * Created on 5/26/15.
 */
public class EventManagementUtil {

    public static String constructEventSyncId(int tenantId, String name, Manager.ManagerType type) {
        return tenantId + "/" + type + "/" + name;
    }

    public static StreamDefinition constructStreamDefinition(String syncId, org.wso2.carbon.databridge.commons.StreamDefinition inStreamDefinition){

        org.wso2.siddhi.query.api.definition.StreamDefinition streamDefinition = new org.wso2.siddhi.query.api.definition.StreamDefinition();
        streamDefinition.setId(syncId);

        List<Attribute> attributes = new ArrayList<Attribute>();
        if (inStreamDefinition.getMetaData() != null) {
            attributes.addAll(inStreamDefinition.getMetaData());
        }
        if (inStreamDefinition.getCorrelationData() != null) {
            attributes.addAll(inStreamDefinition.getCorrelationData());
        }
        if (inStreamDefinition.getPayloadData() != null) {
            attributes.addAll(inStreamDefinition.getPayloadData());
        }
        for (Attribute attr : attributes) {
            streamDefinition.attribute(attr.getName(), org.wso2.siddhi.query.api.definition.Attribute.Type.valueOf(attr.getType().toString()));
        }
        return streamDefinition;
    }
}

@@ -1547,33 +1547,38 @@
             
-a
+for (A
 ttribute
 s.addAll(inS
@@ -1561,33 +1561,32 @@
 r (Attribute
-s.addAll(
+ attr : 
 inStreamDefi
@@ -1602,25 +1602,129 @@
 tMetaData())
-;
+ %7B%0A                attributes.add(new Attribute(%22meta_%22 + attr.getName(), attr.getType()));%0A            %7D
 %0A        %7D%0A 
@@ -1793,33 +1793,38 @@
             
-a
+for (A
 ttribute
 s.addAll(inS
@@ -1807,33 +1807,32 @@
 r (Attribute
-s.addAll(
+ attr : 
 inStreamDefi
@@ -1855,25 +1855,136 @@
 ationData())
-;
+ %7B%0A                attributes.add(new Attribute(%22correlation_%22 + attr.getName(), attr.getType()));%0A            %7D
 %0A        %7D%0A 

Fix for DuplicateAttributeException when same attr name used in correlation, meta and payload data