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