001/*
002 * (C) Copyright 2014 Nuxeo SA (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 *     <a href="mailto:grenard@nuxeo.com">Guillaume</a>
018 */
019package org.nuxeo.ecm.collections.core;
020
021import org.nuxeo.ecm.collections.api.CollectionLocationService;
022import org.nuxeo.ecm.collections.api.CollectionManager;
023import org.nuxeo.ecm.collections.api.FavoritesManager;
024import org.nuxeo.ecm.core.api.CoreSession;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.DocumentNotFoundException;
027import org.nuxeo.runtime.api.Framework;
028import org.nuxeo.runtime.model.DefaultComponent;
029
030/**
031 * @since 5.9.4
032 */
033public class FavoritesManagerImpl extends DefaultComponent implements FavoritesManager {
034
035    @Override
036    public void addToFavorites(DocumentModel document, CoreSession session) {
037        DocumentModel favorites = getFavorites(session);
038        if (favorites == null) {
039            throw new DocumentNotFoundException("No user favorites found");
040        }
041        final CollectionManager collectionManager = Framework.getService(CollectionManager.class);
042        collectionManager.addToCollection(favorites, document, session);
043    }
044
045    @Override
046    public boolean canAddToFavorites(DocumentModel document) {
047        final CollectionManager collectionManager = Framework.getService(CollectionManager.class);
048        return collectionManager.isCollectable(document);
049    }
050
051    @Override
052    @Deprecated
053    public DocumentModel getFavorites(final DocumentModel context, final CoreSession session) {
054        return getFavorites(session);
055    }
056
057    @Override
058    public DocumentModel getFavorites(final CoreSession session) {
059        return Framework.getService(CollectionLocationService.class).getUserFavorites(session);
060    }
061
062    @Override
063    public boolean isFavorite(DocumentModel document, CoreSession session) {
064        final CollectionManager collectionManager = Framework.getService(CollectionManager.class);
065        DocumentModel favorites = getFavorites(session);
066        return favorites != null && collectionManager.isInCollection(favorites, document, session);
067    }
068
069    @Override
070    public void removeFromFavorites(DocumentModel document, CoreSession session) {
071        DocumentModel favorites = getFavorites(session);
072        if (favorites == null) {
073            throw new DocumentNotFoundException("No user favorites found");
074        }
075        final CollectionManager collectionManager = Framework.getService(CollectionManager.class);
076        collectionManager.removeFromCollection(favorites, document, session);
077    }
078
079}