001/*
002 * (C) Copyright 2017 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 *     Florent Guillaume
018 */
019package org.nuxeo.ecm.platform.tag;
020
021import java.util.ArrayList;
022import java.util.HashSet;
023import java.util.List;
024import java.util.Set;
025
026import org.nuxeo.ecm.core.api.CoreSession;
027
028/**
029 * Tag Service delegating to two different backends, for use during migration.
030 *
031 * @since 9.3
032 */
033public class BridgeTagService extends AbstractTagService {
034
035    protected final AbstractTagService first;
036
037    protected final AbstractTagService second;
038
039    public BridgeTagService(TagService first, TagService second) {
040        this.first = (AbstractTagService) first;
041        this.second = (AbstractTagService) second;
042    }
043
044    @Override
045    public boolean hasFeature(Feature feature) {
046        switch (feature) {
047        case TAGS_BELONG_TO_DOCUMENT:
048            return false;
049        default:
050            throw new UnsupportedOperationException(feature.name());
051        }
052    }
053
054    @Override
055    public boolean supportsTag(CoreSession session, String docId) {
056        return first.supportsTag(session, docId) || second.supportsTag(session, docId);
057    }
058
059    @SuppressWarnings("deprecation")
060    @Override
061    public List<Tag> getTagCloud(CoreSession session, String docId, String username, Boolean normalize) {
062        return first.getTagCloud(session, docId, username, normalize);
063    }
064
065    @Override
066    public void doTag(CoreSession session, String docId, String label, String username) {
067        // write to second only
068        second.doTag(session, docId, label, username);
069    }
070
071    @Override
072    public boolean canUntag(CoreSession session, String docId, String label) {
073        // if the tag is not present we can untag, so we have to be careful doing the checks
074        // we call getTags and not doGetTags because the query need to be privileged
075        boolean can1 = !first.getTags(session, docId).contains(label) || first.canUntag(session, docId, label);
076        boolean can2 = !second.getTags(session, docId).contains(label) || second.canUntag(session, docId, label);
077        return can1 && can2;
078    }
079
080    @Override
081    public void doUntag(CoreSession session, String docId, String label) {
082        first.doUntag(session, docId, label);
083        second.doUntag(session, docId, label);
084    }
085
086    @Override
087    public Set<String> doGetTags(CoreSession session, String docId) {
088        Set<String> tags = new HashSet<>();
089        tags.addAll(first.doGetTags(session, docId));
090        tags.addAll(second.doGetTags(session, docId));
091        return tags;
092    }
093
094    @Override
095    public void doCopyTags(CoreSession session, String srcDocId, String dstDocId, boolean removeExistingTags) {
096        first.doCopyTags(session, srcDocId, dstDocId, removeExistingTags);
097        second.doCopyTags(session, srcDocId, dstDocId, removeExistingTags);
098    }
099
100    @Override
101    public List<String> doGetTagDocumentIds(CoreSession session, String label) {
102        Set<String> ids = new HashSet<>();
103        ids.addAll(first.doGetTagDocumentIds(session, label));
104        ids.addAll(second.doGetTagDocumentIds(session, label));
105        return new ArrayList<>(ids);
106    }
107
108    @Override
109    public Set<String> doGetTagSuggestions(CoreSession session, String label) {
110        Set<String> tags = new HashSet<>();
111        tags.addAll(first.doGetTagSuggestions(session, label));
112        tags.addAll(second.doGetTagSuggestions(session, label));
113        return tags;
114    }
115
116}