001/* 002 * Copyright 2013 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.clientsideelement; 017 018import java.util.HashMap; 019import java.util.List; 020import java.util.Map; 021 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.avalon.framework.service.ServiceManager; 024import org.apache.commons.lang.StringUtils; 025 026import org.ametys.cms.contenttype.AbstractMetadataSetElement; 027import org.ametys.cms.contenttype.ContentTypeExtensionPoint; 028import org.ametys.cms.contenttype.ContentTypesHelper; 029import org.ametys.cms.contenttype.Fieldset; 030import org.ametys.cms.contenttype.MetadataSet; 031import org.ametys.cms.repository.Content; 032import org.ametys.core.ui.Callable; 033import org.ametys.core.ui.SimpleMenu; 034import org.ametys.plugins.repository.AmetysObjectResolver; 035 036/** 037 * This element creates a menu for the tag policy gallery. 038 * This gallery must be disabled is the current edited content does not contain any tab. 039 */ 040public class FormEditionModeMenu extends SimpleMenu 041{ 042 /** The ametys resolver */ 043 protected AmetysObjectResolver _resolver; 044 045 /** Content type extension point. */ 046 protected ContentTypeExtensionPoint _contentTypeExtensionPoint; 047 /** Helper for content types */ 048 protected ContentTypesHelper _contentTypesHelper; 049 050 @Override 051 public void service(ServiceManager smanager) throws ServiceException 052 { 053 super.service(smanager); 054 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 055 _contentTypeExtensionPoint = (ContentTypeExtensionPoint) smanager.lookup(ContentTypeExtensionPoint.ROLE); 056 _contentTypesHelper = (ContentTypesHelper) smanager.lookup(ContentTypesHelper.ROLE); 057 } 058 059 /** 060 * Return the current status of this menu. 061 * It determines if the current content contain at least one tab in edition. 062 * @param contentId The id of the content 063 * @param metadataSetName The name of the metadata set in which the search must be done. 064 * @return A Map with ("has-tab", true/false). 065 */ 066 @Callable 067 public Map<String, Object> getStatus(String contentId, String metadataSetName) 068 { 069 Map<String, Object> results = new HashMap<>(); 070 071 Content content = _resolver.resolveById(contentId); 072 MetadataSet metadataSetForEdition = _contentTypesHelper.getMetadataSetForEdition(metadataSetName, content.getTypes(), content.getMixinTypes()); 073 074 if (metadataSetForEdition == null) 075 { 076 String errorMsg = String.format("Unknown metadata set '%s' of type 'edition' for content type(s) '%s'", metadataSetName, StringUtils.join(content.getTypes(), ',')); 077 getLogger().error(errorMsg); 078 throw new IllegalArgumentException(errorMsg); 079 } 080 081 boolean found = _searchForTabFieldSet(metadataSetForEdition.getElements()); 082 results.put("has-tab", found); 083 084 return results; 085 } 086 087 /** 088 * Determines if at least one tab in present in this list of {@link AbstractMetadataSetElement} 089 * @param elements The list of {@link AbstractMetadataSetElement} 090 * @return true if one tab has been found. 091 */ 092 private boolean _searchForTabFieldSet(List<AbstractMetadataSetElement> elements) 093 { 094 // First search in elements. 095 for (AbstractMetadataSetElement metadataSetElement : elements) 096 { 097 if (metadataSetElement instanceof Fieldset) 098 { 099 Fieldset fieldSet = (Fieldset) metadataSetElement; 100 101 if ("tab".equals(fieldSet.getRole().toLowerCase())) 102 { 103 return true; 104 } 105 } 106 } 107 108 // Recursive search. 109 for (AbstractMetadataSetElement metadataSetElement : elements) 110 { 111 if (_searchForTabFieldSet(metadataSetElement.getElements())) 112 { 113 return true; 114 } 115 } 116 117 return false; 118 } 119}