001/* 002 * (C) Copyright 2013 Nuxeo SA (http://nuxeo.com/) and others. 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-2.1.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 * Thomas Roger 016 */ 017 018package org.nuxeo.ecm.platform.types.localconfiguration; 019 020import static org.nuxeo.ecm.platform.types.localconfiguration.UITypesConfigurationConstants.UI_TYPES_CONFIGURATION_DEFAULT_TYPE; 021 022import java.util.ArrayList; 023import java.util.List; 024 025import org.nuxeo.ecm.core.api.DocumentModel; 026import org.nuxeo.ecm.core.api.event.DocumentEventTypes; 027import org.nuxeo.ecm.core.event.Event; 028import org.nuxeo.ecm.core.event.EventContext; 029import org.nuxeo.ecm.core.event.EventListener; 030import org.nuxeo.ecm.core.event.impl.DocumentEventContext; 031 032/** 033 * Listener validating that the selected default type is part of the allowed type on the document. 034 * <p> 035 * If not, the default type is reset. 036 * 037 * @since 5.7.3 038 */ 039public class UITypesConfigurationListener implements EventListener { 040 041 @Override 042 public void handleEvent(Event event) { 043 if (!DocumentEventTypes.BEFORE_DOC_UPDATE.equals(event.getName())) { 044 return; 045 } 046 047 EventContext eventContext = event.getContext(); 048 if (eventContext instanceof DocumentEventContext) { 049 DocumentModel doc = ((DocumentEventContext) eventContext).getSourceDocument(); 050 UITypesConfiguration uiTypesConfiguration = doc.getAdapter(UITypesConfiguration.class, true); 051 if (uiTypesConfiguration != null) { 052 List<String> allowedTypes = new ArrayList<>(uiTypesConfiguration.getAllowedTypes()); 053 if (!allowedTypes.isEmpty()) { 054 String defaultType = uiTypesConfiguration.getDefaultType(); 055 if (!allowedTypes.contains(defaultType)) { 056 // the selected default type is not allowed, reset it 057 doc.setPropertyValue(UI_TYPES_CONFIGURATION_DEFAULT_TYPE, ""); 058 } 059 } 060 } 061 } 062 } 063 064}