001/* 002 * (C) Copyright 2006-2014 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * Florent Guillaume 018 */ 019package org.nuxeo.ecm.core.storage.sql.coremodel; 020 021import java.io.Serializable; 022import java.util.ArrayList; 023import java.util.Calendar; 024import java.util.Collection; 025import java.util.Collections; 026import java.util.HashMap; 027import java.util.List; 028import java.util.Map; 029import java.util.Set; 030import java.util.function.Consumer; 031 032import org.nuxeo.ecm.core.NXCore; 033import org.nuxeo.ecm.core.api.DocumentNotFoundException; 034import org.nuxeo.ecm.core.api.LifeCycleException; 035import org.nuxeo.ecm.core.api.Lock; 036import org.nuxeo.ecm.core.api.NuxeoException; 037import org.nuxeo.ecm.core.api.PropertyException; 038import org.nuxeo.ecm.core.api.model.DocumentPart; 039import org.nuxeo.ecm.core.api.model.Property; 040import org.nuxeo.ecm.core.api.model.PropertyNotFoundException; 041import org.nuxeo.ecm.core.api.model.impl.ComplexProperty; 042import org.nuxeo.ecm.core.blob.BlobManager; 043import org.nuxeo.ecm.core.lifecycle.LifeCycle; 044import org.nuxeo.ecm.core.lifecycle.LifeCycleService; 045import org.nuxeo.ecm.core.model.Document; 046import org.nuxeo.ecm.core.schema.DocumentType; 047import org.nuxeo.ecm.core.schema.SchemaManager; 048import org.nuxeo.ecm.core.schema.types.ComplexType; 049import org.nuxeo.ecm.core.schema.types.Field; 050import org.nuxeo.ecm.core.schema.types.ListType; 051import org.nuxeo.ecm.core.schema.types.Schema; 052import org.nuxeo.ecm.core.schema.types.Type; 053import org.nuxeo.ecm.core.storage.BaseDocument; 054import org.nuxeo.ecm.core.storage.sql.Model; 055import org.nuxeo.ecm.core.storage.sql.Node; 056import org.nuxeo.runtime.api.Framework; 057 058public class SQLDocumentLive extends BaseDocument<Node>implements SQLDocument { 059 060 protected final Node node; 061 062 protected final Type type; 063 064 protected SQLSession session; 065 066 /** Proxy-induced types. */ 067 protected final List<Schema> proxySchemas; 068 069 /** 070 * Read-only flag, used to allow/disallow writes on versions. 071 */ 072 protected boolean readonly; 073 074 protected SQLDocumentLive(Node node, ComplexType type, SQLSession session, boolean readonly) { 075 this.node = node; 076 this.type = type; 077 this.session = session; 078 if (node != null && node.isProxy()) { 079 SchemaManager schemaManager = Framework.getLocalService(SchemaManager.class); 080 proxySchemas = schemaManager.getProxySchemas(type.getName()); 081 } else { 082 proxySchemas = null; 083 } 084 this.readonly = readonly; 085 } 086 087 @Override 088 public void setReadOnly(boolean readonly) { 089 this.readonly = readonly; 090 } 091 092 @Override 093 public boolean isReadOnly() { 094 return readonly; 095 } 096 097 @Override 098 public Node getNode() { 099 return node; 100 } 101 102 @Override 103 public String getName() { 104 return getNode() == null ? null : getNode().getName(); 105 } 106 107 @Override 108 public Long getPos() { 109 return getNode().getPos(); 110 } 111 112 /* 113 * ----- org.nuxeo.ecm.core.model.Document ----- 114 */ 115 116 @Override 117 public DocumentType getType() { 118 return (DocumentType) type; 119 } 120 121 @Override 122 public SQLSession getSession() { 123 return session; 124 } 125 126 @Override 127 public boolean isFolder() { 128 return type == null // null document 129 || ((DocumentType) type).isFolder(); 130 } 131 132 @Override 133 public String getUUID() { 134 return session.idToString(getNode().getId()); 135 } 136 137 @Override 138 public Document getParent() { 139 return session.getParent(getNode()); 140 } 141 142 @Override 143 public String getPath() { 144 return session.getPath(getNode()); 145 } 146 147 @Override 148 public boolean isProxy() { 149 return false; 150 } 151 152 @Override 153 public String getRepositoryName() { 154 return session.getRepositoryName(); 155 } 156 157 @Override 158 protected List<Schema> getProxySchemas() { 159 return proxySchemas; 160 } 161 162 @Override 163 public void remove() { 164 session.remove(getNode()); 165 } 166 167 /** 168 * Reads into the {@link DocumentPart} the values from this {@link SQLDocument}. 169 */ 170 @Override 171 public void readDocumentPart(DocumentPart dp) throws PropertyException { 172 readComplexProperty(getNode(), (ComplexProperty) dp); 173 } 174 175 @Override 176 public Map<String, Serializable> readPrefetch(ComplexType complexType, Set<String> xpaths) 177 throws PropertyException { 178 return readPrefetch(getNode(), complexType, xpaths); 179 } 180 181 @Override 182 public boolean writeDocumentPart(DocumentPart dp, WriteContext writeContext) throws PropertyException { 183 boolean changed = writeComplexProperty(getNode(), (ComplexProperty) dp, writeContext); 184 clearDirtyFlags(dp); 185 return changed; 186 } 187 188 @Override 189 protected Node getChild(Node node, String name, Type type) throws PropertyException { 190 return session.getChildProperty(node, name, type.getName()); 191 } 192 193 @Override 194 protected Node getChildForWrite(Node node, String name, Type type) throws PropertyException { 195 return session.getChildPropertyForWrite(node, name, type.getName()); 196 } 197 198 @Override 199 protected List<Node> getChildAsList(Node node, String name) throws PropertyException { 200 return session.getComplexList(node, name); 201 } 202 203 @Override 204 protected void updateList(Node node, String name, Field field, String xpath, List<Object> values) 205 throws PropertyException { 206 List<Node> childNodes = getChildAsList(node, name); 207 int oldSize = childNodes.size(); 208 int newSize = values.size(); 209 // remove extra list elements 210 if (oldSize > newSize) { 211 for (int i = oldSize - 1; i >= newSize; i--) { 212 session.removeProperty(childNodes.remove(i)); 213 } 214 } 215 // add new list elements 216 if (oldSize < newSize) { 217 String typeName = field.getType().getName(); 218 for (int i = oldSize; i < newSize; i++) { 219 Node childNode = session.addChildProperty(node, name, Long.valueOf(i), typeName); 220 childNodes.add(childNode); 221 } 222 } 223 // write values 224 int i = 0; 225 for (Object v : values) { 226 Node childNode = childNodes.get(i); 227 setValueComplex(childNode, field, xpath + '/' + i, v); 228 i++; 229 } 230 } 231 232 @Override 233 protected List<Node> updateList(Node node, String name, Property property) throws PropertyException { 234 Collection<Property> properties = property.getChildren(); 235 List<Node> childNodes = getChildAsList(node, name); 236 int oldSize = childNodes.size(); 237 int newSize = properties.size(); 238 // remove extra list elements 239 if (oldSize > newSize) { 240 for (int i = oldSize - 1; i >= newSize; i--) { 241 session.removeProperty(childNodes.remove(i)); 242 } 243 } 244 // add new list elements 245 if (oldSize < newSize) { 246 String typeName = ((ListType) property.getType()).getFieldType().getName(); 247 for (int i = oldSize; i < newSize; i++) { 248 Node childNode = session.addChildProperty(node, name, Long.valueOf(i), typeName); 249 childNodes.add(childNode); 250 } 251 } 252 return childNodes; 253 } 254 255 @Override 256 protected String internalName(String name) { 257 return name; 258 } 259 260 @Override 261 public Object getValue(String xpath) throws PropertyException { 262 return getValueObject(getNode(), xpath); 263 } 264 265 @Override 266 public void setValue(String xpath, Object value) throws PropertyException { 267 setValueObject(getNode(), xpath, value); 268 } 269 270 @Override 271 public void visitBlobs(Consumer<BlobAccessor> blobVisitor) throws PropertyException { 272 visitBlobs(getNode(), blobVisitor, NO_DIRTY); 273 } 274 275 @Override 276 public Serializable getPropertyValue(String name) { 277 return getNode().getSimpleProperty(name).getValue(); 278 } 279 280 @Override 281 public void setPropertyValue(String name, Serializable value) { 282 getNode().setSimpleProperty(name, value); 283 } 284 285 protected static final Map<String, String> systemPropNameMap; 286 287 static { 288 systemPropNameMap = new HashMap<String, String>(); 289 systemPropNameMap.put(FULLTEXT_JOBID_SYS_PROP, Model.FULLTEXT_JOBID_PROP); 290 } 291 292 @Override 293 public void setSystemProp(String name, Serializable value) { 294 String propertyName; 295 if (name.startsWith(SIMPLE_TEXT_SYS_PROP)) { 296 propertyName = name.replace(SIMPLE_TEXT_SYS_PROP, Model.FULLTEXT_SIMPLETEXT_PROP); 297 } else if (name.startsWith(BINARY_TEXT_SYS_PROP)) { 298 propertyName = name.replace(BINARY_TEXT_SYS_PROP, Model.FULLTEXT_BINARYTEXT_PROP); 299 } else { 300 propertyName = systemPropNameMap.get(name); 301 } 302 if (propertyName == null) { 303 throw new PropertyNotFoundException(name, "Unknown system property"); 304 } 305 setPropertyValue(propertyName, value); 306 } 307 308 @Override 309 @SuppressWarnings("unchecked") 310 public <T extends Serializable> T getSystemProp(String name, Class<T> type) { 311 String propertyName = systemPropNameMap.get(name); 312 if (propertyName == null) { 313 throw new PropertyNotFoundException(name, "Unknown system property"); 314 } 315 Serializable value = getPropertyValue(propertyName); 316 if (value == null) { 317 if (type == Boolean.class) { 318 value = Boolean.FALSE; 319 } else if (type == Long.class) { 320 value = Long.valueOf(0); 321 } 322 } 323 return (T) value; 324 } 325 326 @Override 327 public String getChangeToken() { 328 if (session.isChangeTokenEnabled()) { 329 return (String) getPropertyValue(Model.MAIN_CHANGE_TOKEN_PROP); 330 } else { 331 Calendar modified; 332 try { 333 modified = (Calendar) getPropertyValue(DC_MODIFIED); 334 } catch (PropertyNotFoundException e) { 335 modified = null; 336 } 337 return modified == null ? null : String.valueOf(modified.getTimeInMillis()); 338 } 339 } 340 341 /* 342 * ----- LifeCycle ----- 343 */ 344 345 @Override 346 public String getLifeCyclePolicy() { 347 return (String) getPropertyValue(Model.MISC_LIFECYCLE_POLICY_PROP); 348 } 349 350 @Override 351 public void setLifeCyclePolicy(String policy) { 352 setPropertyValue(Model.MISC_LIFECYCLE_POLICY_PROP, policy); 353 BlobManager blobManager = Framework.getService(BlobManager.class); 354 blobManager.notifyChanges(this, Collections.singleton(Model.MISC_LIFECYCLE_POLICY_PROP)); 355 } 356 357 @Override 358 public String getLifeCycleState() { 359 return (String) getPropertyValue(Model.MISC_LIFECYCLE_STATE_PROP); 360 } 361 362 @Override 363 public void setCurrentLifeCycleState(String state) { 364 setPropertyValue(Model.MISC_LIFECYCLE_STATE_PROP, state); 365 BlobManager blobManager = Framework.getService(BlobManager.class); 366 blobManager.notifyChanges(this, Collections.singleton(Model.MISC_LIFECYCLE_STATE_PROP)); 367 } 368 369 @Override 370 public void followTransition(String transition) throws LifeCycleException { 371 LifeCycleService service = NXCore.getLifeCycleService(); 372 if (service == null) { 373 throw new NuxeoException("LifeCycleService not available"); 374 } 375 service.followTransition(this, transition); 376 } 377 378 @Override 379 public Collection<String> getAllowedStateTransitions() { 380 LifeCycleService service = NXCore.getLifeCycleService(); 381 if (service == null) { 382 throw new NuxeoException("LifeCycleService not available"); 383 } 384 LifeCycle lifeCycle = service.getLifeCycleFor(this); 385 if (lifeCycle == null) { 386 return Collections.emptyList(); 387 } 388 return lifeCycle.getAllowedStateTransitionsFrom(getLifeCycleState()); 389 } 390 391 /* 392 * ----- org.nuxeo.ecm.core.versioning.VersionableDocument ----- 393 */ 394 395 @Override 396 public boolean isVersion() { 397 return false; 398 } 399 400 @Override 401 public Document getBaseVersion() { 402 if (isCheckedOut()) { 403 return null; 404 } 405 Serializable id = (Serializable) getPropertyValue(Model.MAIN_BASE_VERSION_PROP); 406 if (id == null) { 407 // shouldn't happen 408 return null; 409 } 410 return session.getDocumentById(id); 411 } 412 413 @Override 414 public String getVersionSeriesId() { 415 return getUUID(); 416 } 417 418 @Override 419 public Document getSourceDocument() { 420 return this; 421 } 422 423 @Override 424 public Document checkIn(String label, String checkinComment) { 425 Document version = session.checkIn(getNode(), label, checkinComment); 426 Framework.getService(BlobManager.class).freezeVersion(version); 427 return version; 428 } 429 430 @Override 431 public void checkOut() { 432 session.checkOut(getNode()); 433 } 434 435 @Override 436 public boolean isCheckedOut() { 437 return !Boolean.TRUE.equals(getPropertyValue(Model.MAIN_CHECKED_IN_PROP)); 438 } 439 440 @Override 441 public boolean isMajorVersion() { 442 return false; 443 } 444 445 @Override 446 public boolean isLatestVersion() { 447 return false; 448 } 449 450 @Override 451 public boolean isLatestMajorVersion() { 452 return false; 453 } 454 455 @Override 456 public boolean isVersionSeriesCheckedOut() { 457 return isCheckedOut(); 458 } 459 460 @Override 461 public String getVersionLabel() { 462 return (String) getPropertyValue(Model.VERSION_LABEL_PROP); 463 } 464 465 @Override 466 public String getCheckinComment() { 467 return (String) getPropertyValue(Model.VERSION_DESCRIPTION_PROP); 468 } 469 470 @Override 471 public Document getWorkingCopy() { 472 return this; 473 } 474 475 @Override 476 public Calendar getVersionCreationDate() { 477 return (Calendar) getPropertyValue(Model.VERSION_CREATED_PROP); 478 } 479 480 @Override 481 public void restore(Document version) { 482 if (!version.isVersion()) { 483 throw new NuxeoException("Cannot restore a non-version: " + version); 484 } 485 session.restore(getNode(), ((SQLDocument) version).getNode()); 486 } 487 488 @Override 489 public List<String> getVersionsIds() { 490 String versionSeriesId = getVersionSeriesId(); 491 Collection<Document> versions = session.getVersions(versionSeriesId); 492 List<String> ids = new ArrayList<String>(versions.size()); 493 for (Document version : versions) { 494 ids.add(version.getUUID()); 495 } 496 return ids; 497 } 498 499 @Override 500 public Document getVersion(String label) { 501 String versionSeriesId = getVersionSeriesId(); 502 return session.getVersionByLabel(versionSeriesId, label); 503 } 504 505 @Override 506 public List<Document> getVersions() { 507 String versionSeriesId = getVersionSeriesId(); 508 return session.getVersions(versionSeriesId); 509 } 510 511 @Override 512 public Document getLastVersion() { 513 String versionSeriesId = getVersionSeriesId(); 514 return session.getLastVersion(versionSeriesId); 515 } 516 517 @Override 518 public Document getChild(String name) { 519 return session.getChild(getNode(), name); 520 } 521 522 @Override 523 public List<Document> getChildren() { 524 if (!isFolder()) { 525 return Collections.emptyList(); 526 } 527 return session.getChildren(getNode()); // newly allocated 528 } 529 530 @Override 531 public List<String> getChildrenIds() { 532 if (!isFolder()) { 533 return Collections.emptyList(); 534 } 535 // not optimized as this method doesn't seem to be used 536 List<Document> children = session.getChildren(getNode()); 537 List<String> ids = new ArrayList<String>(children.size()); 538 for (Document child : children) { 539 ids.add(child.getUUID()); 540 } 541 return ids; 542 } 543 544 @Override 545 public boolean hasChild(String name) { 546 if (!isFolder()) { 547 return false; 548 } 549 return session.hasChild(getNode(), name); 550 } 551 552 @Override 553 public boolean hasChildren() { 554 if (!isFolder()) { 555 return false; 556 } 557 return session.hasChildren(getNode()); 558 } 559 560 @Override 561 public Document addChild(String name, String typeName) { 562 if (!isFolder()) { 563 throw new IllegalArgumentException("Not a folder"); 564 } 565 return session.addChild(getNode(), name, null, typeName); 566 } 567 568 @Override 569 public void orderBefore(String src, String dest) { 570 SQLDocument srcDoc = (SQLDocument) getChild(src); 571 if (srcDoc == null) { 572 throw new DocumentNotFoundException("Document " + this + " has no child: " + src); 573 } 574 SQLDocument destDoc; 575 if (dest == null) { 576 destDoc = null; 577 } else { 578 destDoc = (SQLDocument) getChild(dest); 579 if (destDoc == null) { 580 throw new DocumentNotFoundException("Document " + this + " has no child: " + dest); 581 } 582 } 583 session.orderBefore(getNode(), srcDoc.getNode(), destDoc == null ? null : destDoc.getNode()); 584 } 585 586 @Override 587 public Set<String> getAllFacets() { 588 return getNode().getAllMixinTypes(); 589 } 590 591 @Override 592 public String[] getFacets() { 593 return getNode().getMixinTypes(); 594 } 595 596 @Override 597 public boolean hasFacet(String facet) { 598 return getNode().hasMixinType(facet); 599 } 600 601 @Override 602 public boolean addFacet(String facet) { 603 return session.addMixinType(getNode(), facet); 604 } 605 606 @Override 607 public boolean removeFacet(String facet) { 608 return session.removeMixinType(getNode(), facet); 609 } 610 611 /* 612 * ----- PropertyContainer inherited from SQLComplexProperty ----- 613 */ 614 615 /* 616 * ----- toString/equals/hashcode ----- 617 */ 618 619 @Override 620 public String toString() { 621 return getClass().getSimpleName() + '(' + getName() + ',' + getUUID() + ')'; 622 } 623 624 @Override 625 public boolean equals(Object other) { 626 if (other == this) { 627 return true; 628 } 629 if (other == null) { 630 return false; 631 } 632 if (other.getClass() == this.getClass()) { 633 return equals((SQLDocumentLive) other); 634 } 635 return false; 636 } 637 638 private boolean equals(SQLDocumentLive other) { 639 return getNode().equals(other.getNode()); 640 } 641 642 @Override 643 public int hashCode() { 644 return getNode().hashCode(); 645 } 646 647 @Override 648 public Document getTargetDocument() { 649 return null; 650 } 651 652 @Override 653 public void setTargetDocument(Document target) { 654 throw new NuxeoException("Not a proxy"); 655 } 656 657 @Override 658 protected Lock getDocumentLock() { 659 // lock manager can get the lock even on a recently created and unsaved document 660 throw new UnsupportedOperationException(); 661 } 662 663 @Override 664 protected Lock setDocumentLock(Lock lock) { 665 // lock manager can set the lock even on a recently created and unsaved document 666 throw new UnsupportedOperationException(); 667 } 668 669 @Override 670 protected Lock removeDocumentLock(String owner) { 671 // lock manager can remove the lock even on a recently created and unsaved document 672 throw new UnsupportedOperationException(); 673 } 674 675}