001/* 002 * (C) Copyright 2006-2018 Nuxeo (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 * Nuxeo - initial API and implementation 018 */ 019package org.nuxeo.ecm.core.event.impl; 020 021import java.io.Serializable; 022import java.util.Collection; 023import java.util.HashMap; 024import java.util.Map; 025import java.util.Set; 026 027import org.nuxeo.common.utils.Path; 028import org.nuxeo.ecm.core.api.CoreSession; 029import org.nuxeo.ecm.core.api.DataModel; 030import org.nuxeo.ecm.core.api.DocumentModel; 031import org.nuxeo.ecm.core.api.DocumentRef; 032import org.nuxeo.ecm.core.api.IdRef; 033import org.nuxeo.ecm.core.api.LifeCycleConstants; 034import org.nuxeo.ecm.core.api.Lock; 035import org.nuxeo.ecm.core.api.NuxeoPrincipal; 036import org.nuxeo.ecm.core.api.PathRef; 037import org.nuxeo.ecm.core.api.PropertyException; 038import org.nuxeo.ecm.core.api.VersioningOption; 039import org.nuxeo.ecm.core.api.model.DocumentPart; 040import org.nuxeo.ecm.core.api.model.Property; 041import org.nuxeo.ecm.core.api.model.PropertyVisitor; 042import org.nuxeo.ecm.core.api.model.resolver.DocumentPropertyObjectResolverImpl; 043import org.nuxeo.ecm.core.api.model.resolver.PropertyObjectResolver; 044import org.nuxeo.ecm.core.api.security.ACP; 045import org.nuxeo.ecm.core.event.Event; 046import org.nuxeo.ecm.core.event.EventBundle; 047import org.nuxeo.ecm.core.schema.DocumentType; 048 049/** 050 * Light weight {@link DocumentModel} implementation Only holds {@link DocumentRef}, RepositoryName, name, path and 051 * context data. Used to reduce memory footprint of {@link Event} stacked in {@link EventBundle}. 052 * 053 * @author Thierry Delprat 054 */ 055public class ShallowDocumentModel implements DocumentModel { 056 057 private static final long serialVersionUID = 1L; 058 059 private final String id; 060 061 private final String repoName; 062 063 private final String name; 064 065 private final Path path; 066 067 private final String type; 068 069 private final boolean isFolder; 070 071 private final boolean isVersion; 072 073 private final boolean isProxy; 074 075 private final boolean isImmutable; 076 077 private final Map<String, Serializable> contextData; 078 079 private final Set<String> facets; 080 081 private final String lifecycleState; 082 083 public ShallowDocumentModel(DocumentModel doc) { 084 id = doc.getId(); 085 repoName = doc.getRepositoryName(); 086 name = doc.getName(); 087 path = doc.getPath(); 088 type = doc.getType(); 089 isFolder = doc.isFolder(); 090 isVersion = doc.isVersion(); 091 isProxy = doc.isProxy(); 092 isImmutable = doc.isImmutable(); 093 contextData = new HashMap<>(doc.getContextData()); 094 facets = doc.getFacets(); 095 if (doc.isLifeCycleLoaded()) { 096 lifecycleState = doc.getCurrentLifeCycleState(); 097 } else { 098 lifecycleState = null; 099 } 100 } 101 102 public ShallowDocumentModel(String id, String repoName, String name, Path path, String type, boolean isFolder, 103 boolean isVersion, boolean isProxy, boolean isImmutable, Map<String, Serializable> contextData, 104 Set<String> facets, String lifecycleState) { 105 this.id = id; 106 this.repoName = repoName; 107 this.name = name; 108 this.path = path; 109 this.type = type; 110 this.isFolder = isFolder; 111 this.isVersion = isVersion; 112 this.isProxy = isProxy; 113 this.isImmutable = isImmutable; 114 this.contextData = new HashMap<>(contextData); 115 this.facets = facets; 116 this.lifecycleState = lifecycleState; 117 } 118 119 @Override 120 public String getId() { 121 return id; 122 } 123 124 @Override 125 public DocumentRef getRef() { 126 return id == null ? null : new IdRef(id); 127 } 128 129 @Override 130 public String getRepositoryName() { 131 return repoName; 132 } 133 134 @Override 135 public String getName() { 136 return name; 137 } 138 139 @Override 140 public Long getPos() { 141 return null; 142 } 143 144 @Override 145 public Path getPath() { 146 return path; 147 } 148 149 @Override 150 public String getPathAsString() { 151 if (path != null) { 152 return path.toString(); 153 } 154 return null; 155 } 156 157 @Override 158 public DocumentRef getParentRef() { 159 if (path != null) { 160 return new PathRef(path.removeLastSegments(1).toString()); 161 } 162 return null; 163 } 164 165 @Override 166 public String getType() { 167 return type; 168 } 169 170 @Override 171 public boolean isTrashed() { 172 return LifeCycleConstants.DELETED_STATE.equals(lifecycleState); 173 } 174 175 @Override 176 public boolean isFolder() { 177 return isFolder; 178 } 179 180 @Override 181 public boolean isVersion() { 182 return isVersion; 183 } 184 185 @Override 186 public void copyContent(DocumentModel sourceDoc) { 187 throw new UnsupportedOperationException(); 188 } 189 190 @Override 191 public void copyContextData(DocumentModel otherDocument) { 192 throw new UnsupportedOperationException(); 193 } 194 195 @Override 196 public boolean followTransition(String transition) { 197 throw new UnsupportedOperationException(); 198 } 199 200 @Override 201 public ACP getACP() { 202 throw new UnsupportedOperationException(); 203 } 204 205 @Override 206 public void accept(PropertyVisitor visitor, Object arg) { 207 throw new UnsupportedOperationException(); 208 } 209 210 @Override 211 public <T> T getAdapter(Class<T> itf) { 212 throw new UnsupportedOperationException(); 213 } 214 215 @Override 216 public <T> T getAdapter(Class<T> itf, boolean refreshCache) { 217 throw new UnsupportedOperationException(); 218 } 219 220 @Override 221 public Collection<String> getAllowedStateTransitions() { 222 throw new UnsupportedOperationException(); 223 } 224 225 @Override 226 public String getCacheKey() { 227 throw new UnsupportedOperationException(); 228 } 229 230 @Override 231 public Map<String, Serializable> getContextData() { 232 return contextData; 233 } 234 235 @Override 236 public CoreSession getCoreSession() { 237 throw new UnsupportedOperationException(); 238 } 239 240 @Override 241 public void detach(boolean loadAll) { 242 } 243 244 @Override 245 public void attach(CoreSession coreSession) { 246 } 247 248 @Override 249 public boolean isAttached() { 250 return false; 251 } 252 253 @Override 254 public String getCurrentLifeCycleState() { 255 return lifecycleState; 256 } 257 258 @Override 259 @Deprecated 260 public DataModel getDataModel(String schema) { 261 throw new UnsupportedOperationException(); 262 } 263 264 @Override 265 @Deprecated 266 public Map<String, DataModel> getDataModels() { 267 throw new UnsupportedOperationException(); 268 } 269 270 @Override 271 @Deprecated 272 public Collection<DataModel> getDataModelsCollection() { 273 throw new UnsupportedOperationException(); 274 } 275 276 @Override 277 public Set<String> getFacets() { 278 return facets; 279 } 280 281 @Override 282 public String[] getSchemas() { 283 throw new UnsupportedOperationException(); 284 } 285 286 @Override 287 public DocumentType getDocumentType() { 288 throw new UnsupportedOperationException(); 289 } 290 291 @Override 292 public String getLifeCyclePolicy() { 293 throw new UnsupportedOperationException(); 294 } 295 296 @Override 297 @Deprecated 298 public DocumentPart getPart(String schema) { 299 throw new UnsupportedOperationException(); 300 } 301 302 @Override 303 @Deprecated 304 public DocumentPart[] getParts() { 305 throw new UnsupportedOperationException(); 306 } 307 308 @Override 309 public Collection<Property> getPropertyObjects(String schema) { 310 throw new UnsupportedOperationException(); 311 } 312 313 @Override 314 public Map<String, Object> getProperties(String schemaName) { 315 throw new UnsupportedOperationException(); 316 } 317 318 @Override 319 public Property getProperty(String xpath) throws PropertyException { 320 throw new UnsupportedOperationException(); 321 } 322 323 @Override 324 public Object getProperty(String schemaName, String name) { 325 throw new UnsupportedOperationException(); 326 } 327 328 @Override 329 public Property getPropertyObject(String schema, String name) { 330 throw new UnsupportedOperationException(); 331 } 332 333 @Override 334 public Serializable getPropertyValue(String xpath) throws PropertyException { 335 throw new UnsupportedOperationException(); 336 } 337 338 @Override 339 public String getSessionId() { 340 throw new UnsupportedOperationException(); 341 } 342 343 @Override 344 public NuxeoPrincipal getPrincipal() { 345 throw new UnsupportedOperationException(); 346 } 347 348 @Override 349 public String getSourceId() { 350 throw new UnsupportedOperationException(); 351 } 352 353 @Override 354 public <T extends Serializable> T getSystemProp(String systemProperty, Class<T> type) { 355 throw new UnsupportedOperationException(); 356 } 357 358 @Override 359 public String getTitle() { 360 throw new UnsupportedOperationException(); 361 } 362 363 @Override 364 public String getVersionLabel() { 365 throw new UnsupportedOperationException(); 366 } 367 368 @Override 369 public String getCheckinComment() { 370 throw new UnsupportedOperationException(); 371 } 372 373 @Override 374 public boolean hasFacet(String facet) { 375 return facets.contains(facet); 376 } 377 378 @Override 379 public boolean hasSchema(String schema) { 380 throw new UnsupportedOperationException(); 381 } 382 383 @Override 384 public boolean addFacet(String facet) { 385 throw new UnsupportedOperationException(); 386 } 387 388 @Override 389 public boolean removeFacet(String facet) { 390 throw new UnsupportedOperationException(); 391 } 392 393 @Override 394 public boolean isDownloadable() { 395 throw new UnsupportedOperationException(); 396 } 397 398 @Override 399 public boolean isLifeCycleLoaded() { 400 return lifecycleState != null; 401 } 402 403 @Override 404 public boolean isLocked() { 405 throw new UnsupportedOperationException(); 406 } 407 408 @Override 409 public boolean isProxy() { 410 return isProxy; 411 } 412 413 @Override 414 public boolean isImmutable() { 415 return isImmutable; 416 } 417 418 @Override 419 public boolean isDirty() { 420 throw new UnsupportedOperationException(); 421 } 422 423 @Override 424 public boolean isVersionable() { 425 throw new UnsupportedOperationException(); 426 } 427 428 @Override 429 public boolean isPrefetched(String xpath) { 430 return false; 431 } 432 433 @Override 434 public boolean isPrefetched(String schemaName, String name) { 435 return false; 436 } 437 438 @Override 439 public void prefetchCurrentLifecycleState(String lifecycle) { 440 throw new UnsupportedOperationException(); 441 } 442 443 @Override 444 public void prefetchLifeCyclePolicy(String lifeCyclePolicy) { 445 throw new UnsupportedOperationException(); 446 } 447 448 @Override 449 public void putContextData(String key, Serializable value) { 450 throw new UnsupportedOperationException(); 451 } 452 453 @Override 454 public void refresh() { 455 throw new UnsupportedOperationException(); 456 } 457 458 @Override 459 public void refresh(int refreshFlags, String[] schemas) { 460 throw new UnsupportedOperationException(); 461 } 462 463 @Override 464 public void reset() { 465 throw new UnsupportedOperationException(); 466 } 467 468 @Override 469 public void setACP(ACP acp, boolean overwrite) { 470 throw new UnsupportedOperationException(); 471 } 472 473 @Override 474 public Lock setLock() { 475 throw new UnsupportedOperationException(); 476 } 477 478 @Override 479 public Lock getLockInfo() { 480 throw new UnsupportedOperationException(); 481 } 482 483 @Override 484 public Lock removeLock() { 485 throw new UnsupportedOperationException(); 486 } 487 488 @Override 489 public void setPathInfo(String parentPath, String name) { 490 throw new UnsupportedOperationException(); 491 } 492 493 @Override 494 public void setProperties(String schemaName, Map<String, Object> data) { 495 throw new UnsupportedOperationException(); 496 } 497 498 @Override 499 public void setProperty(String schemaName, String name, Object value) { 500 throw new UnsupportedOperationException(); 501 } 502 503 @Override 504 public void setPropertyValue(String xpath, Serializable value) { 505 throw new UnsupportedOperationException(); 506 } 507 508 @Override 509 public DocumentModel clone() throws CloneNotSupportedException { 510 throw new CloneNotSupportedException(); 511 } 512 513 @Override 514 public Serializable getContextData(String key) { 515 if (contextData == null) { 516 return null; 517 } 518 return contextData.get(key); 519 } 520 521 @Override 522 public boolean isCheckedOut() { 523 throw new UnsupportedOperationException(); 524 } 525 526 @Override 527 public void checkOut() { 528 throw new UnsupportedOperationException(); 529 } 530 531 @Override 532 public DocumentRef checkIn(VersioningOption option, String checkinComment) { 533 throw new UnsupportedOperationException(); 534 } 535 536 @Override 537 public String getVersionSeriesId() { 538 throw new UnsupportedOperationException(); 539 } 540 541 @Override 542 public boolean isLatestVersion() { 543 return false; 544 } 545 546 @Override 547 public boolean isMajorVersion() { 548 return false; 549 } 550 551 @Override 552 public boolean isLatestMajorVersion() { 553 return false; 554 } 555 556 @Override 557 public boolean isVersionSeriesCheckedOut() { 558 return true; 559 } 560 561 @Override 562 public String getChangeToken() { 563 return null; 564 } 565 566 @Override 567 public Map<String, String> getBinaryFulltext() { 568 return null; 569 } 570 571 @Override 572 public PropertyObjectResolver getObjectResolver(String xpath) { 573 return DocumentPropertyObjectResolverImpl.create(this, xpath); 574 } 575 576}