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