001/**
002 * Copyright (C) 2014  Universidade de Aveiro, DETI/IEETA, Bioinformatics Group - http://bioinformatics.ua.pt/
003 *
004 * This file is part of Dicoogle/dicoogle.
005 *
006 * Dicoogle/dicoogle is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * Dicoogle/dicoogle is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with Dicoogle.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package pt.ua.dicoogle.plugins.webui;
020
021import net.sf.json.JSONArray;
022import net.sf.json.JSONException;
023import net.sf.json.JSONObject;
024import org.slf4j.LoggerFactory;
025import pt.ua.dicoogle.server.users.Role;
026import pt.ua.dicoogle.server.users.RolesStruct;
027
028import java.util.HashSet;
029import java.util.Set;
030
031/** A POJO data type containing the full description of a Web UI plugin.
032 *
033 * @author Eduardo Pinho <eduardopinho@ua.pt>
034 */
035public class WebUIPlugin implements Cloneable {
036    private String name;
037    private String caption;
038    private String description;
039    private String version;
040    private String slotId;
041    private String moduleFile;
042    private JSONObject settings;
043    private boolean enabled = true;
044    private Set<String> roles = new HashSet();
045
046    public WebUIPlugin() {}
047
048    @Override
049    protected WebUIPlugin clone() throws CloneNotSupportedException {
050        return (WebUIPlugin)super.clone();
051    }
052
053    /** Make a copy of the descriptor.
054     * @return a copy of this descriptor
055     * @todo The settings are shallowly copied at the moment. Hopefully this will not be a problem, but
056     * bear this in mind.
057     */
058    public WebUIPlugin copy() {
059        try {
060            return clone();
061        } catch (CloneNotSupportedException ex) {
062            LoggerFactory.getLogger(WebUIPlugin.class).error("Failed to copy web UI plugin descriptor", ex);
063            return new WebUIPlugin();
064        }
065    }
066
067    /** Retrieve a web UI plugin descriptor out of the contents of a "package.json".
068     * 
069     * @param obj a JSON object of the plugin description
070     * @return a web UI plugin descriptor out of the JSON object
071     * @throws PluginFormatException if the JSON text contains bad information, or not enough of it
072     */
073    public static WebUIPlugin fromPackageJSON(JSONObject obj) throws PluginFormatException {
074        try {
075            WebUIPlugin plugin = new WebUIPlugin();
076            plugin.name = obj.getString("name");
077            plugin.version = obj.optString("version", null);
078            plugin.description = obj.optString("description", null);
079
080            JSONObject objDicoogle = obj.getJSONObject("dicoogle");
081
082            plugin.slotId = objDicoogle.getString("slot-id");
083            plugin.moduleFile = objDicoogle.optString("module-file", "module.js");
084            plugin.caption = objDicoogle.optString("caption", null);
085            if (objDicoogle.containsKey("roles"))
086            {
087                JSONArray rolesArr = objDicoogle.getJSONArray("roles");
088
089                Set<String> roles = new HashSet<>();
090                if (rolesArr != null)
091                    for (Object role : rolesArr) {
092
093                        roles.add((String) role);
094                    }
095                plugin.roles = roles;
096            }
097
098            return plugin;
099        } catch(JSONException ex) {
100            throw new PluginFormatException(ex);
101        }
102    }
103
104    public String getName() {
105        return name;
106    }
107
108    public void setName(String name) {
109        this.name = name;
110    }
111
112    public String getVersion() {
113        return version;
114    }
115
116    public void setVersion(String version) {
117        this.version = version;
118    }
119
120    public String getSlotId() {
121        return slotId;
122    }
123
124    public void setSlotId(String slotId) {
125        this.slotId = slotId;
126    }
127
128    public String getModuleFile() {
129        return moduleFile;
130    }
131
132    public void setModuleFile(String moduleFile) {
133        this.moduleFile = moduleFile;
134    }
135
136    public String getDescription() {
137        return description;
138    }
139
140    public void setDescription(String description) {
141        this.description = description;
142    }
143
144    public void setSettings(JSONObject holder) {
145        this.settings = holder;
146    }
147    
148    /** Getter for the plugin's settings.
149     * 
150     * @return a JSON object containing the settings, empty object if no settings are stored
151     */
152    public JSONObject getSettings() {
153        return this.settings != null ? this.settings : new JSONObject(false);
154    }
155
156    public boolean isEnabled() {
157        return enabled;
158    }
159
160    public void setEnabled(boolean enabled) {
161        this.enabled = enabled;
162    }
163        
164    @Override
165    public String toString() {
166        return "WebUIPlugin{" + "name=" + name + ", description=" + description
167                + ", version=" + version + ", slotId=" + slotId
168                + ", moduleFile=" + moduleFile + ", enabled=" + enabled + '}';
169    }
170
171    public String getCaption() {
172        return caption;
173    }
174
175    public void setCaption(String caption) {
176        this.caption = caption;
177    }
178
179    public Set<String> getRoles() {
180        return roles;
181    }
182}