Coverage Report - org.apache.maven.plugin.jira.ParameterQueryBuilder
 
Classes in this File Line Coverage Branch Coverage Complexity
ParameterQueryBuilder
0%
0/138
0%
0/80
4,231
 
 1  
 package org.apache.maven.plugin.jira;
 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 java.util.HashMap;
 23  
 import java.util.Locale;
 24  
 import java.util.Map;
 25  
 
 26  
 import org.apache.maven.plugin.logging.Log;
 27  
 
 28  
 /**
 29  
  * JIRA 3.x way of constructing a search query based on URL parameters.
 30  
  * 
 31  
  * @author ton.swieb@finalist.com
 32  
  * @version $Id: ParameterQueryBuilder.java 1380895 2012-09-04 21:25:44Z dennisl $
 33  
  * @since 2.8
 34  
  */
 35  
 public class ParameterQueryBuilder
 36  
     implements JiraQueryBuilder
 37  
 {
 38  0
     private String filter = "";
 39  
     /** Log for debug output. */
 40  
     private Log log;
 41  0
     private StringBuilder query = new StringBuilder();
 42  
 
 43  
     /** Mapping containing all allowed JIRA priority values. */
 44  0
     private final Map<String,String> priorityMap = new HashMap<String,String>( 8 );
 45  
     /** Mapping containing all allowed JIRA resolution values. */
 46  0
     private final Map<String,String> resolutionMap = new HashMap<String,String>( 8 );
 47  
     /** Mapping containing all allowed JIRA status values. */
 48  0
     private final Map<String,String> statusMap = new HashMap<String,String>( 8 );
 49  
     /** Mapping containing all allowed JIRA type values. */
 50  0
     private final Map<String,String> typeMap = new HashMap<String,String>( 8 );
 51  
 
 52  
     public ParameterQueryBuilder( Log log )
 53  0
     {
 54  0
         this.log = log;
 55  
 
 56  0
         priorityMap.put( "Blocker", "1" );
 57  0
         priorityMap.put( "Critical", "2" );
 58  0
         priorityMap.put( "Major", "3" );
 59  0
         priorityMap.put( "Minor", "4" );
 60  0
         priorityMap.put( "Trivial", "5" );
 61  
 
 62  0
         resolutionMap.put( "Unresolved", "-1" );
 63  0
         resolutionMap.put( "Fixed", "1" );
 64  0
         resolutionMap.put( "Won't Fix", "2" );
 65  0
         resolutionMap.put( "Duplicate", "3" );
 66  0
         resolutionMap.put( "Incomplete", "4" );
 67  0
         resolutionMap.put( "Cannot Reproduce", "5" );
 68  
 
 69  0
         statusMap.put( "Open", "1" );
 70  0
         statusMap.put( "In Progress", "3" );
 71  0
         statusMap.put( "Reopened", "4" );
 72  0
         statusMap.put( "Resolved", "5" );
 73  0
         statusMap.put( "Closed", "6" );
 74  
 
 75  0
         typeMap.put( "Bug", "1" );
 76  0
         typeMap.put( "New Feature", "2" );
 77  0
         typeMap.put( "Task", "3" );
 78  0
         typeMap.put( "Improvement", "4" );
 79  0
         typeMap.put( "Wish", "5" );
 80  0
         typeMap.put( "Test", "6" );
 81  0
         typeMap.put( "Sub-task", "7" );
 82  0
     }
 83  
 
 84  
     public String build()
 85  
     {
 86  
         // If the user has defined a filter - use that
 87  0
         if ( ( this.filter != null ) && ( this.filter.length() > 0 ) )
 88  
         {
 89  0
             return this.filter;
 90  
         }
 91  
         else
 92  
         {
 93  0
             return query.toString();
 94  
         }
 95  
     }
 96  
 
 97  
     public JiraQueryBuilder components( String components )
 98  
     {
 99  
         // add components
 100  0
         if ( components != null )
 101  
         {
 102  0
             String[] componentsArr = components.split( "," );
 103  
 
 104  0
             for ( String component : componentsArr )
 105  
             {
 106  0
                 component = component.trim();
 107  0
                 if ( component.length() > 0 )
 108  
                 {
 109  0
                     query.append( "&component=" ).append( component );
 110  
                 }
 111  
             }
 112  
         }
 113  0
         return this;
 114  
     }
 115  
 
 116  
     public JiraQueryBuilder filter( String filter )
 117  
     {
 118  0
         this.filter = filter;
 119  0
         return this;
 120  
     }
 121  
 
 122  
     /**
 123  
      * This method has no effect in this implementation.
 124  
      */
 125  
     public JiraQueryBuilder fixVersion( String fixVersion )
 126  
     {
 127  0
         return this;
 128  
     }
 129  
 
 130  
     public JiraQueryBuilder fixVersionIds( String fixVersionIds )
 131  
     {
 132  
         // add fix versions
 133  0
         if ( fixVersionIds != null )
 134  
         {
 135  0
             String[] fixVersions = fixVersionIds.split( "," );
 136  
 
 137  0
             for ( int i = 0; i < fixVersions.length; i++ )
 138  
             {
 139  0
                 if ( fixVersions[i].length() > 0 )
 140  
                 {
 141  0
                     query.append( "&fixfor=" ).append( fixVersions[i].trim() );
 142  
                 }
 143  
             }
 144  
         }
 145  0
         return this;
 146  
     }
 147  
 
 148  
     public Log getLog()
 149  
     {
 150  0
         return log;
 151  
     }
 152  
 
 153  
     public JiraQueryBuilder priorityIds( String priorityIds )
 154  
     {
 155  
         // get the Priority Ids
 156  0
         if ( priorityIds != null )
 157  
         {
 158  0
             String[] prios = priorityIds.split( "," );
 159  
 
 160  0
             for ( String prio : prios )
 161  
             {
 162  0
                 prio = prio.trim();
 163  0
                 String priorityParam = priorityMap.get( prio );
 164  
 
 165  0
                 if ( priorityParam != null )
 166  
                 {
 167  0
                     query.append( "&priorityIds=" ).append( priorityParam );
 168  
                 }
 169  
             }
 170  
         }
 171  0
         return this;
 172  
     }
 173  
 
 174  
     /**
 175  
      * This method has no effect in this implementation.
 176  
      */
 177  
     public JiraQueryBuilder project( String project )
 178  
     {
 179  0
         return this;
 180  
     }
 181  
 
 182  
     public JiraQueryBuilder resolutionIds( String resolutionIds )
 183  
     {
 184  
         // get the Resolution Ids
 185  0
         if ( resolutionIds != null )
 186  
         {
 187  0
             String[] resos = resolutionIds.split( "," );
 188  
 
 189  0
             for ( String reso : resos )
 190  
             {
 191  0
                 reso = reso.trim();
 192  0
                 String resoParam = resolutionMap.get( reso );
 193  
 
 194  0
                 if ( resoParam != null )
 195  
                 {
 196  0
                     query.append( "&resolutionIds=" ).append( resoParam );
 197  
                 }
 198  
             }
 199  
         }
 200  0
         return this;
 201  
     }
 202  
 
 203  
     public JiraQueryBuilder sortColumnNames( String sortColumnNames )
 204  
     {
 205  
         // get the Sort order
 206  0
         int validSortColumnNames = 0;
 207  0
         if ( sortColumnNames != null )
 208  
         {
 209  0
             String[] sortColumnNamesArray = sortColumnNames.split( "," );
 210  
             // N.B. Add in reverse order (it's the way JIRA 3 likes it!!)
 211  0
             for ( int i = sortColumnNamesArray.length - 1; i >= 0; i-- )
 212  
             {
 213  0
                 String lowerColumnName = sortColumnNamesArray[i].trim().toLowerCase( Locale.ENGLISH );
 214  0
                 boolean descending = false;
 215  0
                 String fieldName = null;
 216  0
                 if ( lowerColumnName.endsWith( "desc" ) )
 217  
                 {
 218  0
                     descending = true;
 219  0
                     lowerColumnName = lowerColumnName.substring( 0, lowerColumnName.length() - 4 ).trim();
 220  
                 }
 221  0
                 else if ( lowerColumnName.endsWith( "asc" ) )
 222  
                 {
 223  0
                     descending = false;
 224  0
                     lowerColumnName = lowerColumnName.substring( 0, lowerColumnName.length() - 3 ).trim();
 225  
                 }
 226  
 
 227  0
                 if ( "key".equals( lowerColumnName ) )
 228  
                 {
 229  0
                     fieldName = "issuekey";
 230  
                 }
 231  0
                 else if ( "summary".equals( lowerColumnName ) )
 232  
                 {
 233  0
                     fieldName = lowerColumnName;
 234  
                 }
 235  0
                 else if ( "status".equals( lowerColumnName ) )
 236  
                 {
 237  0
                     fieldName = lowerColumnName;
 238  
                 }
 239  0
                 else if ( "resolution".equals( lowerColumnName ) )
 240  
                 {
 241  0
                     fieldName = lowerColumnName;
 242  
                 }
 243  0
                 else if ( "assignee".equals( lowerColumnName ) )
 244  
                 {
 245  0
                     fieldName = lowerColumnName;
 246  
                 }
 247  0
                 else if ( "reporter".equals( lowerColumnName ) )
 248  
                 {
 249  0
                     fieldName = lowerColumnName;
 250  
                 }
 251  0
                 else if ( "type".equals( lowerColumnName ) )
 252  
                 {
 253  0
                     fieldName = "issuetype";
 254  
                 }
 255  0
                 else if ( "priority".equals( lowerColumnName ) )
 256  
                 {
 257  0
                     fieldName = lowerColumnName;
 258  
                 }
 259  0
                 else if ( "version".equals( lowerColumnName ) )
 260  
                 {
 261  0
                     fieldName = "versions";
 262  
                 }
 263  0
                 else if ( "fix version".equals( lowerColumnName ) )
 264  
                 {
 265  0
                     fieldName = "fixVersions";
 266  
                 }
 267  0
                 else if ( "component".equals( lowerColumnName ) )
 268  
                 {
 269  0
                     fieldName = "components";
 270  
                 }
 271  0
                 else if ( "created".equals( lowerColumnName ) )
 272  
                 {
 273  0
                     fieldName = lowerColumnName;
 274  
                 }
 275  0
                 else if ( "updated".equals( lowerColumnName ) )
 276  
                 {
 277  0
                     fieldName = lowerColumnName;
 278  
                 }
 279  0
                 if ( fieldName != null )
 280  
                 {
 281  0
                     query.append( "&sorter/field=" );
 282  0
                     query.append( fieldName );
 283  0
                     query.append( "&sorter/order=" );
 284  0
                     query.append( descending ? "DESC" : "ASC" );
 285  0
                     validSortColumnNames++;
 286  
                 }
 287  
                 else
 288  
                 {
 289  
                     // Error in the configuration
 290  0
                     getLog().error(
 291  
                         "maven-changes-plugin: The configured value '" + lowerColumnName
 292  
                             + "' for sortColumnNames is not correct." );
 293  
                 }
 294  
             }
 295  0
             if ( validSortColumnNames == 0 )
 296  
             {
 297  
                 // Error in the configuration
 298  0
                 getLog().error(
 299  
                     "maven-changes-plugin: None of the configured sortColumnNames '" + sortColumnNames + "' are correct." );
 300  
             }
 301  
         }
 302  0
         return this;
 303  
     }
 304  
 
 305  
     public JiraQueryBuilder statusIds( String statusIds )
 306  
     {
 307  
         // get the Status Ids
 308  0
         if ( statusIds != null )
 309  
         {
 310  0
             String[] stats = statusIds.split( "," );
 311  0
             for ( String stat : stats )
 312  
             {
 313  0
                 stat = stat.trim();
 314  0
                 String statusParam = statusMap.get( stat );
 315  
 
 316  0
                 if ( statusParam != null )
 317  
                 {
 318  0
                     query.append( "&statusIds=" ).append( statusParam );
 319  
                 }
 320  
                 else
 321  
                 {
 322  
                     // if it's numeric we can handle it too.
 323  
                     try
 324  
                     {
 325  0
                         Integer.parseInt( stat );
 326  0
                         query.append( "&statusIds=" ).append( stat );
 327  
                     }
 328  0
                     catch ( NumberFormatException nfe )
 329  
                     {
 330  0
                         getLog().error( "maven-changes-plugin: invalid statusId " + stat );
 331  0
                     }
 332  
                 }
 333  
             }
 334  
         }
 335  0
         return this;
 336  
     }
 337  
 
 338  
     public JiraQueryBuilder typeIds( String typeIds )
 339  
     {
 340  
         // get the Type Ids
 341  0
         if ( typeIds != null )
 342  
         {
 343  0
             String[] types = typeIds.split( "," );
 344  
 
 345  0
             for ( String type : types )
 346  
             {
 347  0
                 String typeParam = typeMap.get( type.trim() );
 348  
 
 349  0
                 if ( typeParam != null )
 350  
                 {
 351  0
                     query.append( "&type=" ).append( typeParam );
 352  
                 }
 353  
             }
 354  
         }
 355  0
         return this;
 356  
     }
 357  
 }