001/* 002 * Copyright 2016 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.core.user.population; 017 018import java.util.Collections; 019import java.util.List; 020import java.util.Set; 021 022import org.apache.avalon.framework.service.ServiceException; 023import org.apache.avalon.framework.service.ServiceManager; 024import org.apache.avalon.framework.service.Serviceable; 025 026import org.ametys.core.util.I18nUtils; 027import org.ametys.runtime.i18n.I18nizableText; 028import org.ametys.runtime.plugins.admin.notificator.AbstractConfigurableAdministratorNotificator; 029import org.ametys.runtime.plugins.admin.notificator.Notification; 030import org.ametys.runtime.plugins.admin.notificator.Notification.NotificationType; 031 032/** 033 * Notificator for {@link UserPopulation}s in warning because of a invalid configuration (not fatal) 034 */ 035public class MisconfiguredPopulationAdministratorNotificator extends AbstractConfigurableAdministratorNotificator implements Serviceable 036{ 037 private UserPopulationDAO _userPopulationDAO; 038 private I18nUtils _i18nUtils; 039 040 @Override 041 public void service(ServiceManager manager) throws ServiceException 042 { 043 _userPopulationDAO = (UserPopulationDAO) manager.lookup(UserPopulationDAO.ROLE); 044 _i18nUtils = (I18nUtils) manager.lookup(I18nUtils.ROLE); 045 } 046 047 @Override 048 public List<Notification> getNotifications() 049 { 050 Set<String> misconfiguredPopulations = _userPopulationDAO.getMisconfiguredPopulations(); 051 if (!misconfiguredPopulations.isEmpty()) 052 { 053 I18nizableText message = _getMessage (misconfiguredPopulations); 054 Notification notification = new Notification(_getType(misconfiguredPopulations), _title, message, _iconGlyph, _action); 055 return Collections.singletonList(notification); 056 } 057 else 058 { 059 return Collections.EMPTY_LIST; 060 } 061 } 062 063 private NotificationType _getType (Set<String> upIds) 064 { 065 for (String id : upIds) 066 { 067 UserPopulation userPopulation = _userPopulationDAO.getUserPopulation(id); 068 if (userPopulation.isEnabled()) 069 { 070 // If at least one misconfigured population is enabled, the notification should appear as a error 071 return NotificationType.ERROR; 072 } 073 } 074 return NotificationType.WARN; 075 } 076 077 private I18nizableText _getMessage(Set<String> upIds) 078 { 079 StringBuilder sb = new StringBuilder(); 080 081 for (String id : upIds) 082 { 083 UserPopulation userPopulation = _userPopulationDAO.getUserPopulation(id); 084 085 String upLabel = _i18nUtils.translate(userPopulation.getLabel()); 086 sb.append("<li>").append(upLabel).append("</li>"); 087 } 088 089 String i18nParam = sb.toString(); 090 return new I18nizableText(_message.getCatalogue(), _message.getKey(), Collections.singletonList(i18nParam)); 091 } 092}