--- /dev/null
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ * VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.layer0.utils.direct;\r
+\r
+import java.util.ArrayList;\r
+import java.util.Collection;\r
+\r
+import org.simantics.db.AsyncReadGraph;\r
+import org.simantics.db.ReadGraph;\r
+import org.simantics.db.Resource;\r
+import org.simantics.db.common.utils.NameUtils;\r
+import org.simantics.db.exception.DatabaseException;\r
+import org.simantics.db.procedure.AsyncProcedure;\r
+import org.simantics.db.request.Read;\r
+\r
+public class ExceptionUtils {\r
+ \r
+ public static <T> void decipher(AsyncReadGraph graph, Throwable throwable, final AsyncProcedure<T> procedure) {\r
+\r
+ if(throwable instanceof DatabaseException) {\r
+\r
+ System.out.println("decipher " + throwable);\r
+ \r
+ final DatabaseException e = (DatabaseException)throwable;\r
+ \r
+ Collection<Resource> resources = e.getResources();\r
+ if(resources != null) {\r
+\r
+ graph.asyncRequest(new Read<ArrayList<String>>() {\r
+ \r
+ @Override\r
+ public ArrayList<String> perform(ReadGraph graph) throws DatabaseException {\r
+ \r
+ Collection<Resource> resources = e.getResources();\r
+ ArrayList<String> names = new ArrayList<String>();\r
+ \r
+ for(Resource r : resources) {\r
+ names.add(NameUtils.getSafeName(graph, r));\r
+ }\r
+ \r
+ return names;\r
+ \r
+ }\r
+ \r
+ }, new AsyncProcedure<ArrayList<String>>() {\r
+ \r
+ @Override\r
+ public void exception(AsyncReadGraph graph, Throwable throwable) {\r
+ procedure.exception(graph, throwable);\r
+ }\r
+ \r
+ @Override\r
+ public void execute(AsyncReadGraph graph, ArrayList<String> names) {\r
+ e.setNames(names);\r
+ procedure.exception(graph, e);\r
+ }\r
+ \r
+ });\r
+ \r
+ } else {\r
+ \r
+ procedure.exception(graph, throwable);\r
+\r
+ }\r
+ \r
+ } else {\r
+ \r
+ procedure.exception(graph, throwable);\r
+\r
+ }\r
+ \r
+ }\r
+\r
+}\r