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 org.apache.isis.viewer.restfulobjects.applib.JsonRepresentation;
022import org.apache.isis.viewer.restfulobjects.applib.LinkRepresentation;
023import org.apache.isis.viewer.restfulobjects.applib.Rel;
024import org.codehaus.jackson.JsonNode;
025
026public class DomainObjectRepresentation extends DomainRepresentation  {
027
028    public DomainObjectRepresentation(final JsonNode jsonNode) {
029        super(jsonNode);
030    }
031
032    public String getTitle() {
033        return getString("title");
034    }
035
036    /**
037     * Populated only for domain objects, not for domain services.
038     */
039    public String getDomainType() {
040        return getString("domainType");
041    }
042
043    /**
044     * Populated only for domain objects, not for domain services.
045     */
046    public String getInstanceId() {
047        return getString("instanceId");
048    }
049
050    /**
051     * Populated only for domain services, not for domain objects.
052     */
053    public String getServiceId() {
054        return getString("serviceId");
055    }
056
057    public JsonRepresentation getMembers() {
058        return getRepresentation("members");
059    }
060
061    public DomainObjectMemberRepresentation getProperty(final String id) {
062        return getMember(id, "property");
063    }
064
065    public JsonRepresentation getProperties() {
066        return getRepresentation("members[memberType=property]").ensureArray();
067    }
068
069    public DomainObjectMemberRepresentation getCollection(final String id) {
070        return getMember(id, "collection");
071    }
072
073    public JsonRepresentation getCollections() {
074        return getRepresentation("members[memberType=collection]").ensureArray();
075    }
076
077    public DomainObjectMemberRepresentation getAction(final String id) {
078        return getMember(id, "action");
079    }
080
081    private DomainObjectMemberRepresentation getMember(final String id, String memberType) {
082        // TODO: would be nice to use "members.%s[memberType=...]" instead
083        JsonRepresentation jsonRepr = getRepresentation("members.%s", id);
084        if(jsonRepr == null) {
085            return null;
086        }
087        DomainObjectMemberRepresentation member = jsonRepr.as(DomainObjectMemberRepresentation.class);
088        return member.getMemberType().equals(memberType) ? member : null;
089    }
090
091    public JsonRepresentation getActions() {
092        return getRepresentation("members[memberType=action]");
093    }
094
095    /**
096     * Only for transient, persistable, objects
097     */
098    public LinkRepresentation getPersistLink() {
099        return getLinkWithRel(Rel.PERSIST);
100    }
101
102    
103    /**
104     * Isis extension.
105     */
106    public String getOid() {
107        return getString("extensions.oid");
108    }
109
110
111}