View Javadoc

1   /* 
2    * Copyright (c) 2007, 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.text.MessageFormat;
38  import java.util.List;
39  import java.util.Vector;
40  
41  import org.apache.log4j.Logger;
42  import org.ogf.graap.wsag.api.Agreement;
43  import org.ogf.graap.wsag.api.AgreementFactory;
44  import org.ogf.graap.wsag.api.logging.LogMessage;
45  import org.ogf.graap.wsag.server.persistence.PersistedResourceException;
46  import org.ogf.graap.wsag.server.persistence.PersistentAgreement;
47  
48  /**
49   * @author owaeld
50   * 
51   */
52  public class DatabasePersistentAgreementFactory extends AbstractPersistentAgreementFactory
53  {
54  
55      private static final Logger LOG = Logger.getLogger( DatabasePersistentAgreementFactory.class );
56  
57      /**
58       * Creates a new {@link DatabasePersistentAgreementFactory} using the given agreement factory as
59       * delegation target for calls to the {@link AgreementFactory} interface.
60       * 
61       * @param factory
62       *            the agreement factory
63       */
64      public DatabasePersistentAgreementFactory( AgreementFactory factory )
65      {
66          super( factory );
67      }
68  
69      /*
70       * (non-Javadoc)
71       * 
72       * @see
73       * org.ogf.graap.wsag.server.persistence.impl.SimplePersistentAgreementFactory#createAgreement(org.ogf
74       * .graap.wsag .api.AgreementOffer)
75       */
76      @Override
77      public PersistentAgreement persistAgreement( Agreement agreement ) throws PersistedResourceException
78      {
79          try
80          {
81              DatabasePersistentAgreement inserted =
82                  DatabasePersistentAgreement.insertAgreement( agreement, getResourceId() );
83              return inserted;
84          }
85          catch ( Exception e )
86          {
87              throw new PersistedResourceException( e );
88          }
89      }
90  
91      /**
92       * {@inheritDoc}
93       * 
94       * @see org.ogf.graap.wsag.server.persistence.PersistentAgreementFactory#load()
95       */
96      @Override
97      public PersistentAgreement[] doLoad() throws PersistedResourceException
98      {
99          if ( LOG.isDebugEnabled() )
100         {
101             LOG.debug( LogMessage.getMessage(
102                 "Generate list of agreements created by agreement factory ''{0}''.", getResourceId() ) );
103         }
104 
105         try
106         {
107             List<PersistentAgreement> loaded = new Vector<PersistentAgreement>();
108             PersistentAgreementContainer[] persistentAgreementContainers =
109                 PersistentAgreementContainer.listContainers( getResourceId() );
110 
111             // extract all agreements
112             for ( PersistentAgreementContainer persistentAgreementContainer : persistentAgreementContainers )
113             {
114                 PersistentAgreement persistentAgreement =
115                     new DatabasePersistentAgreement( persistentAgreementContainer,
116                         persistentAgreementContainer.getAgreementFactoryId() );
117                 persistentAgreement.load();
118                 loaded.add( persistentAgreement );
119             }
120 
121             if ( LOG.isDebugEnabled() )
122             {
123                 LOG.debug( MessageFormat.format( "Loaded  {0} agreements for agreement factory ''{1}''",
124                     loaded.size(), getResourceId() ) );
125             }
126 
127             return loaded.toArray( new PersistentAgreement[loaded.size()] );
128         }
129         catch ( Exception e )
130         {
131             String message =
132                 MessageFormat.format( "Failed to generate agreement list for factory {0}.", getResourceId() );
133             throw new PersistedResourceException( message, e );
134         }
135     }
136 
137     /**
138      * {@inheritDoc}
139      * 
140      * @see org.ogf.graap.wsag.server.persistence.IAgreementHome#remove(java.lang.String)
141      */
142     @Override
143     public void doRemove( PersistentAgreement toRemove ) throws PersistedResourceException
144     {
145         String agreementId = toRemove.getAgreement().getAgreementId();
146         LOG.debug( LogMessage.getMessage( "Remove agreement ''{0}'' from database.", agreementId ) );
147 
148         //
149         // TODO: instead of removing the container we should set the agreement to not active,
150         // i.e. it will not be loaded for the factory
151         //
152 
153         //
154         // if there is an agreement with the specified id we remove the agreement container
155         //
156         PersistentAgreementContainer container = null;
157         String removeError =
158             MessageFormat.format(
159                 "Agreement ''{0}'' could not be removed. Agreement does not exist in database.", agreementId );
160         try
161         {
162             container =
163                 PersistentAgreementContainer.loadContainer( toRemove.getAgreement().getAgreementId(),
164                     getResourceId() );
165         }
166         catch ( Exception e )
167         {
168             LOG.error( removeError, e );
169             throw new PersistedResourceException( removeError, e );
170         }
171 
172         if ( container == null )
173         {
174             throw new PersistedResourceException( removeError );
175         }
176 
177         try
178         {
179             container.deleteContainer();
180         }
181         catch ( Exception e )
182         {
183             LOG.error( LogMessage.getMessage( "Agreement ''{0}'' could not be removed.", agreementId ), e );
184         }
185 
186         if ( LOG.isDebugEnabled() )
187         {
188             LOG.debug( LogMessage.getMessage( "Agreement ''{0}'' removed.", agreementId ) );
189         }
190     }
191 
192 }