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.ui.admin.beans;
020
021import java.util.Collection;
022
023import javax.management.NotCompliantMBeanException;
024
025import org.apache.commons.lang.StringUtils;
026import org.apache.wiki.Release;
027import org.apache.wiki.WikiContext;
028import org.apache.wiki.WikiEngine;
029import org.apache.wiki.modules.WikiModuleInfo;
030import org.apache.wiki.ui.admin.SimpleAdminBean;
031import org.apache.wiki.util.XHTML;
032import org.apache.wiki.util.XhtmlUtil;
033import org.jdom2.Element;
034
035public abstract class ModuleBean extends SimpleAdminBean {
036
037    protected WikiEngine m_engine;
038
039    private static final String VER_WARNING = "<span class='warning'>This module is not compatible with this version of JSPWiki.</span>";
040
041    public ModuleBean( WikiEngine engine ) throws NotCompliantMBeanException {
042        m_engine = engine;
043    }
044
045    /**
046     * {@inheritDoc}
047     */
048    public String[] getAttributeNames() {
049        return new String[0];
050    }
051
052    /**
053     * {@inheritDoc}
054     */
055    public String[] getMethodNames() {
056        return new String[0];
057    }
058
059    /**
060     * {@inheritDoc}
061     */
062    public String doGet( WikiContext context ) {
063        Collection< WikiModuleInfo > filters = modules();
064        Element root = title();
065        Element tb = containerForModuleDetail( root );
066
067        Element trHead = heading();
068        tb.addContent( trHead );
069
070        for( WikiModuleInfo info : filters ) {
071            Element tr = rowBody( info );
072            tb.addContent( tr );
073        }
074
075        return XhtmlUtil.serialize( root, XhtmlUtil.EXPAND_EMPTY_NODES );
076    }
077
078    protected Element title() {
079        Element root = XhtmlUtil.element( XHTML.div );
080        root.addContent( XhtmlUtil.element( XHTML.h4 ).addContent( getTitle() ) );
081        return root;
082    }
083
084    protected Element containerForModuleDetail( Element root ) {
085        Element tb = XhtmlUtil.element( XHTML.table ).setAttribute( "border", "1" );
086        root.addContent( tb );
087        return tb;
088    }
089
090    /**
091     * Obtains the collection of modules which is going to be inspected at {@link #doGet(WikiContext)}.
092     *
093     * @return a collection of {@link WikiModuleInfo}
094     */
095    protected abstract Collection< WikiModuleInfo > modules();
096
097    /**
098     * html blob describing the values of each {@link WikiModuleInfo} inspected.
099     *
100     * @return {@link Element} describing the values of each {@link WikiModuleInfo} inspected.
101     */
102    protected abstract Element heading();
103
104    /**
105     * html blob describing{@link Element} describing attributes
106     *
107     * @param module {@link WikiModuleInfo} inspected.
108     * @return {@link Element} describing the {@link Element} inspected.
109     */
110    protected abstract Element rowBody( WikiModuleInfo module );
111
112    protected String validModuleVersion( WikiModuleInfo info ) {
113        return Release.isNewerOrEqual( info.getMinVersion() ) && Release.isOlderOrEqual( info.getMaxVersion() )
114               ? StringUtils.EMPTY
115               : VER_WARNING;
116    }
117
118}