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.io.Serializable; 025import java.security.Principal; 026import java.util.HashMap; 027import java.util.Iterator; 028import java.util.List; 029import java.util.Map; 030 031import org.apache.commons.logging.Log; 032import org.apache.commons.logging.LogFactory; 033import org.nuxeo.ecm.core.api.Blob; 034import org.nuxeo.ecm.core.api.CoreSession; 035import org.nuxeo.ecm.core.api.DocumentLocation; 036import org.nuxeo.ecm.core.api.DocumentModel; 037import org.nuxeo.ecm.core.api.event.DocumentEventCategories; 038import org.nuxeo.ecm.core.api.model.PropertyNotFoundException; 039import org.nuxeo.ecm.core.event.Event; 040import org.nuxeo.ecm.core.event.EventProducer; 041import org.nuxeo.ecm.core.event.impl.DocumentEventContext; 042import org.nuxeo.ecm.platform.filemanager.api.FileManager; 043import org.nuxeo.runtime.api.Framework; 044 045public abstract class AbstractUnicityChecker { 046 047 private static final Log log = LogFactory.getLog(AbstractUnicityChecker.class); 048 049 protected FileManager fileManager; 050 051 protected static Boolean unicityCheckEnabled; 052 053 protected static final String DUPLICATED_FILE = "duplicatedFile"; 054 055 protected void doUnicityCheck(DocumentModel doc2Check, CoreSession session, Event event) { 056 057 List<String> xpathFields = getFileManagerService().getFields(); 058 059 if (xpathFields == null || xpathFields.isEmpty()) { 060 unicityCheckEnabled = false; 061 log.info("Unicity check has been automatically disabled"); 062 return; 063 } 064 065 for (String field : xpathFields) { 066 Blob blob; 067 try { 068 blob = (Blob) doc2Check.getPropertyValue(field); 069 } catch (PropertyNotFoundException pnfe) { 070 continue; 071 } 072 if (blob == null) { 073 log.debug("No blob retrieved"); 074 continue; 075 } 076 077 String digest = blob.getDigest(); 078 if (digest == null) { 079 log.debug("Blob has no digest, can not check for unicity"); 080 continue; 081 } 082 083 List<DocumentLocation> existingDocuments = null; 084 existingDocuments = fileManager.findExistingDocumentWithFile(session, doc2Check.getPathAsString(), digest, 085 session.getPrincipal()); 086 087 if (!existingDocuments.isEmpty()) { 088 Iterator<DocumentLocation> existingDocumentsIterator = existingDocuments.iterator(); 089 while (existingDocumentsIterator.hasNext()) { 090 if (existingDocumentsIterator.next().getDocRef() == doc2Check.getRef()) { 091 existingDocumentsIterator.remove(); 092 } 093 } 094 log.debug("Existing Documents[" + existingDocuments.size() + "]"); 095 096 onDuplicatedDoc(session, session.getPrincipal(), doc2Check, existingDocuments, event); 097 } 098 } 099 } 100 101 protected abstract void onDuplicatedDoc(CoreSession session, Principal principal, DocumentModel newDoc, 102 List<DocumentLocation> existingDocs, Event event); 103 104 protected void raiseDuplicatedFileEvent(CoreSession session, Principal principal, DocumentModel newDoc, 105 List<DocumentLocation> existingDocs) { 106 107 DocumentEventContext ctx = new DocumentEventContext(session, principal, newDoc); 108 109 Map<String, Serializable> props = new HashMap<String, Serializable>(); 110 111 props.put("category", DocumentEventCategories.EVENT_CLIENT_NOTIF_CATEGORY); 112 props.put("duplicatedDocLocation", (Serializable) existingDocs); 113 114 Event event = ctx.newEvent(DUPLICATED_FILE); 115 EventProducer producer = Framework.getService(EventProducer.class); 116 producer.fireEvent(event); 117 } 118 119 protected boolean isUnicityCheckEnabled() { 120 if (unicityCheckEnabled == null) { 121 unicityCheckEnabled = getFileManagerService().isUnicityEnabled(); 122 } 123 return unicityCheckEnabled; 124 } 125 126 private FileManager getFileManagerService() { 127 if (fileManager == null) { 128 fileManager = Framework.getRuntime().getService(FileManager.class); 129 } 130 return fileManager; 131 } 132 133}