1 16 package org.outerj.daisy.repository.commonimpl.schema; 17 18 import org.outerj.daisy.repository.schema.*; 19 import org.outerj.daisy.repository.commonimpl.AuthenticatedUser; 20 import org.outerj.daisy.repository.commonimpl.Util; 21 import org.outerj.daisy.repository.RepositoryException; 22 import org.outerx.daisy.x10.DocumentTypeDocument; 23 24 import java.util.*; 25 26 public class DocumentTypeImpl implements DocumentType { 27 private long id = -1; 28 private List partTypeUses = new ArrayList(); 29 private Map partTypeUsesById = new HashMap(); 30 private List fieldTypeUses = new ArrayList(); 31 private Map fieldTypeUsesById = new HashMap(); 32 private String name; 33 private LocaleMap label = new LocaleMap(); 34 private LocaleMap description = new LocaleMap(); 35 private SchemaStrategy schemaStrategy; 36 private CommonRepositorySchema repositorySchema; 37 private long labelId = -1; 38 private long descriptionId = -1; 39 private Date lastModified; 40 private long lastModifier=-1; 41 private AuthenticatedUser currentModifier; 42 private boolean deprecated = false; 43 private boolean readOnly = false; 44 private long updateCount = 0; 45 private IntimateAccess intimateAccess = new IntimateAccess(); 46 private static final String READ_ONLY_MESSAGE = "This DocumentType is read-only."; 47 48 public DocumentTypeImpl(String name, SchemaStrategy schemaStrategy, 49 CommonRepositorySchema repositorySchemaCache, AuthenticatedUser user) { 50 Util.checkName(name); 51 this.name = name; 52 this.schemaStrategy = schemaStrategy; 53 this.repositorySchema = repositorySchemaCache; 54 this.currentModifier = user; 55 } 56 57 public IntimateAccess getIntimateAccess(SchemaStrategy schemaStrategy) { 58 if (this.schemaStrategy == schemaStrategy) 59 return intimateAccess; 60 else 61 return null; 62 } 63 64 public long getId() { 65 return id; 66 } 67 68 public PartTypeUse[] getPartTypeUses() { 69 return (PartTypeUse[])partTypeUses.toArray(new PartTypeUse[partTypeUses.size()]); 70 } 71 72 public void addPartType(PartType partType, boolean required) { 73 if (readOnly) 74 throw new RuntimeException (READ_ONLY_MESSAGE); 75 76 if (!(partType instanceof PartTypeImpl)) 77 throw new IllegalArgumentException ("Unsupported PartType supplied."); 78 79 if (((PartTypeImpl)partType).getIntimateAccess(schemaStrategy) == null) 80 throw new IllegalArgumentException ("PartType is not loaded from the same Repository as this DocumentType."); 81 82 if (partType.getId() == -1) 83 throw new IllegalArgumentException ("Only PartTypes which have already been created in the repository can be added to a DocumentType."); 84 85 if (hasPartType(partType.getId())) 86 throw new IllegalArgumentException ("A DocumentType can only contain the same PartType once."); 87 88 PartTypeUseImpl partTypeUse = new PartTypeUseImpl(partType, required); 89 partTypeUses.add(partTypeUse); 90 partTypeUsesById.put(new Long (partType.getId()), partTypeUse); 91 } 92 93 public void clearPartTypeUses() { 94 if (readOnly) 95 throw new RuntimeException (READ_ONLY_MESSAGE); 96 partTypeUses.clear(); 97 partTypeUsesById.clear(); 98 } 99 100 public boolean hasPartType(long id) { 101 return partTypeUsesById.containsKey(new Long (id)); 102 } 103 104 public PartTypeUse getPartTypeUse(long id) { 105 return (PartTypeUse)partTypeUsesById.get(new Long (id)); 106 } 107 108 public FieldTypeUse[] getFieldTypeUses() { 109 return (FieldTypeUse[])fieldTypeUses.toArray(new FieldTypeUse[fieldTypeUses.size()]); 110 } 111 112 public boolean hasFieldType(long id) { 113 return fieldTypeUsesById.containsKey(new Long (id)); 114 } 115 116 public FieldTypeUse getFieldTypeUse(long id) { 117 return (FieldTypeUse)fieldTypeUsesById.get(new Long (id)); 118 } 119 120 public void addFieldType(FieldType type, boolean required) { 121 if (readOnly) 122 throw new RuntimeException (READ_ONLY_MESSAGE); 123 124 if (!(type instanceof FieldTypeImpl)) 125 throw new IllegalArgumentException ("Unsupported FieldType supplied."); 126 127 if (((FieldTypeImpl)type).getIntimateAccess(schemaStrategy) == null) 128 throw new IllegalArgumentException ("FieldType is not loaded from the same Repository as this DocumentType."); 129 130 if (type.getId() == -1) 131 throw new IllegalArgumentException ("Only FieldTypes which have already been created in the repository can be added to a DocumentType."); 132 133 if (hasFieldType(type.getId())) 134 throw new IllegalArgumentException ("A DocumentType can only contain the same FieldType once."); 135 136 FieldTypeUseImpl fieldTypeUse = new FieldTypeUseImpl(type, required); 137 fieldTypeUses.add(fieldTypeUse); 138 fieldTypeUsesById.put(new Long (type.getId()), fieldTypeUse); 139 } 140 141 public void clearFieldTypeUses() { 142 if (readOnly) 143 throw new RuntimeException (READ_ONLY_MESSAGE); 144 fieldTypeUses.clear(); 145 fieldTypeUsesById.clear(); 146 } 147 148 public String getName() { 149 return name; 150 } 151 152 public void setName(String name) { 153 if (readOnly) 154 throw new RuntimeException (READ_ONLY_MESSAGE); 155 Util.checkName(name); 156 this.name = name; 157 } 158 159 public String getDescription(Locale locale) { 160 return (String )description.get(locale); 161 } 162 163 public String getDescriptionExact(Locale locale) { 164 return (String )description.getExact(locale); 165 } 166 167 public void setDescription(Locale locale, String description) { 168 if (readOnly) 169 throw new RuntimeException (READ_ONLY_MESSAGE); 170 this.description.put(locale, description); 171 } 172 173 public void clearDescriptions() { 174 if (readOnly) 175 throw new RuntimeException (READ_ONLY_MESSAGE); 176 description.clear(); 177 } 178 179 public Locale[] getDescriptionLocales() { 180 return description.getLocales(); 181 } 182 183 public void setLabel(Locale locale, String label) { 184 if (readOnly) 185 throw new RuntimeException (READ_ONLY_MESSAGE); 186 this.label.put(locale, label); 187 } 188 189 public String getLabel(Locale locale) { 190 String result = (String )label.get(locale); 191 return result == null ? getName() : result; 192 } 193 194 public String getLabelExact(Locale locale) { 195 return (String )label.getExact(locale); 196 } 197 198 public void clearLabels() { 199 if (readOnly) 200 throw new RuntimeException (READ_ONLY_MESSAGE); 201 label.clear(); 202 } 203 204 public Locale[] getLabelLocales() { 205 return label.getLocales(); 206 } 207 208 private DocumentTypeDocument getXml(boolean extended) { 209 DocumentTypeDocument documentTypeDocument = DocumentTypeDocument.Factory.newInstance(); 210 211 DocumentTypeDocument.DocumentType documentType = documentTypeDocument.addNewDocumentType(); 212 213 if (id != -1) { 214 documentType.setId(id); 215 GregorianCalendar lastModified = new GregorianCalendar(); 216 lastModified.setTime(this.lastModified); 217 documentType.setLastModified(lastModified); 218 documentType.setLastModifier(lastModifier); 219 } 220 221 documentType.setName(name); 222 documentType.setDeprecated(deprecated); 223 documentType.setLabels(label.getAsLabelsXml()); 224 documentType.setDescriptions(description.getAsDescriptionsXml()); 225 documentType.setUpdateCount(updateCount); 226 227 PartTypeUse[] partTypeUses = getPartTypeUses(); 229 DocumentTypeDocument.DocumentType.PartTypeUses partTypeUsesXml = documentType.addNewPartTypeUses(); 230 for (int i = 0; i < partTypeUses.length; i++) { 231 DocumentTypeDocument.DocumentType.PartTypeUses.PartTypeUse partTypeUseXml = partTypeUsesXml.addNewPartTypeUse(); 232 partTypeUseXml.setPartTypeId(partTypeUses[i].getPartType().getId()); 233 partTypeUseXml.setRequired(partTypeUses[i].isRequired()); 234 if (extended) 235 partTypeUseXml.setPartType(partTypeUses[i].getPartType().getXml().getPartType()); 236 } 237 238 FieldTypeUse[] fieldTypeUses = getFieldTypeUses(); 240 DocumentTypeDocument.DocumentType.FieldTypeUses fieldTypeUsesXml = documentType.addNewFieldTypeUses(); 241 for (int i = 0; i < fieldTypeUses.length; i++) { 242 DocumentTypeDocument.DocumentType.FieldTypeUses.FieldTypeUse fieldTypeUseXml = fieldTypeUsesXml.addNewFieldTypeUse(); 243 fieldTypeUseXml.setFieldTypeId(fieldTypeUses[i].getFieldType().getId()); 244 fieldTypeUseXml.setRequired(fieldTypeUses[i].isRequired()); 245 if (extended) 246 fieldTypeUseXml.setFieldType(fieldTypeUses[i].getFieldType().getXml().getFieldType()); 247 } 248 249 return documentTypeDocument; 250 } 251 252 public DocumentTypeDocument getExtendedXml() { 253 return getXml(true); 254 } 255 256 public DocumentTypeDocument getXml() { 257 return getXml(false); 258 } 259 260 public void save() throws RepositoryException { 261 if (readOnly) 262 throw new RuntimeException (READ_ONLY_MESSAGE); 263 schemaStrategy.store(this); 264 } 265 266 public long getLastModifier() { 267 return lastModifier; 268 } 269 270 public Date getLastModified() { 271 if (lastModified != null) 272 return (Date)lastModified.clone(); 273 else 274 return null; 275 } 276 277 public void setDeprecated(boolean deprecated) { 278 if (readOnly) 279 throw new RuntimeException (READ_ONLY_MESSAGE); 280 this.deprecated = deprecated; 281 } 282 283 public boolean isDeprecated() { 284 return deprecated; 285 } 286 287 public long getUpdateCount() { 288 return updateCount; 289 } 290 291 296 public void makeReadOnly() { 297 this.readOnly = true; 298 } 299 300 public void setAllFromXml(DocumentTypeDocument.DocumentType documentTypeXml) { 301 this.name = documentTypeXml.getName(); 302 this.deprecated = documentTypeXml.getDeprecated(); 303 this.label.clear(); 304 this.label.readFromLabelsXml(documentTypeXml.getLabels()); 305 this.description.clear(); 306 this.description.readFromDescriptionsXml(documentTypeXml.getDescriptions()); 307 308 partTypeUses.clear(); 309 partTypeUsesById.clear(); 310 DocumentTypeDocument.DocumentType.PartTypeUses.PartTypeUse[] partTypeUsesXml = documentTypeXml.getPartTypeUses().getPartTypeUseArray(); 311 for (int i = 0; i < partTypeUsesXml.length; i++) { 312 PartType partType; 313 try { 314 partType = repositorySchema.getPartTypeById(partTypeUsesXml[i].getPartTypeId(), false, currentModifier); 315 } catch (Exception e) { 316 throw new RuntimeException ("Error looking up a PartType.", e); 317 } 318 PartTypeUseImpl partTypeUse = new PartTypeUseImpl(partType, partTypeUsesXml[i].getRequired()); 319 partTypeUses.add(partTypeUse); 320 partTypeUsesById.put(new Long (partType.getId()), partTypeUse); 321 } 322 323 fieldTypeUses.clear(); 324 DocumentTypeDocument.DocumentType.FieldTypeUses.FieldTypeUse[] fieldTypeUsesXml = documentTypeXml.getFieldTypeUses().getFieldTypeUseArray(); 325 for (int i = 0; i < fieldTypeUsesXml.length; i++) { 326 FieldType fieldType; 327 try { 328 fieldType = repositorySchema.getFieldTypeById(fieldTypeUsesXml[i].getFieldTypeId(), false, currentModifier); 329 } catch (Exception e) { 330 throw new RuntimeException ("Error looking up a FieldType.", e); 331 } 332 FieldTypeUseImpl fieldTypeUse = new FieldTypeUseImpl(fieldType, fieldTypeUsesXml[i].getRequired()); 333 fieldTypeUses.add(fieldTypeUse); 334 fieldTypeUsesById.put(new Long (fieldType.getId()), fieldTypeUse); 335 } 336 } 337 338 public class IntimateAccess { 339 private IntimateAccess() { 340 } 341 342 public void setLastModified(Date lastModified) { 343 if (readOnly) 344 throw new RuntimeException (READ_ONLY_MESSAGE); 345 DocumentTypeImpl.this.lastModified = lastModified; 346 } 347 348 public void setLastModifier(long lastModifier) { 349 if (readOnly) 350 throw new RuntimeException (READ_ONLY_MESSAGE); 351 DocumentTypeImpl.this.lastModifier = lastModifier; 352 } 353 354 public AuthenticatedUser getCurrentModifier() { 355 return currentModifier; 356 } 357 358 public LocaleMap getLabels() { 359 if (readOnly) 360 throw new RuntimeException (READ_ONLY_MESSAGE); 361 return label; 362 } 363 364 public LocaleMap getDescriptions() { 365 if (readOnly) 366 throw new RuntimeException (READ_ONLY_MESSAGE); 367 return description; 368 } 369 370 public long getLabelId() { 371 return labelId; 372 } 373 374 public void setLabelId(long labelId) { 375 if (readOnly) 376 throw new RuntimeException (READ_ONLY_MESSAGE); 377 DocumentTypeImpl.this.labelId = labelId; 378 } 379 380 public long getDescriptionId() { 381 return descriptionId; 382 } 383 384 public void setDescriptionId(long descriptionId) { 385 if (readOnly) 386 throw new RuntimeException (READ_ONLY_MESSAGE); 387 DocumentTypeImpl.this.descriptionId = descriptionId; 388 } 389 390 public void setId(long id) { 391 if (readOnly) 392 throw new RuntimeException (READ_ONLY_MESSAGE); 393 DocumentTypeImpl.this.id = id; 394 } 395 396 public void setUpdateCount(long updateCount) { 397 if (readOnly) 398 throw new RuntimeException (READ_ONLY_MESSAGE); 399 DocumentTypeImpl.this.updateCount = updateCount; 400 } 401 } 402 } 403 | Popular Tags |