001/*
002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Nuxeo - initial API and implementation
016 */
017package org.nuxeo.ecm.platform.routing.api.operation;
018
019import org.nuxeo.ecm.automation.core.annotations.Context;
020import org.nuxeo.ecm.automation.core.annotations.Operation;
021import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
022import org.nuxeo.ecm.core.api.CoreSession;
023import org.nuxeo.ecm.core.api.DocumentModel;
024import org.nuxeo.ecm.core.api.DocumentModelList;
025import org.nuxeo.ecm.platform.comment.api.CommentableDocument;
026import org.nuxeo.ecm.platform.routing.api.DocumentRouteStep;
027import org.nuxeo.ecm.platform.routing.api.DocumentRoutingConstants;
028
029/***
030 * Updates the number of comments stored on the {@link DocumentRouteStep}. This is used to avoid unnecessary jena calls
031 * when displaying the number of comments on each step. This updates the number of comments on the documents from the
032 * relations. To invoke it: run from nuxeo-shell : run updateCommentsOnDoc
033 *
034 * @author mcedica
035 */
036@Operation(id = UpdateCommentsInfoOnDocumentOperation.ID, category = DocumentRoutingConstants.OPERATION_CATEGORY_ROUTING_NAME, label = "Update comments number on the document", description = "Update comments number on the document", addToStudio = false)
037public class UpdateCommentsInfoOnDocumentOperation {
038
039    public final static String ID = "Document.Routing.UpdateCommentsInfoOnDocument";
040
041    @Context
042    protected CoreSession session;
043
044    @OperationMethod
045    public void updateCommentsInfo() {
046        DocumentModelList allDocsToUpdate = session.query(String.format(
047                "SELECT * FROM Document WHERE ecm:mixinType = '%s'",
048                DocumentRoutingConstants.COMMENTS_INFO_HOLDER_FACET));
049        if (allDocsToUpdate == null || allDocsToUpdate.size() == 0) {
050            return;
051        }
052        for (DocumentModel documentModel : allDocsToUpdate) {
053            CommentableDocument commentableDoc = documentModel.getAdapter(CommentableDocument.class);
054            documentModel.setPropertyValue(DocumentRoutingConstants.COMMENTS_NO_PROPERTY_NAME,
055                    Integer.valueOf(commentableDoc.getComments().size()));
056            session.saveDocument(documentModel);
057        }
058
059    }
060}