de.fhg.iais.asc.commons.exceptions.AscReportableErrorException.java Source code

Java tutorial

Introduction

Here is the source code for de.fhg.iais.asc.commons.exceptions.AscReportableErrorException.java

Source

package de.fhg.iais.asc.commons.exceptions;

/******************************************************************************
 * Copyright 2011 (c) Fraunhofer IAIS Netmedia  http://www.iais.fraunhofer.de *
 * ************************************************************************** *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may    *
 * not use this file except in compliance with the License.                   *
 * You may obtain a copy of the License at                                    *
 * http://www.apache.org/licenses/LICENSE-2.0                                 *
 * Unless required by applicable law or agreed to in writing,                 *
 * software distributed under the License is distributed on an "AS IS" BASIS, *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   *
 * See the License for the specific language governing permissions and        *
 * limitations under the License.                                             *
 ******************************************************************************/

import java.util.ArrayList;

import org.apache.commons.lang.StringUtils;
import org.jdom.Document;

import com.google.common.base.Strings;

import de.fhg.iais.asc.commons.AscContext;
import de.fhg.iais.commons.constants.Serial;
import de.fhg.iais.cortex.report.common.revision.ErrorReport;
import de.fhg.iais.cortex.report.common.revision.ErrorType;
import de.fhg.iais.cortex.report.common.sip.ErrorSip;

/**
 * Super class for reportable errors that occur in the ASC.
 */
public abstract class AscReportableErrorException extends AscException {
    private static final long serialVersionUID = Serial.GLOBAL_DEFAULT_SERIAL_VERSION_UID;

    private final ErrorType errorType;

    @Deprecated
    protected AscReportableErrorException(String message, ErrorType errorType) {
        super(message);
        this.errorType = errorType;
    }

    @Deprecated
    protected AscReportableErrorException(String message, ErrorType errorType, Throwable cause) {
        super(message, cause);
        this.errorType = errorType;
    }

    protected AscReportableErrorException(String message, ErrorType errorType, AscContext ascContext) {
        super(message, ascContext);
        this.errorType = errorType;
    }

    protected AscReportableErrorException(String message, ErrorType errorType, Throwable cause,
            AscContext ascContext) {
        super(message, cause, ascContext);
        this.errorType = errorType;
    }

    public ErrorType getErrorType() {
        return this.errorType;
    }

    public ErrorSip createErrorSip(String section, String filename, String itemId, Document source,
            Document failedSip) {
        String messages = getAllErrorMessages(this, new ArrayList<String>());
        ErrorReport error = new ErrorReport(itemId, filename, StringUtils.replace(messages, "\"", "'"),
                this.errorType);
        ErrorSip errorSip = new ErrorSip(section, error, source, failedSip);
        return errorSip;
    }

    private String getAllErrorMessages(Throwable e, ArrayList<String> list) {
        if (e != null) {

            if (!Strings.isNullOrEmpty(e.getLocalizedMessage())) {
                String message = e.getLocalizedMessage().trim();
                if (!list.contains(message)) {
                    list.add(message);
                }
            }
            return getAllErrorMessages(e.getCause(), list);
        } else {
            return list.toString();
        }
    }

}