1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35 package org.ogf.graap.wsag.wsrf.persistence;
36
37 import java.text.MessageFormat;
38 import java.util.Iterator;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Vector;
42
43 import javax.persistence.EntityManager;
44 import javax.persistence.NoResultException;
45 import javax.persistence.NonUniqueResultException;
46 import javax.persistence.Query;
47 import javax.persistence.RollbackException;
48
49 import org.apache.log4j.Logger;
50 import org.apache.xmlbeans.XmlObject;
51 import org.ogf.graap.wsag.api.Agreement;
52 import org.ogf.graap.wsag.api.AgreementFactory;
53 import org.ogf.graap.wsag.api.AgreementOffer;
54 import org.ogf.graap.wsag.api.Negotiation;
55 import org.ogf.graap.wsag.api.exceptions.AgreementFactoryException;
56 import org.ogf.graap.wsag.api.exceptions.NegotiationFactoryException;
57 import org.ogf.graap.wsag.server.persistence.EmfRegistry;
58 import org.ogf.graap.wsag.server.persistence.PersistedResourceException;
59 import org.ogf.graap.wsag.server.persistence.PersistentAgreement;
60 import org.ogf.graap.wsag.server.persistence.PersistentAgreementFactory;
61 import org.ogf.schemas.graap.wsAgreement.AgreementTemplateType;
62 import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationContextType;
63 import org.w3.x2005.x08.addressing.EndpointReferenceType;
64
65
66
67
68
69
70
71
72
73 public class WsDatabaseAgreementFactory
74 implements WsPersistentAgreementFactory
75 {
76
77 private static final Logger LOG = Logger.getLogger( WsDatabaseAgreementFactory.class );
78
79 private final String resourceId;
80
81 private final EndpointReferenceType factoryEpr;
82
83 private final PersistentAgreementFactory pFactory;
84
85 private final List<WsDatabasePersistentAgreement> wsAgreements;
86
87
88
89
90
91
92
93
94
95
96 public WsDatabaseAgreementFactory( PersistentAgreementFactory factory, EndpointReferenceType epr )
97 {
98 pFactory = factory;
99 this.factoryEpr = epr;
100 this.resourceId = pFactory.getResourceId();
101 this.wsAgreements = new Vector<WsDatabasePersistentAgreement>();
102 }
103
104
105
106
107
108
109 public String getResourceId()
110 {
111 return resourceId;
112 }
113
114
115
116
117
118
119
120
121 @Deprecated
122 public AgreementFactory getAgreementFactory()
123 {
124 return pFactory.getAgreementFactory();
125 }
126
127
128
129
130
131
132 @SuppressWarnings( "unchecked" )
133 public void load() throws Exception
134 {
135 pFactory.load();
136
137 LOG.debug( "WsSimplePersistentAgreementFactory -> load()" );
138
139 wsAgreements.clear();
140
141 EntityManager em = EmfRegistry.getEntityManager();
142 try
143 {
144 LOG.debug( "WsDatabaseAgreementHome -> list(String agreementFactoryId)" );
145
146
147 Query query = em.createNamedQuery( "AgreementEprContainer.findAllByAgreementFactoryId" );
148 query.setParameter( "agreementFactoryId", resourceId );
149
150 List<AgreementEprContainer> agreementEprContainers = null;
151 try
152 {
153 agreementEprContainers = query.getResultList();
154 }
155 catch ( NoResultException ex )
156 {
157 LOG.error( "Could not find any persisted agreement EPR container." );
158 }
159
160 if ( LOG.isDebugEnabled() )
161 {
162 LOG.debug( "Found " + agreementEprContainers.size()
163 + " AgreementEprContainer for agreementFactoryId '" + resourceId + "'." );
164 }
165
166
167
168 for ( AgreementEprContainer agreementEprContainer : agreementEprContainers )
169 {
170 WsDatabasePersistentAgreement wsDatabasePersistentAgreement =
171 this.findByAgreementId( agreementEprContainer.getAgreementId() );
172 wsAgreements.add( wsDatabasePersistentAgreement );
173 }
174
175 if ( LOG.isDebugEnabled() )
176 {
177 LOG.debug( "Loaded " + wsAgreements.size()
178 + " WsDatabasePersistentAgreement(s) for agreementFactoryId '" + resourceId + "'." );
179 }
180 }
181 catch ( Exception ex )
182 {
183 LOG.error( "Could not load WS database persistent agreements.", ex );
184 }
185 finally
186 {
187
188 em.close();
189 }
190
191 }
192
193
194
195
196
197
198 public void save() throws Exception
199 {
200 pFactory.save();
201 }
202
203
204
205
206
207
208
209
210
211 @Deprecated
212 public void addAgreement( Agreement agreement, EndpointReferenceType epr )
213 {
214 pFactory.addAgreement( agreement, epr );
215 }
216
217
218
219
220
221
222 public AgreementTemplateType[] getTemplates()
223 {
224 return pFactory.getTemplates();
225 }
226
227
228
229
230
231
232 public Agreement createAgreement( AgreementOffer offer ) throws AgreementFactoryException
233 {
234 return pFactory.createAgreement( offer );
235 }
236
237
238
239
240
241
242
243 public Negotiation
244 initiateNegotiation( NegotiationContextType context, XmlObject[] criticalExtensions,
245 XmlObject[] nonCriticalExtensions, Map<String, Object> environment )
246 throws NegotiationFactoryException
247 {
248
249 return pFactory.initiateNegotiation( context, criticalExtensions, nonCriticalExtensions, environment );
250 }
251
252
253
254
255
256
257
258
259
260
261
262
263
264 public WsDatabasePersistentAgreement find( String epr ) throws PersistedResourceException
265 {
266
267 LOG.debug( "WsDatabaseAgreementHome -> find(String epr)" );
268
269
270 EntityManager em = EmfRegistry.getEntityManager();
271 Query query = em.createNamedQuery( "AgreementEprContainer.findByEpr" );
272 query.setParameter( "epr", epr );
273
274 AgreementEprContainer agreementEprContainer = null;
275 try
276 {
277 agreementEprContainer = (AgreementEprContainer) query.getSingleResult();
278 }
279 catch ( NoResultException ex )
280 {
281 LOG.error( "Could not find an agreement for the EPR '" + epr + "'." );
282 throw new PersistedResourceException( "Could not find an agreement for the EPR '" + epr + "'." );
283 }
284 catch ( NonUniqueResultException ex )
285 {
286 LOG.error( "Found multiple agreements for the EPR '" + epr + "'." );
287 throw new PersistedResourceException( "Found multiple agreements for the EPR '" + epr + "'." );
288 }
289
290
291 EndpointReferenceType theEpr = agreementEprContainer.getEpr();
292
293
294 PersistentAgreement databasePersistentAgreement =
295 pFactory.find( agreementEprContainer.getAgreementId() );
296 WsDatabasePersistentAgreement wsDatabasePersistentAgreement =
297 new WsDatabasePersistentAgreement( databasePersistentAgreement, theEpr,
298 agreementEprContainer.getAgreementFactoryId() );
299
300
301 em.close();
302
303 return wsDatabasePersistentAgreement;
304 }
305
306
307
308
309
310
311
312
313
314
315
316
317
318 public WsDatabasePersistentAgreement findByEprAddress( String eprAddress ) throws Exception
319 {
320
321 LOG.debug( "WsDatabaseAgreementHome -> findByEprAddress(String eprAddress)" );
322
323
324 EntityManager em = EmfRegistry.getEntityManager();
325 Query query = em.createNamedQuery( "AgreementEprContainer.findByEprAddress" );
326 query.setParameter( "eprAddress", eprAddress );
327
328 AgreementEprContainer agreementEprContainer = null;
329 try
330 {
331 agreementEprContainer = (AgreementEprContainer) query.getSingleResult();
332 }
333 catch ( NoResultException ex )
334 {
335 LOG.error( "Could not find an agreement for the EPR address '" + eprAddress + "'." );
336 throw new Exception( "Could not find an agreement for the EPR address '\" + epr + \"'." );
337 }
338 catch ( NonUniqueResultException ex )
339 {
340 LOG.error( "Found multiple agreements for the EPR address '" + eprAddress + "'." );
341 throw new Exception( "Found multiple agreements for the EPR address '" + eprAddress + "'." );
342 }
343
344
345 EndpointReferenceType theEpr = agreementEprContainer.getEpr();
346
347
348 PersistentAgreement persistentAgreement = pFactory.find( agreementEprContainer.getAgreementId() );
349 WsDatabasePersistentAgreement wsDatabasePersistentAgreement =
350 new WsDatabasePersistentAgreement( persistentAgreement, theEpr,
351 agreementEprContainer.getAgreementFactoryId() );
352
353
354 em.close();
355
356 return wsDatabasePersistentAgreement;
357 }
358
359
360
361
362
363
364 public WsDatabasePersistentAgreement[] list()
365 {
366 return wsAgreements.toArray( new WsDatabasePersistentAgreement[wsAgreements.size()] );
367 }
368
369
370
371
372
373
374
375
376
377
378
379
380
381 public WsDatabasePersistentAgreement findByAgreementId( String agreementId ) throws Exception
382 {
383
384 EndpointReferenceType epr = AgreementEprContainer.findEPRForAgreement( agreementId );
385
386
387 PersistentAgreement persistentAgreement = pFactory.find( agreementId );
388 WsDatabasePersistentAgreement wsDatabasePersistentAgreement =
389 new WsDatabasePersistentAgreement( persistentAgreement, epr, getResourceId() );
390
391 return wsDatabasePersistentAgreement;
392 }
393
394
395
396
397
398
399 public PersistentAgreement[] list( String agreementFactoryId ) throws Exception
400 {
401 if ( resourceId.equals( agreementFactoryId ) )
402 {
403 return list();
404 }
405 else
406 {
407 String message = "Agreement Factory ''{0}'' can not list agreements of factory {1}.";
408 LOG.warn( MessageFormat.format( message, new Object[] { resourceId, agreementFactoryId } ) );
409 return new PersistentAgreement[0];
410 }
411 }
412
413
414
415
416
417
418
419
420
421
422 public void remove( String epr ) throws PersistedResourceException
423 {
424
425
426
427
428 WsDatabasePersistentAgreement toRemove = null;
429
430 Iterator<WsDatabasePersistentAgreement> it = wsAgreements.iterator();
431 while ( it.hasNext() )
432 {
433 WsDatabasePersistentAgreement current = it.next();
434 if ( current.getAgreementEPR().xmlText().equals( epr ) )
435 {
436 toRemove = current;
437 break;
438 }
439 }
440
441 if ( toRemove == null )
442 {
443 LOG.error( "Could not find an agreement for the EPR '" + epr + "'." );
444 throw new PersistedResourceException( "Could not find an agreement for the EPR '" + epr + "'." );
445 }
446
447
448 EntityManager em = EmfRegistry.getEntityManager();
449 try
450 {
451 Query query = em.createNamedQuery( "AgreementEprContainer.findByEpr" );
452 query.setParameter( "epr", epr );
453
454
455 em.getTransaction().begin();
456
457 AgreementEprContainer agreementEprContainer = null;
458 try
459 {
460 agreementEprContainer = (AgreementEprContainer) query.getSingleResult();
461 }
462 catch ( NoResultException ex )
463 {
464 LOG.error( "Could not find an agreement for the EPR '" + epr + "' in database." );
465 em.getTransaction().rollback();
466 throw new PersistedResourceException( "Could not find an agreement for the EPR '" + epr
467 + "' in database." );
468 }
469 catch ( NonUniqueResultException ex )
470 {
471 LOG.error( "Found multiple agreements for the EPR '" + epr + "'." );
472 em.getTransaction().rollback();
473 throw new PersistedResourceException( "Found multiple agreements for the EPR '" + epr + "'." );
474 }
475
476 try
477 {
478
479 em.remove( agreementEprContainer );
480 em.getTransaction().commit();
481 wsAgreements.remove( toRemove );
482 }
483 catch ( RollbackException ex )
484 {
485 LOG.error( "Could not remove agreement for EPR '" + epr + "'." );
486 em.getTransaction().rollback();
487 throw new PersistedResourceException( "Could not remove agreement for EPR '" + epr + "'." );
488 }
489 }
490 finally
491 {
492
493 em.close();
494 }
495 }
496
497
498
499
500
501
502 public EndpointReferenceType getAgreementFactoryEPR()
503 {
504 return factoryEpr;
505 }
506
507 }