1 package org.apache.maven.scm.provider.integrity;
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 com.mks.api.Command;
23 import com.mks.api.FileOption;
24 import com.mks.api.Option;
25 import com.mks.api.response.APIException;
26 import com.mks.api.response.Response;
27 import com.mks.api.response.WorkItem;
28
29 import java.io.File;
30 import java.util.Date;
31
32 /**
33 * This class represents an Integrity SCM Member
34 * <br>It contains all the necessary metadata to check this file out individually
35 *
36 * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
37 * @version $Id: Member.java 1.2 2011/08/22 13:06:47EDT Cletus D'Souza (dsouza) Exp $
38 * @since 1.6
39 */
40 public class Member
41 {
42 private String memberID;
43
44 private String memberName;
45
46 private Date memberTimestamp;
47
48 private String memberDescription;
49
50 private String projectConfigPath;
51
52 private String memberRev;
53
54 private File targetFile;
55
56 private String relativeFile;
57
58 private String lineTerminator;
59
60 private String overwriteExisting;
61
62 private String restoreTimestamp;
63
64 /**
65 * This class represents an MKS Integrity Source File
66 * It needs the Member Name (relative path to pj), Full Member Path, Project Configuration Path, Revision,
67 * Project's Root Path, and the current Workspace directory (to compute the working file path) for its
68 * instantiation. This helper class will be used to then perform a project checkout from the repository
69 *
70 * @param wi A MKS API Response Work Item representing metadata related to a Integrity Member
71 * @param configPath Configuration Path for this file's project/subproject
72 * @param projectRoot Full path to the root location for this file's parent project
73 * @param workspaceDir Full path to the workspace root directory
74 */
75 public Member( WorkItem wi, String configPath, String projectRoot, String workspaceDir )
76 {
77 // Initialize our parent with the information needed
78 this.projectConfigPath = configPath;
79 this.memberID = wi.getId();
80 this.memberName = wi.getField( "name" ).getValueAsString();
81 this.memberRev = wi.getField( "memberrev" ).getItem().getId();
82 this.memberTimestamp = wi.getField( "membertimestamp" ).getDateTime();
83 if ( null != wi.getField( "memberdescription" ) && null != wi.getField(
84 "memberdescription" ).getValueAsString() )
85 {
86 this.memberDescription = wi.getField( "memberdescription" ).getValueAsString();
87 }
88 else
89 {
90 this.memberDescription = new String( "" );
91 }
92 this.lineTerminator = "native";
93 this.overwriteExisting = "overwriteExisting";
94 this.restoreTimestamp = "restoreTimestamp";
95 this.relativeFile = this.memberName.substring( projectRoot.length() );
96 this.targetFile = new File( workspaceDir + relativeFile );
97 }
98
99 /**
100 * Returns a string representation of this file's full path name, where it will checked out to disk for the build.
101 *
102 * @return
103 */
104 public String getTargetFilePath()
105 {
106 return targetFile.getAbsolutePath();
107 }
108
109 /**
110 * Returns a string representation of this member's revision
111 *
112 * @return
113 */
114 public String getRevision()
115 {
116 return memberRev;
117 }
118
119 /**
120 * Returns the date/time associated with this member revision
121 *
122 * @return
123 */
124 public Date getTimestamp()
125 {
126 return memberTimestamp;
127 }
128
129 /**
130 * Returns any check-in comments associated with this revision
131 *
132 * @return
133 */
134 public String getDescription()
135 {
136 return memberDescription;
137 }
138
139 /**
140 * Returns the full server-side member path for this member
141 *
142 * @return
143 */
144 public String getMemberName()
145 {
146 return memberName;
147 }
148
149 /**
150 * Returns only the file name portion for this full server-side member path
151 *
152 * @return
153 */
154 public String getName()
155 {
156 if ( memberID.indexOf( '/' ) > 0 )
157 {
158 return memberID.substring( memberID.lastIndexOf( '/' ) + 1 );
159 }
160 else if ( memberID.indexOf( '\\' ) > 0 )
161 {
162 return memberID.substring( memberID.lastIndexOf( '\\' ) + 1 );
163 }
164 else
165 {
166 return memberID;
167 }
168 }
169
170 /**
171 * Optionally, one may set a line terminator, if the default is not desired.
172 *
173 * @param lineTerminator
174 */
175 public void setLineTerminator( String lineTerminator )
176 {
177 this.lineTerminator = lineTerminator;
178 }
179
180 /**
181 * Optionally, one may choose not to overwrite existing files, this may speed up the synchronization process.
182 *
183 * @param overwriteExisting
184 */
185 public void setOverwriteExisting( String overwriteExisting )
186 {
187 this.overwriteExisting = overwriteExisting;
188 }
189
190 /**
191 * Optionally, one might want to restore the timestamp, if the build is smart not to recompile files that were not
192 * touched.
193 *
194 * @param restoreTimestamp
195 */
196 public void setRestoreTimestamp( boolean restoreTime )
197 {
198 if ( restoreTime )
199 {
200 this.restoreTimestamp = "restoreTimestamp";
201 }
202 else
203 {
204 this.restoreTimestamp = "norestoreTimestamp";
205 }
206 }
207
208 /**
209 * Performs a checkout of this MKS Integrity Source File to a working file location on the build server represented
210 * by targetFile
211 *
212 * @param api MKS API Session
213 * @return true if the operation succeeded or false if failed
214 * @throws APIException
215 */
216 public boolean checkout( APISession api )
217 throws APIException
218 {
219 // Make sure the directory is created
220 if ( !targetFile.getParentFile().isDirectory() )
221 {
222 targetFile.getParentFile().mkdirs();
223 }
224 // Construct the project check-co command
225 Command coCMD = new Command( Command.SI, "projectco" );
226 coCMD.addOption( new Option( overwriteExisting ) );
227 coCMD.addOption( new Option( "nolock" ) );
228 coCMD.addOption( new Option( "project", projectConfigPath ) );
229 coCMD.addOption( new FileOption( "targetFile", targetFile ) );
230 coCMD.addOption( new Option( restoreTimestamp ) );
231 coCMD.addOption( new Option( "lineTerminator", lineTerminator ) );
232 coCMD.addOption( new Option( "revision", memberRev ) );
233 // Add the member selection
234 coCMD.addSelection( memberID );
235
236 // Execute the checkout command
237 Response res = api.runCommand( coCMD );
238
239 // Return true if we were successful
240 return ( res.getExitCode() == 0 );
241 }
242
243 /**
244 * Uses the name of file for equality check
245 */
246 @Override
247 public boolean equals( Object o )
248 {
249 if ( o instanceof Member )
250 {
251 if ( null != o )
252 {
253 return ( (Member) o ).getMemberName().equals( this.getMemberName() );
254 }
255 }
256 return false;
257 }
258
259 @Override
260 public int hashCode()
261 {
262 return this.getMemberName().hashCode();
263 }
264 }