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 */
016
017package org.ametys.web.live;
018
019import java.util.HashMap;
020import java.util.Map;
021
022import org.apache.avalon.framework.parameters.Parameters;
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.avalon.framework.service.Serviceable;
026import org.apache.avalon.framework.thread.ThreadSafe;
027import org.apache.cocoon.acting.AbstractAction;
028import org.apache.cocoon.environment.Redirector;
029import org.apache.cocoon.environment.SourceResolver;
030import org.apache.commons.lang.StringUtils;
031
032/**
033 * Action calling the {@link RebuildLiveComponent}.
034 */
035public class RebuildLiveWorkspaceAction extends AbstractAction implements ThreadSafe, Serviceable
036{
037    private RebuildLiveComponent _rebuildLiveComponent;
038    
039    @Override
040    public void service(ServiceManager manager) throws ServiceException
041    {
042        _rebuildLiveComponent = (RebuildLiveComponent) manager.lookup(RebuildLiveComponent.ROLE);
043    }
044    
045    @Override
046    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
047    {
048        Map<String, String> sitesError = new HashMap<>(_rebuildLiveComponent.rebuildLiveWorkspace());
049        
050        Map<String, String> result = new HashMap<>();
051        if (sitesError.size() > 0)
052        {
053            boolean hasGeneralError = sitesError.containsKey(null);
054            if (hasGeneralError)
055            {
056                sitesError.remove(null);
057            }
058            result.put("sites-error", StringUtils.join(sitesError.keySet(), ", "));
059            result.put("general-error", Boolean.toString(hasGeneralError));
060        }
061        return result;
062    }
063}