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