View Javadoc

1   /* 
2    * Copyright (c) 2005-2011, Fraunhofer-Gesellschaft
3    * All rights reserved.
4    * 
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions are
7    * met:
8    * 
9    * (1) Redistributions of source code must retain the above copyright
10   *     notice, this list of conditions and the disclaimer at the end.
11   *     Redistributions in binary form must reproduce the above copyright
12   *     notice, this list of conditions and the following disclaimer in
13   *     the documentation and/or other materials provided with the
14   *     distribution.
15   * 
16   * (2) Neither the name of Fraunhofer nor the names of its
17   *     contributors may be used to endorse or promote products derived
18   *     from this software without specific prior written permission.
19   * 
20   * DISCLAIMER
21   * 
22   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33   *  
34   */
35  package org.ogf.graap.wsag.server.persistence.impl;
36  
37  import java.util.List;
38  import java.util.Vector;
39  
40  import org.ogf.graap.wsag.server.persistence.IAgreementFactoryHome;
41  import org.ogf.graap.wsag.server.persistence.IAgreementHome;
42  import org.ogf.graap.wsag.server.persistence.PersistedResourceException;
43  import org.ogf.graap.wsag.server.persistence.PersistentAgreement;
44  import org.ogf.graap.wsag.server.persistence.PersistentAgreementFactory;
45  
46  /**
47   * Facade for all agreement factories known to the WSAG4J engine.
48   * <p>
49   * Database-based implementation of an agreement home. This implementation encapsulates the retrieval of
50   * persisted agreements, the re-building of persisted agreements and the deletion of persisted agreements in
51   * case of an agreement removal.
52   * </p>
53   * 
54   * <p>
55   * Persisted agreements are encapsulated and stored in {@link PersistentAgreementContainer} objects. These
56   * objects store all agreement-related information and are used to re-build the original agreement. To access
57   * the information stored in such a container, {@link DatabasePersistentAgreement} instances are used. This
58   * class is able to provide a transparent access and processing of the required information.
59   * </p>
60   * 
61   * @author T.Weuffel
62   */
63  
64  //
65  // TODO there shouldn't be a single facade for all agreement factories in the system,
66  // instead the home should be implemented by each factory on its own. Re-desing in v2.0.
67  //
68  public class DatabaseAgreementHome implements IAgreementHome
69  {
70  
71      private IAgreementFactoryHome factoryHome;
72  
73      /**
74       * @param factoryHome
75       *            the the factory home for resolving and removing the agreement instances.
76       */
77      public DatabaseAgreementHome( IAgreementFactoryHome factoryHome )
78      {
79          this.factoryHome = factoryHome;
80      }
81  
82      /*
83       * TODO the API would allow to resolve multiple agreements with the same id from different factories. This
84       * issue should be solved in version 2.0 redesign.
85       */
86      /**
87       * {@inheritDoc}
88       */
89      public PersistentAgreement find( String agreementId ) throws PersistedResourceException
90      {
91          try
92          {
93              PersistentAgreementFactory[] factories = factoryHome.list();
94              for ( int i = 0; i < factories.length; i++ )
95              {
96                  PersistentAgreement agreement = factories[i].find( agreementId );
97                  if ( agreement != null )
98                  {
99                      return agreement;
100                 }
101             }
102         }
103         catch ( Exception e )
104         {
105             throw new PersistedResourceException( e );
106         }
107         return null;
108     }
109 
110     /**
111      * {@inheritDoc}
112      */
113     public PersistentAgreement[] list() throws Exception
114     {
115         List<PersistentAgreement> result = new Vector<PersistentAgreement>();
116 
117         PersistentAgreementFactory[] factories = factoryHome.list();
118         for ( int i = 0; i < factories.length; i++ )
119         {
120             PersistentAgreement[] agreements = factories[i].list();
121             for ( int j = 0; j < agreements.length; j++ )
122             {
123                 result.add( agreements[j] );
124             }
125         }
126         return result.toArray( new PersistentAgreement[result.size()] );
127     }
128 
129     /**
130      * {@inheritDoc}
131      */
132     public PersistentAgreement[] list( String agreementFactoryId ) throws Exception
133     {
134         PersistentAgreementFactory[] factories = factoryHome.list();
135         for ( int i = 0; i < factories.length; i++ )
136         {
137             if ( factories[i].getResourceId().equals( agreementFactoryId ) )
138             {
139                 return factories[i].list();
140             }
141         }
142 
143         //
144         // factory with resource id not found
145         //
146         return new PersistentAgreement[0];
147     }
148 
149     /**
150      * 
151      * This method should not be used as this class is a facade.
152      * 
153      * {@inheritDoc}
154      * 
155      * @see IAgreementHome#remove(String)
156      * 
157      * @deprecated
158      */
159     public void remove( String agreementId ) throws PersistedResourceException
160     {
161         PersistentAgreementFactory[] factories = new PersistentAgreementFactory[0];
162 
163         try
164         {
165             factories = factoryHome.list();
166         }
167         catch ( Exception e )
168         {
169             throw new PersistedResourceException( e );
170         }
171 
172         for ( int i = 0; i < factories.length; i++ )
173         {
174             factories[i].remove( agreementId );
175         }
176     }
177 }