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 *     Kevin Leturc <kleturc@nuxeo.com>
018 */
019package org.nuxeo.ecm.collections.core.versioning;
020
021import java.util.List;
022
023import org.nuxeo.ecm.collections.api.CollectionConstants;
024import org.nuxeo.ecm.collections.core.adapter.CollectionMember;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.versioning.VersioningPolicyFilter;
027
028/**
029 * Policy filter which disables automatic versioning for collection actions.
030 *
031 * @since 9.2
032 */
033public class NoVersioningCollectionPolicyFilter implements VersioningPolicyFilter {
034
035    @Override
036    public boolean test(DocumentModel previousDocument, DocumentModel currentDocument) {
037        // we don't want to trigger automatic versioning system for documents with 'Collection' facet
038        if (currentDocument.hasFacet(CollectionConstants.COLLECTION_FACET)) {
039            return true;
040        }
041        // next tests suppose it's an update, don't apply policies referencing this filter for the creation step
042        if (previousDocument == null) {
043            return false;
044        }
045        boolean previousHasMember = previousDocument.hasSchema(CollectionConstants.COLLECTION_MEMBER_SCHEMA_NAME);
046        boolean currentHashMember = currentDocument.hasSchema(CollectionConstants.COLLECTION_MEMBER_SCHEMA_NAME);
047        if (!previousHasMember && currentHashMember) {
048            // case when we add document to a collection
049            // here we suppose that add/remove is the only update of document (default behavior of collectionManager)
050            return true;
051        } else if (previousHasMember && !currentHashMember) {
052            // case when we copy a document and re-init all values from collection members
053            // here we suppose that add/remove is the only update of document (default behavior of collectionManager)
054            return true;
055        } else if (previousHasMember && currentHashMember) {
056            // we need to check if there was changes in members
057            List<String> previousMembers = previousDocument.getAdapter(CollectionMember.class).getCollectionIds();
058            List<String> currentMembers = currentDocument.getAdapter(CollectionMember.class).getCollectionIds();
059            // here we suppose that add/remove is the only update of document (default behavior of collectionManager)
060            return previousMembers.size() != currentMembers.size()
061                    // check if ids are the same - in case we edit the ids instead of removing then adding
062                    || !currentMembers.containsAll(previousMembers);
063        }
064        // last case is !previousHasMember && !currentHashMember - nothing related to collections skip
065        return false;
066    }
067
068}