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.site; 017 018import java.util.HashMap; 019import java.util.Map; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023import org.apache.avalon.framework.service.Serviceable; 024 025import org.ametys.plugins.repository.AmetysObjectIterable; 026import org.ametys.plugins.repository.UnknownAmetysObjectException; 027import org.ametys.runtime.i18n.I18nizableText; 028import org.ametys.runtime.model.Enumerator; 029import org.ametys.web.repository.site.Site; 030import org.ametys.web.repository.site.SiteManager; 031 032/** 033 * Enumerates all sites. The code is the site name, the label is the site title. 034 */ 035public class SiteEnumerator implements Enumerator<String>, Serviceable 036{ 037 /** The site manager. */ 038 private SiteManager _siteManager; 039 040 @Override 041 public void service(ServiceManager manager) throws ServiceException 042 { 043 _siteManager = (SiteManager) manager.lookup(SiteManager.ROLE); 044 } 045 046 public Map<String, I18nizableText> getEntries() throws Exception 047 { 048 Map<String, I18nizableText> entries = new HashMap<>(); 049 050 AmetysObjectIterable<Site> sites = _siteManager.getSites(); 051 052 for (Site site : sites) 053 { 054 entries.put(site.getName(), new I18nizableText(site.getTitle())); 055 } 056 057 return entries; 058 } 059 060 @Override 061 public I18nizableText getEntry(String value) throws Exception 062 { 063 try 064 { 065 Site site = _siteManager.getSite(value); 066 return new I18nizableText(site.getTitle()); 067 } 068 catch (UnknownAmetysObjectException e) 069 { 070 // Ignore, just return null 071 } 072 073 return null; 074 } 075}