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