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.server.util; 020 021import org.apache.isis.core.metamodel.adapter.ObjectAdapter; 022import org.apache.isis.core.metamodel.adapter.oid.OidMarshaller; 023import org.apache.isis.core.metamodel.adapter.oid.RootOid; 024import org.apache.isis.core.metamodel.adapter.oid.RootOidDefault; 025import org.apache.isis.core.runtime.persistence.ObjectNotFoundException; 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 RendererContext renderContext, final ObjectAdapter objectAdapter) { 034 return org.apache.isis.viewer.restfulobjects.rendering.util.OidUtils.getDomainType(objectAdapter); 035 } 036 037 public static String getInstanceId(final RendererContext renderContext, final ObjectAdapter objectAdapter) { 038 return org.apache.isis.viewer.restfulobjects.rendering.util.OidUtils.getInstanceId(renderContext, objectAdapter); 039 } 040 041 // REVIEW: it's a bit hokey to join these together just to split them out again. 042 public static String joinAsOid(final String domainType, final String instanceIdEncoded) { 043 final String instanceIdUnencoded = UrlDecoderUtils.urlDecode(instanceIdEncoded); 044 045 return getOidMarshaller().joinAsOid(domainType, instanceIdUnencoded); 046 } 047 048 /** 049 * 050 * @return {@code null} if not found. 051 */ 052 public static ObjectAdapter getObjectAdapterElseNull(final RendererContext resourceContext, final String domainType, final String instanceId) throws ObjectNotFoundException { 053 try { 054 return getObjectAdapterElseThrowNotFound(resourceContext, domainType, instanceId); 055 } catch(ObjectNotFoundException ex) { 056 return null; 057 } 058 } 059 060 /** 061 * 062 * @throws {@link ObjectNotFoundException} if not found 063 */ 064 public static ObjectAdapter getObjectAdapterElseThrowNotFound(final RendererContext resourceContext, final String domainType, final String instanceId) throws ObjectNotFoundException { 065 final String oidStr = joinAsOid(domainType, instanceId); 066 return getObjectAdapterForUnencodedElseThrowNotFound(resourceContext, oidStr); 067 } 068 069 /** 070 * 071 * @return {@code null} if not found. 072 */ 073 public static ObjectAdapter getObjectAdapterElseNull(final RendererContext resourceContext, final String oidEncodedStr) { 074 try { 075 return getObjectAdapterElseThrowNotFound(resourceContext, oidEncodedStr); 076 } catch(ObjectNotFoundException ex) { 077 return null; 078 } 079 } 080 081 /** 082 * 083 * @throws {@link ObjectNotFoundException} if not found 084 */ 085 public static ObjectAdapter getObjectAdapterElseThrowNotFound(final RendererContext resourceContext, final String oidEncodedStr) { 086 final String oidStr = UrlDecoderUtils.urlDecode(oidEncodedStr); 087 return getObjectAdapterForUnencodedElseThrowNotFound(resourceContext, oidStr); 088 } 089 090 private static ObjectAdapter getObjectAdapterForUnencodedElseThrowNotFound(final RendererContext resourceContext, final String oidStr) { 091 final RootOid rootOid = RootOidDefault.deStringEncoded(oidStr, getOidMarshaller()); 092 return resourceContext.getPersistenceSession().loadObject(rootOid); 093 } 094 095 private static OidMarshaller getOidMarshaller() { 096 return new OidMarshaller(); 097 } 098 099 100}