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.content.referencetable.search; 017 018import java.util.List; 019import java.util.Map; 020import java.util.stream.Collectors; 021 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.avalon.framework.service.ServiceManager; 024import org.apache.solr.client.solrj.util.ClientUtils; 025 026import org.ametys.cms.content.referencetable.HierarchicalReferenceTablesHelper; 027import org.ametys.cms.data.ContentValue; 028import org.ametys.cms.data.type.ModelItemTypeConstants; 029import org.ametys.cms.model.ContentElementDefinition; 030import org.ametys.cms.repository.Content; 031import org.ametys.cms.search.SearchField; 032import org.ametys.cms.search.model.SystemProperty; 033import org.ametys.cms.search.query.Query; 034import org.ametys.cms.search.query.Query.Operator; 035import org.ametys.cms.search.systemprop.AbstractSystemProperty; 036 037/** 038 * {@link SystemProperty} which represents all parents (parent, parent of the parent, etc.) of a simple content. 039 */ 040public class ParentContentSystemProperty extends AbstractSystemProperty<ContentValue, Content> implements ContentElementDefinition 041{ 042 /** The helper component for hierarchical simple contents */ 043 protected HierarchicalReferenceTablesHelper _hierarchicalSimpleContentsHelper; 044 045 @Override 046 public void service(ServiceManager manager) throws ServiceException 047 { 048 super.service(manager); 049 _hierarchicalSimpleContentsHelper = (HierarchicalReferenceTablesHelper) manager.lookup(HierarchicalReferenceTablesHelper.ROLE); 050 } 051 052 @Override 053 public boolean isMultiple() 054 { 055 return true; 056 } 057 058 @Override 059 public boolean isSortable() 060 { 061 return false; 062 } 063 064 @Override 065 public String getRenderer() 066 { 067 return "Ametys.plugins.cms.search.SearchGridHelper.renderContent"; 068 } 069 070 @Override 071 public String getCriterionWidget() 072 { 073 return "edition.hidden"; 074 // return "edition.select-referencetable-content"; 075 } 076 077 @Override 078 public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters) 079 { 080 if (value instanceof String) 081 { 082 return () -> ParentContentSearchField.NAME + ":" + ClientUtils.escapeQueryChars((String) value); 083 } 084 else 085 { 086 return null; 087 } 088 } 089 090 @Override 091 public SearchField getSearchField() 092 { 093 return new ParentContentSearchField(); 094 } 095 096 @Override 097 public Object getValue(Content content) 098 { 099 List<String> parentsAndMe = _hierarchicalSimpleContentsHelper.getAllParents(content); 100 parentsAndMe.add(content.getId()); 101 102 return parentsAndMe.stream() 103 .map(id -> new ContentValue(_resolver, id)) 104 .collect(Collectors.toList()) 105 .toArray(new ContentValue[parentsAndMe.size()]); 106 } 107 108 @Override 109 protected String _getTypeId() 110 { 111 return ModelItemTypeConstants.CONTENT_ELEMENT_TYPE_ID; 112 } 113}