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.automation.core.operations.document; 020 021import java.util.stream.Collectors; 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.automation.core.annotations.Param; 028import org.nuxeo.ecm.core.api.CoreSession; 029import org.nuxeo.ecm.core.api.DocumentModel; 030import org.nuxeo.ecm.core.api.DocumentModelList; 031import org.nuxeo.ecm.core.api.DocumentNotFoundException; 032import org.nuxeo.ecm.core.api.NuxeoException; 033import org.nuxeo.ecm.core.api.impl.DocumentModelListImpl; 034 035/** 036 * @since 10.1 037 */ 038@Operation(id = OrderDocument.ID, category = Constants.CAT_DOCUMENT, label = "Order Document", description = "Given a parent document, order the source child before the destination child.", since = "10.1") 039public class OrderDocument { 040 041 public static final String ID = "Document.Order"; 042 043 public static final String NOT_SAME_FOLDER_ERROR_MSG = "The document can only be ordered within the same folder."; 044 045 @Param(name = "before", required = false) 046 protected DocumentModel before; 047 048 @Context 049 protected CoreSession session; 050 051 @OperationMethod 052 public DocumentModel run(DocumentModel doc) { 053 try { 054 session.orderBefore(doc.getParentRef(), doc.getName(), before != null ? before.getName() : null); 055 session.save(); 056 return session.getDocument(doc.getRef()); 057 } catch (DocumentNotFoundException e) { 058 throw new NuxeoException(NOT_SAME_FOLDER_ERROR_MSG); 059 } 060 } 061 062 @OperationMethod 063 public DocumentModelList run(DocumentModelList docs) { 064 return docs.stream().map(doc -> run(doc)).collect(Collectors.toCollection(DocumentModelListImpl::new)); 065 } 066 067}