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.web.search.systemprop; 017 018import java.util.HashMap; 019import java.util.LinkedHashMap; 020import java.util.Map; 021 022import org.apache.avalon.framework.configuration.Configuration; 023import org.apache.avalon.framework.configuration.ConfigurationException; 024 025import org.ametys.cms.repository.Content; 026import org.ametys.cms.search.SearchField; 027import org.ametys.cms.search.model.SystemProperty; 028import org.ametys.cms.search.query.Query; 029import org.ametys.cms.search.query.Query.Operator; 030import org.ametys.cms.search.systemprop.AbstractSystemProperty; 031import org.ametys.runtime.i18n.I18nizableText; 032import org.ametys.runtime.model.Enumerator; 033import org.ametys.runtime.model.StaticEnumerator; 034import org.ametys.runtime.model.type.DataContext; 035import org.ametys.runtime.model.type.ModelItemTypeConstants; 036import org.ametys.runtime.plugin.component.ThreadSafeComponentManager; 037import org.ametys.web.repository.content.WebContent; 038import org.ametys.web.search.query.ContentPrivacyQuery; 039import org.ametys.web.search.solr.field.ContentPrivacySearchField; 040 041/** 042 * {@link SystemProperty} which represents the privacy level of a Content ("public", "protected" or "private"). 043 */ 044public class ContentPrivacySystemProperty extends AbstractSystemProperty<String, Content> 045{ 046 047 private static final Map<String, I18nizableText> _ENUMERATOR_ENTRIES = new LinkedHashMap<>(); 048 static 049 { 050 _ENUMERATOR_ENTRIES.put("public", new I18nizableText("plugin.web", "PLUGINS_WEB_CONTENT_PRIVACY_PUBLIC_LABEL")); 051 _ENUMERATOR_ENTRIES.put("protected", new I18nizableText("plugin.web", "PLUGINS_WEB_CONTENT_PRIVACY_PROTECTED_LABEL")); 052 _ENUMERATOR_ENTRIES.put("private", new I18nizableText("plugin.web", "PLUGINS_WEB_CONTENT_PRIVACY_PRIVATE_LABEL")); 053 } 054 055 @Override 056 public boolean isMultiple() 057 { 058 return false; 059 } 060 061 @Override 062 public boolean isSortable() 063 { 064 return true; 065 } 066 067 @Override 068 public Integer getColumnWidth() 069 { 070 return 60; 071 } 072 073 @Override 074 public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters) 075 { 076 return new ContentPrivacyQuery(operator, (String) value); 077 } 078 079 @Override 080 public SearchField getSearchField() 081 { 082 return new ContentPrivacySearchField(); 083 } 084 085 @Override 086 public Object getValue(Content content) 087 { 088 if (content instanceof WebContent webContent) 089 { 090 return webContent.getPrivacyLevel(); 091 } 092 093 return null; 094 } 095 096 @Override 097 public Object valueToJSON(Content content, DataContext context) 098 { 099 Object value = getValue(content); 100 101 if (value != null && _ENUMERATOR_ENTRIES.containsKey(value)) 102 { 103 Map<String, Object> infos = new HashMap<>(); 104 infos.put("value", value); 105 infos.put("label", _ENUMERATOR_ENTRIES.get(value)); 106 return infos; 107 } 108 109 return null; 110 } 111 112 public Enumerator getCriterionEnumerator(Configuration configuration, ThreadSafeComponentManager<Enumerator> enumeratorManager) throws ConfigurationException 113 { 114 StaticEnumerator<String> enumerator = new StaticEnumerator<>(); 115 enumerator.addAll(_ENUMERATOR_ENTRIES); 116 return enumerator; 117 } 118 119 @Override 120 protected String _getTypeId() 121 { 122 return ModelItemTypeConstants.STRING_TYPE_ID; 123 } 124}