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