001/*
002 * Sonar, open source software quality management tool.
003 * Copyright (C) 2008-2012 SonarSource
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 */
020package org.sonar.server.plugins;
021
022import org.sonar.updatecenter.common.Plugin;
023import org.sonar.updatecenter.common.Release;
024import org.sonar.updatecenter.common.Version;
025
026public final class PluginUpdate {
027
028  public enum Status {
029    COMPATIBLE, INCOMPATIBLE, REQUIRE_SONAR_UPGRADE
030  }
031
032  private Status status = Status.INCOMPATIBLE;
033  private Release release;
034
035  public Status getStatus() {
036    return status;
037  }
038
039  public boolean isCompatible() {
040    return Status.COMPATIBLE.equals(status);
041  }
042
043  public boolean isIncompatible() {
044    return Status.INCOMPATIBLE.equals(status);
045  }
046
047  public boolean requiresSonarUpgrade() {
048    return Status.REQUIRE_SONAR_UPGRADE.equals(status);
049  }
050
051  public void setStatus(Status status) {
052    this.status = status;
053  }
054
055  public Plugin getPlugin() {
056    return (Plugin)release.getArtifact();
057  }
058
059  public Release getRelease() {
060    return release;
061  }
062
063  public void setRelease(Release release) {
064    this.release = release;
065  }
066
067  public static PluginUpdate createWithStatus(Release pluginRelease, Status status) {
068    PluginUpdate update = new PluginUpdate();
069    update.setRelease(pluginRelease);
070    update.setStatus(status);
071    return update;
072  }
073
074  public static PluginUpdate createForPluginRelease(Release pluginRelease, Version sonarVersion) {
075    PluginUpdate update = new PluginUpdate();
076    update.setRelease(pluginRelease);
077
078    if (pluginRelease.supportSonarVersion(sonarVersion)) {
079      update.setStatus(Status.COMPATIBLE);
080
081    } else {
082      for (Version requiredSonarVersion : pluginRelease.getRequiredSonarVersions()) {
083        if (requiredSonarVersion.compareTo(sonarVersion)>0) {
084          update.setStatus(Status.REQUIRE_SONAR_UPGRADE);
085          break;
086        }
087      }
088    }
089    return update;
090  }
091}