1 4 package org.openedit.repository.filesystem; 5 6 import java.io.File ; 7 import java.util.ArrayList ; 8 import java.util.Collections ; 9 import java.util.Date ; 10 import java.util.Iterator ; 11 import java.util.List ; 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 29 public class XmlVersionRepository extends VersionedRepository 30 { 31 32 protected XmlUtil fieldXmlUtil; 33 34 public XmlVersionRepository() 35 { 36 37 } 38 41 public XmlVersionRepository(File inRootDirectory) 42 { 43 super(inRootDirectory); 44 } 45 protected SAXReader fieldReader; 46 51 protected ContentItem populateRevision(String inPath, String inVersionsDir, Element version) { 52 54 String rootDir = PathUtilities.extractDirectoryPath(inPath); 55 String filename = PathUtilities.extractFileName(inPath); 56 String versionPath = rootDir + "/" + inVersionsDir + "/" + filename + "~" + version.attributeValue("number"); 57 58 ContentItem item = createContentItem(versionPath); 59 item.setPath(inPath); 60 item.setActualPath(versionPath); 61 62 63 66 item.setAuthor(version.attributeValue("author")); 67 item.setVersion(version.attributeValue("number")); 68 String comment = version.getTextTrim(); 69 item.setMessage(comment); 70 item.setType(version.attributeValue("type")); 71 return item; 72 } 73 public List readAll(String inPath, File 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 all = new ArrayList (); 82 String versionsDir = inMetadata.getParentFile().getName(); 83 for (Iterator 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); return all; 90 } 91 92 public void append(ContentItem inRevision, File inMetadata) throws RepositoryException 93 { 94 Document document = null; 95 if ( !inMetadata.exists() ) 96 { 97 document = DocumentHelper.createDocument(); 98 Element root = document.addElement("versions"); 99 } 101 else 102 { 103 document = getXmlUtil().getXml(inMetadata, "UTF-8").getDocument(); 104 } 105 Element version = document.getRootElement().addElement("version"); 107 version.addAttribute("number",inRevision.getVersion()); 108 Date date = inRevision.lastModified(); 109 if ( date == null) 110 { 111 date = new Date (); 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 125 public List getVersions(String inPath) throws RepositoryException 126 { 127 File file = getFile( inPath ); 128 if (file.isDirectory()) 129 { 130 return Collections.EMPTY_LIST; 131 } 132 File versionsDirectory = getVersionsDirectory( file ); 133 if( !versionsDirectory.exists() ) 134 { 135 versionsDirectory.mkdirs(); 136 } 137 138 checkVersion(file, inPath); 139 140 File metadata = getMetaDataFile( file ); 141 List knownContentItems = readAll( inPath, metadata ); 142 return knownContentItems; 143 } 144 public ContentItem getLastVersion(String inPath) 145 { 146 File file = getFile( inPath ); 147 148 File metadata = getMetaDataFile( file ); 149 if ( !metadata.exists() ) 150 { 151 return null; 152 } 153 154 Element root = getXmlUtil().getXml(metadata, "UTF-8"); 155 String versionsDir = metadata.getParentFile().getName(); 156 List 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 file = getFile( inContentItem.getPath() ); 170 if ( file.isDirectory() ) 171 { 172 return; 173 } 174 int maxVersionNum = maxVersionNumber( file ); 175 176 if ( maxVersionNum > 0) 178 { 179 File oldversion = getVersionFile(file,String.valueOf(maxVersionNum ) ); 180 if ( oldversion.length() == file.length()) 181 { 182 return; 185 } 186 } 187 188 inContentItem.setVersion( String.valueOf( maxVersionNum + 1 ) ); 189 190 File 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 metadata = getMetaDataFile( file ); 199 append( inContentItem, metadata ); 200 } 201 catch( Exception 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 inPath) throws RepositoryException 220 { 221 File file = getFile( inPath ); 222 if ( file.isDirectory() ) 223 { 224 return; 225 } 226 File metadata = getMetaDataFile( file ); 227 List knownContentItems = readAll( inPath, metadata ); 228 for (Iterator 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 |