001/*
002 * (C) Copyright 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 *     Kevin Leturc <kleturc@nuxeo.com>
018 */
019package org.nuxeo.ecm.core.trash;
020
021import java.util.HashSet;
022import java.util.List;
023import java.util.Set;
024
025import org.nuxeo.ecm.core.api.CoreSession;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.DocumentRef;
028
029/**
030 * Trash service delegating to two different backends, for use during migration.
031 *
032 * @since 10.2
033 */
034public class BridgeTrashService extends AbstractTrashService {
035
036    protected final TrashService first;
037
038    protected final TrashService second;
039
040    public BridgeTrashService(TrashService first, TrashService second) {
041        this.first = first;
042        this.second = second;
043    }
044
045    @Override
046    public boolean isTrashed(CoreSession session, DocumentRef doc) {
047        return first.isTrashed(session, doc) || second.isTrashed(session, doc);
048    }
049
050    @Override
051    public void trashDocuments(List<DocumentModel> docs) {
052        // write to second only
053        second.trashDocuments(docs);
054    }
055
056    @Override
057    @SuppressWarnings("deprecation")
058    public Set<DocumentRef> undeleteDocuments(List<DocumentModel> docs) {
059        Set<DocumentRef> refs = new HashSet<>();
060        refs.addAll(second.undeleteDocuments(docs));
061        // write to first (basically lifeCycle service) to put documents in project lifeCycle state
062        refs.addAll(first.undeleteDocuments(docs));
063        return refs;
064    }
065
066    @Override
067    public boolean hasFeature(Feature feature) {
068        switch (feature) {
069            case TRASHED_STATE_IN_MIGRATION:
070                return true;
071            case TRASHED_STATE_IS_DEDUCED_FROM_LIFECYCLE:
072            case TRASHED_STATE_IS_DEDICATED_PROPERTY:
073                return false;
074            default:
075                throw new UnsupportedOperationException(feature.name());
076        }
077    }
078
079}