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.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025 026import org.ametys.cms.contenttype.MetadataType; 027import org.ametys.cms.repository.Content; 028import org.ametys.cms.search.SearchField; 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.systemprop.AbstractSystemProperty; 033import org.ametys.runtime.i18n.I18nizableText; 034import org.ametys.web.repository.content.WebContent; 035import org.ametys.web.search.query.ContentPrivacyQuery; 036import org.ametys.web.search.solr.field.ContentPrivacySearchField; 037import org.ametys.web.site.SiteConfigurationExtensionPoint; 038 039/** 040 * {@link SystemProperty} which represents the privacy level of a Content ("public", "protected" or "private"). 041 */ 042public class ContentPrivacySystemProperty extends AbstractSystemProperty 043{ 044 045 private static final Map<String, I18nizableText> _ENUMERATOR_ENTRIES = new LinkedHashMap<>(); 046 static 047 { 048 _ENUMERATOR_ENTRIES.put("public", new I18nizableText("plugin.web", "PLUGINS_WEB_CONTENT_PRIVACY_PUBLIC_LABEL")); 049 _ENUMERATOR_ENTRIES.put("protected", new I18nizableText("plugin.web", "PLUGINS_WEB_CONTENT_PRIVACY_PROTECTED_LABEL")); 050 _ENUMERATOR_ENTRIES.put("private", new I18nizableText("plugin.web", "PLUGINS_WEB_CONTENT_PRIVACY_PRIVATE_LABEL")); 051 } 052 053 private SiteConfigurationExtensionPoint _siteConfigurationEP; 054 055 @Override 056 public void service(ServiceManager manager) throws ServiceException 057 { 058 super.service(manager); 059 _siteConfigurationEP = (SiteConfigurationExtensionPoint) manager.lookup(SiteConfigurationExtensionPoint.ROLE); 060 } 061 062 @Override 063 public MetadataType getType() 064 { 065 return MetadataType.STRING; 066 } 067 068 @Override 069 public boolean isMultiple() 070 { 071 return false; 072 } 073 074 @Override 075 public boolean isSortable() 076 { 077 return true; 078 } 079 080 @Override 081 public Integer getColumnWidth() 082 { 083 return 60; 084 } 085 086 @Override 087 public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters) 088 { 089 return new ContentPrivacyQuery(operator, (String) value); 090 } 091 092 @Override 093 public SearchField getSearchField() 094 { 095 return new ContentPrivacySearchField(); 096 } 097 098 @Override 099 public Object getValue(Content content) 100 { 101 String privacy = null; 102 103 if (content instanceof WebContent) 104 { 105 privacy = content.getMetadataHolder().getString("privacy", null); 106 if (privacy == null) 107 { 108 privacy = _siteConfigurationEP.getValueAsString(((WebContent) content).getSiteName(), "content-privacy"); 109 } 110 } 111 112 return privacy; 113 } 114 115 @Override 116 public Object getJsonValue(Content content, boolean full) 117 { 118 Object value = getValue(content); 119 120 if (value != null && _ENUMERATOR_ENTRIES.containsKey(value)) 121 { 122 Map<String, Object> infos = new HashMap<>(); 123 infos.put("value", value); 124 infos.put("label", _ENUMERATOR_ENTRIES.get(value)); 125 return infos; 126 } 127 128 return null; 129 } 130 131 @Override 132 public EnumeratorDefinition getEnumeratorDefinition(Configuration configuration) 133 { 134 return new EnumeratorDefinition(_ENUMERATOR_ENTRIES); 135 } 136 137}