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