001/* 002 * Copyright 2011 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.plugins.blog; 017 018import java.util.Collections; 019import java.util.LinkedHashMap; 020import java.util.Map; 021 022import org.apache.avalon.framework.configuration.Configurable; 023import org.apache.avalon.framework.configuration.Configuration; 024import org.apache.avalon.framework.configuration.ConfigurationException; 025import org.apache.avalon.framework.logger.AbstractLogEnabled; 026import org.apache.avalon.framework.service.ServiceException; 027import org.apache.avalon.framework.service.ServiceManager; 028import org.apache.avalon.framework.service.Serviceable; 029 030import org.ametys.cms.contenttype.ContentType; 031import org.ametys.cms.contenttype.ContentTypeExtensionPoint; 032import org.ametys.cms.contenttype.MetadataSet; 033import org.ametys.runtime.i18n.I18nizableText; 034import org.ametys.runtime.parameter.Enumerator; 035 036/** 037 * Metadata set enumerator. 038 */ 039public class MetadataSetEnumerator extends AbstractLogEnabled implements Enumerator, Serviceable, Configurable 040{ 041 042 /** Content type extension point. */ 043 protected ContentTypeExtensionPoint _cTypeEP; 044 045 /** The configured content type. */ 046 protected String _contentTypeId; 047 048 @Override 049 public void configure(Configuration configuration) throws ConfigurationException 050 { 051 _contentTypeId = configuration.getChild("enumeration").getChild("custom-enumerator").getChild("content-type").getValue(); 052 } 053 054 @Override 055 public void service(ServiceManager manager) throws ServiceException 056 { 057 _cTypeEP = (ContentTypeExtensionPoint) manager.lookup(ContentTypeExtensionPoint.ROLE); 058 } 059 060 @Override 061 public Map<String, Object> getConfiguration() 062 { 063 return Collections.singletonMap("contentTypeId", _contentTypeId); 064 } 065 066 @Override 067 public Map<Object, I18nizableText> getEntries() throws Exception 068 { 069 Map<Object, I18nizableText> entries = new LinkedHashMap<>(); 070 071 ContentType cType = _cTypeEP.getExtension(_contentTypeId); 072 if (cType != null) 073 { 074 for (String metadataSetName : cType.getViewMetadataSetNames(false)) 075 { 076 MetadataSet metadataSet = cType.getMetadataSetForView(metadataSetName); 077 078 entries.put(metadataSetName, metadataSet.getLabel()); 079 } 080 } 081 082 return entries; 083 } 084 085 @Override 086 public I18nizableText getEntry(String value) throws Exception 087 { 088 I18nizableText entry = null; 089 090 ContentType cType = _cTypeEP.getExtension(_contentTypeId); 091 if (cType != null) 092 { 093 MetadataSet metadataSet = cType.getMetadataSetForView(value); 094 if (metadataSet != null) 095 { 096 entry = metadataSet.getLabel(); 097 } 098 } 099 100 return entry; 101 } 102 103}