001/*
002 *  Copyright 2016 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.runtime.workspace;
017
018import java.io.File;
019import java.util.Collections;
020import java.util.HashSet;
021import java.util.Objects;
022import java.util.Set;
023
024import org.apache.avalon.framework.configuration.Configuration;
025import org.apache.commons.lang3.StringUtils;
026
027import org.ametys.runtime.servlet.RuntimeConfig;
028
029/**
030 * Object representation of a workspace.xml file
031 */
032public class Workspace
033{
034    private String _name;
035    private Configuration _configuration;
036
037    private String _theme;
038    private String _themeLocation;
039    private String _themeURL;
040    private String _baseURI;
041    private File _location;
042    private Set<String> _tags;
043    
044    Workspace(String name, String baseURI)
045    {
046        _name = name;
047        _baseURI = baseURI;
048    }
049
050    Workspace(String name, File location)
051    {
052        _name = name;
053        _location = location;
054    }
055
056    /**
057     * Returns this workspace's name.
058     * @return this workspace's name.
059     */
060    public String getName()
061    {
062        return _name;
063    }
064    
065    /**
066     * Get the workspace base uri if embeded
067     * @return the workspace base uri. Can be null if external
068     */
069    public String getEmbededLocation()
070    {
071        return _baseURI;
072    }
073    
074    /**
075     * GetThe workspace location if external
076     * @return the workspace location. Can be null if embeded
077     */
078    public File getExternalLocation()
079    {
080        return _location;
081    }
082    
083    /**
084     * Get the name of the theme associated to this workspace 
085     * @return The name such as ametys-base
086     */
087    public String getThemeName()
088    {
089        return _theme;
090    }
091    
092    /**
093     * Get the location of the theme associated to this workspace 
094     * @return The location. Such as plugin:core-ui://resources/themes/theme-ametys-base
095     */
096    public String getThemeLocation()
097    {
098        return _themeLocation;
099    }
100
101    /**
102     * Get the location of the theme associated to this workspace 
103     * @return The relative location. Such as /plugins/core-ui/resources/themes/theme-ametys-base
104     */
105    public String getThemeURL()
106    {
107        return _themeURL;
108    }
109    
110    /**
111     * Get the tags of the workspace
112     * @return The non-null set of tags
113     */
114    public Set<String> getTags()
115    {
116        return _tags;
117    }
118    
119    Configuration getConfiguration()
120    {
121        return _configuration;
122    }
123    
124    void configure(Configuration configuration)
125    {
126        _configuration = configuration;
127        
128        Configuration themeConfiguration = configuration.getChild("theme");
129        
130        _theme = Objects.toString(RuntimeConfig.getInstance().getOverridenThemes().get(this.getName()), themeConfiguration.getAttribute("name", "ametys-base"));
131        _themeLocation = themeConfiguration.getAttribute("location", "plugin:core-ui://resources/themes/theme-" + _theme);
132        _themeURL = themeConfiguration.getAttribute("url", "/plugins/core-ui/resources/themes/theme-" + _theme);
133        
134        Set<String> tags = new HashSet<>();
135        for (Configuration tagConfiguration : configuration.getChild("tags").getChildren("tag"))
136        {
137            String tag = tagConfiguration.getValue("");
138            if (StringUtils.isNotBlank(tag))
139            {
140                tags.add(tag);
141            }
142        }
143        _tags = Collections.unmodifiableSet(tags);
144    }
145}