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.exceptions.PluginException;
023import org.apache.wiki.api.plugin.Plugin;
024import org.apache.wiki.pages.PageLock;
025import org.apache.wiki.pages.PageManager;
026import org.apache.wiki.preferences.Preferences;
027
028import java.util.List;
029import java.util.Locale;
030import java.util.Map;
031import java.util.ResourceBundle;
032
033/**
034 *  This is a plugin for the administrator: It allows him to see in a single glance who is editing what.
035 *
036 *  <p>Parameters : </p>
037 *   NONE
038 *  @since 2.0.22.
039 */
040public class ListLocksPlugin implements Plugin {
041
042    @Override
043    public String getDisplayName(Locale locale) {
044        final ResourceBundle rb = ResourceBundle.getBundle(PluginManager.PLUGIN_I18N_RESOURCE, locale);
045        return rb.getString(this.getClass().getSimpleName());
046    }
047   
048    
049    @Override
050    public  String getSnipExample() {
051        return "ListLocksPlugin";
052    }
053    /**
054     *  {@inheritDoc}
055     */
056    @Override
057    public String execute( final Context context, final Map< String, String > params ) throws PluginException {
058        final StringBuilder result = new StringBuilder();
059        final PageManager mgr = context.getEngine().getManager( PageManager.class );
060        final List< PageLock > locks = mgr.getActiveLocks();
061        final ResourceBundle rb = Preferences.getBundle( context, Plugin.CORE_PLUGINS_RESOURCEBUNDLE );
062        result.append("<table class=\"wikitable\">\n");
063        result.append("<tr>\n");
064        result.append( "<th>" ).append( rb.getString( "plugin.listlocks.page" ) ).append( "</th><th>" ).append( rb.getString( "plugin.listlocks.locked.by" ) ).append( "</th><th>" ).append( rb.getString( "plugin.listlocks.acquired" ) ).append( "</th><th>" ).append( rb.getString( "plugin.listlocks.expires" ) ).append( "</th>\n" );
065        result.append("</tr>");
066
067        if(locks.isEmpty()) {
068            result.append( "<tr><td colspan=\"4\" class=\"odd\">" ).append( rb.getString( "plugin.listlocks.no.locks.exist" ) ).append( "</td></tr>\n" );
069        } else {
070            int rowNum = 1;
071            for( final PageLock lock : locks ) {
072                result.append( rowNum % 2 != 0 ? "<tr class=\"odd\">" : "<tr>" );
073                result.append( "<td>" ).append( lock.getPage() ).append( "</td>" );
074                result.append( "<td>" ).append( lock.getLocker() ).append( "</td>" );
075                result.append( "<td>" ).append( Preferences.renderDate( context, lock.getAcquisitionTime(), Preferences.TimeFormat.DATETIME ) ).append( "</td>" );
076                result.append( "<td>" ).append( Preferences.renderDate( context, lock.getExpiryTime(), Preferences.TimeFormat.DATETIME ) ).append( "</td>" );
077                result.append( "</tr>\n" );
078                rowNum++;
079            }
080        }
081        result.append("</table>");
082        return result.toString();
083    }
084
085}