001/* 002 * Copyright 2025 Anyware Services 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 */ 016package org.ametys.cms.content; 017 018import java.util.Optional; 019 020import org.apache.avalon.framework.component.Component; 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023import org.apache.avalon.framework.service.Serviceable; 024import org.apache.commons.lang3.StringUtils; 025 026import org.ametys.cms.contenttype.ContentType; 027import org.ametys.cms.contenttype.ContentTypesHelper; 028import org.ametys.runtime.model.View; 029 030/** 031 * Default implementation of a {@link ContentViewProvider} 032 */ 033public class DefaultContentViewProvider implements ContentViewProvider, Component, Serviceable 034{ 035 /** The content types helper */ 036 protected ContentTypesHelper _contentTypesHelper; 037 038 public void service(ServiceManager manager) throws ServiceException 039 { 040 _contentTypesHelper = (ContentTypesHelper) manager.lookup(ContentTypesHelper.ROLE); 041 } 042 043 public View getView(String viewName, String[] contentTypeIds, String[] mixinIds) 044 { 045 return _contentTypesHelper.getView(viewName, contentTypeIds, mixinIds); 046 } 047 048 public View getViewWithFallback(String viewName, String fallbackViewName, String[] contentTypeIds, String[] mixinIds) 049 { 050 String usedFallbackViewName = Optional.ofNullable(fallbackViewName) 051 .filter(StringUtils::isNotBlank) 052 .orElse(ContentType.MAIN_VIEW_NAME); 053 054 // Use the fallbackViewName if no viewName is provided 055 String usedViewName = Optional.ofNullable(viewName) 056 .filter(StringUtils::isNotBlank) 057 .orElse(usedFallbackViewName); 058 059 View view = getView(usedViewName, contentTypeIds, mixinIds); 060 061 if (view == null && !usedViewName.equals(usedFallbackViewName)) 062 { 063 view = getView(usedFallbackViewName, contentTypeIds, mixinIds); 064 } 065 066 return view; 067 } 068}