001/*
002 * (C) Copyright 2006-2009 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 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.platform.filemanager.core.listener;
023
024import java.security.Principal;
025import java.util.ArrayList;
026import java.util.List;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.ecm.core.api.CoreSession;
031import org.nuxeo.ecm.core.api.DocumentLocation;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.core.api.event.DocumentEventTypes;
034import org.nuxeo.ecm.core.event.Event;
035import org.nuxeo.ecm.core.event.EventBundle;
036import org.nuxeo.ecm.core.event.EventContext;
037import org.nuxeo.ecm.core.event.PostCommitFilteringEventListener;
038import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
039
040public class AsynchronousUnicityCheckListener extends AbstractUnicityChecker implements
041        PostCommitFilteringEventListener {
042
043    private static final Log log = LogFactory.getLog(AsynchronousUnicityCheckListener.class);
044
045    @Override
046    public boolean acceptEvent(Event event) {
047        return isUnicityCheckEnabled();
048    }
049
050    @Override
051    public void handleEvent(EventBundle events) {
052        if (!isUnicityCheckEnabled()) {
053            return;
054        }
055
056        if (events.containsEventName(DocumentEventTypes.DOCUMENT_CREATED)
057                || events.containsEventName(DocumentEventTypes.DOCUMENT_UPDATED)) {
058            List<String> uuids = new ArrayList<String>();
059            for (Event event : events) {
060                if (DocumentEventTypes.DOCUMENT_CREATED.equals(event.getName())
061                        || DocumentEventTypes.DOCUMENT_UPDATED.equals(event.getName())) {
062                    EventContext ctx = event.getContext();
063                    if (ctx instanceof DocumentEventContext) {
064                        DocumentEventContext docCtx = (DocumentEventContext) ctx;
065
066                        DocumentModel doc2Check = docCtx.getSourceDocument();
067                        if (doc2Check.isProxy()) {
068                            continue;
069                        }
070                        if (!uuids.contains(doc2Check.getId())) {
071                            uuids.add(doc2Check.getId());
072                            doUnicityCheck(doc2Check, docCtx.getCoreSession(), event);
073                        }
074                    }
075                }
076            }
077        }
078    }
079
080    @Override
081    protected void onDuplicatedDoc(CoreSession session, Principal principal, DocumentModel newDoc,
082            List<DocumentLocation> existingDocs, Event event) {
083        // simply send a message
084        log.info("Duplicated file detected");
085        raiseDuplicatedFileEvent(session, principal, newDoc, existingDocs);
086    }
087
088}