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