Coverage Report - org.apache.maven.scm.provider.vss.commands.VssParameterContext
 
Classes in this File Line Coverage Branch Coverage Complexity
VssParameterContext
0 %
0/103
0 %
0/94
3,708
 
 1  
 package org.apache.maven.scm.provider.vss.commands;
 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.scm.ScmException;
 23  
 import org.apache.maven.scm.provider.vss.repository.VssScmProviderRepository;
 24  
 
 25  
 import java.io.File;
 26  
 import java.text.DateFormat;
 27  
 import java.text.ParseException;
 28  
 import java.util.Calendar;
 29  
 import java.util.Date;
 30  
 import java.util.GregorianCalendar;
 31  
 
 32  
 
 33  
 /**
 34  
  * @author <a href="mailto:triek@thrx.de">Thorsten Riek</a>
 35  
  * @version $Id: VssParameterContext.java 687495 2008-08-20 22:54:52Z olamy $
 36  
  */
 37  
 public class VssParameterContext
 38  
 {
 39  
 
 40  0
     private String vssPath = null;
 41  
 
 42  
     private String autoResponse;
 43  
 
 44  
     private String ssDir;
 45  
 
 46  
     private String vssLogin;
 47  
 
 48  
     private String comment;
 49  
 
 50  
     private String user;
 51  
 
 52  
     private String fromLabel;
 53  
 
 54  
     private String toLabel;
 55  
 
 56  
     private boolean quiet;
 57  
 
 58  
     private boolean recursive;
 59  
 
 60  
     private boolean writable;
 61  
 
 62  
     private String label;
 63  
 
 64  
     private String style;
 65  
 
 66  
     private String version;
 67  
 
 68  
     private String date;
 69  
 
 70  
     private String localPath;
 71  
 
 72  
     private String timestamp;
 73  
 
 74  
     /**
 75  
      * Behaviour for writable files
 76  
      */
 77  0
     private String writableFiles = null;
 78  
 
 79  
     /**
 80  
      * From date
 81  
      */
 82  0
     private String fromDate = null;
 83  
 
 84  
     /**
 85  
      * To date
 86  
      */
 87  0
     private String toDate = null;
 88  
 
 89  
     /**
 90  
      * Number of days offset for History
 91  
      */
 92  0
     private int numDays = Integer.MIN_VALUE;
 93  
 
 94  
     /**
 95  
      * Get local copy for checkout defaults to true
 96  
      */
 97  0
     private boolean getLocalCopy = true;
 98  
 
 99  
     /**
 100  
      * Date format for History
 101  
      */
 102  0
     private DateFormat dateFormat = DateFormat
 103  
         .getDateInstance( DateFormat.SHORT );
 104  
 
 105  
     private String outputFileName;
 106  
 
 107  
     public static VssParameterContext getInstance( Object obj )
 108  
     {
 109  0
         return new VssParameterContext( (VssScmProviderRepository) obj );
 110  
     }
 111  
 
 112  
     public VssParameterContext( VssScmProviderRepository repo )
 113  0
     {
 114  0
         autoResponse = System.getProperty( "maven.scm.autoResponse" );
 115  0
         this.ssDir = repo.getVssdir();
 116  0
         this.user = repo.getUser();
 117  
 //        this.vssLogin = this.user + (repos.getPassword() == null ? "" : ","+repos.getPassword());
 118  0
     }
 119  
 
 120  
     /**
 121  
      * Builds and returns the -G- flag if required.
 122  
      *
 123  
      * @return An empty string if get local copy is true.
 124  
      */
 125  
     public String getGetLocalCopy()
 126  
     {
 127  0
         return ( !getLocalCopy ) ? VssConstants.FLAG_NO_GET : "";
 128  
     }
 129  
 
 130  
     /**
 131  
      * Calculate the start date for version comparison.
 132  
      * <p/>
 133  
      * Calculate the date numDay days earlier than startdate.
 134  
      *
 135  
      * @param startDate The start date.
 136  
      * @param daysToAdd The number of days to add.
 137  
      * @return The calculated date.
 138  
      * @throws ParseException
 139  
      */
 140  
     private String calcDate( String startDate, int daysToAdd )
 141  
         throws ParseException
 142  
     {
 143  0
         Date currentDate = new Date();
 144  0
         Calendar calendar = new GregorianCalendar();
 145  0
         currentDate = dateFormat.parse( startDate );
 146  0
         calendar.setTime( currentDate );
 147  0
         calendar.add( Calendar.DATE, daysToAdd );
 148  0
         return dateFormat.format( calendar.getTime() );
 149  
     }
 150  
 
 151  
     /**
 152  
      * Gets the value set for the FileTimeStamp. if it equals "current" then we
 153  
      * return -GTC if it equals "modified" then we return -GTM if it equals
 154  
      * "updated" then we return -GTU otherwise we return -GTC
 155  
      *
 156  
      * @return The default file time flag, if not set.
 157  
      */
 158  
     public String getFileTimeStamp()
 159  
     {
 160  0
         if ( timestamp == null )
 161  
         {
 162  0
             return "";
 163  
         }
 164  0
         return timestamp;
 165  
     }
 166  
 
 167  
     /**
 168  
      * Gets the localpath string. "-GLc:\source"
 169  
      * <p/>
 170  
      * The localpath is created if it didn't exist.
 171  
      *
 172  
      * @return An empty string if localpath is not set.
 173  
      */
 174  
     public String getLocalpath()
 175  
         throws ScmException
 176  
     {
 177  0
         String lclPath = ""; // set to empty str if no local path return
 178  0
         if ( localPath != null )
 179  
         {
 180  
             // make sure m_LocalDir exists, create it if it doesn't
 181  0
             File dir = new File( localPath );
 182  0
             if ( !dir.exists() )
 183  
             {
 184  0
                 boolean done = dir.mkdirs();
 185  0
                 if ( !done )
 186  
                 {
 187  0
                     String msg = "Directory " + localPath + " creation was not " + "successful for an unknown reason";
 188  0
                     throw new ScmException( msg );
 189  
                 }
 190  
 //                getLogger().info("Created dir: " + dir.getAbsolutePath());
 191  
             }
 192  0
             lclPath = VssConstants.FLAG_OVERRIDE_WORKING_DIR + localPath;
 193  
         }
 194  0
         return lclPath;
 195  
     }
 196  
 
 197  
     /**
 198  
      * Gets the label string. "-Lbuild1" Max label length is 32 chars
 199  
      *
 200  
      * @return An empty string if label is not set.
 201  
      */
 202  
     public String getLabel()
 203  
     {
 204  0
         String shortLabel = "";
 205  0
         if ( label != null && label.length() > 0 )
 206  
         {
 207  0
             shortLabel = VssConstants.FLAG_LABEL + getShortLabel();
 208  
         }
 209  0
         return shortLabel;
 210  
     }
 211  
 
 212  
     /**
 213  
      * Gets the version string. Returns the first specified of version "-V1.0",
 214  
      * date "-Vd01.01.01", label "-Vlbuild1".
 215  
      *
 216  
      * @return An empty string if a version, date and label are not set.
 217  
      */
 218  
     public String getVersionDateLabel()
 219  
     {
 220  0
         String versionDateLabel = "";
 221  0
         if ( version != null )
 222  
         {
 223  0
             versionDateLabel = VssConstants.FLAG_VERSION + version;
 224  
         }
 225  0
         else if ( date != null )
 226  
         {
 227  0
             versionDateLabel = VssConstants.FLAG_VERSION_DATE + date;
 228  
         }
 229  
         else
 230  
         {
 231  
             // Use getShortLabel() so labels longer then 30 char are truncated
 232  
             // and the user is warned
 233  0
             String shortLabel = getShortLabel();
 234  0
             if ( shortLabel != null && !shortLabel.equals( "" ) )
 235  
             {
 236  0
                 versionDateLabel = VssConstants.FLAG_VERSION_LABEL + shortLabel;
 237  
             }
 238  
         }
 239  0
         return versionDateLabel;
 240  
     }
 241  
 
 242  
     /**
 243  
      * Gets the version string.
 244  
      *
 245  
      * @return An empty string if a version is not set.
 246  
      */
 247  
     public String getVersion()
 248  
     {
 249  0
         return version != null ? VssConstants.FLAG_VERSION + version : "";
 250  
     }
 251  
 
 252  
     /**
 253  
      * Return at most the 30 first chars of the label, logging a warning message
 254  
      * about the truncation
 255  
      *
 256  
      * @return at most the 30 first chars of the label
 257  
      */
 258  
     private String getShortLabel()
 259  
     {
 260  
         String shortLabel;
 261  0
         if ( label != null && label.length() > 31 )
 262  
         {
 263  0
             shortLabel = this.label.substring( 0, 30 );
 264  
 //            getLogger().warn(
 265  
 //                    "Label is longer than 31 characters, truncated to: "
 266  
 //                            + shortLabel);
 267  
         }
 268  
         else
 269  
         {
 270  0
             shortLabel = label;
 271  
         }
 272  0
         return shortLabel;
 273  
     }
 274  
 
 275  
     /**
 276  
      * Gets the style string. "-Lbuild1"
 277  
      *
 278  
      * @return An empty string if label is not set.
 279  
      */
 280  
     public String getStyle()
 281  
     {
 282  0
         return style != null ? style : "";
 283  
     }
 284  
 
 285  
     /**
 286  
      * Gets the recursive string. "-R"
 287  
      *
 288  
      * @return An empty string if recursive is not set or is false.
 289  
      */
 290  
     public String getRecursive()
 291  
     {
 292  0
         return recursive ? VssConstants.FLAG_RECURSION : "";
 293  
     }
 294  
 
 295  
     /**
 296  
      * Gets the writable string. "-W"
 297  
      *
 298  
      * @return An empty string if writable is not set or is false.
 299  
      */
 300  
     public String getWritable()
 301  
     {
 302  0
         return writable ? VssConstants.FLAG_WRITABLE : "";
 303  
     }
 304  
 
 305  
     /**
 306  
      * Gets the quiet string. -O-
 307  
      *
 308  
      * @return An empty string if quiet is not set or is false.
 309  
      */
 310  
     public String getQuiet()
 311  
     {
 312  0
         return quiet ? VssConstants.FLAG_QUIET : "";
 313  
     }
 314  
 
 315  
     public String getVersionLabel()
 316  
     {
 317  0
         if ( fromLabel == null && toLabel == null )
 318  
         {
 319  0
             return "";
 320  
         }
 321  0
         if ( fromLabel != null && toLabel != null )
 322  
         {
 323  0
             if ( fromLabel.length() > 31 )
 324  
             {
 325  0
                 fromLabel = fromLabel.substring( 0, 30 );
 326  
 //                getLogger().warn(
 327  
 //                        "FromLabel is longer than 31 characters, truncated to: "
 328  
 //                                + fromLabel);
 329  
             }
 330  0
             if ( toLabel.length() > 31 )
 331  
             {
 332  0
                 toLabel = toLabel.substring( 0, 30 );
 333  
 //                getLogger().warn(
 334  
 //                        "ToLabel is longer than 31 characters, truncated to: "
 335  
 //                                + toLabel);
 336  
             }
 337  0
             return VssConstants.FLAG_VERSION_LABEL + toLabel + VssConstants.VALUE_FROMLABEL + fromLabel;
 338  
         }
 339  0
         else if ( fromLabel != null )
 340  
         {
 341  0
             if ( fromLabel.length() > 31 )
 342  
             {
 343  0
                 fromLabel = fromLabel.substring( 0, 30 );
 344  
 //                getLogger().warn(
 345  
 //                        "FromLabel is longer than 31 characters, truncated to: "
 346  
 //                                + fromLabel);
 347  
             }
 348  0
             return VssConstants.FLAG_VERSION + VssConstants.VALUE_FROMLABEL + fromLabel;
 349  
         }
 350  
         else
 351  
         {
 352  0
             if ( toLabel.length() > 31 )
 353  
             {
 354  0
                 toLabel = toLabel.substring( 0, 30 );
 355  
 //                getLogger().warn(
 356  
 //                        "ToLabel is longer than 31 characters, truncated to: "
 357  
 //                                + toLabel);
 358  
             }
 359  0
             return VssConstants.FLAG_VERSION_LABEL + toLabel;
 360  
         }
 361  
     }
 362  
 
 363  
     /**
 364  
      * Gets the user string. "-Uusername"
 365  
      *
 366  
      * @return An empty string if user is not set.
 367  
      */
 368  
     public String getUser()
 369  
     {
 370  0
         return user != null ? VssConstants.FLAG_USER + user : "";
 371  
     }
 372  
 
 373  
     /**
 374  
      * Gets the comment string. "-Ccomment text"
 375  
      *
 376  
      * @return A comment of "-" if comment is not set.
 377  
      */
 378  
     public String getComment()
 379  
     {
 380  0
         return comment != null ? VssConstants.FLAG_COMMENT + comment : VssConstants.FLAG_COMMENT + "-";
 381  
     }
 382  
 
 383  
     /**
 384  
      * Gets the login string. This can be user and password, "-Yuser,password"
 385  
      * or just user "-Yuser".
 386  
      *
 387  
      * @return An empty string if login is not set.
 388  
      */
 389  
     public String getLogin()
 390  
     {
 391  0
         return vssLogin != null ? ( VssConstants.FLAG_LOGIN + vssLogin ) : "";
 392  
     }
 393  
 
 394  
     /**
 395  
      * Gets the auto response string. This can be Y "-I-Y" or N "-I-N".
 396  
      *
 397  
      * @return The default value "-I-" if autoresponse is not set.
 398  
      */
 399  
     public String getAutoresponse()
 400  
     {
 401  0
         if ( autoResponse == null )
 402  
         {
 403  0
             return VssConstants.FLAG_AUTORESPONSE_DEF;
 404  
         }
 405  0
         else if ( autoResponse.equalsIgnoreCase( "Y" ) )
 406  
         {
 407  0
             return VssConstants.FLAG_AUTORESPONSE_YES;
 408  
         }
 409  0
         else if ( autoResponse.equalsIgnoreCase( "N" ) )
 410  
         {
 411  0
             return VssConstants.FLAG_AUTORESPONSE_NO;
 412  
         }
 413  
         else
 414  
         {
 415  0
             return VssConstants.FLAG_AUTORESPONSE_DEF;
 416  
         }
 417  
     }
 418  
 
 419  
     /**
 420  
      * Gets the sscommand string. "ss" or "c:\path\to\ss"
 421  
      *
 422  
      * @return The path to ss.exe or just ss if sscommand is not set.
 423  
      */
 424  
     public String getSSCommand()
 425  
     {
 426  0
         if ( ssDir == null )
 427  
         {
 428  0
             return VssConstants.SS_EXE;
 429  
         }
 430  0
         return ssDir.endsWith( File.separator ) ? ssDir + VssConstants.SS_EXE : ssDir + File.separator + VssConstants.SS_EXE;
 431  
     }
 432  
 
 433  
     public String getVssPath()
 434  
     {
 435  0
         return vssPath;
 436  
     }
 437  
 
 438  
 
 439  
     /**
 440  
      * Gets the Version date string.
 441  
      *
 442  
      * @return An empty string if neither Todate or from date are set.
 443  
      * @throws ScmException
 444  
      */
 445  
     public String getVersionDate()
 446  
         throws ScmException
 447  
     {
 448  0
         if ( fromDate == null && toDate == null && numDays == Integer.MIN_VALUE )
 449  
         {
 450  0
             return "";
 451  
         }
 452  0
         if ( fromDate != null && toDate != null )
 453  
         {
 454  0
             return VssConstants.FLAG_VERSION_DATE + toDate + VssConstants.VALUE_FROMDATE + fromDate;
 455  
         }
 456  0
         else if ( toDate != null && numDays != Integer.MIN_VALUE )
 457  
         {
 458  
             try
 459  
             {
 460  0
                 return VssConstants.FLAG_VERSION_DATE + toDate + VssConstants.VALUE_FROMDATE + calcDate( toDate, numDays );
 461  
             }
 462  0
             catch ( ParseException ex )
 463  
             {
 464  0
                 String msg = "Error parsing date: " + toDate;
 465  0
                 throw new ScmException( msg );
 466  
             }
 467  
         }
 468  0
         else if ( fromDate != null && numDays != Integer.MIN_VALUE )
 469  
         {
 470  
             try
 471  
             {
 472  0
                 return VssConstants.FLAG_VERSION_DATE + calcDate( fromDate, numDays ) + VssConstants.VALUE_FROMDATE + fromDate;
 473  
             }
 474  0
             catch ( ParseException ex )
 475  
             {
 476  0
                 String msg = "Error parsing date: " + fromDate;
 477  0
                 throw new ScmException( msg );
 478  
             }
 479  
         }
 480  
         else
 481  
         {
 482  0
             return fromDate != null ? VssConstants.FLAG_VERSION + VssConstants.VALUE_FROMDATE + fromDate : VssConstants.FLAG_VERSION_DATE + toDate;
 483  
         }
 484  
     }
 485  
 
 486  
     /**
 487  
      * Gets the output file string. "-Ooutput.file"
 488  
      *
 489  
      * @return An empty string if user is not set.
 490  
      */
 491  
     public String getOutput()
 492  
     {
 493  0
         return outputFileName != null ? VssConstants.FLAG_OUTPUT + outputFileName : "";
 494  
     }
 495  
 
 496  
     /**
 497  
      * Gets the value to determine the behaviour when encountering writable
 498  
      * files.
 499  
      *
 500  
      * @return An empty String, if not set.
 501  
      */
 502  
     public String getWritableFiles()
 503  
     {
 504  
         // FIXME: Fix this
 505  0
         if ( writableFiles == null )
 506  
         {
 507  0
             return "";
 508  
         }
 509  0
         return writableFiles;
 510  
     }
 511  
 
 512  
 }