001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.isis.viewer.restfulobjects.rendering.util; 020 021import org.apache.isis.core.metamodel.adapter.ObjectAdapter; 022import org.apache.isis.core.metamodel.adapter.oid.Oid; 023import org.apache.isis.core.metamodel.adapter.oid.OidMarshaller; 024import org.apache.isis.core.metamodel.adapter.oid.RootOid; 025import org.apache.isis.core.metamodel.adapter.oid.TypedOid; 026import org.apache.isis.viewer.restfulobjects.rendering.RendererContext; 027 028public final class OidUtils { 029 030 private OidUtils() { 031 } 032 033 public static String getDomainType(final ObjectAdapter objectAdapter) { 034 Oid oid = objectAdapter.getOid(); 035 if(oid == null || !(oid instanceof TypedOid)) { 036 return null; 037 } 038 TypedOid typedOid = (TypedOid) oid; 039 return typedOid.getObjectSpecId().asString(); 040 } 041 042 public static String getInstanceId(final RendererContext renderContext, final ObjectAdapter objectAdapter) { 043 String oidStr = getOidStr(renderContext, objectAdapter); 044 // REVIEW: it's a bit hokey to join these together just to split them out again. 045 return oidStr != null ? getOidMarshaller().splitInstanceId(oidStr): null; 046 } 047 048 049 public static String getOidStr(final RendererContext renderContext, final ObjectAdapter objectAdapter) { 050 final Oid oid = objectAdapter.getOid(); 051 if(!(oid instanceof RootOid)) { 052 throw new IllegalArgumentException("objectAdapter must be a root adapter"); 053 } 054 return oid != null ? oid.enStringNoVersion(getOidMarshaller()) : null; 055 } 056 057 private static OidMarshaller getOidMarshaller() { 058 return new OidMarshaller(); 059 } 060 061 062}