1 16 package org.outerj.daisy.repository.commonimpl.schema; 17 18 import java.util.Date ; 19 import java.util.GregorianCalendar ; 20 import java.util.Locale ; 21 22 import org.outerj.daisy.repository.RepositoryException; 23 import org.outerj.daisy.repository.ValueType; 24 import org.outerj.daisy.repository.query.QueryManager; 25 import org.outerj.daisy.repository.query.SortOrder; 26 import org.outerj.daisy.repository.commonimpl.AuthenticatedUser; 27 import org.outerj.daisy.repository.commonimpl.Util; 28 import org.outerj.daisy.repository.commonimpl.CommonRepository; 29 import org.outerj.daisy.repository.schema.*; 30 import org.outerx.daisy.x10.FieldTypeDocument; 31 import org.outerx.daisy.x10.SelectionListDocument; 32 33 public class FieldTypeImpl implements FieldType { 34 private long id = -1; 35 private ValueType valueType; 36 private String name; 37 private LocaleMap description = new LocaleMap(); 38 private LocaleMap label = new LocaleMap(); 39 private SelectionList selectionList; 40 private CommonRepository repository; 41 private SchemaStrategy schemaStrategy; 42 private Date lastModified; 43 private long lastModifier=-1; 44 private AuthenticatedUser currentModifier; 45 private long labelId = -1; 46 private long descriptionId = -1; 47 private boolean deprecated = false; 48 private boolean aclAllowed = false; 49 private boolean multiValue = false; 50 private int size = 0; 51 private boolean allowFreeEntry = false; 52 private boolean readOnly = false; 53 private long updateCount = 0; 54 private IntimateAccess intimateAccess = new IntimateAccess(); 55 protected static final String READ_ONLY_MESSAGE = "This field type is read-only."; 56 57 public FieldTypeImpl(String name, ValueType fieldType, boolean multiValue, SchemaStrategy schemaStrategy, 58 CommonRepository repository, AuthenticatedUser user) { 59 Util.checkName(name); 60 this.name = name; 61 this.valueType = fieldType; 62 this.multiValue = multiValue; 63 this.schemaStrategy = schemaStrategy; 64 this.repository = repository; 65 this.currentModifier = user; 66 } 67 68 public IntimateAccess getIntimateAccess(SchemaStrategy schemaStrategy) { 69 if (this.schemaStrategy == schemaStrategy) 70 return intimateAccess; 71 else 72 return null; 73 } 74 75 public long getId() { 76 return id; 77 } 78 79 public ValueType getValueType() { 80 return valueType; 81 } 82 83 public String getName() { 84 return name; 85 } 86 87 public void setName(String name) { 88 if (readOnly) 89 throw new RuntimeException (READ_ONLY_MESSAGE); 90 Util.checkName(name); 91 this.name = name; 92 } 93 94 public String getDescription(Locale locale) { 95 return (String )description.get(locale); 96 } 97 98 public String getDescriptionExact(Locale locale) { 99 return (String )description.getExact(locale); 100 } 101 102 public void setDescription(Locale locale, String description) { 103 if (readOnly) 104 throw new RuntimeException (READ_ONLY_MESSAGE); 105 this.description.put(locale, description); 106 } 107 108 public void clearDescriptions() { 109 if (readOnly) 110 throw new RuntimeException (READ_ONLY_MESSAGE); 111 description.clear(); 112 } 113 114 public Locale [] getDescriptionLocales() { 115 return description.getLocales(); 116 } 117 118 public String getLabel(Locale locale) { 119 String result = (String )label.get(locale); 120 return result == null ? getName() : result; 121 } 122 123 public String getLabelExact(Locale locale) { 124 return (String )label.getExact(locale); 125 } 126 127 public void setLabel(Locale locale, String label) { 128 if (readOnly) 129 throw new RuntimeException (READ_ONLY_MESSAGE); 130 this.label.put(locale, label); 131 } 132 133 public void clearLabels() { 134 if (readOnly) 135 throw new RuntimeException (READ_ONLY_MESSAGE); 136 label.clear(); 137 } 138 139 public Locale [] getLabelLocales() { 140 return label.getLocales(); 141 } 142 143 public Date getLastModified() { 144 if (lastModified != null) 145 return (Date )lastModified.clone(); 146 else 147 return null; 148 } 149 150 public long getLastModifier() { 151 return lastModifier; 152 } 153 154 public boolean isAclAllowed() { 155 return aclAllowed; 156 } 157 158 public void setAclAllowed(boolean aclAllowed) { 159 if (readOnly) 160 throw new RuntimeException (READ_ONLY_MESSAGE); 161 this.aclAllowed = aclAllowed; 162 } 163 164 public boolean isMultiValue() { 165 return multiValue; 166 } 167 168 public boolean getAllowFreeEntry() { 169 return allowFreeEntry; 170 } 171 172 public void setAllowFreeEntry(boolean allowFreeEntry) { 173 if (readOnly) 174 throw new RuntimeException (READ_ONLY_MESSAGE); 175 this.allowFreeEntry = allowFreeEntry; 176 } 177 178 public FieldTypeDocument getXml() { 179 FieldTypeDocument fieldTypeDocument = FieldTypeDocument.Factory.newInstance(); 180 FieldTypeDocument.FieldType fieldType = fieldTypeDocument.addNewFieldType(); 181 182 if (id != -1) { 183 fieldType.setId(id); 184 GregorianCalendar lastModified = new GregorianCalendar (); 185 lastModified.setTime(this.lastModified); 186 fieldType.setLastModified(lastModified); 187 fieldType.setLastModifier(lastModifier); 188 } 189 190 fieldType.setName(name); 191 fieldType.setValueType(valueType.toString()); 192 fieldType.setDeprecated(deprecated); 193 fieldType.setAclAllowed(aclAllowed); 194 fieldType.setMultiValue(multiValue); 195 fieldType.setLabels(label.getAsLabelsXml()); 196 fieldType.setDescriptions(description.getAsDescriptionsXml()); 197 fieldType.setUpdateCount(updateCount); 198 fieldType.setSize(size); 199 fieldType.setAllowFreeEntry(allowFreeEntry); 200 201 if (selectionList != null) { 202 selectionList.addToFieldTypeXml(fieldType.addNewSelectionList()); 203 } 204 205 return fieldTypeDocument; 206 } 207 208 public void setAllFromXml(FieldTypeDocument.FieldType fieldTypeXml) { 209 if (readOnly) 210 throw new RuntimeException (READ_ONLY_MESSAGE); 211 this.name = fieldTypeXml.getName(); 212 this.deprecated = fieldTypeXml.getDeprecated(); 213 this.aclAllowed = fieldTypeXml.getAclAllowed(); 214 this.label.clear(); 215 this.label.readFromLabelsXml(fieldTypeXml.getLabels()); 216 this.description.clear(); 217 this.description.readFromDescriptionsXml(fieldTypeXml.getDescriptions()); 218 this.size = fieldTypeXml.getSize(); 219 this.allowFreeEntry = fieldTypeXml.getAllowFreeEntry(); 220 221 if (fieldTypeXml.getSelectionList() != null) { 222 SelectionListDocument.SelectionList selectionListXml = fieldTypeXml.getSelectionList(); 223 if (selectionListXml.isSetStaticSelectionList()) { 224 StaticSelectionList selectionList = this.createStaticSelectionList(); 225 selectionList.setAllFromXml(fieldTypeXml.getSelectionList().getStaticSelectionList()); 226 this.selectionList = selectionList; 227 } else if (selectionListXml.isSetLinkQuerySelectionList()) { 228 this.selectionList = createLinkQuerySelectionList(selectionListXml.getLinkQuerySelectionList().getWhereClause(), 229 selectionListXml.getLinkQuerySelectionList().getFilterVariants()); 230 } else if (selectionListXml.isSetQuerySelectionList()) { 231 this.selectionList = createQuerySelectionList(selectionListXml.getQuerySelectionList().getQuery(), 232 selectionListXml.getQuerySelectionList().getFilterVariants(), 233 SortOrder.fromString(selectionListXml.getQuerySelectionList().getSortOrder())); 234 } 235 } else { 236 clearSelectionList(); 237 } 238 } 239 240 public void save() throws RepositoryException { 241 if (readOnly) 242 throw new RuntimeException (READ_ONLY_MESSAGE); 243 schemaStrategy.store(this); 244 } 245 246 public void setDeprecated(boolean deprecated) { 247 if (readOnly) 248 throw new RuntimeException (READ_ONLY_MESSAGE); 249 this.deprecated = deprecated; 250 } 251 252 public boolean isDeprecated() { 253 return deprecated; 254 } 255 256 public long getUpdateCount() { 257 return updateCount; 258 } 259 260 263 public void makeReadOnly() { 264 this.readOnly = true; 265 } 266 267 public boolean isReadOnly() { 268 return this.readOnly; 269 } 270 271 public SelectionList getSelectionList() { 272 return selectionList; 273 } 274 275 public void clearSelectionList() { 276 if (readOnly) 277 throw new RuntimeException (READ_ONLY_MESSAGE); 278 279 this.selectionList = null; 280 } 281 282 public StaticSelectionList createStaticSelectionList() { 283 if (readOnly) 284 throw new RuntimeException (READ_ONLY_MESSAGE); 285 286 StaticSelectionList newList = new StaticSelectionListImpl(this.schemaStrategy, valueType, this); 287 this.selectionList = newList; 288 289 return newList; 290 } 291 292 public LinkQuerySelectionList createLinkQuerySelectionList(String whereClause, boolean filterVariants) { 293 if (readOnly) 294 throw new RuntimeException (READ_ONLY_MESSAGE); 295 296 QueryManager queryManager = repository.getQueryManager(currentModifier); 297 LinkQuerySelectionListImpl newList = new LinkQuerySelectionListImpl(whereClause, filterVariants, queryManager, this); 298 this.selectionList = newList; 299 300 return newList; 301 } 302 303 public QuerySelectionList createQuerySelectionList(String query, boolean filterVariants, SortOrder sortOrder) { 304 if (readOnly) 305 throw new RuntimeException (READ_ONLY_MESSAGE); 306 307 QueryManager queryManager = repository.getQueryManager(currentModifier); 308 QuerySelectionListImpl newList = new QuerySelectionListImpl(query, filterVariants, sortOrder, queryManager, this); 309 this.selectionList = newList; 310 311 return newList; 312 } 313 314 public int getSize() { 315 return this.size; 316 } 317 318 public void setSize(int size) { 319 if (readOnly) 320 throw new RuntimeException (READ_ONLY_MESSAGE); 321 322 if (size < 0) 323 throw new RuntimeException ("Invalid size specified"); 324 325 this.size = size; 326 } 327 328 public class IntimateAccess { 329 private IntimateAccess() { 330 } 331 332 public long getLabelId() { 333 return labelId; 334 } 335 336 public void setLabelId(long labelId) { 337 if (readOnly) 338 throw new RuntimeException (READ_ONLY_MESSAGE); 339 FieldTypeImpl.this.labelId = labelId; 340 } 341 342 public long getDescriptionId() { 343 return descriptionId; 344 } 345 346 public void setDescriptionId(long descriptionId) { 347 if (readOnly) 348 throw new RuntimeException (READ_ONLY_MESSAGE); 349 FieldTypeImpl.this.descriptionId = descriptionId; 350 } 351 352 public AuthenticatedUser getCurrentModifier() { 353 return currentModifier; 354 } 355 356 public LocaleMap getLabels() { 357 if (readOnly) 358 throw new RuntimeException (READ_ONLY_MESSAGE); 359 return label; 360 } 361 362 public LocaleMap getDescriptions() { 363 if (readOnly) 364 throw new RuntimeException (READ_ONLY_MESSAGE); 365 return description; 366 } 367 368 public void setLastModified(Date lastModified) { 369 if (readOnly) 370 throw new RuntimeException (READ_ONLY_MESSAGE); 371 FieldTypeImpl.this.lastModified = lastModified; 372 } 373 374 public void setLastModifier(long lastModifier) { 375 if (readOnly) 376 throw new RuntimeException (READ_ONLY_MESSAGE); 377 FieldTypeImpl.this.lastModifier = lastModifier; 378 } 379 380 public void setId(long id) { 381 if (readOnly) 382 throw new RuntimeException (READ_ONLY_MESSAGE); 383 FieldTypeImpl.this.id = id; 384 } 385 386 public void setUpdateCount(long updateCount) { 387 if (readOnly) 388 throw new RuntimeException (READ_ONLY_MESSAGE); 389 FieldTypeImpl.this.updateCount = updateCount; 390 } 391 392 396 public void saved() { 397 } 398 399 public void setSize(int size) { 400 FieldTypeImpl.this.size = size; 401 } 402 403 public void setAllowFreeEntry(boolean allowFreeEntry) { 404 FieldTypeImpl.this.allowFreeEntry = allowFreeEntry; 405 } 406 407 public void setSelectionList(SelectionList selList) { 408 selectionList = selList; 409 } 410 } 411 } 412 | Popular Tags |