KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openharmonise > vfs > VersionedVirtualFile


1 /*
2  * The contents of this file are subject to the
3  * Mozilla Public License Version 1.1 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
6  *
7  * Software distributed under the License is distributed on an "AS IS"
8  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
9  * See the License for the specific language governing rights and
10  * limitations under the License.
11  *
12  * The Initial Developer of the Original Code is Simulacra Media Ltd.
13  * Portions created by Simulacra Media Ltd are Copyright (C) Simulacra Media Ltd, 2004.
14  *
15  * All Rights Reserved.
16  *
17  * Contributor(s):
18  */

19 package org.openharmonise.vfs;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24
25 import org.openharmonise.vfs.event.*;
26 import org.openharmonise.vfs.status.*;
27
28
29
30 /**
31  * A versionable virtual file, to go with a versionable virtual file
32  * System.
33  *
34  * @author Matthew Large
35  * @version $Revision: 1.1 $
36  *
37  */

38 public class VersionedVirtualFile extends VirtualFile {
39     
40     /**
41      * Path to the pending version of this virtual file. Null if there is no pending verion.
42      */

43     private String JavaDoc m_sPendingVersionPath = null;
44     
45     /**
46      * List of paths to historical versions of this virtual file.
47      */

48     private ArrayList JavaDoc m_aHistoricalVersionPaths = new ArrayList JavaDoc();
49     
50     /**
51      * true if the list of historical version paths has been populated.
52      */

53     private boolean m_bHistoryPopulated = false;
54     
55     /**
56      * Path to the live version of this virtual file. Null if there is no live version.
57      */

58     private String JavaDoc m_sLiveVersionPath = null;
59     
60     /**
61      * Constructs a Virtual File.
62      *
63      * @param sFullPath FullPath of the file
64      */

65     public VersionedVirtualFile(String JavaDoc sFullPath) {
66         super();
67         this.setFullPath(sFullPath);
68         this.setVersionable(true);
69     }
70     
71     /**
72      * Checks if the list of paths for historical versions of this virtual
73      * file has been populated.
74      *
75      * @return true if the list of historical paths is populated
76      */

77     public boolean isHistoryPopulated() {
78         return this.m_bHistoryPopulated;
79     }
80
81     /**
82      * Constructs a Virtual File.
83      */

84     public VersionedVirtualFile() {
85         super();
86         this.setVersionable(true);
87     }
88
89     /**
90      * Checks to see if this has a pending version associated to it.
91      * Will return false if this is not a versionable, live VirtualFile.
92      *
93      * @return true if this has a pending version associated to it
94      */

95     public boolean hasPendingVersion() {
96         return this.m_sPendingVersionPath!=null;
97     }
98
99     /**
100      * Gets the path of the pending VirtualFile associated with this one.
101      * Will return null if this is not a versionable, live in VirtualFile.
102      *
103      * @return Checked out VirtualFile, null if there isn't one
104      */

105     public String JavaDoc getPendingVersionPath() {
106         if(this.m_sPendingVersionPath!=null) {
107             ((VersionedVirtualFile)this.m_vfs.getVirtualFile(this.m_sPendingVersionPath).getResource()).setLiveVersionPath(this.getFullPath());
108             ((VersionedVirtualFile)this.m_vfs.getVirtualFile(this.m_sPendingVersionPath).getResource()).setState(VirtualFile.STATE_PENDING);
109         }
110         return this.m_sPendingVersionPath;
111     }
112     
113     /**
114      * Sets the path to the pending version of this virtual file.
115      *
116      * @param sPath Full path to the pending version
117      */

118     protected void setPendingVersionPath(String JavaDoc sPath) {
119         this.m_sPendingVersionPath = sPath;
120     }
121     
122     /**
123      * Sets the path to the live version of this virtual file.
124      *
125      * @param sPath Full path to the live version
126      */

127     protected void setLiveVersionPath(String JavaDoc sPath) {
128         this.m_sLiveVersionPath = sPath;
129     }
130     
131     /**
132      * Returns that full path to the live version of this virtual file.
133      *
134      * @return Full path to live version
135      */

136     public String JavaDoc getLiveVersionPath() {
137         return this.m_sLiveVersionPath;
138     }
139     
140     /**
141      * Returns the logical path of this virtual file. If the virtual file
142      * is new or a live version then its own path will be returned. If the
143      * virtual file is a pending or historical version the path of its live
144      * version will be returned.
145      *
146      * @return Full logical path
147      */

148     public String JavaDoc getLogicalPath() {
149         if(this.getState()==VirtualFile.STATE_PENDING) {
150             if(this.getLiveVersionPath()!=null) {
151                 return this.getLiveVersionPath();
152             } else {
153                 return this.getFullPath();
154             }
155         } else if(this.getState()==VirtualFile.STATE_HISTORICAL) {
156             return this.getLiveVersionPath();
157         } else {
158             return this.getFullPath();
159         }
160     }
161     
162     /**
163      * Gets the paths of the historical VirtualFiles associated with this one.
164      * Will return an empty List if this is not a versionable, live in VirtualFile.
165      *
166      * @return List of virtual files that are the historical versions of this one. List will be empty if there are none
167      */

168     public List JavaDoc getHistoricalVersions() {
169         if(!this.m_bHistoryPopulated) {
170             ((AbstractVersioningVFS)this.m_vfs).fullyPopulateFileHistory(this);
171         }
172         Iterator JavaDoc iterator = this.m_aHistoricalVersionPaths.iterator();
173         while (iterator.hasNext()) {
174             String JavaDoc element = ((String JavaDoc) iterator.next());
175             if(element!=null) {
176                 VersionedVirtualFile vfHistorical = (VersionedVirtualFile) this.m_vfs.getVirtualFile(element).getResource();
177                 vfHistorical.setLiveVersionPath(this.getFullPath());
178                 vfHistorical.setState(VirtualFile.STATE_HISTORICAL);
179             }
180         }
181         return (List JavaDoc) this.m_aHistoricalVersionPaths.clone();
182     }
183     
184     /**
185      * Sets that the list of historical versions of this virtual file
186      * has been populated.
187      *
188      * @param bHistoryPopulated true to set that the list of historical versions has been populated
189      */

190     protected void setHistoryPopulated(boolean bHistoryPopulated) {
191         this.m_bHistoryPopulated = bHistoryPopulated;
192     }
193     
194     /**
195      * Adds a path to a historical version.
196      *
197      * @param sPath Path to historical version
198      */

199     protected void addHistoricalVersion(String JavaDoc sPath) {
200         if(!this.m_aHistoricalVersionPaths.contains(sPath)) {
201             this.m_aHistoricalVersionPaths.add(sPath);
202         }
203     }
204     
205     /**
206      * Removes a path to a historical version.
207      *
208      * @param sPath Path to historical version
209      */

210     protected void removeHistoricalVersion(String JavaDoc sPath) {
211         this.m_aHistoricalVersionPaths.remove(sPath);
212     }
213     
214     /**
215      * Makes this VirtualFile the current pending VirtualFile for its associated
216      * live VirtualFile. Will return false if this is not a historical VirtualFile.
217      *
218      * @return true if the method was successful
219      */

220     public StatusData reactivate() {
221         VFSStatus retnStatus = new VFSStatus();
222         retnStatus.setMethodName(VirtualFile.METHOD_CHECKOUT);
223         
224         StatusData vfsStatus = ((AbstractVersioningVFS)this.m_vfs).reactivateVersion(this.getFullPath());
225         retnStatus.addStatusData(vfsStatus);
226         
227         return retnStatus;
228     }
229     
230     /**
231      * Creates a pending VirtualFile associated to this VirtualFile.
232      * Will return false if this is not a versionable, live in VirtualFile or
233      * if there is already a pending version.
234      *
235      * @return true if the method was successful
236      */

237     public StatusData checkout() {
238         VFSStatus retnStatus = new VFSStatus();
239         retnStatus.setMethodName(VirtualFile.METHOD_CHECKOUT);
240         
241         VirtualFile vfLive = this.m_vfs.getVirtualFile(this.getLiveVersionPath()).getResource();
242         StatusData vfsStatus = ((AbstractVersioningVFS)this.m_vfs).checkoutVirtualFile(this.getFullPath());
243         retnStatus.addStatusData(vfsStatus);
244         this.m_vfs.getVirtualFile(vfLive.getFilePath()).getResource().fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
245         
246         return retnStatus;
247     }
248     
249     /**
250      * Unchecks out this VirtualFile, removing it from the system.
251      * Will return false if this is not a pending VirtualFile.
252      *
253      * @return true if the method was successful
254      */

255     public StatusData uncheckout() {
256         return null;
257     }
258     
259     /**
260      * Checks this VirtualFile back in. Making it the current live VirtualFile.
261      * Will return false if this is not a pending VirtualFile.
262      *
263      * @return true if the method was successful
264      */

265     public StatusData checkin() {
266         VFSStatus retnStatus = new VFSStatus();
267         retnStatus.setMethodName(VirtualFile.METHOD_CHECKIN);
268         
269         VirtualFile vfLogicalFile = null;
270         if(this.getLiveVersionPath()!=null) {
271             vfLogicalFile = this.m_vfs.getVirtualFile(this.getLiveVersionPath()).getResource();
272         } else {
273             vfLogicalFile = this;
274         }
275         
276         StatusData vfsStatus = ((AbstractVersioningVFS)this.m_vfs).checkinVirtualFile(this.getFullPath());
277         retnStatus.addStatusData(vfsStatus);
278         
279         if(retnStatus.isOK()) {
280             this.fireVirtualFileEvent(VirtualFileEvent.FILE_CHECKED_IN);
281             if(vfLogicalFile!=null) {
282                 this.setState(VirtualFile.STATE_LIVE);
283                 this.m_vfs.getVirtualFile( vfLogicalFile.getFilePath() ).getResource().fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
284             } else {
285                 this.setState(VirtualFile.STATE_LIVE);
286                 this.m_vfs.getVirtualFile(this.getFilePath()).getResource().fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
287             }
288         }
289         
290         return retnStatus;
291     }
292     
293     /**
294      * Tags the current version of the file.
295      *
296      * @param sTag Tag to apply to the file
297      */

298     public StatusData tag(String JavaDoc sTag) {
299         VFSStatus retnStatus = new VFSStatus();
300         retnStatus.setMethodName(VirtualFile.METHOD_TAG);
301         
302         StatusData vfsStatus = ((AbstractVersioningVFS)this.m_vfs).tagVirtualFile(this.getFullPath(), sTag);
303         retnStatus.addStatusData(vfsStatus);
304         
305         return retnStatus;
306     }
307     
308     /**
309      * Deletes this virtual file.
310      *
311      * @return true if the method was successful
312      */

313     public StatusData delete() {
314         VFSStatus retnStatus = new VFSStatus();
315         retnStatus.setMethodName(VirtualFile.METHOD_DELETE);
316         
317         VersionedVirtualFile vfLiveFile = null;
318         VirtualFile vfParent = null;
319         if(this.getState().equals(VirtualFile.STATE_PENDING) && this.m_sLiveVersionPath!=null) {
320             vfParent = this.m_vfs.getVirtualFile(this.m_vfs.getVirtualFile(this.m_sLiveVersionPath).getResource().getFilePath()).getResource();
321             vfLiveFile = (VersionedVirtualFile) this.m_vfs.getVirtualFile(m_sLiveVersionPath).getResource();
322         } else {
323             vfParent = this.m_vfs.getVirtualFile( this.m_vfs.getParentPath(this.getFullPath())).getResource();
324         }
325         
326         StatusData vfsStatus = this.m_vfs.deleteVirtualFile(this.getFullPath());
327         retnStatus.addStatusData(vfsStatus);
328         
329         if(retnStatus.isOK()) {
330             this.fireVirtualFileEvent(VirtualFileEvent.FILE_DELETED);
331             if(this.getState().equals(VirtualFile.STATE_PENDING) && this.m_sLiveVersionPath!=null) {
332                 vfLiveFile.setPendingVersionPath(null);
333             }
334             vfParent.removeChild(this.getFullPath());
335             vfParent.fireVirtualFileEvent(VirtualFileEvent.FILE_MEMBERS_CHANGED);
336         }
337         
338         return retnStatus;
339     }
340
341 }
342
Popular Tags