001/* 002 * (C) Copyright 2006-2011 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 020package org.nuxeo.ecm.core.api.localconfiguration; 021 022import org.apache.commons.logging.Log; 023import org.apache.commons.logging.LogFactory; 024import org.nuxeo.ecm.core.api.CoreSession; 025import org.nuxeo.ecm.core.api.DocumentModel; 026import org.nuxeo.ecm.core.api.DocumentRef; 027import org.nuxeo.runtime.model.DefaultComponent; 028 029/** 030 * Default implementation of {@code LocalConfigurationService}. 031 * 032 * @author <a href="mailto:[email protected]">Thomas Roger</a> 033 * @since 5.4.2 034 */ 035public class LocalConfigurationServiceImpl extends DefaultComponent implements LocalConfigurationService { 036 037 private static final Log log = LogFactory.getLog(LocalConfigurationServiceImpl.class); 038 039 @Override 040 public <T extends LocalConfiguration> T getConfiguration(Class<T> configurationClass, String configurationFacet, 041 DocumentModel currentDoc) { 042 if (currentDoc == null || currentDoc.getRef() == null) { 043 return null; 044 } 045 046 CoreSession session = currentDoc.getCoreSession(); 047 if (session == null) { 048 return null; 049 } 050 051 T localConfiguration = session.adaptFirstMatchingDocumentWithFacet(currentDoc.getRef(), configurationFacet, 052 configurationClass); 053 if (localConfiguration == null) { 054 // no local configuration found 055 return null; 056 } 057 while (localConfiguration.canMerge()) { 058 DocumentRef parentRef = session.getParentDocumentRef(localConfiguration.getDocumentRef()); 059 if (parentRef == null) { 060 DocumentModel parentDoc = session.getParentDocument(localConfiguration.getDocumentRef()); 061 if (parentDoc == null) { 062 break; 063 } 064 parentRef = parentDoc.getRef(); 065 if (parentRef == null) { 066 break; 067 } 068 } 069 T parentConfiguration = session.adaptFirstMatchingDocumentWithFacet(parentRef, configurationFacet, 070 configurationClass); 071 if (parentConfiguration == null) { 072 // stop merging 073 break; 074 } 075 localConfiguration.merge(parentConfiguration); 076 } 077 return localConfiguration; 078 } 079 080}