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