<?xml version="1.0" encoding="UTF-8"?>
<!--
  Copyright 2002-2004 The Apache Software Foundation or its licensors,
  as applicable.
  
  Licensed 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.
-->
<!DOCTYPE document PUBLIC "-//WAMBLEE.ORG//ENTITIES Include Extension for Xdoc V1//EN"
"includeExtension-v1.dtd">
<document> 
  <header> 
    <title>Photos Application Model</title> 
  </header> 
  <body> 
    <p>This document explains the model used in the photos application as well as its implementation
      and several non-functional aspects.</p>
    
    <section>
      <title>Introduction</title>
      
      <p>
        The model is the central part of the photos application because it provides the interface
        for dealing with photo albums inside the application.
        This version of the documentation is very high level. It will be extended in the future as
        more work is done on the model.
        In particular, file system based storage and concurrency support also exist 
        but are not discussed in this document. This is because storage and concurrency will be
        handled differently in the future.  The current document focuses exclusively on the 
        photo model and on authorization. 
      </p>
    </section>
    <section id="requirements">
      <title>Requirements</title>
      
      <ol>
        <li>It shall be possible to represent albums of containing entries where each entry can be a
          photo or and album (recursive).</li>
        <li>
          It shall be possible to restrict access to photo albums based on authorization rules. 
        </li>
      </ol>
      
      <section>
        <title>Non-functional Requirements</title>
        <ol>
          <li>
            The overhead of evaluating authorization rules shall be negligible.
          </li>
        </ol>
      </section>
    </section>
    
    <section id="design">
      <title>Design</title>
      <p>
        The model is a basic example of the composite pattern and allows arbitrarily deep nesting of
        photo albums. Authorization is implemented as a decorator which filters the list of photo
        entries for an album based on authorization rules using the
        <code>AuthorizationService</code>.  
      </p>
      
      <p>
        The general design filosophy of the model is based on the factory method and decorator 
        design patterns. The model is cleanly separated into: 
      </p>
      <ul>
        <li>
          Interface definitions: used by clients of the model. A client never needs to know the
          concrete type of an album of photo because the correct objects will be created by the
          implementations. 
        </li>
        <li>
          Storage based implementation: One or more implementations that store the model in 
          some way (e.g. file system, database, in memory). 
        </li>
        <li>
          Decorators for implementing other concerns such as security/authorization and 
          concurrency. 
        </li>
      </ul>
      
    </section>
    
    <section id="structure">
      <title>Structure</title>
      
      <section>
        <title>Package Overview</title>
        
        <p>
          The relevant packages for the model are:
        </p>
        <ul>
          <li><code>org.wamblee.photos.model</code>: Containing the model interfaces</li>
          <li><code>org.wamblee.photos.model.authorization</code>: Containing a decorator for 
            the model that applies authorization rules. </li>
          <li><code>org.wamblee.photos.authorizationrules</code>:  Containing the authorization
            rules for the photos application.</li>
        </ul>
        <figure src="../umlpictures/class-diagram-org.wamblee.photos.model.packages.jpg" 
          alt="Package overview." />
      
        <p>
          Note that the authorization rules are not used directly by the
          <code>authorization</code> package. This is because the <code>AuthorizationService</code>
          is used and the rules are configured in the authorization service and never accessed
          directly by the model. 
        </p>
      </section>
     
      <section>
        <title>Model</title>
        
        <p>
          The model is a standard example of the Composite pattern. The general abstraction is that
          of a photo entry which can be either a photo or a photo album. The model provides a basic
          API for manipulating photos and iterating through the photo album. In addition, the
          <code>Path</code> utility class is used for manipulating paths. 
        </p>
        <figure src="../umlpictures/class-diagram-org.wamblee.photos.model.main.jpg" 
          alt="The model. " />
        
      </section>
      
      <section>
        <title>Authorization Rules</title>
        
        <p>
          To implement authorization for the photos application, two rules are provided:
        </p>
        <ul>
          <li><code>PhotoAuthorizationRule</code>: This rule grants access to photo albums belonging
          to the groups that a user belongs to. For instance, if a user belongs to group 'XY' and
            'YA', then that user has access to the albums 'XY' and 'YA'. This means that the root 
          album itself and these two entries may be accessed as well as all other entries below
            these albums. </li>
          <li>
            <code>PageAuthorizationRule</code>: This rule is used for granting access to certain
            pages to certain users. This rule is used implicitly when <code>ProtectedPage</code> checks access
            to a page. It is relevant for the model. 
          </li>
        </ul>
        <figure src="../umlpictures/class-diagram-org.wamblee.photos.authorization.jpg" 
          alt="Authorization Rules. " />
        
      </section>
      
      <section>
        <title>Authorization Decorator for the Model</title>
        
        <p>
          Authorization is implemented as a decorator of the model. One common base class
          <code>AuthorizedPhotoEntry</code> stores a reference to the decorated object. 
          Authorization is done in the <code>AuthorizedAlbum</code> class which computes a list of
          authorized entries to which a user may have access using the
          <code>AuthorizationService</code>. 
        </p>
        <figure src="../umlpictures/class-diagram-org.wamblee.photos.model.authorization.main.jpg" 
          alt="Authorization decorator for the model. " />
        
        <p>
          The list of authorized photo entries is cached using <code>CachedObject</code> (not shown
          in the figure). If an appropriate
          cache is used, then at certain time, this list will expire and the <code>CachedObject</code>
          will call a computation callback on the <code>AuthorizedAlbum</code> to recompute the list
          of authorized entries. To create a unique id for the cached photo entry an id must be used
          as key for the cache which is guaranteed to uniquely identify a session. 
        </p>
        <p>
          The caching approach ensures efficiency because the authorization rules
          are checked less frequently, and because the authorized photo entries are not cached 
          indefinitely. 
          Note that for this approach to work, the authorized album should be specific to a specific
          user, which means it is best stored in session scope in the web tier. 
        </p>
      </section>
     
      <section>
        <title>Specific Operations</title>
        
        <p>
          In addition to the basic operations on the model, some additional operations are defined
          to allow operations in the user interface for creating albums and uploading photos. 
        </p>
        <figure src="../umlpictures/class-diagram-org.wamblee.photos.model.authorization.operations.jpg" 
          alt="Specific operations for the model. " />
        
      </section>
      
    </section>
    
    <section id="dynamics">
      <title>Dynamics</title>
     
      <section>
        <title>Get a Photo Entry</title>
        <p>
          The scenario below explains what happens when a photo entry is obtained. 
        </p>
        <figure src="../umlpictures/sequence-diagram-org.wamblee.photos.model.authorization.getentry.jpg" 
          alt="Get a photo entry. " />
        
      </section>
      
    </section>
    
    <section>
      <title>Design Rules</title>
      
      <p>-</p>
    </section>
    
  </body>
</document>
