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) {
189        Map<String, String> folders = getFolders(session);
190        Map<String, Count> documentsCountByFolder = computeDocumentsCountByFolder(session, folders);
191        saveDocumentsCount(session, documentsCountByFolder);
192    }
193
194    protected Map<String, String> getFolders(CoreSession session) {
195        IterableQueryResult res = session.queryAndFetch(
196                "SELECT ecm:uuid, ecm:parentId FROM Document WHERE ecm:mixinType = 'Folderish'", "NXQL");
197        try {
198            Map<String, String> folders = new HashMap<String, String>();
199
200            for (Map<String, Serializable> r : res) {
201                folders.put((String) r.get("ecm:uuid"), (String) r.get("ecm:parentId"));
202            }
203            return folders;
204        } finally {
205            if (res != null) {
206                res.close();
207            }
208        }
209    }
210
211    protected Map<String, Count> computeDocumentsCountByFolder(CoreSession session, Map<String, String> folders) {
212        IterableQueryResult res = session.queryAndFetch("SELECT ecm:uuid, ecm:parentId FROM Document", "NXQL");
213        try {
214            Map<String, Count> foldersCount = new HashMap<String, Count>();
215            for (Map<String, Serializable> r : res) {
216                String uuid = (String) r.get("ecm:uuid");
217                if (folders.containsKey(uuid)) {
218                    // a folder
219                    continue;
220                }
221
222                String folderId = (String) r.get("ecm:parentId");
223                if (!foldersCount.containsKey(folderId)) {
224                    foldersCount.put(folderId, new Count());
225                }
226                Count count = foldersCount.get(folderId);
227                count.childrenCount++;
228                count.descendantsCount++;
229
230                updateParentsDocumentsCount(folders, foldersCount, folderId);
231            }
232            return foldersCount;
233        } finally {
234            if (res != null) {
235                res.close();
236            }
237        }
238    }
239
240    protected void updateParentsDocumentsCount(Map<String, String> folders, Map<String, Count> foldersCount,
241            String folderId) {
242        String parent = folders.get(folderId);
243        while (parent != null) {
244            if (!foldersCount.containsKey(parent)) {
245                foldersCount.put(parent, new Count());
246            }
247            Count c = foldersCount.get(parent);
248            c.descendantsCount++;
249            parent = folders.get(parent);
250        }
251    }
252
253    protected void saveDocumentsCount(CoreSession session, Map<String, Count> foldersCount) {
254        long docsCount = 0;
255        for (Map.Entry<String, Count> entry : foldersCount.entrySet()) {
256            String folderId = entry.getKey();
257            if (folderId == null) {
258                continue;
259            }
260            DocumentModel folder;
261            try {
262                folder = session.getDocument(new IdRef(folderId));
263            } catch (DocumentNotFoundException e) {
264                log.warn(e);
265                log.debug(e, e);
266                continue;
267            }
268            if (folder.getPath().isRoot()) {
269                // Root document
270                continue;
271            }
272            saveDocumentsCount(session, folder, entry.getValue());
273            docsCount++;
274            if (docsCount % BATCH_SIZE == 0) {
275                session.save();
276                if (TransactionHelper.isTransactionActive()) {
277                    TransactionHelper.commitOrRollbackTransaction();
278                    TransactionHelper.startTransaction();
279                }
280            }
281        }
282        session.save();
283    }
284
285    protected void saveDocumentsCount(CoreSession session, DocumentModel folder, Count count) {
286        if (!folder.hasFacet(DOCUMENTS_COUNT_STATISTICS_FACET)) {
287            folder.addFacet(DOCUMENTS_COUNT_STATISTICS_FACET);
288        }
289        folder.setPropertyValue(DOCUMENTS_COUNT_STATISTICS_CHILDREN_COUNT_PROPERTY, Long.valueOf(count.childrenCount));
290        folder.setPropertyValue(DOCUMENTS_COUNT_STATISTICS_DESCENDANTS_COUNT_PROPERTY,
291                Long.valueOf(count.descendantsCount));
292        // do not send notifications
293        QuotaUtils.disableListeners(folder);
294        DocumentModel origFolder = folder;
295        session.saveDocument(folder);
296        QuotaUtils.clearContextData(origFolder);
297    }
298
299    /**
300     * Object to store documents count for a folder
301     */
302    private static class Count {
303
304        public long childrenCount = 0;
305
306        public long descendantsCount = 0;
307    }
308
309    @Override
310    protected void processDocumentTrashOp(CoreSession session, DocumentModel doc, String transition) {
311        // do nothing for count
312    }
313
314    @Override
315    protected void processDocumentRestored(CoreSession session, DocumentModel doc) {
316        // do nothing
317    }
318
319    @Override
320    protected void processDocumentBeforeRestore(CoreSession session, DocumentModel doc) {
321        // do nothing
322    }
323}