001/*
002 *  Copyright 2020 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.alert;
017
018import java.util.Collections;
019import java.util.Set;
020
021import org.apache.commons.lang.IllegalClassException;
022import org.apache.commons.lang.StringUtils;
023
024import org.ametys.plugins.explorer.ExplorerNode;
025import org.ametys.plugins.explorer.resources.ModifiableResourceCollection;
026import org.ametys.plugins.explorer.resources.jcr.JCRResourcesCollectionFactory;
027import org.ametys.plugins.repository.AmetysRepositoryException;
028import org.ametys.plugins.workspaces.AbstractWorkspaceModule;
029import org.ametys.plugins.workspaces.project.objects.Project;
030import org.ametys.runtime.i18n.I18nizableText;
031import org.ametys.web.repository.page.ModifiablePage;
032import org.ametys.web.repository.page.Page.PageType;
033import org.ametys.web.repository.sitemap.Sitemap;
034
035/**
036 * Workspaces module for alerts
037 */
038public class AlertWorkspaceModule extends AbstractWorkspaceModule
039{
040    /** The id of alert module */
041    public static final String ALERT_MODULE_ID = AlertWorkspaceModule.class.getName();
042    
043    /** Workspaces alerts node name */
044    private static final String __WORKSPACES_ALERTS_NODE_NAME = "alerts";
045    
046    public String getId()
047    {
048        return ALERT_MODULE_ID;
049    }
050
051    public int getOrder()
052    {
053        return ORDER_ALERTS;
054    }
055
056    public String getModuleName()
057    {
058        return __WORKSPACES_ALERTS_NODE_NAME;
059    }
060
061    public Set<String> getAllowedEventTypes()
062    {
063        return Collections.EMPTY_SET;
064    }
065
066    public ModifiableResourceCollection getModuleRoot(Project project, boolean create)
067    {
068        try
069        {
070            ExplorerNode projectRootNode = project.getExplorerRootNode();
071            
072            if (projectRootNode instanceof ModifiableResourceCollection)
073            {
074                ModifiableResourceCollection projectRootNodeRc = (ModifiableResourceCollection) projectRootNode;
075                return _getAmetysObject(projectRootNodeRc, __WORKSPACES_ALERTS_NODE_NAME, JCRResourcesCollectionFactory.RESOURCESCOLLECTION_NODETYPE, create);
076            }
077            else
078            {
079                throw new IllegalClassException(ModifiableResourceCollection.class, projectRootNode.getClass());
080            }
081        }
082        catch (AmetysRepositoryException e)
083        {
084            throw new AmetysRepositoryException("Error getting the news root node.", e);
085        }
086    }
087
088    @Override
089    protected String getModulePageName()
090    {
091        return "alerts";
092    }
093
094    public I18nizableText getModuleTitle()
095    {
096        return new I18nizableText("plugin." + _pluginName, "PLUGINS_WORKSPACES_PROJECT_MODULE_ALERTS_LABEL");
097    }
098    public I18nizableText getModuleDescription()
099    {
100        return new I18nizableText("plugin." + _pluginName, "PLUGINS_WORKSPACES_PROJECT_MODULE_ALERTS_DESCRIPTION");
101    }
102
103    @Override
104    protected I18nizableText getModulePageTitle()
105    {
106        return new I18nizableText("plugin." + _pluginName, "PLUGINS_WORKSPACES_PROJECT_WORKSPACE_PAGE_ALERTS_TITLE");
107    }
108    
109    @Override
110    protected ModifiablePage _createModulePage(Project project, Sitemap sitemap, String name, I18nizableText pageTitle, String skinTemplate)
111    {
112        if (!sitemap.hasChild(name))
113        {
114            ModifiablePage page = sitemap.createChild(name, "ametys:defaultPage");
115            
116            // Title should not be missing, but just in case if the i18n message or the whole catalog does not exists in the requested language
117            // to prevent a non-user-friendly error and still generate the project workspace.
118            page.setTitle(StringUtils.defaultIfEmpty(_i18nUtils.translate(pageTitle, sitemap.getName()), "Missing title"));
119            page.setType(PageType.NODE);
120            page.setSiteName(sitemap.getSiteName());
121            page.setSitemapName(sitemap.getName());
122            
123            sitemap.saveChanges();
124            
125            return page;
126        }
127        else
128        {
129            return null;
130        }
131    }
132
133    @Override
134    protected void initializeModulePage(ModifiablePage modulePage)
135    {
136        modulePage.untag("SECTION");
137    }
138    
139    @Override
140    protected String getModulePageTemplate() 
141    {
142        return "page";
143    }
144
145    @Override
146    protected boolean _showActivatedStatus()
147    {
148        return false;
149    }
150}