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 org.apache.commons.lang3.StringUtils;
022import org.apache.wiki.api.Release;
023import org.apache.wiki.api.core.Context;
024import org.apache.wiki.api.core.Engine;
025import org.apache.wiki.modules.WikiModuleInfo;
026import org.apache.wiki.ui.admin.SimpleAdminBean;
027import org.apache.wiki.util.XHTML;
028import org.apache.wiki.util.XhtmlUtil;
029import org.jdom2.Element;
030
031import javax.management.NotCompliantMBeanException;
032import java.util.Collection;
033
034public abstract class ModuleBean extends SimpleAdminBean {
035
036    private static final String VER_WARNING = "<span class='warning'>This module is not compatible with this version of JSPWiki.</span>";
037
038    public ModuleBean( final Engine engine ) throws NotCompliantMBeanException {
039        m_engine = engine;
040    }
041
042    /**
043     * {@inheritDoc}
044     */
045    @Override
046    public String[] getAttributeNames() {
047        return new String[0];
048    }
049
050    /**
051     * {@inheritDoc}
052     */
053    @Override
054    public String[] getMethodNames() {
055        return new String[0];
056    }
057
058    /**
059     * {@inheritDoc}
060     */
061    @Override
062    public String doGet( final Context context ) {
063        final Collection< WikiModuleInfo > filters = modules();
064        final Element root = title();
065        final Element tb = containerForModuleDetail( root );
066
067        final Element trHead = heading();
068        tb.addContent( trHead );
069
070        for( final WikiModuleInfo info : filters ) {
071            final 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        final Element root = XhtmlUtil.element( XHTML.div );
080        root.addContent( XhtmlUtil.element( XHTML.h4 ).addContent( getTitle() ) );
081        return root;
082    }
083
084    protected Element containerForModuleDetail( final Element root ) {
085        final 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(Context)}.
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( final WikiModuleInfo info ) {
113        return Release.isNewerOrEqual( info.getMinVersion() ) && Release.isOlderOrEqual( info.getMaxVersion() )
114               ? StringUtils.EMPTY
115               : VER_WARNING;
116    }
117
118}