001    /*
002     * Sonar, open source software quality management tool.
003     * Copyright (C) 2009 SonarSource SA
004     * mailto:contact AT sonarsource DOT com
005     *
006     * Sonar is free software; you can redistribute it and/or
007     * modify it under the terms of the GNU Lesser General Public
008     * License as published by the Free Software Foundation; either
009     * version 3 of the License, or (at your option) any later version.
010     *
011     * Sonar 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 GNU
014     * Lesser General Public License for more details.
015     *
016     * You should have received a copy of the GNU Lesser General Public
017     * License along with Sonar; if not, write to the Free Software
018     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
019     */
020    package org.sonar.core.plugin;
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.apache.commons.lang.builder.ToStringBuilder;
024    import org.apache.commons.lang.builder.ToStringStyle;
025    import org.hibernate.annotations.Cascade;
026    import org.sonar.api.database.BaseIdentifiable;
027    
028    import javax.persistence.*;
029    import java.net.URI;
030    import java.util.ArrayList;
031    import java.util.Date;
032    import java.util.List;
033    
034    /**
035     * Installed plugins
036     *
037     * @since 2.2
038     */
039    @Entity
040    @Table(name = "plugins")
041    public class JpaPlugin extends BaseIdentifiable {
042    
043      @Column(name = "plugin_key", updatable = true, nullable = false, length = 100)
044      private String key;
045    
046      @Column(name = "version", updatable = true, nullable = true, length = 100)
047      private String version;
048    
049      @Column(name = "name", updatable = true, nullable = true, length = 100)
050      private String name;
051    
052      @Column(name = "description", updatable = true, nullable = true, length = 3000)
053      private String description;
054    
055      @Column(name = "organization", updatable = true, nullable = true, length = 100)
056      private String organization;
057    
058      @Column(name = "organization_url", updatable = true, nullable = true, length = 500)
059      private String organizationUrl;
060    
061      @Column(name = "license", updatable = true, nullable = true, length = 50)
062      private String license;
063    
064      @Column(name = "installation_date", updatable = true, nullable = true)
065      private Date installationDate;
066    
067      @Column(name = "plugin_class", updatable = true, nullable = true, length = 100)
068      private String pluginClass;
069    
070      @Column(name = "homepage", updatable = true, nullable = true, length = 500)
071      private String homepage;
072    
073      @Column(name = "core", updatable = true, nullable = true)
074      private Boolean core;
075    
076      @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,
077                org.hibernate.annotations.CascadeType.DELETE,
078                org.hibernate.annotations.CascadeType.MERGE,
079                org.hibernate.annotations.CascadeType.PERSIST,
080                org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
081      @OneToMany(mappedBy = "plugin", cascade = {CascadeType.ALL}, fetch = FetchType.EAGER)
082      private List<JpaPluginFile> files = new ArrayList<JpaPluginFile>();
083    
084      public JpaPlugin() {
085      }
086    
087      public JpaPlugin(String pluginKey) {
088        if (StringUtils.isBlank(pluginKey)) {
089          throw new IllegalArgumentException("LocalExtension.pluginKey can not be blank");
090        }
091        this.key = pluginKey;
092      }
093    
094      public String getKey() {
095        return key;
096      }
097    
098      public JpaPlugin setKey(String s) {
099        this.key = s;
100        return this;
101      }
102    
103      public String getName() {
104        return name;
105      }
106    
107      public JpaPlugin setName(String name) {
108        this.name = name;
109        return this;
110      }
111    
112      public String getDescription() {
113        return description;
114      }
115    
116      public JpaPlugin setDescription(String description) {
117        this.description = description;
118        return this;
119      }
120    
121      public String getOrganization() {
122        return organization;
123      }
124    
125      public JpaPlugin setOrganization(String organization) {
126        this.organization = organization;
127        return this;
128      }
129    
130      public String getOrganizationUrl() {
131        return organizationUrl;
132      }
133    
134      public JpaPlugin setOrganizationUrl(URI uri) {
135        this.organizationUrl = (uri != null ? uri.toString() : null);
136        return this;
137      }
138    
139      public JpaPlugin setOrganizationUrl(String s) {
140        this.organizationUrl = s;
141        return this;
142      }
143    
144      public String getLicense() {
145        return license;
146      }
147    
148      public JpaPlugin setLicense(String license) {
149        this.license = license;
150        return this;
151      }
152    
153      public String getVersion() {
154        return version;
155      }
156    
157      public JpaPlugin setVersion(String s) {
158        this.version = s;
159        return this;
160      }
161    
162      public Date getInstallationDate() {
163        return installationDate;
164      }
165    
166      public JpaPlugin setInstallationDate(Date installationDate) {
167        this.installationDate = installationDate;
168        return this;
169      }
170    
171      public String getPluginClass() {
172        return pluginClass;
173      }
174    
175      public JpaPlugin setPluginClass(String s) {
176        this.pluginClass = s;
177        return this;
178      }
179    
180      public String getHomepage() {
181        return homepage;
182      }
183    
184      public JpaPlugin setHomepage(URI uri) {
185        this.homepage = (uri != null ? uri.toString() : null);
186        return this;
187      }
188    
189      public JpaPlugin setHomepage(String s) {
190        this.homepage = s;
191        return this;
192      }
193    
194      public Boolean isCore() {
195        return core;
196      }
197    
198      public JpaPlugin setCore(Boolean b) {
199        this.core = b;
200        return this;
201      }
202    
203      public void createFile(String filename) {
204        JpaPluginFile file = new JpaPluginFile(this, filename);
205        this.files.add(file);
206      }
207    
208      public List<JpaPluginFile> getFiles() {
209        return files;
210      }
211    
212      public void removeFiles() {
213        files.clear();
214      }
215      
216      @Override
217      public boolean equals(Object o) {
218        if (this == o) {
219          return true;
220        }
221        if (o == null || getClass() != o.getClass()) {
222          return false;
223        }
224        JpaPlugin other = (JpaPlugin) o;
225        return key.equals(other.key);
226      }
227    
228      @Override
229      public int hashCode() {
230        return key.hashCode();
231      }
232    
233      @Override
234      public String toString() {
235        return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)
236            .append("id", getId())
237            .append("key", key)
238            .append("version", version)
239            .append("homepage", homepage)
240            .append("installationDate", installationDate)
241            .toString();
242      }
243    
244    
245      public static JpaPlugin create(String pluginKey) {
246        return new JpaPlugin(pluginKey);
247      }
248    
249    }