001/*
002 * (C) Copyright 2006-2012 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
016 */
017
018package org.nuxeo.ecm.quota.size;
019
020import org.nuxeo.ecm.core.api.RecoverableClientException;
021import org.nuxeo.ecm.core.api.DocumentModel;
022
023/**
024 * Exception throws by the {@link QuotaSyncListenerChecker} to enforce Quotas in case a transaction tries to add too
025 * much Blobs
026 * 
027 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
028 * @since 5.6
029 */
030public class QuotaExceededException extends RecoverableClientException {
031
032    private static final long serialVersionUID = 1L;
033
034    protected long quotaValue;
035
036    protected String targetPath;
037
038    protected String addedDocumentID;
039
040    public QuotaExceededException(DocumentModel targetDocument, String message) {
041        super(message, "label.quotaException." + message, new String[] { targetDocument.getPathAsString() });
042        this.targetPath = targetDocument.getPathAsString();
043    }
044
045    public QuotaExceededException(DocumentModel targetDocument, DocumentModel addedDocument, long quotaValue) {
046        this(targetDocument.getPathAsString(), addedDocument.getId(), quotaValue);
047    }
048
049    public QuotaExceededException(String targetDocumentPath, String addedDocumentID, long quotaValue) {
050        super("QuotaExceeded", "label.quotaException.QuotaExceeded",
051                new String[] { targetDocumentPath, addedDocumentID });
052        this.quotaValue = quotaValue;
053        this.addedDocumentID = addedDocumentID;
054        this.targetPath = targetDocumentPath;
055    }
056
057    public long getQuotaValue() {
058        return quotaValue;
059    }
060
061    public String getTargetPath() {
062        return targetPath;
063    }
064
065    public String getAddedDocumentID() {
066        return addedDocumentID;
067    }
068
069    public static QuotaExceededException unwrap(Throwable e) {
070        if (e instanceof QuotaExceededException) {
071            return (QuotaExceededException) e;
072        } else {
073            if (e.getCause() != null) {
074                return unwrap(e.getCause());
075            } else {
076                return null;
077            }
078        }
079    }
080
081    public static boolean isQuotaExceededException(Throwable e) {
082        return unwrap(e) != null;
083    }
084}