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 groovy.lang.Binding; 022import groovy.lang.GroovyClassLoader; 023import groovy.lang.Script; 024 025import java.io.Serializable; 026import java.util.ArrayList; 027import java.util.HashMap; 028import java.util.List; 029import java.util.Map; 030 031import org.codehaus.groovy.runtime.InvokerHelper; 032import org.nuxeo.common.xmap.annotation.XNode; 033import org.nuxeo.common.xmap.annotation.XNodeList; 034import org.nuxeo.common.xmap.annotation.XNodeMap; 035import org.nuxeo.common.xmap.annotation.XObject; 036 037@XObject("mapper") 038public class SegmentIOMapper { 039 040 @XNode("@name") 041 String name; 042 043 @XNode("@targetAPI") 044 String target = "track"; 045 046 @XNodeList(value = "events/event", type = ArrayList.class, componentType = String.class) 047 List<String> events; 048 049 @XNode("groovy") 050 String groovyMapping; 051 052 @XNodeMap(value = "parameters/parameter", key = "@name", type = HashMap.class, componentType = String.class) 053 Map<String, String> parameters = new HashMap<String, String>(); 054 055 @Override 056 public boolean equals(Object obj) { 057 if (obj instanceof SegmentIOMapper) { 058 return name.equals(((SegmentIOMapper)obj).name); 059 } 060 return super.equals(obj); 061 } 062 063 public boolean isIdentify() { 064 return "identify".equalsIgnoreCase(target); 065 } 066 067 public Map<String, Serializable> getMappedData(Map<String, Object> context) { 068 069 Map<String, Serializable> mapping = new HashMap<String, Serializable>(); 070 context.put("mapping", mapping); 071 072 StringBuffer sb = new StringBuffer(); 073 for (String key : parameters.keySet()) { 074 sb.append("mapping.put(\""); 075 sb.append(key); 076 sb.append("\", "); 077 sb.append(parameters.get(key)); 078 sb.append(");\n"); 079 } 080 081 if (groovyMapping!=null && !groovyMapping.isEmpty()) { 082 sb.append(groovyMapping); 083 } 084 085 Binding binding = new Binding(context); 086 GroovyClassLoader loader = new GroovyClassLoader(this.getClass().getClassLoader()); 087 Class<?> klass = loader.parseClass(sb.toString()); 088 Script script = InvokerHelper.createScript(klass, binding); 089 script.run(); 090 return mapping; 091 } 092 093 public String getName() { 094 return name; 095 } 096 097 public String getTarget() { 098 return target; 099 } 100 101}