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)
089        {
090            WebContent webContent = (WebContent) content;
091            Site site = webContent.getSite();
092            properties.put("isWebContent", true);
093            properties.put("siteName", webContent.getSiteName());
094            properties.put("siteTitle", site.getTitle());
095
096            boolean canChangePrivacy = _rightManager.hasRight(_currentUserProvider.getUser(), "WEB_Rights_Content_ChangePrivacy", content) == RightResult.RIGHT_ALLOW;
097            properties.put("canChangePrivacy", canChangePrivacy);
098
099            String privacy = webContent.getMetadataHolder().getString("privacy", null);
100            if (privacy == null)
101            {
102                privacy = site.getValue("content-privacy", true, null);
103            }
104            properties.put("privacy", privacy);
105        }
106        
107        if (content instanceof SharedContent)
108        {
109            Content initialContent = ((SharedContent) content).getInitialContent();
110            
111            properties.put("isShared", true);
112            if (initialContent != null)
113            {
114                properties.put("initialContentId", initialContent.getId());
115            }
116        }
117
118        properties.put("hasShares", _sharedContentManager.hasSharedContents(content));
119        properties.put("hasReferencingPages", this._hasReferencingPages(content));
120        
121        return properties;
122    }
123    
124    private boolean _hasReferencingPages(Content content)
125    {
126        return content instanceof WebContent && !((WebContent) content).getReferencingPages().isEmpty();
127    }
128    @Override
129    protected boolean _isContentReferenced(Content content)
130    {
131        if (_hasReferencingPages(content))
132        {
133            return true;
134        }
135        
136        Collection<Content> referencingContents = content.getReferencingContents();
137        for (Content refContent : referencingContents)
138        {
139            // Ignore shared contents
140            if (!(refContent instanceof SharedContent))
141            {
142                return true;
143            }
144        }
145        return false;
146    }
147    
148    @Override
149    protected Map<String, Object> _getEventParametersForDeletion(Content content)
150    {
151        Map<String, Object> eventParams = super._getEventParametersForDeletion(content);
152        if (content instanceof WebContent)
153        {
154            eventParams.put(ObservationConstants.ARGS_SITE_NAME, ((WebContent) content).getSiteName());
155        }
156        return eventParams;
157    }
158}