001/*
002 *  Copyright 2011 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.HashMap;
019import java.util.List;
020import java.util.Map;
021
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024
025import org.ametys.core.observation.Event;
026import org.ametys.core.observation.ObservationManager;
027import org.ametys.core.ui.Callable;
028import org.ametys.core.ui.SimpleMenu;
029import org.ametys.core.user.UserManager;
030import org.ametys.plugins.repository.AmetysObjectResolver;
031import org.ametys.web.ObservationConstants;
032import org.ametys.web.repository.content.ModifiableWebContent;
033
034/**
035 * This element represents the privacy level of contents
036 */
037public class ContentPrivacyMenu extends SimpleMenu
038{
039    /** Runtime users manager */
040    protected UserManager _userManager;
041    /** Repository content */
042    protected AmetysObjectResolver _resolver;
043    private ObservationManager _observationManager;
044    
045    @Override
046    public void service(ServiceManager smanager) throws ServiceException
047    {
048        super.service(smanager);
049        _userManager = (UserManager) smanager.lookup(UserManager.ROLE);
050        _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE);
051        _observationManager = (ObservationManager) smanager.lookup(ObservationManager.ROLE);
052    }
053    
054    /**
055     * Update the privacy level of a list of contents
056     * @param contentsId The ids of the contents
057     * @param level The level of privacy
058     */
059    @Callable
060    public void updatePrivacy(List<String> contentsId, String level)
061    {
062        for (String contentId : contentsId)
063        {
064            ModifiableWebContent content = _resolver.resolveById(contentId);
065            content.getMetadataHolder().setMetadata("privacy", level);
066            content.saveChanges();
067            
068            // Notify observers that the tag has been added.
069            
070            Map<String, Object> eventParams = new HashMap<>();
071            eventParams.put(org.ametys.cms.ObservationConstants.ARGS_CONTENT, content);
072            eventParams.put("level", level);
073            _observationManager.notify(new Event(ObservationConstants.EVENT_CONTENT_PRIVACY_CHANGED, _currentUserProvider.getUser(), eventParams));
074        }
075    }
076}