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