001/*
002 * Copyright (c) 2013 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Florent Guillaume
011 */
012package org.nuxeo.ecm.core.storage.sql.listeners;
013
014import org.nuxeo.ecm.core.api.ConcurrentUpdateException;
015import org.nuxeo.ecm.core.api.CoreSession;
016import org.nuxeo.ecm.core.api.DocumentModel;
017import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
018import org.nuxeo.ecm.core.event.Event;
019import org.nuxeo.ecm.core.event.EventBundle;
020import org.nuxeo.ecm.core.event.EventContext;
021import org.nuxeo.ecm.core.event.PostCommitEventListener;
022import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
023
024public class DummyAsyncRetryListener implements PostCommitEventListener {
025
026    protected static int countHandled;
027
028    protected static int countOk;
029
030    @Override
031    public void handleEvent(EventBundle events) {
032        countHandled++;
033
034        // accessing the iterator reconnects the events
035        DocumentModel doc = null;
036        for (Event event : events) {
037            EventContext context = event.getContext();
038            if (!(context instanceof DocumentEventContext)) {
039                continue;
040            }
041            DocumentEventContext documentEventContext = (DocumentEventContext) context;
042            doc = documentEventContext.getSourceDocument();
043        }
044
045        if (countHandled == 1) {
046            // simulate error
047            throw new ConcurrentUpdateException();
048        }
049        if (doc != null && ((String) doc.getPropertyValue("dc:title")).startsWith("title")) {
050            countOk++;
051        }
052    }
053
054    public static void clear() {
055        countHandled = 0;
056        countOk = 0;
057    }
058
059    public static int getCountHandled() {
060        return countHandled;
061    }
062
063    public static int getCountOk() {
064        return countOk;
065    }
066
067}