001/*
002 *  Copyright 2015 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.clientsideelement;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023
024import org.ametys.core.ui.Callable;
025import org.ametys.web.repository.page.ModifiableZoneItem;
026import org.ametys.web.repository.page.ZoneItem;
027import org.ametys.web.repository.page.ZoneItem.ZoneType;
028import org.ametys.web.service.Service;
029import org.ametys.web.service.ServiceExtensionPoint;
030
031/**
032 * This client side handles a controller enabled if the active zone item is a modifiable service with at least one parameter.
033 *
034 */
035public class ConfigureServiceClientSideElement extends AbstractPageClientSideElement
036{
037    private ServiceExtensionPoint _serviceExtensionPoint;
038
039    @Override
040    public void service(ServiceManager smanager) throws ServiceException
041    {
042        super.service(smanager);
043        _serviceExtensionPoint = (ServiceExtensionPoint) smanager.lookup(ServiceExtensionPoint.ROLE);
044    }
045
046    /**
047     * Get the status of the zone item corresponding the the given id
048     * @param zoneItemId the id of the zone item
049     * @return the status of the zone item
050     */
051    @Callable
052    public Map<String, Object> getStatus (String zoneItemId)
053    {
054        Map<String, Object> results = new HashMap<>();
055        
056        ZoneItem zoneItem = _resolver.resolveById(zoneItemId);
057        
058        if (zoneItem.getType() != ZoneType.SERVICE)
059        {
060            // Invalid selection (should never append because it is already protected by client-side)
061            results.put("no-service", true);
062            return results;
063        }
064        
065        String serviceId = zoneItem.getServiceId();
066        Service service = _serviceExtensionPoint.getExtension(serviceId);
067        
068        if (service.getParameters().isEmpty())
069        {
070            // No parameters
071            results.put("no-parameters", true);
072        }
073        else if (!(zoneItem instanceof ModifiableZoneItem))
074        {
075            // Zone item is not modifiable (should never append because it is already protected by client-side)
076            results.put("nomodifiable-zoneitem", true);
077        }
078        else if (!hasRight(zoneItem.getZone().getPage()))
079        {
080            // User has no sufficient right (should never append because it is already protected by client-side)
081            results.put("noright-zoneitem", true);
082        }
083        else
084        {
085            results.put("allright-zoneitem", true);
086        }
087        return results;
088    }
089
090}