1 16 package org.outerj.daisy.repository.commonimpl; 17 import java.util.Date ; 18 import java.util.GregorianCalendar ; 19 20 import org.outerj.daisy.repository.DocumentCollection; 21 import org.outerj.daisy.repository.RepositoryException; 22 import org.outerx.daisy.x10.CollectionDocument; 23 24 34 public class DocumentCollectionImpl implements DocumentCollection, Comparable { 35 private final AuthenticatedUser requestingUser; 36 private String name; 37 private Date lastModified; 38 private long lastModifier=-1; 39 private boolean readOnly = false; 40 private long updateCount = 0; 41 private static final String READ_ONLY_MESSAGE = "This collection is read-only."; 42 43 59 private long id = -1; 60 private CollectionStrategy collectionStrategy; 61 private IntimateAccess intimateAccess = new IntimateAccess(); 62 63 71 public DocumentCollectionImpl(CollectionStrategy collectionStrategy, String name, AuthenticatedUser requestingUser) { 72 this.requestingUser = requestingUser; 73 this.name = name; 74 this.collectionStrategy = collectionStrategy; 75 } 76 77 public long getId() { 78 return id; 79 } 80 81 public String getName() { 82 return name; 83 } 84 85 public void setName(String collectionName) { 86 if (readOnly) 87 throw new RuntimeException (READ_ONLY_MESSAGE); 88 89 if (collectionName == null) 90 throw new NullPointerException ("Collection name should not be null."); 91 92 name = collectionName; 93 } 94 95 public void save() throws RepositoryException { 96 if (readOnly) 97 throw new RuntimeException (READ_ONLY_MESSAGE); 98 99 collectionStrategy.store(this); 100 } 101 102 105 public CollectionDocument getXml() { 106 CollectionDocument collectionDocument = CollectionDocument.Factory.newInstance(); 107 CollectionDocument.Collection collectionXml = collectionDocument.addNewCollection(); 108 collectionXml.setName(name); 109 collectionXml.setUpdatecount(updateCount); 110 111 if (id != -1) { 112 collectionXml.setId(id); 113 GregorianCalendar lastModifiedCalendar = new GregorianCalendar (); 114 lastModifiedCalendar.setTime(lastModified); 115 collectionXml.setLastModified(lastModifiedCalendar); 116 collectionXml.setLastModifier(lastModifier); 117 } 118 119 return collectionDocument; 120 } 121 122 public Date getLastModified() { 123 if (lastModified != null) 124 return (Date )lastModified.clone(); 125 else 126 return null; 127 } 128 129 public long getLastModifier() { 130 return lastModifier; 131 } 132 133 139 public IntimateAccess getIntimateAccess(CollectionStrategy strategy) { 140 if (this.collectionStrategy == strategy) { 141 return intimateAccess; 142 } 143 else 144 return null; 145 } 146 147 public long getUpdateCount() { 148 return updateCount; 149 } 150 151 public void makeReadOnly() { 152 this.readOnly = true; 153 } 154 155 public int compareTo(Object o) { 156 DocumentCollection otherCollection = (DocumentCollection)o; 157 return getName().compareTo(otherCollection.getName()); 158 } 159 160 167 public class IntimateAccess { 168 private IntimateAccess() {} 169 170 public void setId(long id) { 171 if (readOnly) 172 throw new RuntimeException (READ_ONLY_MESSAGE); 173 DocumentCollectionImpl.this.id=id; 174 } 175 176 public void setLastModified(Date d) { 177 if (readOnly) 178 throw new RuntimeException (READ_ONLY_MESSAGE); 179 DocumentCollectionImpl.this.lastModified=d; 180 } 181 182 public void setLastModifier(long lastModifier) { 183 if (readOnly) 184 throw new RuntimeException (READ_ONLY_MESSAGE); 185 DocumentCollectionImpl.this.lastModifier=lastModifier; 186 } 187 188 public AuthenticatedUser getCurrentUser() { 189 return DocumentCollectionImpl.this.requestingUser; 190 } 191 192 public void setUpdateCount(long updateCount) { 193 if (readOnly) 194 throw new RuntimeException (READ_ONLY_MESSAGE); 195 DocumentCollectionImpl.this.updateCount = updateCount; 196 } 197 198 204 public void saved (long id, String name, Date lastModified, long lastModifier, long updateCount) { 205 if (readOnly) 206 throw new RuntimeException (READ_ONLY_MESSAGE); 207 208 DocumentCollectionImpl.this.id = id; 209 DocumentCollectionImpl.this.name = name; 210 DocumentCollectionImpl.this.lastModified = lastModified; 211 DocumentCollectionImpl.this.lastModifier = lastModifier; 212 DocumentCollectionImpl.this.updateCount = updateCount; 213 } 214 } 215 } 216 | Popular Tags |