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.odfweb.service.search; 017 018import org.apache.avalon.framework.component.Component; 019import org.apache.avalon.framework.service.ServiceException; 020import org.apache.avalon.framework.service.ServiceManager; 021import org.apache.avalon.framework.service.Serviceable; 022import org.apache.commons.lang.StringUtils; 023 024import org.ametys.cms.contenttype.ContentType; 025import org.ametys.cms.contenttype.ContentTypeExtensionPoint; 026import org.ametys.web.frontoffice.search.metamodel.FacetDefinition; 027import org.ametys.web.frontoffice.search.metamodel.SearchCriterionDefinition; 028import org.ametys.web.frontoffice.search.metamodel.impl.WordingSearchCriterionDefinition; 029 030/** 031 * A helper for ODF search 032 */ 033public class ODFSearchHelper implements Serviceable, Component 034{ 035 /** The avalon role. */ 036 public static final String ROLE = ODFSearchHelper.class.getName(); 037 038 /** The content type id for abstract program */ 039 private static final String __ABSTRACT_PROGRAM_CONTENT_TYPE_ID = "org.ametys.plugins.odf.Content.abstractProgram"; 040 041 /** The content type extension point */ 042 protected ContentTypeExtensionPoint _cTypeEP; 043 044 public void service(ServiceManager manager) throws ServiceException 045 { 046 _cTypeEP = (ContentTypeExtensionPoint) manager.lookup(ContentTypeExtensionPoint.ROLE); 047 } 048 049 /** 050 * Determines if the criterion matches with program and subprogram 051 * @param criterion the criterion 052 * @return <code>true</code> if the criterion match with program and subprogram 053 */ 054 public boolean isCriterionOnBothProgramAndSubProgram(SearchCriterionDefinition criterion) 055 { 056 ContentType abstractProgramType = _cTypeEP.getExtension(__ABSTRACT_PROGRAM_CONTENT_TYPE_ID); 057 String fieldPath = getPathFromSearchFieldId(criterion.getId()); 058 059 return criterion instanceof WordingSearchCriterionDefinition 060 || abstractProgramType.hasModelItem(fieldPath); 061 } 062 063 /** 064 * True if the facet match with program and subprogram 065 * @param facet the criterion 066 * @return <code>true</code> if the facet match with program and subprogram 067 */ 068 public boolean isFacetOnBothProgramAndSubProgram(FacetDefinition facet) 069 { 070 ContentType abstractProgramType = _cTypeEP.getExtension(__ABSTRACT_PROGRAM_CONTENT_TYPE_ID); 071 String fieldPath = getPathFromSearchFieldId(facet.getId()); 072 073 return abstractProgramType.hasModelItem(fieldPath); 074 } 075 076 /** 077 * Get the path of the search field 078 * @param fieldId the field id 079 * @return the path of the search field 080 */ 081 public String getPathFromSearchFieldId(String fieldId) 082 { 083 return StringUtils.substringAfterLast(fieldId, "$"); 084 } 085}