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.wiki.WikiBackgroundThread;
026import org.apache.wiki.WikiContext;
027import org.apache.wiki.WikiEngine;
028import org.apache.wiki.WikiPage;
029import org.apache.wiki.search.SearchManager;
030import org.apache.wiki.ui.admin.SimpleAdminBean;
031import org.apache.wiki.ui.progress.ProgressItem;
032
033/**
034 *  The SearchManagerBean is a simple AdminBean interface
035 *  to the SearchManager.  It currently can be used to force
036 *  a reload of all of the pages.
037 *
038 *  @since 2.6
039 */
040public class SearchManagerBean extends SimpleAdminBean
041{
042    private static final String PROGRESS_ID = "searchmanagerbean.reindexer";
043
044    private static final String[] METHODS = { "reload" };
045
046    // private static Logger log = Logger.getLogger( SearchManagerBean.class );
047
048    private WikiBackgroundThread m_updater;
049
050    public SearchManagerBean(WikiEngine engine) throws NotCompliantMBeanException
051    {
052        super();
053        initialize(engine);
054    }
055
056    public String[] getAttributeNames()
057    {
058        return new String[0];
059    }
060
061    public String[] getMethodNames()
062    {
063        return METHODS;
064    }
065
066    public String getTitle()
067    {
068        return "Search manager";
069    }
070
071    /**
072     *  Starts a background thread which goes through all the pages and adds them
073     *  to the reindex queue.
074     *  <p>
075     *  This method prevents itself from being called twice.
076     */
077    public synchronized void reload()
078    {
079        if( m_updater == null )
080        {
081            m_updater = new WikiBackgroundThread(m_engine, 0) {
082                int m_count;
083                int m_max;
084
085                public void startupTask() throws Exception
086                {
087                    super.startupTask();
088
089                    setName("Reindexer started");
090                }
091
092                public void backgroundTask() throws Exception
093                {
094                    Collection<WikiPage> allPages = m_engine.getPageManager().getAllPages();
095
096                    SearchManager mgr = m_engine.getSearchManager();
097                    m_max = allPages.size();
098
099                    ProgressItem pi = new ProgressItem() {
100                        public int getProgress()
101                        {
102                            return 100 * m_count / m_max;
103                        }
104                    };
105
106                    m_engine.getProgressManager().startProgress( pi, PROGRESS_ID );
107
108                    for( WikiPage page : allPages )
109                    {
110                        mgr.reindexPage(page);
111                        m_count++;
112                    }
113
114                    m_engine.getProgressManager().stopProgress( PROGRESS_ID );
115                    shutdown();
116                    m_updater = null;
117                }
118
119            };
120
121            m_updater.start();
122        }
123    }
124
125    public int getType()
126    {
127        return CORE;
128    }
129
130    public String doGet(WikiContext context)
131    {
132        if( m_updater != null )
133        {
134            return "Update already in progress ("+
135                   context.getEngine().getProgressManager().getProgress(PROGRESS_ID)+
136                   "%)";
137        }
138
139        return "<input type='submit' id='searchmanagerbean-reload' name='searchmanagerbean-reload' value='Force index reload'/>"+
140               "<div class='description'>Forces JSPWiki search engine to reindex all pages.  Use this if you think some pages are not being found even if they should.</div>";
141    }
142
143    public String doPost(WikiContext context)
144    {
145        String val = context.getHttpParameter("searchmanagerbean-reload");
146
147        if( val != null )
148        {
149            reload();
150
151            context.getWikiSession().addMessage( "Started reload of all indexed pages..." );
152
153            return "";
154        }
155
156        return doGet(context);
157    }
158
159}