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