001/*
002 *  Copyright 2015 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.repository.content;
017
018import java.util.Collection;
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.cms.repository.Content;
026import org.ametys.cms.repository.ContentDAO;
027import org.ametys.core.right.RightManager.RightResult;
028import org.ametys.web.ObservationConstants;
029import org.ametys.web.repository.content.shared.SharedContentManager;
030import org.ametys.web.repository.page.Page;
031import org.ametys.web.repository.site.Site;
032
033/**
034 * DAO for manipulating web contents
035 *
036 */
037public class WebContentDAO extends ContentDAO
038{
039    /** Avalon Role */
040    @SuppressWarnings("hiding")
041    public static final String ROLE = WebContentDAO.class.getName();
042    
043    /** The manager for shared contents */
044    protected SharedContentManager _sharedContentManager;
045    
046    @Override
047    public void service(ServiceManager smanager) throws ServiceException
048    {
049        super.service(smanager);
050        _sharedContentManager = (SharedContentManager) smanager.lookup(SharedContentManager.ROLE);
051    }
052    
053    /**
054     * Returns the referencing pages of a content
055     * @param contentId The content's id 
056     * @return The list of referencing pages
057     */
058    public Collection<Page> getReferencingPages (String contentId)
059    {
060        Content content = _resolver.resolveById(contentId);
061        if (content instanceof WebContent webContent)
062        {
063            return webContent.getReferencingPages();
064        }
065        
066        return List.of();
067    }
068    
069    @Override
070    public Map<String, Object> getContentProperties(Content content)
071    {
072        Map<String, Object> properties = super.getContentProperties(content);
073        
074        if (content instanceof WebContent webContent)
075        {
076            Site site = webContent.getSite();
077            properties.put("isWebContent", true);
078            properties.put("siteName", webContent.getSiteName());
079            properties.put("siteTitle", site.getTitle());
080
081            boolean canChangePrivacy = _rightManager.hasRight(_currentUserProvider.getUser(), "WEB_Rights_Content_ChangePrivacy", content) == RightResult.RIGHT_ALLOW;
082            properties.put("canChangePrivacy", canChangePrivacy);
083
084            properties.put("privacy", webContent.getPrivacyLevel());
085        }
086        
087        if (content instanceof SharedContent)
088        {
089            Content initialContent = ((SharedContent) content).getInitialContent();
090            
091            properties.put("isShared", true);
092            if (initialContent != null)
093            {
094                properties.put("initialContentId", initialContent.getId());
095            }
096        }
097
098        properties.put("hasShares", _sharedContentManager.hasSharedContents(content));
099        properties.put("hasReferencingPages", this._hasReferencingPages(content));
100        
101        return properties;
102    }
103    
104    private boolean _hasReferencingPages(Content content)
105    {
106        return content instanceof WebContent && !((WebContent) content).getReferencingPages().isEmpty();
107    }
108    @Override
109    protected boolean _isContentReferenced(Content content)
110    {
111        if (_hasReferencingPages(content))
112        {
113            return true;
114        }
115        
116        Collection<Content> referencingContents = content.getReferencingContents();
117        for (Content refContent : referencingContents)
118        {
119            // Ignore shared contents
120            if (!(refContent instanceof SharedContent))
121            {
122                return true;
123            }
124        }
125        return false;
126    }
127    
128    @Override
129    protected Map<String, Object> _getEventParametersForDeletion(Content content)
130    {
131        Map<String, Object> eventParams = super._getEventParametersForDeletion(content);
132        if (content instanceof WebContent)
133        {
134            eventParams.put(ObservationConstants.ARGS_SITE_NAME, ((WebContent) content).getSiteName());
135        }
136        return eventParams;
137    }
138}