001/* 
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.  
018 */
019package org.apache.wiki.plugin;
020
021import org.apache.wiki.api.core.Context;
022import org.apache.wiki.api.core.ContextEnum;
023import org.apache.wiki.api.core.Engine;
024import org.apache.wiki.api.exceptions.PluginException;
025import org.apache.wiki.api.plugin.Plugin;
026import org.apache.wiki.auth.authorize.GroupManager;
027import org.apache.wiki.url.URLConstructor;
028import org.apache.wiki.util.comparators.PrincipalComparator;
029
030import java.security.Principal;
031import java.util.Arrays;
032import java.util.Comparator;
033import java.util.Locale;
034import java.util.Map;
035import java.util.ResourceBundle;
036
037/**
038 *  <p>Prints the groups managed by this wiki, separated by commas.
039 *  <br>The groups are sorted in ascending order, and are hyperlinked to the page that displays the group's members.
040 *  </p>
041 *  <p>Parameters : </p>
042 *  NONE
043 *
044 *  @since 2.4.19
045 */
046public class Groups implements Plugin {
047
048    private static final Comparator<Principal> COMPARATOR = new PrincipalComparator();
049    
050    @Override
051    public String getDisplayName(Locale locale) {
052       
053        final ResourceBundle rb = ResourceBundle.getBundle(PluginManager.PLUGIN_I18N_RESOURCE, locale);
054        return rb.getString(this.getClass().getSimpleName());
055    }
056    
057    /**
058     *  {@inheritDoc}
059     */
060    @Override
061    public String execute( final Context context, final Map<String, String> params ) throws PluginException {
062        // Retrieve groups, and sort by name
063        final Engine engine = context.getEngine();
064        final GroupManager groupMgr = engine.getManager( GroupManager.class );
065        final Principal[] groups = groupMgr.getRoles();
066        Arrays.sort( groups, COMPARATOR );
067
068        final StringBuilder s = new StringBuilder();
069        
070        for ( int i = 0; i < groups.length; i++ ) {
071            final String name = groups[ i ].getName();
072            
073            // Make URL
074            final String url = engine.getManager( URLConstructor.class ).makeURL( ContextEnum.GROUP_VIEW.getRequestContext(), name,  null );
075            
076            // Create hyperlink
077            s.append( "<a href=\"" );
078            s.append( url );
079            s.append( "\">" );
080            s.append( name );
081            s.append( "</a>" );
082            
083            // If not the last one, add a comma and space
084            if ( i < ( groups.length - 1 ) ) {
085                s.append( ',' );
086                s.append( ' ' );
087            }
088        }
089        return s.toString();
090    }
091
092}