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.indexing.IndexableElementType; 026import org.ametys.cms.model.CMSDataContext; 027import org.ametys.cms.search.model.CriterionDefinitionAwareElementDefinition; 028import org.ametys.cms.search.model.CriterionDefinitionHelper; 029import org.ametys.cms.search.model.SystemProperty; 030import org.ametys.cms.search.query.Query; 031import org.ametys.cms.search.query.Query.Operator; 032import org.ametys.cms.search.query.UsersQuery; 033import org.ametys.core.user.UserIdentity; 034import org.ametys.plugins.core.user.UserHelper; 035 036/** 037 * Abstract class providing base functionality for a user-typed {@link SystemProperty}. 038 * @param <X> type of ametys object supported by this property 039 */ 040public abstract class AbstractUserIndexableSystemProperty<X extends ModelAwareDataAwareAmetysObject> extends AbstractUserSystemProperty<X> implements CriterionDefinitionAwareElementDefinition<UserIdentity>, IndexationAwareSystemProperty<UserIdentity, X> 041{ 042 /** The user helper */ 043 protected UserHelper _userHelper; 044 045 /** The criterion definition helper */ 046 protected CriterionDefinitionHelper _criterionDefinitionHelper; 047 048 @Override 049 public void service(ServiceManager manager) throws ServiceException 050 { 051 super.service(manager); 052 _userHelper = (UserHelper) manager.lookup(UserHelper.ROLE); 053 _criterionDefinitionHelper = (CriterionDefinitionHelper) manager.lookup(CriterionDefinitionHelper.ROLE); 054 } 055 056 @Override 057 public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters) 058 { 059 return new UsersQuery(getSolrFieldName(), operator, operator != Operator.EXISTS ? (UserIdentity[]) value : new UserIdentity[] {}); 060 } 061 062 @Override 063 public Object getValue(X ametysObject) 064 { 065 return _getUserIdentityValue(ametysObject); 066 } 067 068 public String getSolrSortFieldName() 069 { 070 return Optional.ofNullable(getSolrFieldName()) 071 .map(solrFieldName -> solrFieldName.concat("_sort")) 072 .orElse(null); 073 } 074 075 public String getSolrFacetFieldName() 076 { 077 return Optional.ofNullable(getSolrFieldName()) 078 .map(solrFieldName -> solrFieldName.concat("_dv")) 079 .orElse(null); 080 } 081 082 @Override 083 public Object getSortValue(X content) 084 { 085 UserIdentity user = (UserIdentity) getValue(content); 086 if (user != null) 087 { 088 return _userHelper.getUserSortableName(user); 089 } 090 091 return user; 092 } 093 094 public IndexableElementType getDefaultCriterionType() 095 { 096 CMSDataContext context = CMSDataContext.newInstance() 097 .withModelItem(this); 098 099 String typeId = getType().getDefaultCriterionTypeId(context); 100 return _criterionDefinitionHelper.getCriterionDefinitionType(typeId); 101 } 102}