001/* 002 * Copyright 2010 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.cms.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; 025 026import org.ametys.cms.repository.Content; 027import org.ametys.core.right.RightManager.RightResult; 028import org.ametys.core.ui.Callable; 029import org.ametys.core.ui.StaticClientSideElement; 030import org.ametys.core.user.CurrentUserProvider; 031import org.ametys.core.user.User; 032import org.ametys.core.user.UserIdentity; 033import org.ametys.core.user.UserManager; 034import org.ametys.plugins.repository.AmetysObjectResolver; 035import org.ametys.plugins.repository.lock.LockHelper; 036import org.ametys.plugins.repository.lock.LockableAmetysObject; 037import org.ametys.runtime.i18n.I18nizableText; 038 039/** 040 * This element creates a toggle button representing the lock state. 041 */ 042public class LockedContentClientSideElement extends StaticClientSideElement 043{ 044 /** Runtime users manager */ 045 protected UserManager _userManager; 046 /** Repository content */ 047 protected AmetysObjectResolver _resolver; 048 049 @Override 050 public void service(ServiceManager smanager) throws ServiceException 051 { 052 super.service(smanager); 053 _userManager = (UserManager) smanager.lookup(UserManager.ROLE); 054 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 055 _currentUserProvider = (CurrentUserProvider) smanager.lookup(CurrentUserProvider.ROLE); 056 } 057 058 /** 059 * Get the lock state of given contents 060 * @param contentsId The ids to contents to test 061 * @return the lock state 062 */ 063 @Callable 064 public Map<String, Object> getLockState(List<String> contentsId) 065 { 066 Map<String, Object> results = new HashMap<>(); 067 068 results.put("locked-contents", new ArrayList<Map<String, Object>>()); 069 results.put("unlocked-contents", new ArrayList<Map<String, Object>>()); 070 results.put("locked-owner-contents", new ArrayList<Map<String, Object>>()); 071 results.put("locked-notowner-contents", new ArrayList<Map<String, Object>>()); 072 073 for (String contentId : contentsId) 074 { 075 Content content = _resolver.resolveById(contentId); 076 077 if (content instanceof LockableAmetysObject) 078 { 079 LockableAmetysObject lockableContent = (LockableAmetysObject) content; 080 boolean canUnlockAll = _rightManager.hasRight(_currentUserProvider.getUser(), "CMS_Rights_UnlockAll", "/cms") == RightResult.RIGHT_ALLOW; 081 082 if (!lockableContent.isLocked()) 083 { 084 List<String> unlockI18nParameters = new ArrayList<>(); 085 unlockI18nParameters.add(content.getTitle()); 086 087 I18nizableText ed = (I18nizableText) this._script.getParameters().get("unlocked-content-description"); 088 I18nizableText msg = new I18nizableText(ed.getCatalogue(), ed.getKey(), unlockI18nParameters); 089 090 Map<String, Object> contentParams = getContentDefaultParameters (content); 091 contentParams.put("description", msg); 092 093 @SuppressWarnings("unchecked") 094 List<Map<String, Object>> unlockContents = (List<Map<String, Object>>) results.get("unlocked-contents"); 095 unlockContents.add(contentParams); 096 } 097 else if (LockHelper.isLockOwner(lockableContent, _currentUserProvider.getUser()) || canUnlockAll) 098 { 099 List<String> lockI18nParameters = new ArrayList<>(); 100 UserIdentity lockOwner = lockableContent.getLockOwner(); 101 User currentLocker = lockOwner != null ? _userManager.getUser(lockOwner.getPopulationId(), lockOwner.getLogin()) : null; 102 lockI18nParameters.add(content.getTitle()); 103 lockI18nParameters.add(currentLocker != null ? currentLocker.getFullName() : ""); 104 lockI18nParameters.add(lockOwner != null ? lockOwner.getLogin() : "Anonymous"); 105 106 I18nizableText ed = (I18nizableText) this._script.getParameters().get("locked-owner-content-description"); 107 I18nizableText msg = new I18nizableText(ed.getCatalogue(), ed.getKey(), lockI18nParameters); 108 109 Map<String, Object> contentParams = getContentDefaultParameters (content); 110 contentParams.put("description", msg); 111 112 if (!_currentUserProvider.getUser().equals(lockableContent.getLockOwner())) 113 { 114 @SuppressWarnings("unchecked") 115 List<Map<String, Object>> notOwnerlockedContents = (List<Map<String, Object>>) results.get("locked-notowner-contents"); 116 notOwnerlockedContents.add(contentParams); 117 } 118 119 @SuppressWarnings("unchecked") 120 List<Map<String, Object>> ownerlockedContents = (List<Map<String, Object>>) results.get("locked-owner-contents"); 121 ownerlockedContents.add(contentParams); 122 } 123 else 124 { 125 UserIdentity currentLockerIdentity = lockableContent.getLockOwner(); 126 User currentLocker = _userManager.getUser(currentLockerIdentity.getPopulationId(), currentLockerIdentity.getLogin()); 127 List<String> lockI18nParameters = new ArrayList<>(); 128 lockI18nParameters.add(content.getTitle()); 129 lockI18nParameters.add(currentLocker != null ? currentLocker.getFullName() : ""); 130 lockI18nParameters.add(currentLockerIdentity.getLogin()); 131 132 I18nizableText ed = (I18nizableText) this._script.getParameters().get("locked-content-description"); 133 I18nizableText msg = new I18nizableText(ed.getCatalogue(), ed.getKey(), lockI18nParameters); 134 135 Map<String, Object> contentParams = getContentDefaultParameters (content); 136 contentParams.put("description", msg); 137 138 @SuppressWarnings("unchecked") 139 List<Map<String, Object>> lockedContents = (List<Map<String, Object>>) results.get("locked-contents"); 140 lockedContents.add(contentParams); 141 } 142 } 143 } 144 145 return results; 146 } 147 148 /** 149 * Get the default content's parameters 150 * @param content The content 151 * @return The default parameters 152 */ 153 protected Map<String, Object> getContentDefaultParameters (Content content) 154 { 155 Map<String, Object> contentParams = new HashMap<>(); 156 contentParams.put("id", content.getId()); 157 contentParams.put("title", content.getTitle()); 158 159 return contentParams; 160 } 161 162 /** 163 * Get the context to check rights 164 * @param content The content 165 * @return the right context 166 */ 167 protected String getRightContext (Content content) 168 { 169 return "/cms"; 170 } 171}