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.Iterator;
021import java.util.List;
022import java.util.Map;
023
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026
027import org.ametys.plugins.repository.AmetysObjectIterable;
028import org.ametys.plugins.repository.AmetysObjectResolver;
029import org.ametys.plugins.survey.dao.SurveyDAO;
030import org.ametys.plugins.survey.repository.Survey;
031import org.ametys.runtime.i18n.I18nizableText;
032import org.ametys.core.ui.Callable;
033import org.ametys.core.ui.StaticClientSideElement;
034import org.ametys.web.repository.page.Page;
035import org.ametys.web.repository.page.ZoneItem;
036
037/**
038 * This element creates a button to send invitations
039 *
040 */
041public class InvitationsClientSideElement extends StaticClientSideElement
042{
043    /** Repository content */
044    protected AmetysObjectResolver _resolver;
045    private SurveyDAO _surveyDAO;
046    
047    @Override
048    public void service(ServiceManager smanager) throws ServiceException
049    {
050        super.service(smanager);
051        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
052        _surveyDAO = (SurveyDAO) smanager.lookup(SurveyDAO.ROLE);
053    }
054    
055    /**
056     * Get the invitations status for a given survey
057     * @param surveyId The survey id
058     * @return The invitiations status
059     */
060    @Callable
061    public Map<String, Object> getStatus(String surveyId)
062    {
063        Map<String, Object> results = new HashMap<>();
064        
065        Survey survey = _resolver.resolveById(surveyId);
066        
067        boolean isPrivate = _surveyDAO.isPrivate(survey);
068        boolean isValided = survey.isValidated();
069        
070        String siteName = survey.getSiteName();
071        String language = survey.getLanguage();
072        Page page = null;
073        
074        String xpathQuery = "//element(" + siteName + ", ametys:site)/ametys-internal:sitemaps/" + language
075                        + "//element(*, ametys:zoneItem)[@ametys-internal:service = 'org.ametys.survey.service.Display' and ametys:service_parameters/@ametys:surveyId = '" + surveyId + "']";
076        
077        AmetysObjectIterable<ZoneItem> zoneItems = _resolver.query(xpathQuery);
078        Iterator<ZoneItem> it = zoneItems.iterator();
079        if (it.hasNext())
080        {
081            page = it.next().getZone().getPage();
082        }
083        
084        if (!isPrivate)
085        {
086            List<String> i18nParameters = new ArrayList<>();
087            i18nParameters.add(survey.getLabel());
088            
089            I18nizableText ed = (I18nizableText) this._script.getParameters().get("no-private-description");
090            I18nizableText msg = new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters);
091            
092            results.put("status", "no-private");
093            results.put("description", msg);
094        }
095        
096        if (!isValided)
097        {
098            List<String> i18nParameters = new ArrayList<>();
099            i18nParameters.add(survey.getLabel());
100            
101            I18nizableText ed = (I18nizableText) this._script.getParameters().get("no-validated-description");
102            I18nizableText msg = new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters);
103            
104            results.put("status", "no-validated");
105            results.put("description", msg);
106        }
107        
108        if (page == null)
109        {
110            List<String> i18nParameters = new ArrayList<>();
111            i18nParameters.add(survey.getLabel());
112            
113            I18nizableText ed = (I18nizableText) this._script.getParameters().get("no-page-description");
114            I18nizableText msg = new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters);
115            
116            results.put("status", "no-page");
117            results.put("description", msg);
118        }
119        
120        if (isPrivate && isValided && page != null)
121        {
122            List<String> i18nParameters = new ArrayList<>();
123            i18nParameters.add(survey.getLabel());
124            
125            I18nizableText ed = (I18nizableText) this._script.getParameters().get("all-right-description");
126            I18nizableText msg = new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters);
127            
128            results.put("page-id", new I18nizableText(page.getId()));
129            results.put("status", "all-right");
130            results.put("description", msg);
131        }
132        
133        
134        return results;
135    }
136    
137}