001/*
002 *  Copyright 2017 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.plugins.workspaces.documents.actions;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import org.apache.avalon.framework.parameters.Parameters;
022import org.apache.avalon.framework.service.ServiceException;
023import org.apache.avalon.framework.service.ServiceManager;
024import org.apache.cocoon.acting.ServiceableAction;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.environment.Redirector;
027import org.apache.cocoon.environment.Request;
028import org.apache.cocoon.environment.SourceResolver;
029
030import org.ametys.core.util.I18nUtils;
031import org.ametys.core.util.URIUtils;
032import org.ametys.plugins.explorer.resources.ModifiableResourceCollection;
033import org.ametys.plugins.workspaces.documents.DocumentWorkspaceModule;
034import org.ametys.plugins.workspaces.project.ProjectManager;
035import org.ametys.plugins.workspaces.project.modules.WorkspaceModuleExtensionPoint;
036import org.ametys.plugins.workspaces.project.objects.Project;
037import org.ametys.runtime.i18n.I18nizableText;
038
039/**
040 * Return the necessary URI to create an archive of the documents module
041 */
042public class DocumentArchiveUriAction extends ServiceableAction
043{
044    private WorkspaceModuleExtensionPoint _moduleEP;
045    private ProjectManager _projectManager;
046    private I18nUtils _i18nUtils;
047    
048    @Override
049    public void service(ServiceManager smanager) throws ServiceException
050    {
051        super.service(smanager);
052        _projectManager = (ProjectManager) smanager.lookup(ProjectManager.ROLE);
053        _moduleEP = (WorkspaceModuleExtensionPoint) smanager.lookup(WorkspaceModuleExtensionPoint.ROLE);
054        _i18nUtils = (I18nUtils) smanager.lookup(I18nUtils.ROLE);
055    }
056
057    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String source, Parameters parameters) throws Exception
058    {
059        Request request = ObjectModelHelper.getRequest(objectModel);
060        
061        String projectName = request.getParameter("projectName");
062        Project project = _projectManager.getProject(projectName);
063        DocumentWorkspaceModule module = _moduleEP.getModule(DocumentWorkspaceModule.DOCUMENT_MODULE_ID);
064        ModifiableResourceCollection moduleRoot = module.getModuleRoot(project, false);
065        
066        String uri = "cocoon://_plugins/explorer/folder/archive.zip";
067        Map<String, String> params = new HashMap<>();
068        params.put("id", moduleRoot.getId());
069        params.put("name", project.getTitle() + " - " + _i18nUtils.translate(new I18nizableText("plugin.workspaces", "PLUGINS_WORKSPACES_PROJECT_SERVICE_MODULE_DOCUMENT_TREE_FOLDERS_TITLE")));
070        Map<String, Object> result = new HashMap<>();
071        result.put("uri", URIUtils.encodeURI(uri, params));
072        return result;
073    }
074
075
076}