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.quota.count.Constants.DOCUMENTS_COUNT_STATISTICS_CHILDREN_COUNT_PROPERTY;
023import static org.nuxeo.ecm.quota.count.Constants.DOCUMENTS_COUNT_STATISTICS_DESCENDANTS_COUNT_PROPERTY;
024
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.PropertyException;
027import org.nuxeo.ecm.core.api.quota.QuotaStatsNonFolderishCount;
028
029/**
030 * Adapter implementing {@code QuotaStatsNonFolderishCount} to have information about children and descendants count.
031 *
032 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
033 * @since 5.5
034 */
035public class QuotaStatsNonFolderishCountAdapter implements QuotaStatsNonFolderishCount {
036
037    private final DocumentModel doc;
038
039    public QuotaStatsNonFolderishCountAdapter(DocumentModel doc) {
040        this.doc = doc;
041    }
042
043    @Override
044    public long getIntrinsic() {
045        return 0;
046    }
047
048    @Override
049    public long getChildren() {
050        try {
051            Long count = (Long) doc.getPropertyValue(DOCUMENTS_COUNT_STATISTICS_CHILDREN_COUNT_PROPERTY);
052            return count != null ? count : 0;
053        } catch (PropertyException e) {
054            return 0;
055        }
056    }
057
058    @Override
059    public long getTotal() {
060        try {
061            Long count = (Long) doc.getPropertyValue(DOCUMENTS_COUNT_STATISTICS_DESCENDANTS_COUNT_PROPERTY);
062            return count != null ? count : 0;
063        } catch (PropertyException e) {
064            return 0;
065        }
066    }
067
068}