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 *     Guillaume Renard <grenard@nuxeo.com>
018 */
019package org.nuxeo.ecm.platform.rendition.operation;
020
021import static org.nuxeo.ecm.core.query.sql.NXQL.ECM_UUID;
022
023import org.nuxeo.ecm.automation.core.Constants;
024import org.nuxeo.ecm.automation.core.annotations.Context;
025import org.nuxeo.ecm.automation.core.annotations.Operation;
026import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
027import org.nuxeo.ecm.core.api.CoreSession;
028import org.nuxeo.ecm.core.api.DocumentModel;
029import org.nuxeo.ecm.core.api.IdRef;
030import org.nuxeo.ecm.core.query.sql.NXQL;
031
032/**
033 * Unpublish all publications of the document. Only publications that current user can remove will be unpublished.
034 *
035 * @since 10.3
036 */
037@Operation(id = UnpublishAll.ID, category = Constants.CAT_DOCUMENT, label = "Unpublish Document's Publications", description = "Unpublish all publications of the input document. Only publications that current user can remove will be unpublished.")
038public class UnpublishAll {
039
040    public static final String ID = "Document.UnpublishAll";
041
042    @Context
043    protected CoreSession session;
044
045    @OperationMethod
046    public void run(DocumentModel doc) {
047        String escapedId = NXQL.escapeString(doc.getId());
048        session.queryProjection(
049                String.format(org.nuxeo.ecm.platform.rendition.Constants.ALL_PUBLICATION_QUERY, escapedId, escapedId),
050                0, 0)
051               .stream()
052               .map(publication -> new IdRef(publication.get(ECM_UUID).toString()))
053               .filter(session::canRemoveDocument)
054               .forEach(session::removeDocument);
055        // XXX in case of published renditions, do we want to clean up the placeless stored rendition?
056        // AFAIK, if a document is published a couple of time with the same rendition,
057        // there's only one placeless stored rendition. So it may be worth keeping it
058    }
059
060}