001/* 002 * Copyright 2019 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.odf.clientsideelement; 017 018import java.time.LocalDate; 019import java.util.List; 020 021import org.apache.avalon.framework.service.ServiceException; 022import org.apache.avalon.framework.service.ServiceManager; 023import org.apache.commons.lang3.StringUtils; 024 025import org.ametys.cms.repository.Content; 026import org.ametys.cms.repository.ModifiableContent; 027import org.ametys.core.right.RightManager; 028import org.ametys.core.right.RightManager.RightResult; 029import org.ametys.core.ui.Callable; 030import org.ametys.core.user.UserIdentity; 031import org.ametys.odf.course.ShareableCourseStatusHelper; 032import org.ametys.odf.course.ShareableCourseStatusHelper.ShareableStatus; 033 034/** 035 * Client side element for shareable course status button 036 */ 037public class ShareableCourseStatusButtonClientSideElement extends ShareableCourseContentClientSideElement 038{ 039 /** The shareable course status helper */ 040 protected ShareableCourseStatusHelper _shareableCourseStatusHelper; 041 042 @Override 043 public void service(ServiceManager manager) throws ServiceException 044 { 045 super.service(manager); 046 _shareableCourseStatusHelper = (ShareableCourseStatusHelper) manager.lookup(ShareableCourseStatusHelper.ROLE); 047 _rightManager = (RightManager) manager.lookup(RightManager.ROLE); 048 } 049 050 /** 051 * True if the button is enable 052 * @param buttonShareableStatus the button status 053 * @param contentShareableStatus the content status 054 * @return true if the button is enable 055 */ 056 public boolean canChangeStatus(ShareableStatus buttonShareableStatus, ShareableStatus contentShareableStatus) 057 { 058 switch (buttonShareableStatus) 059 { 060 case NONE: 061 return contentShareableStatus != ShareableStatus.NONE && contentShareableStatus != ShareableStatus.REFUSED; 062 case PROPOSED: 063 return contentShareableStatus == ShareableStatus.NONE || contentShareableStatus == ShareableStatus.REFUSED; 064 case VALIDATED: 065 return contentShareableStatus != ShareableStatus.VALIDATED; 066 default: 067 break; 068 } 069 070 return false; 071 } 072 073 /** 074 * Set the shareable course status to the contents 075 * @param contentIds the list of content ids 076 * @param status the shareable course status 077 * @param comment the comment. Can be null 078 */ 079 @Callable (rights = Callable.CHECKED_BY_IMPLEMENTATION) 080 public void setShareableCourseStatus(List<String> contentIds, String status, String comment) 081 { 082 for (String contentId : contentIds) 083 { 084 ShareableStatus buttonShareableStatus = StringUtils.isNotBlank(status) ? ShareableStatus.valueOf(status.toUpperCase()) : ShareableStatus.NONE; 085 086 ModifiableContent content = _resolver.resolveById(contentId); 087 ShareableStatus contentShareableStatus = _shareableCourseStatusHelper.getShareableStatus(content); 088 089 if (canChangeStatus(buttonShareableStatus, contentShareableStatus) && _checkUserRight(buttonShareableStatus, content)) 090 { 091 UserIdentity user = _currentUserProvider.getUser(); 092 _shareableCourseStatusHelper.setWorkflowStateAttribute(content, LocalDate.now(), user, buttonShareableStatus, comment, false); 093 } 094 } 095 } 096 097 private boolean _checkUserRight(ShareableStatus status, Content content) 098 { 099 switch (status) 100 { 101 case PROPOSED: 102 return _rightManager.currentUserHasRight("ODF_Rights_Shareable_Course_Propose", content) == RightResult.RIGHT_ALLOW; 103 case VALIDATED: 104 return _rightManager.currentUserHasRight("ODF_Rights_Shareable_Course_Validate", content) == RightResult.RIGHT_ALLOW; 105 case NONE: 106 return _rightManager.currentUserHasRight("ODF_Rights_Shareable_Course_Refuse", content) == RightResult.RIGHT_ALLOW; 107 default: 108 break; 109 } 110 111 return false; 112 } 113}