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.applib.domainobjects; 020 021import java.util.Map; 022import com.google.common.base.Predicate; 023import com.google.common.collect.Iterables; 024import org.apache.isis.viewer.restfulobjects.applib.JsonRepresentation; 025import org.apache.isis.viewer.restfulobjects.applib.LinkRepresentation; 026import org.apache.isis.viewer.restfulobjects.applib.Rel; 027import org.codehaus.jackson.JsonNode; 028 029public class DomainObjectRepresentation extends DomainRepresentation { 030 031 public DomainObjectRepresentation(final JsonNode jsonNode) { 032 super(jsonNode); 033 } 034 035 public String getTitle() { 036 return getString("title"); 037 } 038 039 /** 040 * Populated only for domain objects, not for domain services. 041 */ 042 public String getDomainType() { 043 return getString("domainType"); 044 } 045 046 /** 047 * Populated only for domain objects, not for domain services. 048 */ 049 public String getInstanceId() { 050 return getString("instanceId"); 051 } 052 053 /** 054 * Populated only for domain services, not for domain objects. 055 */ 056 public String getServiceId() { 057 return getString("serviceId"); 058 } 059 060 public JsonRepresentation getMembers() { 061 return getRepresentation("members"); 062 } 063 064 public DomainObjectMemberRepresentation getProperty(final String id) { 065 return getMember(id, "property"); 066 } 067 068 public JsonRepresentation getProperties() { 069 return getMembersOfType("property"); 070 } 071 072 public DomainObjectMemberRepresentation getCollection(final String id) { 073 return getMember(id, "collection"); 074 } 075 076 public JsonRepresentation getCollections() { 077 return getMembersOfType("collection"); 078 } 079 080 public DomainObjectMemberRepresentation getAction(final String id) { 081 return getMember(id, "action"); 082 } 083 084 public JsonRepresentation getActions() { 085 return getMembersOfType("action"); 086 } 087 088 /** 089 * Only for transient, persistable, objects 090 */ 091 public LinkRepresentation getPersistLink() { 092 return getLinkWithRel(Rel.PERSIST); 093 } 094 095 096 /** 097 * Isis extension. 098 */ 099 public String getOid() { 100 return getString("extensions.oid"); 101 } 102 103 104 105 private DomainObjectMemberRepresentation getMember(final String id, String memberType) { 106 // TODO: would be nice to use "members.%s[memberType=...]" instead 107 JsonRepresentation jsonRepr = getRepresentation("members.%s", id); 108 if(jsonRepr == null) { 109 return null; 110 } 111 DomainObjectMemberRepresentation member = jsonRepr.as(DomainObjectMemberRepresentation.class); 112 return member.getMemberType().equals(memberType) ? member : null; 113 } 114 115 private JsonRepresentation getMembersOfType(String memberTypeOf) { 116 final JsonRepresentation members = getRepresentation("members"); 117 return JsonRepresentation.newMap().mapPut( 118 Iterables.filter(members.mapIterable(), havingMemberTypeOf(memberTypeOf))); 119 } 120 121 private static Predicate<Map.Entry<String, JsonRepresentation>> havingMemberTypeOf(final String memberTypeOf) { 122 return new Predicate<Map.Entry<String, JsonRepresentation>>() { 123 @Override 124 public boolean apply(Map.Entry<String, JsonRepresentation> input) { 125 final JsonRepresentation value = input.getValue(); 126 final String memberType = value.getRepresentation("memberType").asString(); 127 return memberTypeOf.equals(memberType); 128 } 129 }; 130 } 131 132 133}