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;
021
022import static org.nuxeo.ecm.core.api.LifeCycleConstants.DELETE_TRANSITION;
023import static org.nuxeo.ecm.core.api.LifeCycleConstants.TRANSITION_EVENT;
024import static org.nuxeo.ecm.core.api.LifeCycleConstants.TRANSTION_EVENT_OPTION_TRANSITION;
025import static org.nuxeo.ecm.core.api.LifeCycleConstants.UNDELETE_TRANSITION;
026import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.ABOUT_TO_REMOVE;
027import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.ABOUT_TO_REMOVE_VERSION;
028import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.BEFORE_DOC_RESTORE;
029import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.BEFORE_DOC_UPDATE;
030import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_CHECKEDIN;
031import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_CHECKEDOUT;
032import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_CREATED;
033import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_CREATED_BY_COPY;
034import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_MOVED;
035import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_RESTORED;
036import static org.nuxeo.ecm.core.api.event.DocumentEventTypes.DOCUMENT_UPDATED;
037
038import java.io.Serializable;
039import java.util.ArrayList;
040import java.util.List;
041
042import org.apache.commons.logging.Log;
043import org.apache.commons.logging.LogFactory;
044import org.nuxeo.ecm.core.api.CoreSession;
045import org.nuxeo.ecm.core.api.DocumentModel;
046import org.nuxeo.ecm.core.api.DocumentRef;
047import org.nuxeo.ecm.core.api.event.CoreEventConstants;
048import org.nuxeo.ecm.core.event.Event;
049import org.nuxeo.ecm.core.event.impl.DocumentEventContext;
050import org.nuxeo.ecm.quota.size.QuotaExceededException;
051
052/**
053 * Abstract class implementing {@code QuotaStatsUpdater} to handle common cases.
054 * <p>
055 * Provides abstract methods to override for common events.
056 *
057 * @author <a href="mailto:troger@nuxeo.com">Thomas Roger</a>
058 * @since 5.5
059 */
060public abstract class AbstractQuotaStatsUpdater implements QuotaStatsUpdater {
061
062    protected String name;
063
064    protected String label;
065
066    protected String descriptionLabel;
067
068    protected static Log log = LogFactory.getLog(AbstractQuotaStatsUpdater.class);
069
070    @Override
071    public void setName(String name) {
072        this.name = name;
073    }
074
075    @Override
076    public String getName() {
077        return name;
078    }
079
080    @Override
081    public void setLabel(String label) {
082        this.label = label;
083    }
084
085    @Override
086    public String getLabel() {
087        return label;
088    }
089
090    @Override
091    public void setDescriptionLabel(String descriptionLabel) {
092        this.descriptionLabel = descriptionLabel;
093    }
094
095    @Override
096    public String getDescriptionLabel() {
097        return descriptionLabel;
098    }
099
100    @Override
101    public void updateStatistics(CoreSession session, DocumentEventContext docCtx, Event event) {
102        DocumentModel doc = docCtx.getSourceDocument();
103        if (!needToProcessEventOnDocument(event, doc)) {
104            log.debug("Exit Listener !!!!");
105            return;
106        }
107        try {
108            switch (event.getName()) {
109            case DOCUMENT_CREATED:
110                processDocumentCreated(session, doc);
111                break;
112            case ABOUT_TO_REMOVE:
113            case ABOUT_TO_REMOVE_VERSION:
114                processDocumentAboutToBeRemoved(session, doc);
115                break;
116            case DOCUMENT_CREATED_BY_COPY:
117                processDocumentCopied(session, doc);
118                break;
119            case DOCUMENT_MOVED:
120                DocumentRef sourceParentRef = (DocumentRef) docCtx.getProperty(CoreEventConstants.PARENT_PATH);
121                DocumentRef destinationRef = (DocumentRef) docCtx.getProperty(CoreEventConstants.DESTINATION_REF);
122                DocumentModel sourceParent = sourceParentRef == null ? null : session.getDocument(sourceParentRef);
123                DocumentModel parent = destinationRef == null ? null : session.getDocument(destinationRef);
124                if (sourceParent == null && parent == null
125                        || sourceParent != null && parent != null && sourceParent.getId().equals(parent.getId())) {
126                    // rename
127                    break;
128                }
129                processDocumentMoved(session, doc, sourceParent);
130                break;
131            case DOCUMENT_UPDATED:
132                processDocumentUpdated(session, doc);
133                break;
134            case BEFORE_DOC_UPDATE:
135                processDocumentBeforeUpdate(session, doc);
136                break;
137            case TRANSITION_EVENT:
138                String transition = (String) docCtx.getProperty(TRANSTION_EVENT_OPTION_TRANSITION);
139                if (!DELETE_TRANSITION.equals(transition) && !UNDELETE_TRANSITION.equals(transition)) {
140                    break;
141                }
142                processDocumentTrashOp(session, doc, transition);
143                break;
144            case DOCUMENT_CHECKEDIN:
145                processDocumentCheckedIn(session, doc);
146                break;
147            case DOCUMENT_CHECKEDOUT:
148                processDocumentCheckedOut(session, doc);
149                break;
150            case BEFORE_DOC_RESTORE:
151                processDocumentBeforeRestore(session, doc);
152                break;
153            case DOCUMENT_RESTORED:
154                processDocumentRestored(session, doc);
155                break;
156            }
157        } catch (QuotaExceededException e) {
158            handleQuotaExceeded(e, event);
159            throw e;
160        }
161    }
162
163    /** Gets all the ancestors of the document, including the root. */
164    protected List<DocumentModel> getAncestors(CoreSession session, DocumentModel doc) {
165        List<DocumentModel> ancestors = new ArrayList<>();
166        for (DocumentRef documentRef : session.getParentDocumentRefs(doc.getRef())) {
167            ancestors.add(session.getDocument(documentRef));
168        }
169        return ancestors;
170    }
171
172    protected abstract void handleQuotaExceeded(QuotaExceededException e, Event event);
173
174    protected abstract boolean needToProcessEventOnDocument(Event event, DocumentModel doc);
175
176    protected abstract void processDocumentCreated(CoreSession session, DocumentModel doc);
177
178    protected abstract void processDocumentCopied(CoreSession session, DocumentModel doc);
179
180    protected abstract void processDocumentCheckedIn(CoreSession session, DocumentModel doc);
181
182    protected abstract void processDocumentCheckedOut(CoreSession session, DocumentModel doc);
183
184    protected abstract void processDocumentUpdated(CoreSession session, DocumentModel doc);
185
186    protected abstract void processDocumentMoved(CoreSession session, DocumentModel doc, DocumentModel sourceParent);
187
188    protected abstract void processDocumentAboutToBeRemoved(CoreSession session, DocumentModel doc);
189
190    protected abstract void processDocumentBeforeUpdate(CoreSession session, DocumentModel doc);
191
192    protected abstract void processDocumentTrashOp(CoreSession session, DocumentModel doc, String transition);
193
194    protected abstract void processDocumentRestored(CoreSession session, DocumentModel doc);
195
196    protected abstract void processDocumentBeforeRestore(CoreSession session, DocumentModel doc);
197
198}