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