001/* 002 * Copyright 2015 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.systemprop; 017 018import java.util.Map; 019import java.util.Optional; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023 024import org.ametys.cms.data.ametysobject.ModelAwareDataAwareAmetysObject; 025import org.ametys.cms.data.type.ModelItemTypeConstants; 026import org.ametys.cms.search.model.SystemProperty; 027import org.ametys.cms.search.query.Query; 028import org.ametys.cms.search.query.Query.Operator; 029import org.ametys.cms.search.query.UsersQuery; 030import org.ametys.core.user.UserIdentity; 031import org.ametys.plugins.core.user.UserHelper; 032 033/** 034 * Abstract class providing base functionality for a user-typed {@link SystemProperty}. 035 * @param <X> type of ametys object supported by this property 036 */ 037public abstract class AbstractUserIndexableSystemProperty<X extends ModelAwareDataAwareAmetysObject> extends AbstractIndexableSystemProperty<UserIdentity, UserIdentity, X> 038{ 039 /** The user helper */ 040 protected UserHelper _userHelper; 041 042 @Override 043 public void service(ServiceManager manager) throws ServiceException 044 { 045 super.service(manager); 046 _userHelper = (UserHelper) manager.lookup(UserHelper.ROLE); 047 } 048 049 @Override 050 public String getRenderer() 051 { 052 return "Ametys.grid.GridColumnHelper.renderUser"; 053 } 054 055 /** 056 * Get the value as {@link UserIdentity} 057 * @param ametysObject the ametys object 058 * @return the user identity 059 */ 060 protected abstract UserIdentity _getUserIdentityValue(X ametysObject); 061 062 @Override 063 public Object getValue(X ametysObject) 064 { 065 return _getUserIdentityValue(ametysObject); 066 } 067 068 @Override 069 protected String getTypeId() 070 { 071 return ModelItemTypeConstants.USER_ELEMENT_TYPE_ID; 072 } 073 074 @Override 075 public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters) 076 { 077 return new UsersQuery(getSolrFieldName(), operator, operator != Operator.EXISTS ? (UserIdentity[]) value : new UserIdentity[] {}); 078 } 079 080 @Override 081 public String getSolrSortFieldName() 082 { 083 return Optional.ofNullable(getSolrFieldName()) 084 .map(solrFieldName -> solrFieldName.concat("_sort")) 085 .orElse(null); 086 } 087 088 @Override 089 public String getSolrFacetFieldName() 090 { 091 return Optional.ofNullable(getSolrFieldName()) 092 .map(solrFieldName -> solrFieldName.concat("_dv")) 093 .orElse(null); 094 } 095 096 @Override 097 protected Object getSortValue(X content) 098 { 099 UserIdentity user = (UserIdentity) getValue(content); 100 if (user != null) 101 { 102 return _userHelper.getUserSortableName(user); 103 } 104 105 return user; 106 } 107}