001/* 002 * Copyright 2018 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.tag; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022import java.util.Set; 023 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.commons.lang3.StringUtils; 027 028import org.ametys.cms.tag.jcr.CMSJCRTagProvider; 029import org.ametys.core.ui.Callable; 030 031/** 032 * DAO for manipulating tags 033 * 034 */ 035public class TagsDAO extends AbstractTagsDAO 036{ 037 /** The Avalon role */ 038 public static final String ROLE = TagsDAO.class.getName(); 039 040 /** The attribute name for the target type */ 041 public static final String TARGET_TYPE_ATTRIBUTE_NAME = "target"; 042 043 /** Target types */ 044 protected TagTargetTypeExtensionPoint _targetTypeEP; 045 046 @Override 047 public void service(ServiceManager manager) throws ServiceException 048 { 049 super.service(manager); 050 _targetTypeEP = (TagTargetTypeExtensionPoint) manager.lookup(TagTargetTypeExtensionPoint.ROLE); 051 } 052 053 @Override 054 public String getTagProviderEPRole() 055 { 056 return TagProviderExtensionPoint.ROLE; 057 } 058 059 /** 060 * Get the existing target types 061 * @return the target types 062 */ 063 @Callable 064 public List<Map<String, Object>> getTargetTypes () 065 { 066 List<Map<String, Object>> targetTypes = new ArrayList<>(); 067 068 Set<String> ids = _targetTypeEP.getExtensionsIds(); 069 070 for (String id : ids) 071 { 072 TagTargetType type = _targetTypeEP.getExtension(id); 073 074 Map<String, Object> type2json = new HashMap<>(); 075 type2json.put("name", type.getName()); 076 type2json.put("label", type.getLabel()); 077 type2json.put("description", type.getDescription()); 078 079 targetTypes.add(type2json); 080 } 081 082 return targetTypes; 083 } 084 085 @Override 086 protected String _getFilteredTagName(TagProvider<? extends Tag> tagProvider, String tagName, Map<String, Object> otherParameters, Map<String, Object> contextualParameters) 087 { 088 String targetType = (String) otherParameters.get(TARGET_TYPE_ATTRIBUTE_NAME); 089 if (StringUtils.isNotEmpty(targetType)) 090 { 091 CMSTag tag = (CMSTag) tagProvider.getTag(tagName, contextualParameters); 092 if (tag.getTarget().getName().equals(targetType)) 093 { 094 return tagName; 095 } 096 } 097 else 098 { 099 return tagName; 100 } 101 102 return null; 103 } 104 105 @Override 106 protected List<TagProvider<? extends Tag>> getCustomTagProvider() 107 { 108 List<TagProvider<? extends Tag>> providers = new ArrayList<>(); 109 providers.add(_tagProviderExtPt.getExtension(CMSJCRTagProvider.class.getName())); 110 111 return providers; 112 } 113}