]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db/src/org/simantics/db/exception/DatabaseException.java
Restored old NoSingleResultException constructors without result count
[simantics/platform.git] / bundles / org.simantics.db / src / org / simantics / db / exception / DatabaseException.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.exception;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16
17 import org.simantics.db.ReadGraph;
18 import org.simantics.db.Resource;
19
20 /**
21  *
22  * A base class for exceptions in org.simantics.db.
23  * 
24  * There are three classes of DatabaseException
25  * <ul>
26  * <li>ServiceException is a class of serious failures e.g. (connection failure, database corruption) 
27  * <li>ValidationException is a class of exceptions due to invalid semantic graph 
28  * <li>AssumptionException is a class of exceptions due to unsatisfied user assumptions (e.g. user assumes a single result or existence of an adapter)
29  * </ul>
30  * 
31  * The resources related to an exception are available for pretty printing uses. 
32  * 
33  * @author Antti Villberg
34  * @version 0.7
35  * @see ValidationException
36  * @see ServiceException
37  * @see AssumptionException
38  * 
39  */
40 public class DatabaseException extends Exception {
41
42     private static final long serialVersionUID = -6234485044648476711L;
43
44     private ArrayList<Resource> resources;
45     private ArrayList<String> names;
46     private ArrayList<Integer> indices;
47     
48     public DatabaseException() {
49         super();
50     }
51
52     public DatabaseException(String message, Throwable cause) {
53         super(message, cause);
54     }
55
56     public DatabaseException(String message, Throwable cause, Resource ... rs) {
57         super(message, cause);
58         resources = new ArrayList<Resource>();
59         for(Resource r : rs) resources.add(r); 
60     }
61
62     public DatabaseException(Throwable cause, Resource ... rs) {
63         super(cause);
64         resources = new ArrayList<Resource>();
65         for(Resource r : rs) resources.add(r); 
66     }
67
68     public DatabaseException(String message, Resource ... rs) {
69         super(message);
70         resources = new ArrayList<Resource>();
71         for(Resource r : rs) resources.add(r); 
72     }
73
74     public DatabaseException(String message) {
75         super(message);
76     }
77
78     public DatabaseException(String message, int ... resources) {
79         super(message);
80         for(int r : resources) addIndex(r);
81     }
82
83     public DatabaseException(int ... resources) {
84         super();
85         for(int r : resources) addIndex(r);
86     }
87
88     public DatabaseException(Throwable cause) {
89         super(cause);
90     }
91     
92     public void addResource(Resource resource) {
93
94         assert(resource != null);
95         
96         if(resources == null) resources = new ArrayList<Resource>();
97         
98         resources.add(resource);
99         
100     }
101     
102     public void addIndex(int index) {
103
104         assert(index != 0);
105         
106         if(indices == null) indices = new ArrayList<Integer>();
107         
108         indices.add(index);
109         
110     }
111
112     public int getIndex(int index) {
113         return indices.get(index);
114     }
115
116     public Collection<Resource> getResources() {
117         return resources;
118     }
119     
120     public void setNames(ArrayList<String> names) {
121         this.names  = names;
122     }
123     
124     /**
125      * Throws the exception caused by the invocation of this request. The
126      * parameter allows checked exceptions to be thrown.
127      * 
128      * @param <T>
129      * @param clazz
130      * @throws T
131      */
132     @SuppressWarnings("unchecked")
133     public <T extends Throwable> void throwCause(Class<T> clazz) throws T {
134         Throwable t = getCause();
135         if (t == null)
136             return;
137         if (clazz.isInstance(t))
138             throw (T) t;
139         if (t instanceof RuntimeException)
140             throw (RuntimeException) t;
141         if (t instanceof Error)
142             throw (Error) t;
143         throw new RuntimeException("See cause for the real exception.", t);
144     }
145
146     public DatabaseException newStack() {
147         return new DatabaseException(this);
148     }
149     
150     @Override
151     public String toString() {
152         if(names != null) System.out.println(names);
153         return super.toString();
154     }
155     
156     public String getShortExplanation() {
157         return getMessage();
158     }
159     
160     public String getExplanation(ReadGraph graph) throws DatabaseException {
161         return getShortExplanation();
162     }
163     
164 }