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.core.metamodel.services.bookmarks;
020
021import org.apache.isis.applib.annotation.Hidden;
022import org.apache.isis.applib.annotation.NotContributed;
023import org.apache.isis.applib.annotation.NotContributed.As;
024import org.apache.isis.applib.annotation.NotInServiceMenu;
025import org.apache.isis.applib.annotation.Programmatic;
026import org.apache.isis.applib.services.bookmark.Bookmark;
027import org.apache.isis.applib.services.bookmark.BookmarkHolder;
028import org.apache.isis.applib.services.bookmark.BookmarkService;
029import org.apache.isis.core.metamodel.adapter.DomainObjectServices;
030import org.apache.isis.core.metamodel.adapter.DomainObjectServicesAware;
031import org.apache.isis.core.runtime.persistence.ObjectNotFoundException;
032
033public class BookmarkServiceDefault implements BookmarkService, DomainObjectServicesAware {
034
035    private DomainObjectServices domainObjectServices;
036    
037    /**
038     * Contributed action contributed to
039     * any class that implements {@link BookmarkHolder}.
040     * 
041     * <p>
042     * If required, applications can suppress by subclassing and annotating the
043     * overridden method with <tt>@Hidden</tt>:
044     * <pre>
045     * @Hidden
046     * public Object lookup(final BookmarkHolder bookmarkHolder) {
047     *     return super.lookup(bookmarkHolder);
048     * }
049     * </pre>
050     */
051    @Override
052    @NotInServiceMenu
053    @NotContributed(As.ASSOCIATION)
054    public Object lookup(final BookmarkHolder bookmarkHolder) {
055        Bookmark bookmark = bookmarkHolder.bookmark();
056        return bookmark != null? lookup(bookmark): null;
057    }
058
059    /**
060     * Contributed property (named '<tt>Object</tt>'), contributed to
061     * any class that implements {@link BookmarkHolder}.
062     * 
063     * <p>
064     * If required, applications can suppress by subclassing and annotating the
065     * overridden method with <tt>@Hidden</tt>:
066     * <pre>
067     * @Hidden
068     * public Object object(final BookmarkHolder bookmarkHolder) {
069     *     return super.object(bookmarkHolder);
070     * }
071     * </pre>
072     */
073    @NotInServiceMenu
074    @NotContributed(As.ACTION)
075    public Object object(final BookmarkHolder bookmarkHolder) {
076        return lookup(bookmarkHolder);
077    }
078    
079    @Hidden
080    @Override
081    public Object lookup(final Bookmark bookmark) {
082        if(bookmark == null) {
083            return null;
084        }
085        try {
086            return domainObjectServices.lookup(bookmark);
087        } catch(ObjectNotFoundException ex) {
088            return null;
089        }
090    }
091
092    @SuppressWarnings("unchecked")
093    @Hidden
094    @Override
095    public <T> T lookup(final Bookmark bookmark, Class<T> cls) {
096        return (T) lookup(bookmark);
097    }
098
099    @Hidden
100    @Override
101    public Bookmark bookmarkFor(final Object domainObject) {
102        if(domainObject == null) {
103            return null;
104        }
105        return domainObjectServices.bookmarkFor(domainObject);
106    }
107
108    @Override
109    public Bookmark bookmarkFor(Class<?> cls, String identifier) {
110        return domainObjectServices.bookmarkFor(cls, identifier);
111    }
112
113
114    @Override
115    @Programmatic
116    public void setDomainObjectServices(final DomainObjectServices domainObjectServices) {
117        this.domainObjectServices = domainObjectServices;
118    }
119
120
121
122}