View Javadoc

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   *
36   */
37  public class VssParameterContext
38  {
39  
40      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      private String writableFiles = null;
78  
79      /**
80       * From date
81       */
82      private String fromDate = null;
83  
84      /**
85       * To date
86       */
87      private String toDate = null;
88  
89      /**
90       * Number of days offset for History
91       */
92      private int numDays = Integer.MIN_VALUE;
93  
94      /**
95       * Get local copy for checkout defaults to true
96       */
97      private boolean getLocalCopy = true;
98  
99      /**
100      * Date format for History
101      */
102     private DateFormat dateFormat = DateFormat
103         .getDateInstance( DateFormat.SHORT );
104 
105     private String outputFileName;
106 
107     public static VssParameterContext getInstance( Object obj )
108     {
109         return new VssParameterContext( (VssScmProviderRepository) obj );
110     }
111 
112     public VssParameterContext( VssScmProviderRepository repo )
113     {
114         autoResponse = System.getProperty( "maven.scm.autoResponse" );
115         this.ssDir = repo.getVssdir();
116         this.user = repo.getUser();
117 //        this.vssLogin = this.user + (repos.getPassword() == null ? "" : ","+repos.getPassword());
118     }
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         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         Date currentDate = new Date();
144         Calendar calendar = new GregorianCalendar();
145         currentDate = dateFormat.parse( startDate );
146         calendar.setTime( currentDate );
147         calendar.add( Calendar.DATE, daysToAdd );
148         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         if ( timestamp == null )
161         {
162             return "";
163         }
164         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         String lclPath = ""; // set to empty str if no local path return
178         if ( localPath != null )
179         {
180             // make sure m_LocalDir exists, create it if it doesn't
181             File dir = new File( localPath );
182             if ( !dir.exists() )
183             {
184                 boolean done = dir.mkdirs();
185                 if ( !done )
186                 {
187                     String msg = "Directory " + localPath + " creation was not " + "successful for an unknown reason";
188                     throw new ScmException( msg );
189                 }
190 //                getLogger().info("Created dir: " + dir.getAbsolutePath());
191             }
192             lclPath = VssConstants.FLAG_OVERRIDE_WORKING_DIR + localPath;
193         }
194         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         String shortLabel = "";
205         if ( label != null && label.length() > 0 )
206         {
207             shortLabel = VssConstants.FLAG_LABEL + getShortLabel();
208         }
209         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         String versionDateLabel = "";
221         if ( version != null )
222         {
223             versionDateLabel = VssConstants.FLAG_VERSION + version;
224         }
225         else if ( date != null )
226         {
227             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             String shortLabel = getShortLabel();
234             if ( shortLabel != null && !shortLabel.equals( "" ) )
235             {
236                 versionDateLabel = VssConstants.FLAG_VERSION_LABEL + shortLabel;
237             }
238         }
239         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         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         if ( label != null && label.length() > 31 )
262         {
263             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             shortLabel = label;
271         }
272         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         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         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         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         return quiet ? VssConstants.FLAG_QUIET : "";
313     }
314 
315     public String getVersionLabel()
316     {
317         if ( fromLabel == null && toLabel == null )
318         {
319             return "";
320         }
321         if ( fromLabel != null && toLabel != null )
322         {
323             if ( fromLabel.length() > 31 )
324             {
325                 fromLabel = fromLabel.substring( 0, 30 );
326 //                getLogger().warn(
327 //                        "FromLabel is longer than 31 characters, truncated to: "
328 //                                + fromLabel);
329             }
330             if ( toLabel.length() > 31 )
331             {
332                 toLabel = toLabel.substring( 0, 30 );
333 //                getLogger().warn(
334 //                        "ToLabel is longer than 31 characters, truncated to: "
335 //                                + toLabel);
336             }
337             return VssConstants.FLAG_VERSION_LABEL + toLabel + VssConstants.VALUE_FROMLABEL + fromLabel;
338         }
339         else if ( fromLabel != null )
340         {
341             if ( fromLabel.length() > 31 )
342             {
343                 fromLabel = fromLabel.substring( 0, 30 );
344 //                getLogger().warn(
345 //                        "FromLabel is longer than 31 characters, truncated to: "
346 //                                + fromLabel);
347             }
348             return VssConstants.FLAG_VERSION + VssConstants.VALUE_FROMLABEL + fromLabel;
349         }
350         else
351         {
352             if ( toLabel.length() > 31 )
353             {
354                 toLabel = toLabel.substring( 0, 30 );
355 //                getLogger().warn(
356 //                        "ToLabel is longer than 31 characters, truncated to: "
357 //                                + toLabel);
358             }
359             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         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         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         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         if ( autoResponse == null )
402         {
403             return VssConstants.FLAG_AUTORESPONSE_DEF;
404         }
405         else if ( autoResponse.equalsIgnoreCase( "Y" ) )
406         {
407             return VssConstants.FLAG_AUTORESPONSE_YES;
408         }
409         else if ( autoResponse.equalsIgnoreCase( "N" ) )
410         {
411             return VssConstants.FLAG_AUTORESPONSE_NO;
412         }
413         else
414         {
415             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         if ( ssDir == null )
427         {
428             return VssConstants.SS_EXE;
429         }
430         return ssDir.endsWith( File.separator ) ? ssDir + VssConstants.SS_EXE : ssDir + File.separator + VssConstants.SS_EXE;
431     }
432 
433     public String getVssPath()
434     {
435         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         if ( fromDate == null && toDate == null && numDays == Integer.MIN_VALUE )
449         {
450             return "";
451         }
452         if ( fromDate != null && toDate != null )
453         {
454             return VssConstants.FLAG_VERSION_DATE + toDate + VssConstants.VALUE_FROMDATE + fromDate;
455         }
456         else if ( toDate != null && numDays != Integer.MIN_VALUE )
457         {
458             try
459             {
460                 return VssConstants.FLAG_VERSION_DATE + toDate + VssConstants.VALUE_FROMDATE + calcDate( toDate, numDays );
461             }
462             catch ( ParseException ex )
463             {
464                 String msg = "Error parsing date: " + toDate;
465                 throw new ScmException( msg );
466             }
467         }
468         else if ( fromDate != null && numDays != Integer.MIN_VALUE )
469         {
470             try
471             {
472                 return VssConstants.FLAG_VERSION_DATE + calcDate( fromDate, numDays ) + VssConstants.VALUE_FROMDATE + fromDate;
473             }
474             catch ( ParseException ex )
475             {
476                 String msg = "Error parsing date: " + fromDate;
477                 throw new ScmException( msg );
478             }
479         }
480         else
481         {
482             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         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         if ( writableFiles == null )
506         {
507             return "";
508         }
509         return writableFiles;
510     }
511 
512 }