1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.impl;
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.List;
18 import org.simantics.db.ReadGraph;
19 import org.simantics.db.Resource;
20 import org.simantics.db.common.utils.Literals;
21 import org.simantics.db.exception.ServiceException;
22 import org.simantics.db.exception.ValidationException;
23 import org.simantics.layer0.Layer0;
25 public class DatabaseUtils {
27 public static String getReadableName(ReadGraph graph, Resource resource) throws ValidationException, ServiceException {
28 Layer0 b = Layer0.getInstance(graph);
30 String uri = graph.getPossibleURI(resource);
31 if(uri != null) return uri;
33 List<String> names = new ArrayList<String>(1);
34 for(Resource nameResource : graph.getObjects(resource, b.HasName)) {
35 Object value = graph.getPossibleValue(nameResource);
37 names.add(Literals.shortString(Literals.literalToString(value)));
40 if(!names.isEmpty()) {
44 StringBuilder bb = new StringBuilder();
46 for(int i=0;i<names.size();++i) {
49 bb.append(names.get(i));
55 StringBuilder bb = new StringBuilder();
56 Object val = graph.getPossibleValue(resource);
58 if(val instanceof double[])
59 bb.append(Literals.shortString(Arrays.toString((double[])val)));
60 else if(val instanceof float[])
61 bb.append(Literals.shortString(Arrays.toString((float[])val)));
62 else if(val instanceof int[])
63 bb.append(Literals.shortString(Arrays.toString((int[])val)));
64 else if(val instanceof boolean[])
65 bb.append(Literals.shortString(Arrays.toString((boolean[])val)));
66 else if(val instanceof long[])
67 bb.append(Literals.shortString(Arrays.toString((long[])val)));
68 else if(val instanceof byte[])
69 bb.append(Literals.shortString(Arrays.toString((byte[])val)));
70 else if(val instanceof String[])
71 bb.append(Literals.shortString(Arrays.toString((String[])val)));
73 bb.append(Literals.shortString(val));
76 bb.append(resource.getResourceId());
78 for(Resource r : graph.getObjects(resource, b.InstanceOf)) {
79 bb.append(" : (" + getReadableName(graph, r) + ")");
83 for(Resource r : graph.getObjects(resource, b.Inherits)) {
84 bb.append(" <T (" + getReadableName(graph, r) + ")");
88 for(Resource r : graph.getObjects(resource, b.SubrelationOf)) {
89 bb.append(" <R (" + getReadableName(graph, r) + ")");