001/* 002 * Copyright 2020 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.workspaces.categories; 017 018import java.util.Collection; 019import java.util.HashMap; 020import java.util.Map; 021import java.util.Map.Entry; 022import java.util.stream.Collectors; 023 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.avalon.framework.service.Serviceable; 027import org.apache.commons.lang3.StringUtils; 028 029import org.ametys.cms.tag.AbstractTagProvider; 030import org.ametys.cms.tag.CMSTag; 031import org.ametys.cms.tag.TagTargetType; 032import org.ametys.cms.tag.TagTargetTypeExtensionPoint; 033 034/** 035 * Class representing categories as CMS tag provider. 036 * This directly use {@link CategoryJCRProvider} and transform the categories in {@link CMSTag} 037 */ 038public class CategoryTagProvider extends AbstractTagProvider<CMSTag> implements Serviceable 039{ 040 /** Target types */ 041 protected TagTargetTypeExtensionPoint _targetTypeEP; 042 043 /** Category tag provider */ 044 protected CategoryProviderExtensionPoint _categoryProviderEP; 045 046 /** The JCR category provider */ 047 protected CategoryJCRProvider _jcrCategoryProvider; 048 049 @Override 050 public void service(ServiceManager smanager) throws ServiceException 051 { 052 _targetTypeEP = (TagTargetTypeExtensionPoint) smanager.lookup(TagTargetTypeExtensionPoint.ROLE); 053 _categoryProviderEP = (CategoryProviderExtensionPoint) smanager.lookup(CategoryProviderExtensionPoint.ROLE); 054 _jcrCategoryProvider = (CategoryJCRProvider) _categoryProviderEP.getExtension(CategoryJCRProvider.class.getName()); 055 } 056 057 public Map<String, CMSTag> getTags(Map<String, Object> contextualParameters) 058 { 059 Map<String, CMSTag> cmsTags = new HashMap<>(); 060 061 Map<String, Category> categories = _jcrCategoryProvider.getTags(contextualParameters); 062 063 for (Entry<String, Category> category : categories.entrySet()) 064 { 065 CategoryCMSTag categoryTag = _transformToCMSTag(category.getValue(), contextualParameters); 066 cmsTags.put(categoryTag.getId(), _transformToCMSTag(category.getValue(), contextualParameters)); 067 } 068 069 return cmsTags; 070 } 071 072 public CMSTag getTag(String tagName, Map<String, Object> contextualParameters) 073 { 074 String categoryName = tagName; 075 if (categoryName.startsWith(CategoryCMSTag.TAG_PREFIX)) 076 { 077 categoryName = StringUtils.substringAfter(tagName, CategoryCMSTag.TAG_PREFIX); 078 } 079 return _transformToCMSTag(_jcrCategoryProvider.getTag(categoryName, contextualParameters), contextualParameters); 080 } 081 082 public Collection<CMSTag> getTags(String tagName, Map<String, Object> contextualParameters) 083 { 084 String categoryName = tagName; 085 if (categoryName.startsWith(CategoryCMSTag.TAG_PREFIX)) 086 { 087 categoryName = StringUtils.substringAfter(tagName, CategoryCMSTag.TAG_PREFIX); 088 } 089 090 Collection<Category> categories = _jcrCategoryProvider.getTags(categoryName, contextualParameters); 091 092 return categories.stream() 093 .map(tag -> _transformToCMSTag(tag, contextualParameters)) 094 .collect(Collectors.toList()); 095 } 096 097 public boolean hasTag(String tagName, Map<String, Object> contextualParameters) 098 { 099 String categoryName = tagName; 100 if (categoryName.startsWith(CategoryCMSTag.TAG_PREFIX)) 101 { 102 categoryName = StringUtils.substringAfter(tagName, CategoryCMSTag.TAG_PREFIX); 103 } 104 return _jcrCategoryProvider.hasTag(categoryName, contextualParameters); 105 } 106 107 /** 108 * Transform a {@link Category} in a {@link CategoryCMSTag}, forcing visibility to public and target to CONTENT 109 * @param category the category to transform 110 * @param contextualParameters the contextual parameters 111 * @return a {@link CategoryCMSTag} with the same values 112 */ 113 protected CategoryCMSTag _transformToCMSTag(Category category, Map<String, Object> contextualParameters) 114 { 115 if (category != null) 116 { 117 CMSTag parentTag = null; 118 Category parentCategory = category.getParent(); 119 if (parentCategory != null) 120 { 121 parentTag = this.getTag(parentCategory.getName(), contextualParameters); 122 } 123 124 String typeName = "CONTENT"; 125 TagTargetType targetType = _targetTypeEP.getTagTargetType(typeName); 126 127 return new CategoryCMSTag(category, parentTag, targetType); 128 } 129 else 130 { 131 return null; 132 } 133 } 134}