001/*
002 * (C) Copyright 2010 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 *     Nuxeo - initial API and implementation
018 */
019package org.nuxeo.ecm.platform.routing.api.operation;
020
021import org.nuxeo.ecm.automation.core.annotations.Context;
022import org.nuxeo.ecm.automation.core.annotations.Operation;
023import org.nuxeo.ecm.automation.core.annotations.OperationMethod;
024import org.nuxeo.ecm.core.api.CoreSession;
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.DocumentModelList;
027import org.nuxeo.ecm.platform.comment.api.CommentableDocument;
028import org.nuxeo.ecm.platform.routing.api.DocumentRouteStep;
029import org.nuxeo.ecm.platform.routing.api.DocumentRoutingConstants;
030
031/***
032 * Updates the number of comments stored on the {@link DocumentRouteStep}. This is used to avoid unnecessary jena calls
033 * when displaying the number of comments on each step. This updates the number of comments on the documents from the
034 * relations. To invoke it: run from nuxeo-shell : run updateCommentsOnDoc
035 *
036 * @author mcedica
037 */
038@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)
039public class UpdateCommentsInfoOnDocumentOperation {
040
041    public final static String ID = "Document.Routing.UpdateCommentsInfoOnDocument";
042
043    @Context
044    protected CoreSession session;
045
046    @OperationMethod
047    public void updateCommentsInfo() {
048        DocumentModelList allDocsToUpdate = session.query(String.format(
049                "SELECT * FROM Document WHERE ecm:mixinType = '%s'",
050                DocumentRoutingConstants.COMMENTS_INFO_HOLDER_FACET));
051        if (allDocsToUpdate == null || allDocsToUpdate.size() == 0) {
052            return;
053        }
054        for (DocumentModel documentModel : allDocsToUpdate) {
055            CommentableDocument commentableDoc = documentModel.getAdapter(CommentableDocument.class);
056            documentModel.setPropertyValue(DocumentRoutingConstants.COMMENTS_NO_PROPERTY_NAME,
057                    Integer.valueOf(commentableDoc.getComments().size()));
058            session.saveDocument(documentModel);
059        }
060
061    }
062}