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