001/*
002 *  Copyright 2011 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.plugins.survey.clientsideelement;
017
018import java.util.ArrayList;
019import java.util.HashMap;
020import java.util.List;
021import java.util.Map;
022
023import org.apache.avalon.framework.service.ServiceException;
024import org.apache.avalon.framework.service.ServiceManager;
025import org.apache.commons.lang.StringUtils;
026
027import org.ametys.plugins.repository.AmetysObjectResolver;
028import org.ametys.plugins.survey.repository.Survey;
029import org.ametys.runtime.i18n.I18nizableText;
030import org.ametys.core.ui.Callable;
031import org.ametys.core.ui.StaticClientSideElement;
032
033/**
034 * This element creates a toggle button representing the validated state of a survey
035 */
036public class ValidateSurveyClientSideElement extends StaticClientSideElement
037{
038    /** The ametys object resolver. */
039    protected AmetysObjectResolver _resolver;
040    
041    @Override
042    public void service(ServiceManager serviceManager) throws ServiceException
043    {
044        super.service(serviceManager);
045        _resolver = (AmetysObjectResolver) serviceManager.lookup(AmetysObjectResolver.ROLE);
046    }
047    
048    /**
049     * Get the validation status for a given survey
050     * @param surveyId The survey id
051     * @return The validation status
052     */
053    @Callable
054    public Map<String, Object> getStatus(String surveyId)
055    {
056        Map<String, Object> results = new HashMap<>();
057        
058        Survey survey = _resolver.resolveById(surveyId);
059        
060        boolean isValidated = survey.isValidated();
061        
062        List<String> i18nParameters = new ArrayList<>();
063        i18nParameters.add(StringUtils.isNotEmpty(survey.getTitle()) ? survey.getTitle() : survey.getLabel());
064        
065        I18nizableText msg = null;
066        if (isValidated)
067        {
068            I18nizableText ed = (I18nizableText) this._script.getParameters().get("validated-description");
069            msg = new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters);
070        }
071        else
072        {
073            I18nizableText ed = (I18nizableText) this._script.getParameters().get("to-validate-description");
074            msg = new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters);
075        }
076        
077        
078        results.put("validate-description", msg);
079        results.put("validated", Boolean.toString(isValidated));
080        
081        return results;
082    }
083
084}