001/* 002 * Copyright 2013 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.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.core.right.RightManager.RightResult; 028import org.ametys.core.ui.StaticClientSideElement; 029import org.ametys.plugins.repository.AmetysObjectResolver; 030import org.ametys.runtime.i18n.I18nizableText; 031import org.ametys.web.repository.page.Page; 032import org.ametys.web.repository.page.SitemapElement; 033import org.ametys.web.synchronization.SynchronizeComponent; 034 035/** 036 * Abstract client side element for pages. 037 * 038 */ 039public abstract class AbstractPageClientSideElement extends StaticClientSideElement 040{ 041 /** The Ametys object resolver */ 042 protected AmetysObjectResolver _resolver; 043 /** The synchronize helper */ 044 protected SynchronizeComponent _synchronizeComponent; 045 046 @Override 047 public void service(ServiceManager smanager) throws ServiceException 048 { 049 super.service(smanager); 050 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 051 _synchronizeComponent = (SynchronizeComponent) smanager.lookup(SynchronizeComponent.ROLE); 052 } 053 054 /** 055 * Get the default page's parameters 056 * @param page The page 057 * @return The default parameters 058 */ 059 protected Map<String, Object> getPageDefaultParameters (Page page) 060 { 061 Map<String, Object> pageParams = new HashMap<>(); 062 pageParams.put("id", page.getId()); 063 pageParams.put("title", page.getTitle()); 064 065 return pageParams; 066 } 067 068 /** 069 * Determines if user has convenient right on sitemap element 070 * @param sitemapElement The page or sitemap 071 * @return true if the user has convenient right 072 */ 073 protected boolean hasRight (SitemapElement sitemapElement) 074 { 075 if (_rights.isEmpty()) 076 { 077 return true; 078 } 079 080 for (String rightToCheck : _rights.keySet()) 081 { 082 if (StringUtils.isNotEmpty(rightToCheck) 083 && _rightManager.hasRight(_currentUserProvider.getUser(), rightToCheck, sitemapElement) == RightResult.RIGHT_ALLOW) 084 { 085 return true; 086 } 087 } 088 089 return false; 090 } 091 092 /** 093 * Get the description when user has no right on page 094 * @param page the page 095 * @return the description 096 */ 097 protected I18nizableText getNoRightPageDescription (Page page) 098 { 099 List<String> i18nParameters = new ArrayList<>(); 100 i18nParameters.add(page.getTitle()); 101 102 I18nizableText ed = (I18nizableText) this._script.getParameters().get("noright-page-description"); 103 return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters); 104 } 105 106 /** 107 * Get the description when page is not modifiable 108 * @param page the page 109 * @return the description 110 */ 111 protected I18nizableText getNoModifiablePageDescription (Page page) 112 { 113 List<String> i18nParameters = new ArrayList<>(); 114 i18nParameters.add(page.getTitle()); 115 116 I18nizableText ed = (I18nizableText) this._script.getParameters().get("nomodifiable-page-description"); 117 return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters); 118 } 119 120 /** 121 * Get i18n description for all right page 122 * @param page The page 123 * @return The {@link I18nizableText} description 124 */ 125 protected I18nizableText getAllRightPageDescription (Page page) 126 { 127 List<String> i18nParameters = new ArrayList<>(); 128 i18nParameters.add(page.getTitle()); 129 130 I18nizableText ed = (I18nizableText) this._script.getParameters().get("allright-page-description"); 131 return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters); 132 } 133}