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.listener;
020
021import java.io.Serializable;
022import java.security.Principal;
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.nuxeo.ecm.core.api.NuxeoPrincipal;
031import org.nuxeo.ecm.core.event.Event;
032import org.nuxeo.ecm.core.event.EventBundle;
033import org.nuxeo.ecm.core.event.PostCommitEventListener;
034import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
035import org.nuxeo.ecm.platform.usermanager.UserManager;
036import org.nuxeo.runtime.api.Framework;
037import org.nuxeo.segment.io.SegmentIO;
038import org.nuxeo.segment.io.SegmentIOMapper;
039
040public class SegmentIOAsyncListener implements PostCommitEventListener {
041
042    private static Log log = LogFactory.getLog(SegmentIOAsyncListener.class);
043
044    @Override
045    public void handleEvent(EventBundle bundle) {
046
047        SegmentIO service = Framework.getService(SegmentIO.class);
048
049        List<String> eventToProcess = new ArrayList<String>();
050
051        for (String event : service.getMappedEvents()) {
052            if (bundle.containsEventName(event)) {
053                eventToProcess.add(event);
054            }
055        }
056
057        if (eventToProcess.size() > 0) {
058            Map<String, List<SegmentIOMapper>> event2Mappers = service.getMappers(eventToProcess);
059            processEvents(event2Mappers, bundle);
060        }
061
062    }
063
064    protected void processEvents(Map<String, List<SegmentIOMapper>> event2Mappers, EventBundle bundle) {
065
066        for (Event event : bundle) {
067            List<SegmentIOMapper> mappers = event2Mappers.get(event.getName());
068            if (mappers == null || mappers.size() == 0) {
069                continue;
070            }
071
072            for (SegmentIOMapper mapper : mappers) {
073
074                Map<String, Object> ctx = new HashMap<String, Object>();
075
076                Principal princ = event.getContext().getPrincipal();
077                NuxeoPrincipal principal = null;
078                if (princ instanceof NuxeoPrincipal) {
079                    principal = (NuxeoPrincipal) princ;
080                } else {
081                    principal = Framework.getService(UserManager.class).getPrincipal(princ.getName());
082                }
083
084                ctx.put("event", event);
085                ctx.put("eventContext", event.getContext());
086                ctx.put("principal", principal);
087                if (event.getContext() instanceof DocumentEventContext) {
088                    DocumentEventContext docCtx = (DocumentEventContext) event.getContext();
089                    ctx.put("doc", docCtx.getSourceDocument());
090                    ctx.put("repository", docCtx.getRepositoryName());
091                    ctx.put("session", docCtx.getCoreSession());
092                    ctx.put("dest", docCtx.getDestination());
093                }
094                Map<String, Serializable> mapped = mapper.getMappedData(ctx);
095
096                if (mapper.isIdentify()) {
097                    Framework.getService(SegmentIO.class).identify(principal, mapped);
098                } else {
099                    Framework.getService(SegmentIO.class).track(principal, event.getName(), mapped);
100                }
101
102            }
103        }
104    }
105
106}