<?xml version="1.0"?>
<!--
  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.
-->
<document xmlns="http://maven.apache.org/XDOC/2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/XDOC/2.0 http://maven.apache.org/xsd/xdoc-2.0.xsd">
  <properties>
    <title>design model</title>
  </properties>
  <body>
    <section name="Table of contents">
      <macro name="toc">
        <param name="section" value="0"/>
        <param name="fromDepth" value="0"/>
        <param name="toDepth" value="4"/>
      </macro>
      <p>This document explains the model used in the photos application as well as its implementation
      and several non-functional aspects.</p>
    </section>
    <section name="Introduction">
      <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 name="Requirements">
      <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>
      <subsection name="Non-functional Requirements">
        <ol>
          <li>
            The overhead of evaluating authorization rules shall be negligible.
          </li>
        </ol>
      </subsection>
    </section>
    <section name="Design">
      <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 name="Structure">
      <subsection name="Package Overview">
        <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>
        <img 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>
      </subsection>
      <subsection name="Model">
        <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>
        <img src="../umlpictures/class-diagram-org.wamblee.photos.model.main.jpg" alt="The model. "/>
      </subsection>
      <subsection name="Authorization Rules">
        <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>
        <img src="../umlpictures/class-diagram-org.wamblee.photos.authorization.jpg" alt="Authorization Rules. "/>
      </subsection>
      <subsection name="Authorization Decorator for the Model">
        <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>
        <img 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>
      </subsection>
      <subsection name="Specific Operations">
        <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>
        <img src="../umlpictures/class-diagram-org.wamblee.photos.model.authorization.operations.jpg" alt="Specific operations for the model. "/>
      </subsection>
    </section>
    <section name="Dynamics">
      <subsection name="Get a Photo Entry">
        <p>
          The scenario below explains what happens when a photo entry is obtained. 
        </p>
        <img src="../umlpictures/sequence-diagram-org.wamblee.photos.model.authorization.getentry.jpg" alt="Get a photo entry. "/>
      </subsection>
    </section>
    <section name="Design Rules">
      <p>-</p>
    </section>
  </body>
</document>
