001/*
002 * (C) Copyright 2006-2016 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 *     Thomas Roger
018 *     Florent Guillaume
019 */
020package org.nuxeo.ecm.quota.count;
021
022import static org.nuxeo.ecm.core.schema.FacetNames.FOLDERISH;
023import static org.nuxeo.ecm.quota.count.Constants.DOCUMENTS_COUNT_STATISTICS_CHILDREN_COUNT_PROPERTY;
024import static org.nuxeo.ecm.quota.count.Constants.DOCUMENTS_COUNT_STATISTICS_DESCENDANTS_COUNT_PROPERTY;
025import static org.nuxeo.ecm.quota.count.Constants.DOCUMENTS_COUNT_STATISTICS_FACET;
026
027import java.io.Serializable;
028import java.util.HashMap;
029import java.util.List;
030import java.util.Map;
031
032import org.apache.commons.logging.Log;
033import org.apache.commons.logging.LogFactory;
034import org.nuxeo.ecm.core.api.CoreSession;
035import org.nuxeo.ecm.core.api.DocumentModel;
036import org.nuxeo.ecm.core.api.DocumentNotFoundException;
037import org.nuxeo.ecm.core.api.IdRef;
038import org.nuxeo.ecm.core.api.IterableQueryResult;
039import org.nuxeo.ecm.core.api.model.DeltaLong;
040import org.nuxeo.ecm.core.event.Event;
041import org.nuxeo.ecm.quota.AbstractQuotaStatsUpdater;
042import org.nuxeo.ecm.quota.QuotaStatsInitialWork;
043import org.nuxeo.ecm.quota.QuotaUtils;
044import org.nuxeo.ecm.quota.size.QuotaExceededException;
045import org.nuxeo.runtime.transaction.TransactionHelper;
046
047/**
048 * {@link org.nuxeo.ecm.quota.QuotaStatsUpdater} counting the non folderish documents.
049 * <p>
050 * Store the descendant and children count on {@code Folderish} documents.
051 *
052 * @since 5.5
053 */
054public class DocumentsCountUpdater extends AbstractQuotaStatsUpdater {
055
056    private static final Log log = LogFactory.getLog(DocumentsCountUpdater.class);
057
058    public static final int BATCH_SIZE = 50;
059
060    @Override
061    protected void processDocumentCreated(CoreSession session, DocumentModel doc) {
062        if (doc.isVersion()) {
063            return;
064        }
065        List<DocumentModel> ancestors = getAncestors(session, doc);
066        long docCount = getCount(doc);
067        updateCountStatistics(session, doc, ancestors, docCount);
068    }
069
070    @Override
071    protected void processDocumentCopied(CoreSession session, DocumentModel doc) {
072        List<DocumentModel> ancestors = getAncestors(session, doc);
073        long docCount = getCount(doc);
074        updateCountStatistics(session, doc, ancestors, docCount);
075    }
076
077    @Override
078    protected void processDocumentCheckedIn(CoreSession session, DocumentModel doc) {
079        // NOP
080    }
081
082    @Override
083    protected void processDocumentCheckedOut(CoreSession session, DocumentModel doc) {
084        // NOP
085    }
086
087    @Override
088    protected void processDocumentUpdated(CoreSession session, DocumentModel doc) {
089    }
090
091    @Override
092    protected void processDocumentMoved(CoreSession session, DocumentModel doc, DocumentModel sourceParent) {
093        List<DocumentModel> ancestors = getAncestors(session, doc);
094        List<DocumentModel> sourceAncestors = getAncestors(session, sourceParent);
095        sourceAncestors.add(0, sourceParent);
096        long docCount = getCount(doc);
097        updateCountStatistics(session, doc, ancestors, docCount);
098        updateCountStatistics(session, doc, sourceAncestors, -docCount);
099    }
100
101    @Override
102    protected void processDocumentAboutToBeRemoved(CoreSession session, DocumentModel doc) {
103        List<DocumentModel> ancestors = getAncestors(session, doc);
104        long docCount = getCount(doc);
105        updateCountStatistics(session, doc, ancestors, -docCount);
106    }
107
108    @Override
109    protected void handleQuotaExceeded(QuotaExceededException e, Event event) {
110        // never rollback on Exceptions
111    }
112
113    @Override
114    protected boolean needToProcessEventOnDocument(Event event, DocumentModel doc) {
115        return true;
116    }
117
118    @Override
119    protected void processDocumentBeforeUpdate(CoreSession session, DocumentModel doc) {
120        // NOP
121    }
122
123    protected void updateCountStatistics(CoreSession session, DocumentModel doc, List<DocumentModel> ancestors,
124            long count) {
125        if (ancestors == null || ancestors.isEmpty()) {
126            return;
127        }
128        if (count == 0) {
129            return;
130        }
131
132        if (!doc.hasFacet(FOLDERISH)) {
133            DocumentModel parent = ancestors.get(0);
134            updateParentChildrenCount(session, parent, count);
135        }
136
137        for (DocumentModel ancestor : ancestors) {
138            Number previous;
139            if (ancestor.hasFacet(DOCUMENTS_COUNT_STATISTICS_FACET)) {
140                previous = (Number) ancestor.getPropertyValue(DOCUMENTS_COUNT_STATISTICS_DESCENDANTS_COUNT_PROPERTY);
141            } else {
142                ancestor.addFacet(DOCUMENTS_COUNT_STATISTICS_FACET);
143                previous = null;
144            }
145            DeltaLong descendantsCount = DeltaLong.valueOf(previous, count);
146            ancestor.setPropertyValue(DOCUMENTS_COUNT_STATISTICS_DESCENDANTS_COUNT_PROPERTY, descendantsCount);
147            // do not send notifications
148            QuotaUtils.disableListeners(ancestor);
149            DocumentModel origAncestor = ancestor;
150            session.saveDocument(ancestor);
151            QuotaUtils.clearContextData(origAncestor);
152        }
153
154        session.save();
155    }
156
157    protected void updateParentChildrenCount(CoreSession session, DocumentModel parent, long count) {
158        Number previous;
159        if (parent.hasFacet(DOCUMENTS_COUNT_STATISTICS_FACET)) {
160            previous = (Number) parent.getPropertyValue(DOCUMENTS_COUNT_STATISTICS_CHILDREN_COUNT_PROPERTY);
161        } else {
162            parent.addFacet(DOCUMENTS_COUNT_STATISTICS_FACET);
163            previous = null;
164        }
165        DeltaLong childrenCount = DeltaLong.valueOf(previous, count);
166        parent.setPropertyValue(DOCUMENTS_COUNT_STATISTICS_CHILDREN_COUNT_PROPERTY, childrenCount);
167        // do not send notifications
168        QuotaUtils.disableListeners(parent);
169        DocumentModel origParent = parent;
170        session.saveDocument(parent);
171        QuotaUtils.clearContextData(origParent);
172    }
173
174    protected long getCount(DocumentModel doc) {
175        if (doc.hasFacet(FOLDERISH)) {
176            if (doc.hasFacet(DOCUMENTS_COUNT_STATISTICS_FACET)) {
177                Number count = (Number) doc.getPropertyValue(DOCUMENTS_COUNT_STATISTICS_DESCENDANTS_COUNT_PROPERTY);
178                return count == null ? 0 : count.longValue();
179            } else {
180                return 0;
181            }
182        } else {
183            return 1;
184        }
185    }
186
187    @Override
188    public void computeInitialStatistics(CoreSession session, QuotaStatsInitialWork currentWorker, String path) {
189        // TODO path != null
190        Map<String, String> folders = getFolders(session);
191        Map<String, Count> documentsCountByFolder = computeDocumentsCountByFolder(session, folders);
192        saveDocumentsCount(session, documentsCountByFolder);
193    }
194
195    protected Map<String, String> getFolders(CoreSession session) {
196        IterableQueryResult res = session.queryAndFetch(
197                "SELECT ecm:uuid, ecm:parentId FROM Document WHERE ecm:mixinType = 'Folderish'", "NXQL");
198        try {
199            Map<String, String> folders = new HashMap<String, String>();
200
201            for (Map<String, Serializable> r : res) {
202                folders.put((String) r.get("ecm:uuid"), (String) r.get("ecm:parentId"));
203            }
204            return folders;
205        } finally {
206            if (res != null) {
207                res.close();
208            }
209        }
210    }
211
212    protected Map<String, Count> computeDocumentsCountByFolder(CoreSession session, Map<String, String> folders) {
213        IterableQueryResult res = session.queryAndFetch("SELECT ecm:uuid, ecm:parentId FROM Document", "NXQL");
214        try {
215            Map<String, Count> foldersCount = new HashMap<String, Count>();
216            for (Map<String, Serializable> r : res) {
217                String uuid = (String) r.get("ecm:uuid");
218                if (folders.containsKey(uuid)) {
219                    // a folder
220                    continue;
221                }
222
223                String folderId = (String) r.get("ecm:parentId");
224                if (!foldersCount.containsKey(folderId)) {
225                    foldersCount.put(folderId, new Count());
226                }
227                Count count = foldersCount.get(folderId);
228                count.childrenCount++;
229                count.descendantsCount++;
230
231                updateParentsDocumentsCount(folders, foldersCount, folderId);
232            }
233            return foldersCount;
234        } finally {
235            if (res != null) {
236                res.close();
237            }
238        }
239    }
240
241    protected void updateParentsDocumentsCount(Map<String, String> folders, Map<String, Count> foldersCount,
242            String folderId) {
243        String parent = folders.get(folderId);
244        while (parent != null) {
245            if (!foldersCount.containsKey(parent)) {
246                foldersCount.put(parent, new Count());
247            }
248            Count c = foldersCount.get(parent);
249            c.descendantsCount++;
250            parent = folders.get(parent);
251        }
252    }
253
254    protected void saveDocumentsCount(CoreSession session, Map<String, Count> foldersCount) {
255        long docsCount = 0;
256        for (Map.Entry<String, Count> entry : foldersCount.entrySet()) {
257            String folderId = entry.getKey();
258            if (folderId == null) {
259                continue;
260            }
261            DocumentModel folder;
262            try {
263                folder = session.getDocument(new IdRef(folderId));
264            } catch (DocumentNotFoundException e) {
265                log.warn(e);
266                log.debug(e, e);
267                continue;
268            }
269            if (folder.getPath().isRoot()) {
270                // Root document
271                continue;
272            }
273            saveDocumentsCount(session, folder, entry.getValue());
274            docsCount++;
275            if (docsCount % BATCH_SIZE == 0) {
276                session.save();
277                if (TransactionHelper.isTransactionActive()) {
278                    TransactionHelper.commitOrRollbackTransaction();
279                    TransactionHelper.startTransaction();
280                }
281            }
282        }
283        session.save();
284    }
285
286    protected void saveDocumentsCount(CoreSession session, DocumentModel folder, Count count) {
287        if (!folder.hasFacet(DOCUMENTS_COUNT_STATISTICS_FACET)) {
288            folder.addFacet(DOCUMENTS_COUNT_STATISTICS_FACET);
289        }
290        folder.setPropertyValue(DOCUMENTS_COUNT_STATISTICS_CHILDREN_COUNT_PROPERTY, Long.valueOf(count.childrenCount));
291        folder.setPropertyValue(DOCUMENTS_COUNT_STATISTICS_DESCENDANTS_COUNT_PROPERTY,
292                Long.valueOf(count.descendantsCount));
293        // do not send notifications
294        QuotaUtils.disableListeners(folder);
295        DocumentModel origFolder = folder;
296        session.saveDocument(folder);
297        QuotaUtils.clearContextData(origFolder);
298    }
299
300    /**
301     * Object to store documents count for a folder
302     */
303    private static class Count {
304
305        public long childrenCount = 0;
306
307        public long descendantsCount = 0;
308    }
309
310    @Override
311    protected void processDocumentTrashOp(CoreSession session, DocumentModel doc, String transition) {
312        // do nothing for count
313    }
314
315    @Override
316    protected void processDocumentRestored(CoreSession session, DocumentModel doc) {
317        // do nothing
318    }
319
320    @Override
321    protected void processDocumentBeforeRestore(CoreSession session, DocumentModel doc) {
322        // do nothing
323    }
324}