001    /*
002     * SonarQube, open source software quality management tool.
003     * Copyright (C) 2008-2013 SonarSource
004     * mailto:contact AT sonarsource DOT com
005     *
006     * SonarQube 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     * SonarQube 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 License
017     * along with this program; if not, write to the Free Software Foundation,
018     * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
019     */
020    package org.sonar.api.rules;
021    
022    import org.sonar.check.Priority;
023    
024    /**
025     * A class to hold rules priority
026     */
027    public enum RulePriority {
028    
029      /**
030       * WARNING : DO NOT CHANGE THE ENUMERATION ORDER
031       * the enum ordinal is used for db persistence
032       */
033      INFO, MINOR, MAJOR, CRITICAL, BLOCKER;
034    
035      private static final String UNKNOWN_PRIORITY = "Unknown priority ";
036    
037    
038      /**
039       * A class to map priority level prior to Sonar 1.10 to the new ones
040       *
041       * @param level an old priority level : Error or Warning
042       * @return the corresponding RulePriority
043       * @deprecated in 3.6
044       */
045      @Deprecated
046      public static RulePriority valueOfString(String level) {
047        try {
048          return RulePriority.valueOf(level.toUpperCase());
049    
050        } catch (IllegalArgumentException ex) {
051          // backward compatibility
052          if ("ERROR".equalsIgnoreCase(level)) {
053            return RulePriority.MAJOR;
054          } else if ("WARNING".equalsIgnoreCase(level)) {
055            return RulePriority.INFO;
056          }
057        }
058        throw new IllegalArgumentException(UNKNOWN_PRIORITY + level);
059      }
060    
061    
062      public static RulePriority fromCheckPriority(Priority checkPriority) {
063        if (checkPriority == Priority.BLOCKER) {
064          return RulePriority.BLOCKER;
065        }
066        if (checkPriority == Priority.CRITICAL) {
067          return RulePriority.CRITICAL;
068        }
069        if (checkPriority == Priority.MAJOR) {
070          return RulePriority.MAJOR;
071        }
072        if (checkPriority == Priority.MINOR) {
073          return RulePriority.MINOR;
074        }
075        if (checkPriority == Priority.INFO) {
076          return RulePriority.INFO;
077        }
078        throw new IllegalArgumentException(UNKNOWN_PRIORITY + checkPriority);
079      }
080    }