001/*
002 *  Copyright 2012 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.workflow.archive;
017import java.util.ArrayList;
018import java.util.List;
019
020import org.apache.cocoon.environment.Request;
021import org.apache.commons.lang.StringUtils;
022
023import org.ametys.cms.content.archive.ArchiveConstants;
024import org.ametys.cms.repository.Content;
025import org.ametys.core.user.population.PopulationContextHelper;
026import org.ametys.web.repository.content.WebContent;
027
028/**
029 * Runnable engine that archive the contents that have an scheduled archiving
030 * date set before the current date.
031 */
032public class ArchiveContentsEngine extends org.ametys.cms.workflow.archive.ArchiveContentsEngine
033{
034    @Override
035    protected String getArchiveActionUri(String contentId)
036    {
037        return "cocoon://_plugins/web/archives/archive/" + ArchiveConstants.ARCHIVE_WORKFLOW_ACTION_ID + "?contentId=" + contentId;
038    }
039    
040    @Override
041    protected void setRequestAttributes(Request request, Content content)
042    {
043        if (content instanceof WebContent)
044        {
045            String siteName = ((WebContent) content).getSiteName();
046            request.setAttribute("siteName", siteName);
047            
048            // Set the population contexts to be able to get allowed users
049            List<String> populationContexts = new ArrayList<>();
050            populationContexts.add("/sites/" + siteName);
051            populationContexts.add("/sites-fo/" + siteName);
052            
053            request.setAttribute(PopulationContextHelper.POPULATION_CONTEXTS_REQUEST_ATTR, populationContexts);
054        }
055    }
056    
057    @Override
058    protected List<String> getBodyParamsForContributors (Content content, boolean archived)
059    {
060        List<String> params = super.getBodyParamsForContributors(content, archived);
061        
062        if (content instanceof WebContent)
063        {
064            WebContent webContent = (WebContent) content;
065            params.add(webContent.getSite().getTitle());  // {3}
066        }
067        
068        return params;
069    }
070    
071    @Override
072    protected String _getRequestURI(Content content)
073    {
074        String requestUri = super._getRequestURI(content);
075        if (content != null && content instanceof WebContent)
076        {
077            return requestUri + "/" + ((WebContent) content).getSiteName();
078        }
079        
080        return requestUri;
081    }
082    
083    @Override
084    protected String _getContentsListAsString (List<Content> contents)
085    {
086        List<String> contentNames = new ArrayList<>();
087        for (Content content : contents)
088        {
089            String contentName = "- " + content.getTitle(null);
090            if (content instanceof WebContent)
091            {
092                contentName += " (" + ((WebContent) content).getSiteName() + ")";
093            }
094            contentNames.add(contentName);
095        }
096        
097        if (contentNames.size() > 0)
098        {
099            return StringUtils.join(contentNames, "\n");
100        }
101        else
102        {
103            return "";
104        }
105    }
106    
107}