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.webapp.collections;
020
021import java.io.Serializable;
022import java.util.List;
023
024import org.jboss.seam.Component;
025import org.jboss.seam.ScopeType;
026import org.jboss.seam.annotations.Name;
027import org.jboss.seam.annotations.Scope;
028import org.jboss.seam.annotations.intercept.BypassInterceptors;
029import org.nuxeo.ecm.collections.api.CollectionConstants;
030import org.nuxeo.ecm.collections.api.CollectionManager;
031import org.nuxeo.ecm.core.api.CoreSession;
032import org.nuxeo.ecm.core.api.DocumentModel;
033import org.nuxeo.ecm.platform.ui.web.api.NavigationContext;
034import org.nuxeo.runtime.api.Framework;
035
036/**
037 * @since 5.9.3
038 */
039@Name("currentDocumentCollection")
040@Scope(ScopeType.PAGE)
041@BypassInterceptors
042public class CurrentDocumentCollectionBean implements Serializable {
043
044    private static final long serialVersionUID = 1L;
045
046    protected boolean hasCurrentDocumentMoreCollectionToDisplay = false;
047
048    private boolean isDisplayAll;
049
050    public List<DocumentModel> getCurrentDocumentCollections() {
051        final NavigationContext navigationContext = (NavigationContext) Component.getInstance("navigationContext", true);
052        final DocumentModel currentDocument = navigationContext.getCurrentDocument();
053        final CollectionManager collectionManager = Framework.getService(CollectionManager.class);
054        if (!collectionManager.isCollectable(currentDocument)) {
055            return null;
056        }
057        final CoreSession session = (CoreSession) Component.getInstance("documentManager", true);
058        List<DocumentModel> result = collectionManager.getVisibleCollection(currentDocument,
059                isDisplayAll ? CollectionConstants.MAX_COLLECTION_RETURNED
060                        : CollectionConstants.DEFAULT_COLLECTION_RETURNED, session);
061        if (!isDisplayAll && result.size() == CollectionConstants.DEFAULT_COLLECTION_RETURNED) {
062            hasCurrentDocumentMoreCollectionToDisplay = true;
063        } else {
064            isDisplayAll = true;
065            hasCurrentDocumentMoreCollectionToDisplay = false;
066        }
067        return result;
068    }
069
070    public Boolean getHasCurrentDocumentMoreCollectionToDisplay() {
071        return hasCurrentDocumentMoreCollectionToDisplay;
072    }
073
074    public boolean isDisplayAll() {
075        return isDisplayAll;
076    }
077
078    public void setDisplayAll(final boolean isDisplayAll) {
079        this.isDisplayAll = isDisplayAll;
080    }
081
082}