Package org.apache.isis.core.metamodel.facets.object.viewmodel.annotation

Source Code of org.apache.isis.core.metamodel.facets.object.viewmodel.annotation.ViewModelFacetAnnotation

/*
*  Licensed to the Apache Software Foundation (ASF) under one
*  or more contributor license agreements.  See the NOTICE file
*  distributed with this work for additional information
*  regarding copyright ownership.  The ASF licenses this file
*  to you under the Apache License, Version 2.0 (the
*  "License"); you may not use this file except in compliance
*  with the License.  You may obtain a copy of the License at
*
*        http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing,
*  software distributed under the License is distributed on an
*  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
*  KIND, either express or implied.  See the License for the
*  specific language governing permissions and limitations
*  under the License.
*/

package org.apache.isis.core.metamodel.facets.object.viewmodel.annotation;

import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.apache.isis.applib.ViewModel;
import org.apache.isis.applib.services.bookmark.Bookmark;
import org.apache.isis.applib.services.bookmark.BookmarkService;
import org.apache.isis.applib.services.memento.MementoService;
import org.apache.isis.core.metamodel.adapter.ObjectAdapter;
import org.apache.isis.core.metamodel.adapter.mgr.AdapterManager;
import org.apache.isis.core.metamodel.adapter.oid.Oid;
import org.apache.isis.core.metamodel.adapter.oid.RootOidDefault;
import org.apache.isis.core.metamodel.facetapi.FacetHolder;
import org.apache.isis.core.metamodel.facets.object.viewmodel.ViewModelFacetAbstract;
import org.apache.isis.core.metamodel.facets.properties.update.modify.PropertySetterFacet;
import org.apache.isis.core.metamodel.runtimecontext.ServicesInjector;
import org.apache.isis.core.metamodel.spec.ObjectSpecId;
import org.apache.isis.core.metamodel.spec.ObjectSpecification;
import org.apache.isis.core.metamodel.spec.SpecificationLoader;
import org.apache.isis.core.metamodel.spec.feature.Contributed;
import org.apache.isis.core.metamodel.spec.feature.OneToOneAssociation;

public class ViewModelFacetAnnotation extends ViewModelFacetAbstract {

    private final SpecificationLoader specificationLoader;
    private final ServicesInjector servicesInjector;
    private final AdapterManager adapterManager;

    public ViewModelFacetAnnotation(
            final FacetHolder holder,
            final SpecificationLoader specificationLoader,
            final AdapterManager adapterManager,
            final ServicesInjector servicesInjector) {
        super(holder);
        this.specificationLoader = specificationLoader;
        this.servicesInjector = servicesInjector;
        this.adapterManager = adapterManager;
    }

    @Override
    public void initialize(Object viewModelPojo, String mementoStr) {

        final MementoService mementoService = servicesInjector.lookupService(MementoService.class);
        final BookmarkService bookmarkService = servicesInjector.lookupService(BookmarkService.class);

        final MementoService.Memento memento = mementoService.parse(mementoStr);

        final Set<String> mementoKeys = memento.keySet();

        // manually recreate the adapter in order to be able to query state via the metamodel
        ObjectAdapter viewModelAdapter = adapterManager.getAdapterFor(viewModelPojo);
        if(viewModelAdapter == null) {
            final ObjectSpecification objectSpecification = specificationLoader.loadSpecification(viewModelPojo.getClass());
            final ObjectSpecId objectSpecId = objectSpecification.getSpecId();
            viewModelAdapter = adapterManager.mapRecreatedPojo(new RootOidDefault(objectSpecId, mementoStr, Oid.State.VIEWMODEL), viewModelPojo);
        }

        final ObjectSpecification spec = viewModelAdapter.getSpecification();
        final List<OneToOneAssociation> properties = spec.getProperties(Contributed.EXCLUDED);
        for (OneToOneAssociation property : properties) {
            final String propertyId = property.getId();

            Object propertyValue = null;

            if(mementoKeys.contains(propertyId)) {
                final Class<?> propertyType = property.getSpecification().getCorrespondingClass();
                propertyValue = memento.get(propertyId, propertyType);
            } else if(mementoKeys.contains(propertyId + ".bookmark")) {
                final Bookmark propertyValueBookmark = memento.get(propertyId + ".bookmark", Bookmark.class);
                propertyValue = bookmarkService.lookup(propertyValueBookmark);
            }

            if(propertyValue != null) {
                property.set(viewModelAdapter, adapterManager.adapterFor(propertyValue));
            }
        }
    }
   
    @Override
    public String memento(Object viewModelPojo) {

        final MementoService mementoService = servicesInjector.lookupService(MementoService.class);
        final BookmarkService bookmarkService = servicesInjector.lookupService(BookmarkService.class);

        final MementoService.Memento memento = mementoService.create();

        // this is horrible, but there's a catch-22 here...
        // we need an adapter in order to query the state of the object via the metamodel, on the other hand
        // we can't create an adapter without the identifier, which is what we're trying to derive
        // so... we create a temporary transient adapter, use it to wrap this adapter and interrogate this pojo,
        // then throw away that adapter (remove from the adapter map)
        boolean createdTemporaryAdapter = false;
        ObjectAdapter viewModelAdapter = adapterManager.getAdapterFor(viewModelPojo);
        if(viewModelAdapter == null) {
            final ObjectSpecification objectSpecification = specificationLoader.loadSpecification(viewModelPojo.getClass());
            final ObjectSpecId objectSpecId = objectSpecification.getSpecId();
            viewModelAdapter = adapterManager.mapRecreatedPojo(RootOidDefault.create(objectSpecId, UUID.randomUUID().toString()), viewModelPojo);

            createdTemporaryAdapter = true;
        }

        try {
            final ObjectSpecification spec = viewModelAdapter.getSpecification();
            final List<OneToOneAssociation> properties = spec.getProperties(Contributed.EXCLUDED);
            for (OneToOneAssociation property : properties) {
                // ignore read-only
                if(!property.containsDoOpFacet(PropertySetterFacet.class)) {
                    continue;
                }
                // ignore those explicitly annotated as @NotPersisted
                if(property.isNotPersisted()) {
                    continue;
                }

                // otherwise, include
                final ObjectAdapter propertyValueAdapter = property.get(adapterManager.adapterFor(viewModelPojo));
                if(propertyValueAdapter != null) {
                    final Object propertyValue = propertyValueAdapter.getObject();
                    if(mementoService.canSet(propertyValue)) {
                        memento.set(property.getId(), propertyValue);
                    } else {
                        final Bookmark propertyValueBookmark = bookmarkService.bookmarkFor(propertyValue);
                        memento.set(property.getId() + ".bookmark", propertyValueBookmark);
                    }
                }
            }
            return memento.asString();
        } finally {
            if(createdTemporaryAdapter) {
                adapterManager.removeAdapter(viewModelAdapter);
            }
        }
    }


}
TOP

Related Classes of org.apache.isis.core.metamodel.facets.object.viewmodel.annotation.ViewModelFacetAnnotation

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.