001/*
002 * (C) Copyright 2006-2009 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.filemanager.core.listener;
021
022import java.security.Principal;
023import java.util.ArrayList;
024import java.util.List;
025
026import org.apache.commons.logging.Log;
027import org.apache.commons.logging.LogFactory;
028import org.nuxeo.ecm.core.api.CoreSession;
029import org.nuxeo.ecm.core.api.DocumentLocation;
030import org.nuxeo.ecm.core.api.DocumentModel;
031import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
032import org.nuxeo.ecm.core.event.Event;
033import org.nuxeo.ecm.core.event.EventBundle;
034import org.nuxeo.ecm.core.event.EventContext;
035import org.nuxeo.ecm.core.event.PostCommitFilteringEventListener;
036import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
037
038public class AsynchronousUnicityCheckListener extends AbstractUnicityChecker implements
039        PostCommitFilteringEventListener {
040
041    private static final Log log = LogFactory.getLog(AsynchronousUnicityCheckListener.class);
042
043    @Override
044    public boolean acceptEvent(Event event) {
045        return isUnicityCheckEnabled();
046    }
047
048    @Override
049    public void handleEvent(EventBundle events) {
050        if (!isUnicityCheckEnabled()) {
051            return;
052        }
053
054        if (events.containsEventName(DocumentEventTypes.DOCUMENT_CREATED)
055                || events.containsEventName(DocumentEventTypes.DOCUMENT_UPDATED)) {
056            List<String> uuids = new ArrayList<String>();
057            for (Event event : events) {
058                if (DocumentEventTypes.DOCUMENT_CREATED.equals(event.getName())
059                        || DocumentEventTypes.DOCUMENT_UPDATED.equals(event.getName())) {
060                    EventContext ctx = event.getContext();
061                    if (ctx instanceof DocumentEventContext) {
062                        DocumentEventContext docCtx = (DocumentEventContext) ctx;
063
064                        DocumentModel doc2Check = docCtx.getSourceDocument();
065                        if (doc2Check.isProxy()) {
066                            continue;
067                        }
068                        if (!uuids.contains(doc2Check.getId())) {
069                            uuids.add(doc2Check.getId());
070                            doUnicityCheck(doc2Check, docCtx.getCoreSession(), event);
071                        }
072                    }
073                }
074            }
075        }
076    }
077
078    @Override
079    protected void onDuplicatedDoc(CoreSession session, Principal principal, DocumentModel newDoc,
080            List<DocumentLocation> existingDocs, Event event) {
081        // simply send a message
082        log.info("Duplicated file detected");
083        raiseDuplicatedFileEvent(session, principal, newDoc, existingDocs);
084    }
085
086}