001/*
002 * (C) Copyright 2013 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 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.core.storage.sql.listeners;
020
021import org.nuxeo.ecm.core.api.ConcurrentUpdateException;
022import org.nuxeo.ecm.core.api.CoreSession;
023import org.nuxeo.ecm.core.api.DocumentModel;
024import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
025import org.nuxeo.ecm.core.event.Event;
026import org.nuxeo.ecm.core.event.EventBundle;
027import org.nuxeo.ecm.core.event.EventContext;
028import org.nuxeo.ecm.core.event.PostCommitEventListener;
029import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
030
031public class DummyAsyncRetryListener implements PostCommitEventListener {
032
033    protected static int countHandled;
034
035    protected static int countOk;
036
037    @Override
038    public void handleEvent(EventBundle events) {
039        countHandled++;
040
041        // accessing the iterator reconnects the events
042        DocumentModel doc = null;
043        for (Event event : events) {
044            EventContext context = event.getContext();
045            if (!(context instanceof DocumentEventContext)) {
046                continue;
047            }
048            DocumentEventContext documentEventContext = (DocumentEventContext) context;
049            doc = documentEventContext.getSourceDocument();
050        }
051
052        if (countHandled == 1) {
053            // simulate error
054            throw new ConcurrentUpdateException();
055        }
056        if (doc != null && ((String) doc.getPropertyValue("dc:title")).startsWith("title")) {
057            countOk++;
058        }
059    }
060
061    public static void clear() {
062        countHandled = 0;
063        countOk = 0;
064    }
065
066    public static int getCountHandled() {
067        return countHandled;
068    }
069
070    public static int getCountOk() {
071        return countOk;
072    }
073
074}