001/*
002 *  Copyright 2010 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.web.content.consistency;
017
018import java.util.Collections;
019import java.util.HashMap;
020import java.util.Map;
021
022import org.ametys.core.ui.Callable;
023
024/**
025 * Cocoon action starting generation
026 */
027public class ConsistencyClientSideElement extends org.ametys.cms.content.consistency.ConsistencyClientSideElement
028{
029    /**
030     * Get the state of the consistency engine for the given site
031     * @param siteName the name of the site
032     * @return a map containing information on the consistency engine's status
033     */
034    @Callable
035    public Map<String, Object> getState(String siteName)
036    {
037        Map<String, Object> params = new HashMap<>();
038        params.put("engine-running", ContentConsistencyEngine.isRunning(siteName));
039        return params;
040    }
041    
042    /**
043     * Start a consistency report
044     * @param siteName the name of the site
045     * @return a map containing information on the consistency engine's status
046     */
047    @Callable
048    public Map<String, String> startReport(String siteName)
049    {
050        Map<String, String> results = new HashMap<> ();
051        if (ContentConsistencyEngine.isRunning(siteName))
052        {
053            results = Collections.singletonMap("error", "already-running");
054        }
055        else
056        {
057            ContentConsistencyEngine contentConsistencyEngine = new ContentConsistencyEngine();
058            
059            try
060            {
061                // Initialize and configure the engine.
062                contentConsistencyEngine.initialize(_manager, _context, siteName);
063                contentConsistencyEngine.configure(_configuration);
064                
065                // Create the thread, mark it as daemon and start it.
066                Thread engineThread = new Thread(contentConsistencyEngine, "WebContentConsistencyEngine");
067                engineThread.setDaemon(true);
068                engineThread.start();
069                
070                return Collections.emptyMap();
071            }
072            catch (Exception e)
073            {
074                throw new RuntimeException("Unable to initialize the content consistency engine", e);
075            }
076        }
077        
078        return results;
079    }
080   
081}