X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2FDatabaseUtils.java;fp=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2FDatabaseUtils.java;h=830e93d7f0a66d7cf9d50956c49634497132c81f;hb=969bd23cab98a79ca9101af33334000879fb60c5;hp=0000000000000000000000000000000000000000;hpb=866dba5cd5a3929bbeae85991796acb212338a08;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/DatabaseUtils.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/DatabaseUtils.java new file mode 100644 index 000000000..830e93d7f --- /dev/null +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/DatabaseUtils.java @@ -0,0 +1,97 @@ +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.db.impl; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.utils.Literals; +import org.simantics.db.exception.ServiceException; +import org.simantics.db.exception.ValidationException; +import org.simantics.layer0.Layer0; + +public class DatabaseUtils { + + public static String getReadableName(ReadGraph graph, Resource resource) throws ValidationException, ServiceException { + Layer0 b = Layer0.getInstance(graph); + + String uri = graph.getPossibleURI(resource); + if(uri != null) return uri; + + List names = new ArrayList(1); + for(Resource nameResource : graph.getObjects(resource, b.HasName)) { + Object value = graph.getPossibleValue(nameResource); + if(value != null) { + names.add(Literals.shortString(Literals.literalToString(value))); + } + } + if(!names.isEmpty()) { + if(names.size() == 1) + return names.get(0); + else { + StringBuilder bb = new StringBuilder(); + bb.append('['); + for(int i=0;i0) + bb.append(", "); + bb.append(names.get(i)); + } + bb.append(']'); + return bb.toString(); + } + } + StringBuilder bb = new StringBuilder(); + Object val = graph.getPossibleValue(resource); + if(val != null) { + if(val instanceof double[]) + bb.append(Literals.shortString(Arrays.toString((double[])val))); + else if(val instanceof float[]) + bb.append(Literals.shortString(Arrays.toString((float[])val))); + else if(val instanceof int[]) + bb.append(Literals.shortString(Arrays.toString((int[])val))); + else if(val instanceof boolean[]) + bb.append(Literals.shortString(Arrays.toString((boolean[])val))); + else if(val instanceof long[]) + bb.append(Literals.shortString(Arrays.toString((long[])val))); + else if(val instanceof byte[]) + bb.append(Literals.shortString(Arrays.toString((byte[])val))); + else if(val instanceof String[]) + bb.append(Literals.shortString(Arrays.toString((String[])val))); + else + bb.append(Literals.shortString(val)); + } + else + bb.append(resource.getResourceId()); + boolean ok = false; + for(Resource r : graph.getObjects(resource, b.InstanceOf)) { + bb.append(" : (" + getReadableName(graph, r) + ")"); + ok = true; + } + if(!ok) { + for(Resource r : graph.getObjects(resource, b.Inherits)) { + bb.append("