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