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