View Javadoc
1   package org.apache.maven.plugin.issues;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.commons.collections.bidimap.DualHashBidiMap;
23  import org.apache.maven.plugin.logging.Log;
24  
25  import java.util.ArrayList;
26  import java.util.Iterator;
27  import java.util.List;
28  import java.util.Map;
29  
30  /**
31   * A helper class for generation of reports based on issues.
32   *
33   * @author Dennis Lundberg
34   * @version $Id: IssuesReportHelper.java 1685894 2015-06-16 19:29:09Z khmarbaise $
35   * @since 2.4
36   */
37  public class IssuesReportHelper
38  {
39      public static final int COLUMN_ASSIGNEE = 0;
40  
41      public static final int COLUMN_COMPONENT = 1;
42  
43      public static final int COLUMN_CREATED = 2;
44  
45      public static final int COLUMN_FIX_VERSION = 3;
46  
47      public static final int COLUMN_ID = 4;
48  
49      public static final int COLUMN_KEY = 5;
50  
51      public static final int COLUMN_PRIORITY = 6;
52  
53      public static final int COLUMN_REPORTER = 7;
54  
55      public static final int COLUMN_RESOLUTION = 8;
56  
57      public static final int COLUMN_STATUS = 9;
58  
59      public static final int COLUMN_SUMMARY = 10;
60  
61      public static final int COLUMN_TYPE = 11;
62  
63      public static final int COLUMN_UPDATED = 12;
64  
65      public static final int COLUMN_VERSION = 13;
66  
67      /**
68       * Get a list of id:s for the columns that are to be included in the report.
69       *
70       * @param columnNames The names of the columns
71       * @param allColumns A mapping from column name to column id
72       * @return A List of column id:s
73       */
74      public static List<Integer> getColumnIds( String columnNames, Map<String, Integer> allColumns )
75      {
76          return getColumnIds( columnNames, allColumns, null, null );
77      }
78  
79      /**
80       * Get a list of id:s for the columns that are to be included in the report. This method also handles deprecated
81       * column names, which will still work. If deprecated column names are used they generate a warning, indicating the
82       * replacement column name.
83       *
84       * @param columnNames The names of the columns
85       * @param allColumns A mapping from column name to column id
86       * @param deprecatedColumns A mapping from deprecated column name to column id
87       * @param log A log
88       * @return A List of column id:s
89       */
90      public static List<Integer> getColumnIds( String columnNames, Map<String, Integer> allColumns,
91                                                Map<String, Integer> deprecatedColumns, Log log )
92      {
93          DualHashBidiMap bidiColumns = null;
94          List<Integer> columnIds = new ArrayList<Integer>();
95          String[] columnNamesArray = columnNames.split( "," );
96  
97          if ( deprecatedColumns != null )
98          {
99              bidiColumns = new DualHashBidiMap( allColumns );
100         }
101 
102         // Loop through the names of the columns, to validate each of them and add their id to the list
103         for ( String aColumnNamesArray : columnNamesArray )
104         {
105             String columnName = aColumnNamesArray.trim();
106             if ( allColumns.containsKey( columnName ) )
107             {
108                 columnIds.add( allColumns.get( columnName ) );
109             }
110             else if ( deprecatedColumns != null && deprecatedColumns.containsKey( columnName ) )
111             {
112                 Integer columnId = deprecatedColumns.get( columnName );
113                 columnIds.add( columnId );
114                 if ( log != null )
115                 {
116                     log.warn( "The columnName '" + columnName + "' has been deprecated." + " Please use "
117                         + "the columnName '" + bidiColumns.getKey( columnId ) + "' instead." );
118                 }
119             }
120         }
121         return columnIds;
122     }
123 
124     /**
125      * Print a list of values separated by commas.
126      *
127      * @param values The values to print
128      * @return A nicely formatted string of values.
129      */
130     public static String printValues( List<String> values )
131     {
132         StringBuilder sb = new StringBuilder();
133         if ( values != null )
134         {
135             Iterator<String> iterator = values.iterator();
136             while ( iterator.hasNext() )
137             {
138                 String value = iterator.next();
139                 sb.append( value );
140                 if ( iterator.hasNext() )
141                 {
142                     sb.append( ", " );
143                 }
144             }
145         }
146         return sb.toString();
147     }
148 
149     /**
150      * Convert a List of Integers to an int array.
151      *
152      * @param list The List to convert
153      * @return An in array
154      */
155     public static int[] toIntArray( List<Integer> list )
156     {
157         int[] intArray = new int[list.size()];
158         for ( int j = 0; j < intArray.length; j++ )
159         {
160             intArray[j] = list.get( j );
161         }
162         return intArray;
163     }
164 }