001/*
002 * (C) Copyright 2014 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Thierry Delprat
018 */
019package org.nuxeo.segment.io;
020
021import java.io.IOException;
022import java.io.Serializable;
023import java.util.ArrayList;
024import java.util.HashMap;
025import java.util.List;
026import java.util.Map;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.codehaus.groovy.runtime.InvokerHelper;
031import org.nuxeo.common.xmap.annotation.XNode;
032import org.nuxeo.common.xmap.annotation.XNodeList;
033import org.nuxeo.common.xmap.annotation.XNodeMap;
034import org.nuxeo.common.xmap.annotation.XObject;
035
036import groovy.lang.Binding;
037import groovy.lang.GroovyClassLoader;
038import groovy.lang.Script;
039import org.nuxeo.segment.io.SegmentIO.ACTIONS;
040
041@XObject("mapper")
042public class SegmentIOMapper {
043
044    private static Log log = LogFactory.getLog(SegmentIOMapper.class);
045
046    @XNode("@name")
047    String name;
048
049    @XNode("@targetAPI")
050    String target = "track";
051
052    @XNodeList(value = "events/event", type = ArrayList.class, componentType = String.class)
053    List<String> events;
054
055    @XNode("groovy")
056    String groovyMapping;
057
058    @XNodeMap(value = "parameters/parameter", key = "@name", type = HashMap.class, componentType = String.class)
059    Map<String, String> parameters = new HashMap<String, String>();
060
061    @Override
062    public boolean equals(Object obj) {
063        if (obj instanceof SegmentIOMapper) {
064            return name.equals(((SegmentIOMapper) obj).name);
065        }
066        return super.equals(obj);
067    }
068
069    public boolean isIdentify() {
070        return ACTIONS.identify.name().equalsIgnoreCase(target);
071    }
072
073    public boolean isPage() {
074        return ACTIONS.page.name().equalsIgnoreCase(target);
075    }
076
077    public boolean isScreen() {
078        return ACTIONS.screen.name().equalsIgnoreCase(target);
079    }
080
081    public Map<String, Serializable> getMappedData(Map<String, Object> context) {
082
083        Map<String, Serializable> mapping = new HashMap<String, Serializable>();
084        context.put("mapping", mapping);
085
086        StringBuffer sb = new StringBuffer();
087        for (String key : parameters.keySet()) {
088            sb.append("mapping.put(\"");
089            sb.append(key);
090            sb.append("\", ");
091            sb.append(parameters.get(key));
092            sb.append(");\n");
093        }
094
095        if (groovyMapping != null && !groovyMapping.isEmpty()) {
096            sb.append(groovyMapping);
097        }
098
099        Binding binding = new Binding(context);
100        try (GroovyClassLoader loader = new GroovyClassLoader(this.getClass().getClassLoader())) {
101            Class<?> klass = loader.parseClass(sb.toString());
102            Script script = InvokerHelper.createScript(klass, binding);
103            script.run();
104        } catch (IOException e) {
105            log.error(String.format("Error during Groovy script execution for the '%s' segmentIO mapper", name), e);
106        }
107        return mapping;
108    }
109
110    public String getName() {
111        return name;
112    }
113
114    public String getTarget() {
115        return target;
116    }
117
118}