001/*
002 *  Copyright 2016 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.workspaces.repository.maintenance.ui;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024
025import org.ametys.core.ui.StaticClientSideElement;
026import org.ametys.plugins.repositoryapp.RepositoryProvider;
027import org.ametys.runtime.i18n.I18nizableText;
028
029/**
030 * Client side element for maintenance tasks. Checks if it is a JNDI repository
031 */
032public class MaintenanceTaskClientSideElement extends StaticClientSideElement
033{
034    private RepositoryProvider _repositoryProvider;
035
036    @Override
037    public void service(ServiceManager smanager) throws ServiceException
038    {
039        super.service(smanager);
040        if (smanager.hasService(RepositoryProvider.ROLE))
041        {
042            // In safe-mode, the repository provider can not be retrieved
043            _repositoryProvider = (RepositoryProvider) smanager.lookup(RepositoryProvider.ROLE);
044        }
045    }
046    
047    @Override
048    public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters)
049    {
050        List<Script> clonedScripts = new ArrayList<>();
051        
052        List<Script> scripts = super.getScripts(ignoreRights, contextParameters);
053        for (Script script : scripts)
054        {
055            Script clonedScript = new Script(script);
056            
057            I18nizableText unavailableDescription = getUnavailableDescription();
058            if (unavailableDescription != null)
059            {
060                clonedScript.getParameters().put("unavailable", true);
061                clonedScript.getParameters().put("unavailable-description", unavailableDescription);
062            }
063            
064            clonedScripts.add(clonedScript);
065        }
066        
067        return clonedScripts;
068    }
069    
070    /**
071     * Get the i18n description if the task is unavailable
072     * @return the i18n description or null if the task is available
073     */
074    protected I18nizableText getUnavailableDescription ()
075    {
076        if (isJndi())
077        {
078            return new I18nizableText("plugin.repositoryapp", "PLUGINS_REPOSITORYAPP_BUTTON_MAINTENANCE_TASK_JNDI_DESC");
079        }
080        return null;
081    }
082    
083    /**
084     * Determines if the repository is obtained through JNDI
085     * @return true if the repository is obtained through JNDI
086     */
087    protected boolean isJndi ()
088    {
089        return _repositoryProvider.isJndi();
090    }
091}
092