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