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.List; 019 020import org.apache.commons.lang3.StringUtils; 021 022import org.ametys.cms.search.JoinableSearchField; 023 024/** 025 * Metadata (abstract) generic search field. 026 */ 027public abstract class AbstractMetadataSearchField implements JoinableSearchField 028{ 029 /** The join paths */ 030 protected List<String> _joinPaths; 031 /** The field path. */ 032 protected String _path; 033 034 /** 035 * Build a Metadata SearchField 036 * @param path The field path 037 */ 038 protected AbstractMetadataSearchField(String path) 039 { 040 this(null, path); 041 } 042 043 /** 044 * Build a Metadata SearchField 045 * @param joinPaths The join field paths 046 * @param finalPath The final field path 047 */ 048 protected AbstractMetadataSearchField(List<String> joinPaths, String finalPath) 049 { 050 _joinPaths = joinPaths; 051 _path = finalPath; 052 } 053 054 @Override 055 public String getSortField() 056 { 057 String sortFieldSuffix = _getSortFieldSuffix(); 058 if (sortFieldSuffix == null) 059 { 060 return null; // not sortable 061 } 062 063 if (isJoined()) 064 { 065 String join = getJoinedPath(); 066 return "join(" + join + "," + _path + sortFieldSuffix + ")"; 067 } 068 return _path + sortFieldSuffix; 069 } 070 071 /** 072 * Get the sort field suffix corresponding to this field. Return null if not sortable. 073 * @return the sort field suffix. Null if not sortable. 074 */ 075 protected abstract String _getSortFieldSuffix(); 076 077 @Override 078 public boolean isJoined() 079 { 080 return _joinPaths != null && _joinPaths.size() > 0; 081 } 082 083 @Override 084 public String getJoinedPath() 085 { 086 return _joinPaths != null ? StringUtils.join(_joinPaths, "->") : ""; 087 } 088 089 /** 090 * Gets the final field path 091 * @return the final field path 092 */ 093 public String getFinalPath() 094 { 095 return _path; 096 } 097 098 @Override 099 public String getFacetField() 100 { 101 return getFinalPath() + _getFacetFieldSuffix(); 102 } 103 104 /** 105 * Get the facet field suffix corresponding to this field. Return null if not facetable. 106 * @return the facet field suffix. Null if not facetable. 107 */ 108 protected abstract String _getFacetFieldSuffix(); 109 110 @Override 111 public String getFacetFunction() 112 { 113 String facetFieldSuffix = _getFacetFieldSuffix(); 114 if (facetFieldSuffix == null) 115 { 116 return null; // not facetable 117 } 118 119 if (isJoined()) 120 { 121 String join = getJoinedPath(); 122 return "join(" + join + "," + _path + facetFieldSuffix + ")"; 123 } 124 return _path + facetFieldSuffix; 125 } 126 127 @Override 128 public String getName() 129 { 130 if (isJoined()) 131 { 132 return StringUtils.join(_joinPaths, '/') + '/' + _path; 133 } 134 return _path; 135 } 136}