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.wsrf.persistence;
36  
37  import javax.persistence.EntityManager;
38  import javax.persistence.RollbackException;
39  
40  import org.apache.log4j.Logger;
41  import org.ogf.graap.wsag.api.Agreement;
42  import org.ogf.graap.wsag.server.persistence.EmfRegistry;
43  import org.ogf.graap.wsag.server.persistence.PersistentAgreement;
44  import org.w3.x2005.x08.addressing.EndpointReferenceType;
45  
46  /**
47   * Web service based extension of the
48   * {@link org.ogf.graap.wsag.server.persistence.impl.DatabasePersistentAgreement}. It is used to bind an EPR
49   * to the agreement, which is stored inside the {@link AgreementEprContainer} instance.
50   * 
51   * @author T.Weuffel
52   */
53  public class WsDatabasePersistentAgreement implements PersistentAgreement
54  {
55  
56      private final Logger log = Logger.getLogger( WsDatabasePersistentAgreement.class );
57  
58      private EndpointReferenceType epr;
59  
60      private PersistentAgreement agreement;
61  
62      private String factoryId;
63  
64      /**
65       * Creates a new persisted agreement.
66       * 
67       * @param agreement
68       *            the agreement to persist
69       * @param epr
70       *            the agreement EPR
71       * @param agreementFactoryId
72       *            the id of the agreement factory that created the agreement
73       * 
74       * @see javax.persistence.EntityManagerFactory
75       * @see org.ogf.graap.wsag.server.persistence.impl.PersistentAgreementContainer
76       */
77      public WsDatabasePersistentAgreement( PersistentAgreement agreement, EndpointReferenceType epr,
78                                            String agreementFactoryId )
79      {
80          // set EPR
81          this.epr = epr;
82          this.agreement = agreement;
83          this.factoryId = agreementFactoryId;
84      }
85  
86      /**
87       * {@inheritDoc}
88       */
89      public void save() throws Exception
90      {
91          log.warn( "WsDatabasePersistentAgreement -> save()" );
92  
93          // try to save the agreements EPR
94          EntityManager em = EmfRegistry.getEntityManager();
95  
96          // open a transaction
97          em.getTransaction().begin();
98  
99          AgreementEprContainer agreementEprContainer = null;
100         try
101         {
102             // create a new AgreementEprContainer
103             agreementEprContainer = new AgreementEprContainer();
104             agreementEprContainer.setAgreementId( agreement.getAgreement().getAgreementId() );
105             agreementEprContainer.setAgreementFactoryId( factoryId );
106             agreementEprContainer.setEpr( epr );
107 
108             // persist the EPR
109             em.persist( agreementEprContainer );
110 
111             // commit
112             em.getTransaction().commit();
113         }
114         catch ( RollbackException ex )
115         {
116             log.error( "Could not persist the agreements' EPR. Rollback the commit." );
117 
118             em.getTransaction().rollback();
119 
120             throw new Exception( "Could not persist the agreements' EPR." );
121         }
122 
123         // try to save the agreement itself
124         try
125         {
126             agreement.save();
127         }
128         catch ( Exception ex )
129         {
130             // persisting the agreement itself was not successful, so remove the EPR
131             em.getTransaction().begin();
132             em.remove( agreementEprContainer );
133             em.getTransaction().commit();
134         }
135 
136         // close the entity manager
137         em.close();
138     }
139 
140     /**
141      * @return the endpoint reference of the web service agreement resource
142      */
143     public EndpointReferenceType getAgreementEPR()
144     {
145         return epr;
146     }
147 
148     /**
149      * 
150      * @param agreementEpr
151      *            the agreement endpoint reference
152      */
153     public void setAgreementEPR( EndpointReferenceType agreementEpr )
154     {
155         this.epr = agreementEpr;
156     }
157 
158     @Override
159     public String toString()
160     {
161         return "WsDatabasePersistentAgreement [agreementId: '" + agreement.getAgreement().getAgreementId()
162             + "', name: '" + agreement.getAgreement().getName() + "', agreementFactoryId: '" + factoryId
163             + "']";
164     }
165 
166     /**
167      * @return the concrete agreement instance that is associated with the web service resource
168      * 
169      * @see org.ogf.graap.wsag.server.persistence.PersistentAgreement#getAgreement()
170      */
171     public Agreement getAgreement()
172     {
173         return agreement.getAgreement();
174     }
175 
176     /**
177      * {@inheritDoc}
178      * 
179      * @see org.ogf.graap.wsag.server.persistence.PersistentAgreement#load()
180      */
181     public void load() throws Exception
182     {
183         agreement.load();
184     }
185 }