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