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