]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.layer0.utils/src/org/simantics/layer0/utils/direct/ExceptionUtils.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.layer0.utils / src / org / simantics / layer0 / utils / direct / ExceptionUtils.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.layer0.utils.direct;
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16
17 import org.simantics.db.AsyncReadGraph;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.common.utils.NameUtils;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.procedure.AsyncProcedure;
23 import org.simantics.db.request.Read;
24
25 public class ExceptionUtils {
26     
27     public static <T> void decipher(AsyncReadGraph graph, Throwable throwable, final AsyncProcedure<T> procedure) {
28
29         if(throwable instanceof DatabaseException) {
30
31             System.out.println("decipher " + throwable);
32             
33             final DatabaseException e = (DatabaseException)throwable;
34             
35             Collection<Resource> resources = e.getResources();
36             if(resources != null) {
37
38                 graph.asyncRequest(new Read<ArrayList<String>>() {
39     
40                     @Override
41                     public ArrayList<String> perform(ReadGraph graph) throws DatabaseException {
42                         
43                         Collection<Resource> resources = e.getResources();
44                         ArrayList<String> names = new ArrayList<String>();
45                         
46                         for(Resource r : resources) {
47                             names.add(NameUtils.getSafeName(graph, r));
48                         }
49     
50                         return names;
51                         
52                     }
53                     
54                 }, new AsyncProcedure<ArrayList<String>>() {
55     
56                     @Override
57                     public void exception(AsyncReadGraph graph, Throwable throwable) {
58                         procedure.exception(graph, throwable);
59                     }
60     
61                     @Override
62                     public void execute(AsyncReadGraph graph, ArrayList<String> names) {
63                         e.setNames(names);
64                         procedure.exception(graph, e);
65                     }
66                     
67                 });
68                 
69             } else {
70                 
71                 procedure.exception(graph, throwable);
72
73             }
74             
75         } else {
76             
77             procedure.exception(graph, throwable);
78
79         }
80         
81     }
82
83 }