001/* 002 * Copyright 2010 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.category; 017 018import java.io.InputStream; 019import java.util.Date; 020 021import org.apache.avalon.framework.component.Component; 022import org.apache.avalon.framework.configuration.Configuration; 023import org.apache.avalon.framework.configuration.ConfigurationException; 024import org.apache.avalon.framework.configuration.DefaultConfigurationBuilder; 025import org.apache.avalon.framework.logger.AbstractLogEnabled; 026import org.apache.avalon.framework.service.ServiceException; 027import org.apache.avalon.framework.service.ServiceManager; 028import org.apache.avalon.framework.service.Serviceable; 029import org.apache.excalibur.source.Source; 030import org.apache.excalibur.source.SourceResolver; 031 032import org.ametys.runtime.i18n.I18nizableText; 033 034/** 035 * Represent a newsletter template. 036 */ 037public class NewsletterTemplate extends AbstractLogEnabled implements Serviceable, Component 038{ 039 private SourceResolver _sourceResolver; 040 041 private String _id; 042 private String _skinId; 043 private I18nizableText _label; 044 private I18nizableText _description; 045 private String _smallImage; 046 private String _mediumImage; 047 private String _largeImage; 048 private long _lastConfUpdate; 049 050 /** 051 * Creates a template 052 * @param skinId The skin id 053 * @param templateId The template id 054 */ 055 public NewsletterTemplate(String skinId, String templateId) 056 { 057 this._skinId = skinId; 058 this._id = templateId; 059 } 060 061 @Override 062 public void service(ServiceManager manager) throws ServiceException 063 { 064 _sourceResolver = (SourceResolver) manager.lookup(SourceResolver.ROLE); 065 } 066 067 /** 068 * The template id 069 * @return the id 070 */ 071 public String getId() 072 { 073 return _id; 074 } 075 076 /** 077 * The template label 078 * @return The label 079 */ 080 public I18nizableText getLabel() 081 { 082 return _label; 083 } 084 /** 085 * The template description 086 * @return The description. Can not be null but can be empty 087 */ 088 public I18nizableText getDescription() 089 { 090 return _description; 091 } 092 093 /** 094 * The small image file uri 095 * @return The small image file uri 096 */ 097 public String getSmallImage() 098 { 099 return _smallImage; 100 } 101 102 /** 103 * The medium image file uri 104 * @return The medium image file uri 105 */ 106 public String getMediumImage() 107 { 108 return _mediumImage; 109 } 110 111 /** 112 * The large image file uri 113 * @return The large image file uri 114 */ 115 public String getLargeImage() 116 { 117 return _largeImage; 118 } 119 120 121 /** 122 * Refresh the configuration values 123 */ 124 public void refreshValues () 125 { 126 Source configurationFile = null; 127 try 128 { 129 configurationFile = _sourceResolver.resolveURI("context://skins/" + this._skinId + "/newsletter/" + this._id + "/template.xml"); 130 if (configurationFile.exists()) 131 { 132 if (_lastConfUpdate < configurationFile.getLastModified()) 133 { 134 _defaultValues(); 135 136 _lastConfUpdate = configurationFile.getLastModified(); 137 try (InputStream is = configurationFile.getInputStream()) 138 { 139 Configuration configuration = new DefaultConfigurationBuilder().build(is); 140 141 this._label = _configureI18n(configuration.getChild("label", false), this._label); 142 this._description = _configureI18n(configuration.getChild("description", false), this._description); 143 this._smallImage = _configureThumbnail(configuration.getChild("thumbnail").getChild("small").getValue(null), this._smallImage); 144 this._mediumImage = _configureThumbnail(configuration.getChild("thumbnail").getChild("medium").getValue(null), this._mediumImage); 145 this._largeImage = _configureThumbnail(configuration.getChild("thumbnail").getChild("marge").getValue(null), this._largeImage); 146 } 147 } 148 } 149 else 150 { 151 _defaultValues(); 152 } 153 } 154 catch (Exception e) 155 { 156 _defaultValues(); 157 } 158 finally 159 { 160 _sourceResolver.release(configurationFile); 161 } 162 } 163 164 private void _defaultValues() 165 { 166 _lastConfUpdate = new Date().getTime(); 167 168 this._label = new I18nizableText(this._id); 169 this._description = new I18nizableText(""); 170 this._smallImage = "/plugins/newsletter/resources/img/category/template_16.png"; 171 this._mediumImage = "/plugins/newsletter/resources/img/category/template_32.png"; 172 this._largeImage = "/plugins/newsletter/resources/img/category/template_48.png"; 173 } 174 175 private I18nizableText _configureI18n(Configuration child, I18nizableText defaultValue) throws ConfigurationException 176 { 177 if (child != null) 178 { 179 String value = child.getValue(); 180 if (child.getAttributeAsBoolean("i18n", false)) 181 { 182 return new I18nizableText("skin." + this._skinId, value); 183 } 184 else 185 { 186 return new I18nizableText(value); 187 } 188 } 189 else 190 { 191 return defaultValue; 192 } 193 } 194 195 private String _configureThumbnail(String value, String defaultImage) 196 { 197 if (value == null) 198 { 199 return defaultImage; 200 } 201 else 202 { 203 return "/skins/" + this._skinId + "/newsletter/" + this._id + "/resources/" + value; 204 } 205 } 206 207}