001/* 002 * Copyright 2019 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.web.frontoffice.search.metamodel.impl; 017 018import java.util.Collection; 019 020import org.apache.avalon.framework.configuration.Configuration; 021import org.apache.avalon.framework.configuration.ConfigurationException; 022import org.apache.commons.lang3.StringUtils; 023 024import org.ametys.cms.contenttype.ContentType; 025import org.ametys.web.frontoffice.search.metamodel.AdditionalParameterValueMap; 026import org.ametys.web.frontoffice.search.metamodel.Returnable; 027import org.ametys.web.frontoffice.search.metamodel.ReturnableSaxer; 028import org.ametys.web.frontoffice.search.metamodel.Searchable; 029 030/** 031 * This class is a generic returnable to be used to search on a {@link ContentType#isPrivate() private} {@link ContentType}. 032 * <br>You <b>must</b> associate it with a {@link Searchable} which is or extends {@link PrivateContentSearchable} 033 * <pre> 034 * <extension point="org.ametys.web.frontoffice.search.metamodel.ReturnableExtensionPoint" 035 * id="my.returnable.id" 036 * class="org.ametys.web.frontoffice.search.metamodel.impl.PrivateContentReturnable"> 037 * <label i18n="true">...</label> 038 * <associatedSearchable>my.searchable.id</associatedSearchable> 039 * </extension> 040 * </pre> 041 */ 042public class PrivateContentReturnable extends AbstractContentBasedReturnable 043{ 044 /** The Avalon role of this Returnable */ 045 protected String _avalonRole; 046 /** The "short version" of the Avalon role of this Returnable */ 047 protected String _shortAvalonRole; 048 /** The associated content searchable role */ 049 protected String _associatedContentSearchableRole; 050 /** The {@link #getDefinitionPrefix() prefix} for facet and sort definitions */ 051 protected String _definitionPrefix; 052 053 @Override 054 public void configure(Configuration configuration) throws ConfigurationException 055 { 056 _initAvalonRole(configuration); 057 _initShortAvalonRole(); 058 _configureAssociatedContentReturnableRole(configuration); 059 super.configure(configuration); 060 } 061 062 @Override 063 protected String associatedContentSearchableRole() 064 { 065 return _associatedContentSearchableRole; 066 } 067 068 @Override 069 public String getId() 070 { 071 return _avalonRole; 072 } 073 074 @Override 075 protected Collection<String> getContentTypes(AdditionalParameterValueMap additionalParameterValues) 076 { 077 PrivateContentSearchable associatedSearchable = _getAssociatedSearchable(); 078 return associatedSearchable.getContentTypes(additionalParameterValues); 079 } 080 081 /** 082 * Gets the associated {@link PrivateContentSearchable} 083 * @return the associated {@link PrivateContentSearchable} 084 */ 085 protected PrivateContentSearchable _getAssociatedSearchable() 086 { 087 return (PrivateContentSearchable) _associatedContentSearchable; 088 } 089 090 @Override 091 public ReturnableSaxer getSaxer(Collection<Returnable> allReturnables, AdditionalParameterValueMap additionalParameterValues) 092 { 093 return new ContentSaxer(this, getViewForSax(additionalParameterValues), _contentTypesHelper, getContentTypes(additionalParameterValues)); 094 } 095 096 /** 097 * Gets the view for saxing 098 * @param additionalParameterValues The additional parameter values 099 * @return the view for saxing 100 */ 101 protected String getViewForSax(AdditionalParameterValueMap additionalParameterValues) 102 { 103 return "main"; 104 } 105 106 @Override 107 protected String getDefinitionPrefix() 108 { 109 return _definitionPrefix; 110 } 111 112 /** 113 * Initializes {@link #_avalonRole} field from configuration 114 * @param configuration The configuration 115 * @throws ConfigurationException If a configuration value cannot be retrieved 116 */ 117 protected void _initAvalonRole(Configuration configuration) throws ConfigurationException 118 { 119 _avalonRole = configuration.getAttribute("id"); 120 } 121 122 /** 123 * Initializes {@link #_shortAvalonRole} field 124 */ 125 protected void _initShortAvalonRole() 126 { 127 // Pray for shortId to be unique. If not, method visibilities are protected for implementing you own logic. 128 _shortAvalonRole = StringUtils.substringAfterLast(_avalonRole, "."); 129 } 130 131 /** 132 * Initializes {@link #_associatedContentSearchableRole} field from configuration 133 * @param configuration The configuration 134 * @throws ConfigurationException If a configuration value cannot be retrieved 135 */ 136 protected void _configureAssociatedContentReturnableRole(Configuration configuration) throws ConfigurationException 137 { 138 _associatedContentSearchableRole = configuration.getChild("associatedSearchable").getValue(); 139 } 140 141 /** 142 * Initializes {@link #_definitionPrefix} field 143 */ 144 protected void _initDefinitionPrefix() 145 { 146 _definitionPrefix = _shortAvalonRole + "$"; 147 } 148}