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