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