001/* 002 * Copyright 2017 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.search.solr.field; 017 018import java.util.Collections; 019import java.util.List; 020import java.util.Optional; 021 022import org.apache.commons.lang3.StringUtils; 023 024import org.ametys.cms.search.SearchField; 025 026/** 027 * Metadata (abstract) generic search field. 028 */ 029public abstract class AbstractMetadataSearchField implements SearchField 030{ 031 /** The join paths */ 032 protected List<String> _joinPaths; 033 /** The field path. */ 034 protected String _path; 035 036 /** 037 * Build a Metadata SearchField 038 * @param path The field path 039 */ 040 protected AbstractMetadataSearchField(String path) 041 { 042 this(null, path); 043 } 044 045 /** 046 * Build a Metadata SearchField 047 * @param joinPaths The join field paths 048 * @param finalPath The final field path 049 */ 050 protected AbstractMetadataSearchField(List<String> joinPaths, String finalPath) 051 { 052 _joinPaths = joinPaths; 053 _path = finalPath; 054 } 055 056 @Override 057 public String getSortField() 058 { 059 String sortFieldSuffix = _getSortFieldSuffix(); 060 if (sortFieldSuffix == null) 061 { 062 return null; // not sortable 063 } 064 065 if (isJoined()) 066 { 067 String join = StringUtils.join(_joinPaths, "->"); 068 return "join(" + join + "," + _path + sortFieldSuffix + ")"; 069 } 070 return _path + sortFieldSuffix; 071 } 072 073 /** 074 * Get the sort field suffix corresponding to this field. Return null if not sortable. 075 * @return the sort field suffix. Null if not sortable. 076 */ 077 protected abstract String _getSortFieldSuffix(); 078 079 @Override 080 public boolean isJoined() 081 { 082 return _joinPaths != null && _joinPaths.size() > 0; 083 } 084 085 @Override 086 public List<String> getJoinedPaths() 087 { 088 return Optional.ofNullable(_joinPaths).orElse(Collections.emptyList()); 089 } 090 091 @Override 092 public String getFacetField() 093 { 094 return _path + _getFacetFieldSuffix(); 095 } 096 097 /** 098 * Get the facet field suffix corresponding to this field. Return null if not facetable. 099 * @return the facet field suffix. Null if not facetable. 100 */ 101 protected abstract String _getFacetFieldSuffix(); 102 103 @Override 104 public String getName() 105 { 106 if (isJoined()) 107 { 108 return StringUtils.join(_joinPaths, '/') + '/' + _path; 109 } 110 return _path; 111 } 112}