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.Basic;
38  import javax.persistence.Column;
39  import javax.persistence.Entity;
40  import javax.persistence.EntityManager;
41  import javax.persistence.GeneratedValue;
42  import javax.persistence.GenerationType;
43  import javax.persistence.Id;
44  import javax.persistence.Lob;
45  import javax.persistence.NamedQueries;
46  import javax.persistence.NamedQuery;
47  import javax.persistence.NoResultException;
48  import javax.persistence.NonUniqueResultException;
49  import javax.persistence.Query;
50  import javax.persistence.Table;
51  import javax.persistence.Transient;
52  import javax.persistence.UniqueConstraint;
53  
54  import org.apache.log4j.Logger;
55  import org.ogf.graap.wsag.server.persistence.EmfRegistry;
56  import org.ogf.graap.wsag.server.persistence.PersistedResourceException;
57  import org.w3.x2005.x08.addressing.EndpointReferenceType;
58  
59  /**
60   * Entity class for the agreement persistence. It is used to build the relationship between an agreement and
61   * an EPR. It defines the entity fields (the tables' columns), the named-queries used to access the stored
62   * data, and the corresponding getter/setter methods.
63   * 
64   * @author T.Weuffel
65   */
66  @Entity
67  @Table( name = "AGREEMENT_EPR_CONTAINER", uniqueConstraints = @UniqueConstraint( columnNames = {
68      "agreement_id", "agreement_factory_id" } ) )
69  @NamedQueries( {
70      @NamedQuery( name = "AgreementEprContainer.findAll", query = "SELECT a FROM AgreementEprContainer a" ),
71      @NamedQuery(
72                      name = "AgreementEprContainer.findAllByAgreementFactoryId",
73                      query = "SELECT a FROM AgreementEprContainer a WHERE a.agreementFactoryId = :agreementFactoryId" ),
74      @NamedQuery( name = "AgreementEprContainer.findByEpr",
75                      query = "SELECT a FROM AgreementEprContainer a WHERE a.epr = :epr" ),
76      @NamedQuery( name = "AgreementEprContainer.findByEprAddress",
77                      query = "SELECT a FROM AgreementEprContainer a WHERE a.eprAddress = :eprAddress" ),
78      @NamedQuery( name = "AgreementEprContainer.findByAgreementId",
79                      query = "SELECT a FROM AgreementEprContainer a WHERE a.agreementId = :agreementId" ) } )
80  public class AgreementEprContainer
81  {
82  
83      @Transient
84      private static final int EPR_FIELD_SIZE = 163840;
85  
86      // the logger should not be persisted
87      @Transient
88      private static final Logger LOG = Logger.getLogger( AgreementEprContainer.class );
89  
90      // primary-key of the stored entity
91      @SuppressWarnings( "unused" )
92      @Id
93      @Column( name = "id" )
94      @GeneratedValue( strategy = GenerationType.TABLE )
95      @Basic( optional = false )
96      private Integer id;
97  
98      // ID of the agreement
99      @Column( name = "agreement_id", nullable = false )
100     @Basic( optional = false )
101     private String agreementId;
102 
103     // ID of the agreement factory, which created the agreement
104     @Column( name = "agreement_factory_id", nullable = false )
105     @Basic( optional = false )
106     private String agreementFactoryId;
107 
108     // XML document of the EPR
109     @Lob
110     @Column( name = "epr", nullable = false, length = EPR_FIELD_SIZE )
111     @Basic( optional = false )
112     private String epr;
113 
114     // address of the EPR
115     @Column( name = "epr_address", nullable = false )
116     @Basic( optional = false )
117     private String eprAddress;
118 
119     /**
120      * The default constructor is required by the JPA2 environment. It is used to instantiate an instance of
121      * this entity. All values are then passed to this instance by setter-injection.
122      */
123     public AgreementEprContainer()
124     {
125     }
126 
127     /**
128      * Used to create an instance of this entity ba hand.
129      * 
130      * @param agreementId
131      *            ID of the agreement.
132      * @param epr
133      *            EPR of the agreement.
134      */
135     public AgreementEprContainer( String agreementId, EndpointReferenceType epr )
136     {
137         this.agreementId = agreementId;
138         this.setEpr( epr );
139     }
140 
141     /**
142      * 
143      * @return the agreement id
144      */
145     public String getAgreementId()
146     {
147         return agreementId;
148     }
149 
150     /**
151      * 
152      * @param agreementId
153      *            the agreement id to set
154      */
155     public void setAgreementId( String agreementId )
156     {
157         this.agreementId = agreementId;
158     }
159 
160     /**
161      * Returns the EPR of the persisted resourse.
162      * 
163      * @return the agreement endpoint reference
164      * 
165      * @throws PersistedResourceException
166      *             indicates that the persisted EPR could not be de-serialized
167      */
168     public EndpointReferenceType getEpr() throws PersistedResourceException
169     {
170         try
171         {
172             EndpointReferenceType theEpr = EndpointReferenceType.Factory.parse( epr );
173             return theEpr;
174         }
175         catch ( Exception e )
176         {
177             throw new PersistedResourceException( e );
178         }
179     }
180 
181     /**
182      * 
183      * @param epr
184      *            the agreement endpoint reference to set
185      */
186     public void setEpr( EndpointReferenceType epr )
187     {
188         this.epr = epr.xmlText();
189 
190         assert epr != null;
191         assert epr.getAddress() != null;
192         this.eprAddress = epr.getAddress().getStringValue();
193     }
194 
195     /**
196      * 
197      * @return the agreement factory id
198      */
199     public String getAgreementFactoryId()
200     {
201         return agreementFactoryId;
202     }
203 
204     /**
205      * 
206      * @param agreementFactoryId
207      *            the agreement factory id to set
208      */
209     public void setAgreementFactoryId( String agreementFactoryId )
210     {
211         this.agreementFactoryId = agreementFactoryId;
212     }
213 
214     /**
215      * @return the address of the EPR
216      */
217     public String getEprAddress()
218     {
219         return eprAddress;
220     }
221 
222     /**
223      * 
224      * @param eprAddress
225      *            the address of the EPR to set
226      */
227     public void setEprAddress( String eprAddress )
228     {
229         this.eprAddress = eprAddress;
230     }
231 
232     // /////////////////////////////////////////////////////////////////////////
233     // /// Methods for EPR lookup and manipulation /////
234     // /////////////////////////////////////////////////////////////////////////
235 
236     /**
237      * Finds the EPR for the agreement with the given id.
238      * 
239      * @param agreementId
240      *            identifies the agreement for which the EPR is resolved
241      * 
242      * @return the agreement EPR
243      * 
244      * @throws Exception
245      *             an exception if the agreement was not found or an other error occurred
246      */
247     public static EndpointReferenceType findEPRForAgreement( String agreementId ) throws Exception
248     {
249         LOG.trace( "AgreementEprContainer -> findEPRForAgreement(String agreementId)" );
250 
251         // try to find the agreement EPR
252         EntityManager em = EmfRegistry.getEntityManager();
253         AgreementEprContainer agreementEprContainer = null;
254         try
255         {
256             Query query = em.createNamedQuery( "AgreementEprContainer.findByAgreementId" );
257             query.setParameter( "agreementId", agreementId );
258 
259             try
260             {
261                 agreementEprContainer = (AgreementEprContainer) query.getSingleResult();
262             }
263             catch ( NoResultException ex )
264             {
265                 LOG.error( "Could not find an agreement for the agreement id '" + agreementId + "'." );
266                 throw new Exception( "Could not find an agreement for the EPR address '" + agreementId + "'." );
267             }
268             catch ( NonUniqueResultException ex )
269             {
270                 LOG.error( "Found multiple agreements for the agreement id '" + agreementId + "'." );
271                 throw new Exception( "Found multiple agreements for the agreement id '" + agreementId + "'." );
272             }
273         }
274         finally
275         {
276             em.close();
277         }
278 
279         // build the agreements' EPR
280         return agreementEprContainer.getEpr();
281     }
282 }