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.EventContext;
034import org.nuxeo.ecm.core.event.EventListener;
035import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
036
037public class SynchronousUnicityCheckListener extends AbstractUnicityChecker implements EventListener {
038
039    private static final Log log = LogFactory.getLog(SynchronousUnicityCheckListener.class);
040
041    public void handleEvent(Event event) {
042        if (!isUnicityCheckEnabled()) {
043            return;
044        }
045
046        List<String> uuids = new ArrayList<String>();
047        if (DocumentEventTypes.DOCUMENT_CREATED.equals(event.getName())
048                || DocumentEventTypes.DOCUMENT_UPDATED.equals(event.getName())) {
049            EventContext ctx = event.getContext();
050            if (ctx instanceof DocumentEventContext) {
051                DocumentEventContext docCtx = (DocumentEventContext) ctx;
052
053                DocumentModel doc2Check = docCtx.getSourceDocument();
054                if (doc2Check.isProxy()) {
055                    // NOP
056                }
057                if (!uuids.contains(doc2Check.getId())) {
058                    uuids.add(doc2Check.getId());
059                    doUnicityCheck(doc2Check, docCtx.getCoreSession(), event);
060                }
061            }
062        }
063    }
064
065    @Override
066    protected void onDuplicatedDoc(CoreSession session, Principal principal, DocumentModel newDoc,
067            List<DocumentLocation> existingDocs, Event event) {
068        // simply send a message
069        log.info("Duplicated file detected");
070        raiseDuplicatedFileEvent(session, principal, newDoc, existingDocs);
071    }
072
073}