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 *     <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
018 */
019
020package org.nuxeo.ecm.quota.size;
021
022import org.nuxeo.ecm.core.api.RecoverableClientException;
023import org.nuxeo.ecm.core.api.DocumentModel;
024
025/**
026 * Exception throws by the {@link DocumentsSizeUpdater} to enforce Quotas in case a transaction tries to add too
027 * much Blobs
028 * 
029 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
030 * @since 5.6
031 */
032public class QuotaExceededException extends RecoverableClientException {
033
034    private static final long serialVersionUID = 1L;
035
036    protected long quotaValue;
037
038    protected String targetPath;
039
040    protected String addedDocumentID;
041
042    public QuotaExceededException(DocumentModel targetDocument, String message) {
043        super(message, "label.quotaException." + message, new String[] { targetDocument.getPathAsString() });
044        this.targetPath = targetDocument.getPathAsString();
045    }
046
047    public QuotaExceededException(DocumentModel targetDocument, DocumentModel addedDocument, long quotaValue) {
048        this(targetDocument.getPathAsString(), addedDocument.getId(), quotaValue);
049    }
050
051    public QuotaExceededException(String targetDocumentPath, String addedDocumentID, long quotaValue) {
052        super("QuotaExceeded", "label.quotaException.QuotaExceeded",
053                new String[] { targetDocumentPath, addedDocumentID });
054        this.quotaValue = quotaValue;
055        this.addedDocumentID = addedDocumentID;
056        this.targetPath = targetDocumentPath;
057    }
058
059    public long getQuotaValue() {
060        return quotaValue;
061    }
062
063    public String getTargetPath() {
064        return targetPath;
065    }
066
067    public String getAddedDocumentID() {
068        return addedDocumentID;
069    }
070
071    public static QuotaExceededException unwrap(Throwable e) {
072        if (e instanceof QuotaExceededException) {
073            return (QuotaExceededException) e;
074        } else {
075            if (e.getCause() != null) {
076                return unwrap(e.getCause());
077            } else {
078                return null;
079            }
080        }
081    }
082
083    public static boolean isQuotaExceededException(Throwable e) {
084        return unwrap(e) != null;
085    }
086}