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.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    /**
043     *  {@inheritDoc}
044     */
045    @Override
046    public String execute( final Context context, final Map< String, String > params ) throws PluginException {
047        final StringBuilder result = new StringBuilder();
048        final PageManager mgr = context.getEngine().getManager( PageManager.class );
049        final List< PageLock > locks = mgr.getActiveLocks();
050        final ResourceBundle rb = Preferences.getBundle( context, Plugin.CORE_PLUGINS_RESOURCEBUNDLE );
051        result.append("<table class=\"wikitable\">\n");
052        result.append("<tr>\n");
053        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" );
054        result.append("</tr>");
055
056        if(locks.isEmpty()) {
057            result.append( "<tr><td colspan=\"4\" class=\"odd\">" ).append( rb.getString( "plugin.listlocks.no.locks.exist" ) ).append( "</td></tr>\n" );
058        } else {
059            int rowNum = 1;
060            for( final PageLock lock : locks ) {
061                result.append( rowNum % 2 != 0 ? "<tr class=\"odd\">" : "<tr>" );
062                result.append( "<td>" ).append( TextUtil.replaceEntities( lock.getPage() ) ).append( "</td>" );
063                result.append( "<td>" ).append( TextUtil.replaceEntities( lock.getLocker() ) ).append( "</td>" );
064                result.append( "<td>" ).append( Preferences.renderDate( context, lock.getAcquisitionTime(), Preferences.TimeFormat.DATETIME ) ).append( "</td>" );
065                result.append( "<td>" ).append( Preferences.renderDate( context, lock.getExpiryTime(), Preferences.TimeFormat.DATETIME ) ).append( "</td>" );
066                result.append( "</tr>\n" );
067                rowNum++;
068            }
069        }
070        result.append("</table>");
071        return result.toString();
072    }
073
074}