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 1098344 2011-05-01 14:49:27Z dennisl $
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.
81       * This method also handles deprecated column names, which will still work.
82       * If deprecated column names are used they generate a warning, indicating
83       * the replacement column name.
84       *
85       * @param columnNames The names of the columns
86       * @param allColumns A mapping from column name to column id
87       * @param deprecatedColumns A mapping from deprecated column name to column id
88       * @param log A log
89       * @return A List of column id:s
90       */
91      public static List<Integer> getColumnIds( String columnNames, Map<String,Integer> allColumns,
92                                                Map<String,Integer> deprecatedColumns, Log log )
93      {
94          DualHashBidiMap bidiColumns = null;
95          List<Integer> columnIds = new ArrayList<Integer>();
96          String[] columnNamesArray = columnNames.split( "," );
97  
98          if ( deprecatedColumns != null )
99          {
100             bidiColumns = new DualHashBidiMap( allColumns );
101         }
102 
103         // Loop through the names of the columns, to validate each of them and add their id to the list
104         for ( int i = 0; i < columnNamesArray.length; i++ )
105         {
106             String columnName = columnNamesArray[i].trim();
107             if ( allColumns.containsKey( columnName ) )
108             {
109                 columnIds.add( allColumns.get( columnName ) );
110             }
111             else if ( deprecatedColumns != null && deprecatedColumns.containsKey( columnName ) )
112             {
113                 Integer columnId = deprecatedColumns.get( columnName );
114                 columnIds.add( columnId );
115                 if ( log != null )
116                 {
117                     log.warn( "The columnName '" + columnName + "' has been deprecated." + " Please use "
118                         + "the columnName '" + bidiColumns.getKey( columnId ) + "' instead." );
119                 }
120             }
121         }
122         return columnIds;
123     }
124 
125     /**
126      * Print a list of values separated by commas.
127      *
128      * @param values The values to print
129      * @return A nicely formatted string of values.
130      */
131     public static String printValues( List<String> values )
132     {
133         StringBuffer sb = new StringBuffer();
134         if ( values != null )
135         {
136             Iterator<String> iterator = values.iterator();
137             while ( iterator.hasNext() )
138             {
139                 String value = iterator.next();
140                 sb.append( value );
141                 if ( iterator.hasNext() )
142                 {
143                     sb.append( ", " );
144                 }
145             }
146         }
147         return sb.toString();
148     }
149 
150     /**
151      * Convert a List of Integers to an int array.
152      *
153      * @param list The List to convert
154      * @return An in array
155      */
156     public static int[] toIntArray( List<Integer> list )
157     {
158         int[] intArray = new int[list.size()];
159         for ( int j = 0; j < intArray.length; j++ )
160         {
161             intArray[j] = ( list.get( j ) ).intValue();
162         }
163         return intArray;
164     }
165 }