001/* 002 * Copyright 2012 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.plugins.newsletter.auto; 017 018import java.util.ArrayList; 019import java.util.Collection; 020import java.util.Collections; 021import java.util.HashMap; 022import java.util.List; 023import java.util.Map; 024 025import org.apache.avalon.framework.configuration.Configurable; 026import org.apache.avalon.framework.configuration.Configuration; 027import org.apache.avalon.framework.configuration.ConfigurationException; 028import org.apache.avalon.framework.logger.AbstractLogEnabled; 029import org.apache.commons.lang.StringUtils; 030 031import org.ametys.runtime.i18n.I18nizableText; 032import org.ametys.runtime.plugin.component.PluginAware; 033 034/** 035 * Static implementation of {@link AutomaticNewsletter}. 036 */ 037public class StaticAutomaticNewsletter extends AbstractLogEnabled implements AutomaticNewsletter, Configurable, PluginAware 038{ 039 040 /** The label. */ 041 protected I18nizableText _label; 042 043 /** The description. */ 044 protected I18nizableText _description; 045 046 /** The newsletter title. */ 047 protected I18nizableText _newsletterTitle; 048 049 /** The frequency type. */ 050 protected FrequencyType _frequencyType; 051 052 /** The day numbers. */ 053 protected List<Integer> _dayNumbers; 054 055 /** The filters. */ 056 protected Map<String, String> _filters; 057 058 /** The plugin name. */ 059 protected String _pluginName; 060 061 @Override 062 public void setPluginInfo(String pluginName, String featureName, String id) 063 { 064 _pluginName = pluginName; 065 } 066 067 @Override 068 public void configure(Configuration configuration) throws ConfigurationException 069 { 070 _label = _parseI18nizableText(configuration, "label"); 071 _description = _parseI18nizableText(configuration, "description"); 072 _newsletterTitle = _parseI18nizableText(configuration, "newsletter-title"); 073 074 Configuration frequencyConf = configuration.getChild("frequency"); 075 String frequencyType = frequencyConf.getAttribute("type"); 076 String dayNumbersStr = frequencyConf.getValue(); 077 078 _frequencyType = FrequencyType.valueOf(frequencyType.toUpperCase()); 079 _dayNumbers = new ArrayList<>(); 080 for (String dayNumber : StringUtils.split(dayNumbersStr, ", ")) 081 { 082 try 083 { 084 int number = Integer.parseInt(dayNumber); 085 _dayNumbers.add(number); 086 } 087 catch (NumberFormatException e) 088 { 089 throw new ConfigurationException("Error parsing the day numbers.", configuration, e); 090 } 091 } 092 093 _filters = new HashMap<>(); 094 Configuration[] filterConfigurations = configuration.getChild("filters").getChildren("filter"); 095 for (Configuration filterConf : filterConfigurations) 096 { 097 String name = filterConf.getAttribute("name"); 098 String id = filterConf.getAttribute("id"); 099 100 _filters.put(name, id); 101 } 102 } 103 104 @Override 105 public I18nizableText getLabel() 106 { 107 return _label; 108 } 109 110 @Override 111 public I18nizableText getDescription() 112 { 113 return _description; 114 } 115 116 @Override 117 public I18nizableText getNewsletterTitle() 118 { 119 return _newsletterTitle; 120 } 121 122 @Override 123 public FrequencyType getFrequencyType() 124 { 125 return _frequencyType; 126 } 127 128 @Override 129 public Collection<Integer> getDayNumbers() 130 { 131 return Collections.unmodifiableList(_dayNumbers); 132 } 133 134 @Override 135 public Map<String, String> getFilters() 136 { 137 return Collections.unmodifiableMap(_filters); 138 } 139 140 /** 141 * Parse an i18n text. 142 * @param config the configuration to use. 143 * @param name the child name. 144 * @return the i18n text. 145 * @throws ConfigurationException if the configuration is not valid. 146 */ 147 protected I18nizableText _parseI18nizableText(Configuration config, String name) throws ConfigurationException 148 { 149 Configuration textConfig = config.getChild(name); 150 boolean i18nSupported = textConfig.getAttributeAsBoolean("i18n", false); 151 String text = textConfig.getValue(""); 152 153 if (i18nSupported) 154 { 155 String catalogue = textConfig.getAttribute("catalogue", null); 156 157 if (catalogue == null) 158 { 159 catalogue = "plugin." + _pluginName; 160 } 161 162 return new I18nizableText(catalogue, text); 163 } 164 else 165 { 166 return new I18nizableText(text); 167 } 168 } 169 170}