KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openedit > repository > filesystem > XmlVersionRepository


1 /*
2  * Created on Jan 4, 2005
3  */

4 package org.openedit.repository.filesystem;
5
6 import java.io.File JavaDoc;
7 import java.util.ArrayList JavaDoc;
8 import java.util.Collections JavaDoc;
9 import java.util.Date JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12
13 import org.dom4j.Document;
14 import org.dom4j.DocumentHelper;
15 import org.dom4j.Element;
16 import org.dom4j.io.SAXReader;
17 import org.openedit.repository.ContentItem;
18 import org.openedit.repository.RepositoryException;
19
20 import com.openedit.util.PathUtilities;
21 import com.openedit.util.XmlUtil;
22
23 /**
24  * We need to move more stuff out of the Base Repository into here so that
25  * one day we can implement a Macromedia Contribute version of this
26  * @author cburkey
27  *
28  */

29 public class XmlVersionRepository extends VersionedRepository
30 {
31
32     protected XmlUtil fieldXmlUtil;
33     
34     public XmlVersionRepository()
35     {
36         
37     }
38     /**
39      * @param inRootDirectory
40      */

41     public XmlVersionRepository(File JavaDoc inRootDirectory)
42     {
43         super(inRootDirectory);
44     }
45     protected SAXReader fieldReader;
46     /**
47      * @param inPath
48      * @param version
49      * @return
50      */

51     protected ContentItem populateRevision(String JavaDoc inPath, String JavaDoc inVersionsDir, Element version) {
52         // <version number="1" author="admin" date="2323525233" type="added">Some message</version>
53

54         String JavaDoc rootDir = PathUtilities.extractDirectoryPath(inPath);
55         String JavaDoc filename = PathUtilities.extractFileName(inPath);
56         String JavaDoc versionPath = rootDir + "/" + inVersionsDir + "/" + filename + "~" + version.attributeValue("number");
57
58         ContentItem item = createContentItem(versionPath);
59         item.setPath(inPath);
60         item.setActualPath(versionPath);
61
62         
63         //We dont need the date here since we use a FileItem that points to the date on the disk
64
//This should be changed if we ever have problems with it.
65

66         item.setAuthor(version.attributeValue("author"));
67         item.setVersion(version.attributeValue("number"));
68         String JavaDoc comment = version.getTextTrim();
69         item.setMessage(comment);
70         item.setType(version.attributeValue("type"));
71         return item;
72     }
73     public List JavaDoc readAll(String JavaDoc inPath, File JavaDoc inMetadata) throws RepositoryException
74     {
75             if ( !inMetadata.exists())
76             {
77                 return Collections.EMPTY_LIST;
78             }
79             Element root = getXmlUtil().getXml(inMetadata, "UTF-8");
80             
81             List JavaDoc all = new ArrayList JavaDoc();
82             String JavaDoc versionsDir = inMetadata.getParentFile().getName();
83             for (Iterator JavaDoc versions = root.elementIterator("version"); versions.hasNext();) {
84                 Element version = (Element) versions.next();
85                 ContentItem item = populateRevision(inPath,versionsDir, version);
86                 all.add(item);
87             }
88             Collections.reverse(all); //put oldest on bottom
89
return all;
90     }
91     
92     public void append(ContentItem inRevision, File JavaDoc inMetadata) throws RepositoryException
93     {
94             Document document = null;
95             if ( !inMetadata.exists() )
96             {
97                 document = DocumentHelper.createDocument();
98                 Element root = document.addElement("versions");
99                 //root.addAttribute("path", inRevision.getPath());
100
}
101             else
102             {
103                 document = getXmlUtil().getXml(inMetadata, "UTF-8").getDocument();
104             }
105             //TODO check that its not in there already
106
Element version = document.getRootElement().addElement("version");
107             version.addAttribute("number",inRevision.getVersion());
108             Date JavaDoc date = inRevision.lastModified();
109             if ( date == null)
110             {
111                 date = new Date JavaDoc();
112             }
113             version.addAttribute("date",String.valueOf( date.getTime()));
114             version.addAttribute("author",inRevision.getAuthor());
115             version.addAttribute("type",inRevision.getType());
116             version.setText(inRevision.getMessage());
117
118             inMetadata.getParentFile().mkdirs();
119             getXmlUtil().saveXml(document, inMetadata );
120     }
121     
122     /* (non-javadoc)
123      * @see com.einnovation.repository.filesystem.BaseRepository#getContentItems(java.lang.String)
124      */

125     public List JavaDoc getVersions(String JavaDoc inPath) throws RepositoryException
126     {
127         File JavaDoc file = getFile( inPath );
128         if (file.isDirectory())
129         {
130             return Collections.EMPTY_LIST;
131         }
132         File JavaDoc versionsDirectory = getVersionsDirectory( file );
133         if( !versionsDirectory.exists() )
134         {
135             versionsDirectory.mkdirs();
136         }
137     
138         checkVersion(file, inPath);
139
140         File JavaDoc metadata = getMetaDataFile( file );
141         List JavaDoc knownContentItems = readAll( inPath, metadata );
142         return knownContentItems;
143     }
144     public ContentItem getLastVersion(String JavaDoc inPath)
145     {
146         File JavaDoc file = getFile( inPath );
147
148         File JavaDoc metadata = getMetaDataFile( file );
149         if ( !metadata.exists() )
150         {
151             return null;
152         }
153
154         Element root = getXmlUtil().getXml(metadata, "UTF-8");
155         String JavaDoc versionsDir = metadata.getParentFile().getName();
156         List JavaDoc all = root.elements("version");
157         if( all.size() == 0)
158         {
159             return null;
160         }
161         Element last = (Element)all.get(all.size() - 1);
162         ContentItem item = populateRevision(inPath, versionsDir, last);
163         return item;
164     }
165
166     
167     protected void saveVersion( ContentItem inContentItem ) throws RepositoryException
168     {
169         File JavaDoc file = getFile( inContentItem.getPath() );
170         if ( file.isDirectory() )
171         {
172             return;
173         }
174         int maxVersionNum = maxVersionNumber( file );
175
176         //check that this is not the same content as last version
177
if ( maxVersionNum > 0)
178         {
179             File JavaDoc oldversion = getVersionFile(file,String.valueOf(maxVersionNum ) );
180             if ( oldversion.length() == file.length())
181             {
182                 //these might be the same
183
//TODO: Do a string comparison on them
184
return;
185             }
186         }
187         
188         inContentItem.setVersion( String.valueOf( maxVersionNum + 1 ) );
189         
190         File JavaDoc newversion = getVersionFile( file, inContentItem.getVersion() );
191         try
192         {
193             newversion.getParentFile().mkdirs();
194             if ( file.exists() )
195             {
196                 getFileUtils().copyFiles( file, newversion );
197             }
198             File JavaDoc metadata = getMetaDataFile( file );
199             append( inContentItem, metadata );
200         }
201         catch( Exception JavaDoc e )
202         {
203             throw new RepositoryException( "Error saving ContentItem " + inContentItem.getVersion()
204                     + " for path " + inContentItem.getPath(), e );
205         }
206     }
207     public XmlUtil getXmlUtil()
208     {
209         if( fieldXmlUtil == null)
210         {
211             fieldXmlUtil = new XmlUtil();
212         }
213         return fieldXmlUtil;
214     }
215     public void setXmlUtil(XmlUtil inXmlUtil)
216     {
217         fieldXmlUtil = inXmlUtil;
218     }
219     public void deleteOldVersions(String JavaDoc inPath) throws RepositoryException
220     {
221         File JavaDoc file = getFile( inPath );
222         if ( file.isDirectory() )
223         {
224             return;
225         }
226         File JavaDoc metadata = getMetaDataFile( file );
227         List JavaDoc knownContentItems = readAll( inPath, metadata );
228         for (Iterator JavaDoc iterator = knownContentItems.iterator(); iterator.hasNext();)
229         {
230             FileItem item = (FileItem) iterator.next();
231             item.getFile().delete();
232         }
233         metadata.delete();
234     }
235
236 }
237
Popular Tags