1 16 package org.outerj.daisy.repository.commonimpl.schema; 17 18 import org.outerj.daisy.repository.schema.PartType; 19 import org.outerj.daisy.repository.RepositoryException; 20 import org.outerj.daisy.repository.commonimpl.AuthenticatedUser; 21 import org.outerj.daisy.repository.commonimpl.Util; 22 import org.outerx.daisy.x10.PartTypeDocument; 23 24 import java.util.*; 25 26 public class PartTypeImpl implements PartType { 27 private long id = -1; 28 private String name; 29 private LocaleMap label = new LocaleMap(); 30 private long labelId = -1; 31 private LocaleMap description = new LocaleMap(); 32 private long descriptionId = -1; 33 private String mimeTypes; 34 private boolean daisyHtml = false; 35 private String linkExtractor; 36 private SchemaStrategy schemaStrategy; 37 private Date lastModified; 38 private long lastModifier=-1; 39 private AuthenticatedUser currentModifier; 40 private boolean deprecated = false; 41 private boolean readOnly = false; 42 private long updateCount = 0; 43 private IntimateAccess intimateAccess = new IntimateAccess(); 44 private static final String READ_ONLY_MESSAGE = "This PartType is read-only."; 45 46 public PartTypeImpl(String name, String mimeTypes, SchemaStrategy schemaStrategy, AuthenticatedUser user) { 47 Util.checkName(name); 48 this.name = name; 49 setMimeTypes(mimeTypes); 50 this.schemaStrategy = schemaStrategy; 51 this.currentModifier = user; 52 } 53 54 public IntimateAccess getIntimateAccess(SchemaStrategy schemaStrategy) { 55 if (this.schemaStrategy == schemaStrategy) 56 return intimateAccess; 57 else 58 return null; 59 } 60 61 public void setAllFromXml(PartTypeDocument.PartType partTypeXml) { 62 if (readOnly) 63 throw new RuntimeException (READ_ONLY_MESSAGE); 64 this.name = partTypeXml.getName(); 65 setMimeTypes(partTypeXml.getMimeTypes()); 66 this.deprecated = partTypeXml.getDeprecated(); 67 this.label.clear(); 68 this.label.readFromLabelsXml(partTypeXml.getLabels()); 69 this.description.clear(); 70 this.description.readFromDescriptionsXml(partTypeXml.getDescriptions()); 71 this.daisyHtml = partTypeXml.getDaisyHtml(); 72 this.linkExtractor = partTypeXml.getLinkExtractor(); 73 } 74 75 public long getId() { 76 return id; 77 } 78 79 public String getName() { 80 return name; 81 } 82 83 public void setName(String name) { 84 if (readOnly) 85 throw new RuntimeException (READ_ONLY_MESSAGE); 86 Util.checkName(name); 87 this.name = name; 88 } 89 90 public String getLabel(Locale locale) { 91 String result = (String )label.get(locale); 92 return result == null ? getName() : result; 93 } 94 95 public String getLabelExact(Locale locale) { 96 return (String )label.getExact(locale); 97 } 98 99 public void setLabel(Locale locale, String label) { 100 if (readOnly) 101 throw new RuntimeException (READ_ONLY_MESSAGE); 102 this.label.put(locale, label); 103 } 104 105 public void clearLabels() { 106 if (readOnly) 107 throw new RuntimeException (READ_ONLY_MESSAGE); 108 label.clear(); 109 } 110 111 public Locale[] getLabelLocales() { 112 return label.getLocales(); 113 } 114 115 public String getDescription(Locale locale) { 116 return (String )description.get(locale); 117 } 118 119 public String getDescriptionExact(Locale locale) { 120 return (String )description.getExact(locale); 121 } 122 123 public void setDescription(Locale locale, String description) { 124 if (readOnly) 125 throw new RuntimeException (READ_ONLY_MESSAGE); 126 this.description.put(locale, description); 127 } 128 129 public void clearDescriptions() { 130 if (readOnly) 131 throw new RuntimeException (READ_ONLY_MESSAGE); 132 description.clear(); 133 } 134 135 public Locale[] getDescriptionLocales() { 136 return description.getLocales(); 137 } 138 139 public String getMimeTypes() { 140 return mimeTypes; 141 } 142 143 public void setMimeTypes(String mimeTypes) { 144 if (readOnly) 145 throw new RuntimeException (READ_ONLY_MESSAGE); 146 147 if (mimeTypes == null) 148 throw new NullPointerException ("mimeTypes cannot be null."); 149 150 this.mimeTypes = mimeTypes; 151 } 152 153 public boolean mimeTypeAllowed(String mimeType) { 154 if (this.mimeTypes.equals("")) 155 return true; 156 157 StringTokenizer mimeTypesTokenizer = new StringTokenizer(mimeTypes, ","); 158 while (mimeTypesTokenizer.hasMoreTokens()) { 159 if (mimeType.equals(mimeTypesTokenizer.nextToken())) 160 return true; 161 } 162 return false; 163 } 164 165 public boolean isDaisyHtml() { 166 return daisyHtml; 167 } 168 169 public void setDaisyHtml(boolean daisyHtml) { 170 if (readOnly) 171 throw new RuntimeException (READ_ONLY_MESSAGE); 172 this.daisyHtml = daisyHtml; 173 if (daisyHtml) { 174 this.linkExtractor = "daisy-html"; 176 } 177 } 178 179 public void setLinkExtractor(String name) { 180 if (readOnly) 181 throw new RuntimeException (READ_ONLY_MESSAGE); 182 this.linkExtractor = name; 183 } 184 185 public String getLinkExtractor() { 186 return linkExtractor; 187 } 188 189 public Date getLastModified() { 190 if (lastModified != null) 191 return (Date)lastModified.clone(); 192 else 193 return null; 194 } 195 196 public long getLastModifier() { 197 return lastModifier; 198 } 199 200 public PartTypeDocument getXml() { 201 PartTypeDocument partTypeDocument = PartTypeDocument.Factory.newInstance(); 202 PartTypeDocument.PartType partType = partTypeDocument.addNewPartType(); 203 partType.setName(name); 204 partType.setMimeTypes(mimeTypes); 205 partType.setDaisyHtml(daisyHtml); 206 if (linkExtractor != null) 207 partType.setLinkExtractor(linkExtractor); 208 partType.setDeprecated(deprecated); 209 partType.setLabels(label.getAsLabelsXml()); 210 partType.setDescriptions(description.getAsDescriptionsXml()); 211 partType.setUpdateCount(updateCount); 212 213 if (id != -1) { 214 partType.setId(id); 215 GregorianCalendar lastModified = new GregorianCalendar(); 216 lastModified.setTime(this.lastModified); 217 partType.setLastModified(lastModified); 218 partType.setLastModifier(lastModifier); 219 } 220 221 return partTypeDocument; 222 } 223 224 public void save() throws RepositoryException { 225 if (readOnly) 226 throw new RuntimeException (READ_ONLY_MESSAGE); 227 schemaStrategy.store(this); 228 } 229 230 public void setDeprecated(boolean deprecated) { 231 if (readOnly) 232 throw new RuntimeException (READ_ONLY_MESSAGE); 233 this.deprecated = deprecated; 234 } 235 236 public boolean isDeprecated() { 237 return deprecated; 238 } 239 240 public long getUpdateCount() { 241 return updateCount; 242 } 243 244 247 public void makeReadOnly() { 248 this.readOnly = true; 249 } 250 251 public class IntimateAccess { 252 private IntimateAccess() { 253 } 254 255 public long getLabelId() { 256 return labelId; 257 } 258 259 public void setLabelId(long labelId) { 260 if (readOnly) 261 throw new RuntimeException (READ_ONLY_MESSAGE); 262 PartTypeImpl.this.labelId = labelId; 263 } 264 265 public long getDescriptionId() { 266 return descriptionId; 267 } 268 269 public void setDescriptionId(long descriptionId) { 270 if (readOnly) 271 throw new RuntimeException (READ_ONLY_MESSAGE); 272 PartTypeImpl.this.descriptionId = descriptionId; 273 } 274 275 public LocaleMap getLabels() { 276 if (readOnly) 277 throw new RuntimeException (READ_ONLY_MESSAGE); 278 return label; 279 } 280 281 public LocaleMap getDescriptions() { 282 if (readOnly) 283 throw new RuntimeException (READ_ONLY_MESSAGE); 284 return description; 285 } 286 287 public void setLastModified(Date lastModified) { 288 if (readOnly) 289 throw new RuntimeException (READ_ONLY_MESSAGE); 290 PartTypeImpl.this.lastModified = lastModified; 291 } 292 293 public void setLastModifier(long lastModifier) { 294 if (readOnly) 295 throw new RuntimeException (READ_ONLY_MESSAGE); 296 PartTypeImpl.this.lastModifier = lastModifier; 297 } 298 299 public AuthenticatedUser getCurrentModifier() { 300 return currentModifier; 301 } 302 303 public void setId(long id) { 304 if (readOnly) 305 throw new RuntimeException (READ_ONLY_MESSAGE); 306 PartTypeImpl.this.id = id; 307 } 308 309 public void setUpdateCount(long updateCount) { 310 if (readOnly) 311 throw new RuntimeException (READ_ONLY_MESSAGE); 312 PartTypeImpl.this.updateCount = updateCount; 313 } 314 } 315 } 316 | Popular Tags |