Persistence/OrderSystemExample/OrderSystemPersistenceTeam

Team OrderSystemPersistenceTeam of the OT/JPA example "Order-System".

package org.objectteams.samples.ordersystem.persistence;

import java.util.Collection;

import org.objectteams.Team;

import base org.objectteams.samples.ordersystem.gui.ModelAdapterTeam;
import base org.objectteams.samples.ordersystem.gui.OrderSystemMainFrame;
import base org.objectteams.samples.ordersystem.order.StockOrder;
import base org.objectteams.samples.ordersystem.reservation.Reservations;
import base org.objectteams.samples.ordersystem.reservation.StockReservations;

/**
 * This team wires the Order System to the OT/JPA framework.
 *  
 * @author stephan
 * @role ModelBridge a bridge to the {@link ModelAdapterTeam}
 */
@SuppressWarnings("abstractrelevantrole") // ReservationsBridge0
public team class OrderSystemPersistenceTeam extends AbstractPersistenceTeam
{

        /** Configure the concrete persistence unit: */
        protected String getPersistenceUnitName() {
                return "ordersystem";
        }
    
        public void simpleLog(String msg) {
                System.out.print("OrderSystem: ");
                super.simpleLog(msg);
        }
    
        /** Define the trigger for closing the persistence context. */
        protected class ShutDownHook playedBy OrderSystemMainFrame {
                void shutDown() {
                        OrderSystemPersistenceTeam.this.lifeCycle.closePersistenceContext();
                }

                void shutDown() <- before void closeApp();
        }
        
        // ========== Persisting/retrieving the Reservations component (singleton): ==========
        
        boolean reservationsRetrieved = false;
        
        protected abstract class ReservationsBridge0 playedBy Reservations
        {
                /**
                 * Triggers for persisting new Reservables.
                 * NB: base methods are called by a super-calls, must intercept at the exact class.
                 */
                @SuppressWarnings("decapsulation") // accessing base type Reservable
                void persist(Object item) <- after int reserve(Reservable<@base> item, int count),
                                                   int release(Reservable<@base> item, int count),
                                                   void deliver(Reservable<@base> item, int count);
                // alternative short version:
                // persist <- after reserve, release, deliver;

                static void persist(Object item) {
                        persistInTransaction(item);
                }
        }
        protected class ReservationsBridge extends ReservationsBridge0 playedBy StockReservations
        {
                // Technical note: 
                //     This role accesses its base class StockReservations in positions 
                //     that are not covered by the given base import.
                //     Such access is generally discouraged and requires the base class
                //     to be referenced using the fully qualified name.
                //     This situation arises because the role retrieves an instance of its base
                //     and installs it as the base class's singleton instance.
                //     If the role would persist itself, too, all communication could use
                //     lifting and lowering instead of direct references to the base class.

                @SuppressWarnings("decapsulation")
                void setInstance(org.objectteams.samples.ordersystem.reservation.StockReservations instance) 
                        -> set Reservations instance;
                
                org.objectteams.samples.ordersystem.reservation.StockReservations theInstance() 
                        <- replace StockReservations theInstance();

                /**
                 * When the singleton instance of {@link StockReservations} is accessed for the
                 * first time, retrieve or persist it as needed.
                 */
                @SuppressWarnings("basecall")
                static callin org.objectteams.samples.ordersystem.reservation.StockReservations theInstance() 
                {
                        Collection<org.objectteams.samples.ordersystem.reservation.StockReservations> ress;
                        org.objectteams.samples.ordersystem.reservation.StockReservations res;
                        
                        if (!OrderSystemPersistenceTeam.this.reservationsRetrieved) 
                        {
                                OrderSystemPersistenceTeam.this.reservationsRetrieved = true;
                                simpleLog("checking existing reservations.");
                                ress = findAllEntities("StockReservations");
                                if (ress != null && ress.size() > 0) {
                                        simpleLog("retrieved reservations: "+ress.size());
                                        res = ress.iterator().next();
                                        setInstance(res);
                                        return res;
                                }
                        }
                        res = base.theInstance();
                        persistInTransaction(res);
                        return res;
                }
        }

        // ========== Persisting Orders: ==========
        
        protected team class StockOrderBridge playedBy StockOrder 
        {
                private void persist(Object item) {
                        persistInTransaction(item);
                        activate(ALL_THREADS); // activate to enable triggers in nested role ItemBridge
                }

                void persist(Object item) <- after void activate(Thread t)
                        base when (t == Team.ALL_THREADS)
                        with { item <- base }
                
                void persist(Object item) <- after void check() // might directly bind to persistInTransaction(item); (based on Trac #324)
                        with { item <- base }
                
                @SuppressWarnings("decapsulation")
                protected class ItemBridge playedBy Item<@base> 
                {
                        void persistItem(Object item) {
                                persist(item);
                        }
                        
                        void persistItem(Object item) <- after void check()  // might directly bind to persistInTransaction(item); (based on Trac #324)
                                with { item <- base }
                }
        }       
}