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.ModifiableContent;
026import org.ametys.core.ui.Callable;
027import org.ametys.core.user.UserIdentity;
028import org.ametys.odf.course.ShareableCourseStatusHelper;
029import org.ametys.odf.course.ShareableCourseStatusHelper.ShareableStatus;
030
031/**
032 * Client side element for shareable course status button
033 */
034public class ShareableCourseStatusButtonClientSideElement extends ShareableCourseContentClientSideElement
035{
036    /** The shareable course status helper */
037    protected ShareableCourseStatusHelper _shareableCourseStatusHelper; 
038    
039    @Override
040    public void service(ServiceManager manager) throws ServiceException
041    {
042        super.service(manager);
043        _shareableCourseStatusHelper = (ShareableCourseStatusHelper) manager.lookup(ShareableCourseStatusHelper.ROLE);
044    }
045    
046    /**
047     * True if the button is enable
048     * @param buttonShareableStatus the button status
049     * @param contentShareableStatus the content status
050     * @return true if the button is enable
051     */
052    public boolean canChangeStatus(ShareableStatus buttonShareableStatus, ShareableStatus contentShareableStatus)
053    {
054        switch (buttonShareableStatus)
055        {
056            case NONE:
057                return contentShareableStatus == ShareableStatus.PROPOSED;
058            case PROPOSED:
059                return contentShareableStatus == ShareableStatus.NONE;
060            case VALIDATED:
061                return contentShareableStatus != ShareableStatus.VALIDATED;
062            default:
063                break;
064        }
065        
066        return false;
067    }
068    
069    /**
070     * Set the shareable course status to the contents
071     * @param contentIds the list of content ids
072     * @param status the shareable course status
073     */
074    @Callable
075    public void setShareableCourseStatus(List<String> contentIds, String status)
076    {
077        for (String contentId : contentIds)
078        {
079            ShareableStatus buttonShareableStatus = StringUtils.isNotBlank(status) ? ShareableStatus.valueOf(status.toUpperCase()) : ShareableStatus.NONE;
080
081            ModifiableContent content = _resolver.resolveById(contentId);
082            ShareableStatus contentShareableStatus = _shareableCourseStatusHelper.getShareableStatus(content);
083            
084            if (canChangeStatus(buttonShareableStatus, contentShareableStatus))
085            {
086                UserIdentity user = _currentUserProvider.getUser();
087                _shareableCourseStatusHelper.setWorkflowStateAttribute(content, LocalDate.now(), user, buttonShareableStatus, false);
088            }
089        }
090    }
091}