]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/DatabaseUtils.java
Fail safe import fixes made by Antti
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / DatabaseUtils.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.impl;
13
14 import java.util.ArrayList;
15 import java.util.Arrays;
16 import java.util.List;
17
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;
24
25 public class DatabaseUtils {
26
27     public static String getReadableName(ReadGraph graph, Resource resource) throws ValidationException, ServiceException {
28         Layer0 b = Layer0.getInstance(graph);
29         
30         String uri = graph.getPossibleURI(resource);
31         if(uri != null) return uri;
32         
33         List<String> names = new ArrayList<String>(1);
34         for(Resource nameResource : graph.getObjects(resource, b.HasName)) {
35             Object value = graph.getPossibleValue(nameResource);
36             if(value != null) {
37                 names.add(Literals.shortString(Literals.literalToString(value)));
38             }
39         }
40         if(!names.isEmpty()) {
41             if(names.size() == 1)
42                 return names.get(0);
43             else {
44                 StringBuilder bb = new StringBuilder();
45                 bb.append('[');
46                 for(int i=0;i<names.size();++i) {
47                     if(i>0)
48                         bb.append(", ");
49                     bb.append(names.get(i));
50                 }
51                 bb.append(']');
52                 return bb.toString();
53             }
54         }
55         StringBuilder bb = new StringBuilder();
56         Object val = graph.getPossibleValue(resource);
57         if(val != null) {
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)));
72             else 
73                 bb.append(Literals.shortString(val));
74         }
75         else
76             bb.append(resource.getResourceId());
77         boolean ok = false;
78         for(Resource r : graph.getObjects(resource, b.InstanceOf)) {
79             bb.append(" : (" + getReadableName(graph, r) + ")");
80             ok = true;
81         }
82         if(!ok) {
83             for(Resource r : graph.getObjects(resource, b.Inherits)) {
84                 bb.append(" <T (" + getReadableName(graph, r) + ")");
85                 ok = true;
86             }   
87             if(!ok) {
88                 for(Resource r : graph.getObjects(resource, b.SubrelationOf)) {
89                     bb.append(" <R (" + getReadableName(graph, r) + ")");
90                     ok = true;
91                 }   
92             }
93         }
94         return bb.toString();
95     }
96     
97 }