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.api.types;
36  
37  import org.ogf.graap.wsag.api.AgreementOffer;
38  import org.ogf.schemas.graap.wsAgreement.AgreementContextType;
39  import org.ogf.schemas.graap.wsAgreement.AgreementOfferDocument;
40  import org.ogf.schemas.graap.wsAgreement.AgreementTemplateType;
41  import org.ogf.schemas.graap.wsAgreement.AgreementType;
42  import org.ogf.schemas.graap.wsAgreement.CreateAgreementInputDocument;
43  import org.ogf.schemas.graap.wsAgreement.CreateAgreementInputType;
44  import org.ogf.schemas.graap.wsAgreement.NoncriticalExtensionType;
45  import org.ogf.schemas.graap.wsAgreement.TermTreeType;
46  import org.ogf.schemas.graap.wsAgreement.negotiation.NegotiationOfferType;
47  import org.w3.x2005.x08.addressing.EndpointReferenceType;
48  
49  /**
50   * AgreementOfferType
51   * 
52   * @author Oliver Waeldrich
53   */
54  public class AgreementOfferType extends WSAGXmlType
55      implements AgreementOffer
56  {
57  
58      private final CreateAgreementInputDocument agreementOfferDocument =
59          CreateAgreementInputDocument.Factory.newInstance();
60  
61      private AgreementType offer;
62  
63      /**
64       * Initializes an agreement offer from a template.
65       * 
66       * @param template
67       *            the template that is used to create the offer
68       */
69      public AgreementOfferType( AgreementTemplateType template )
70      {
71          //
72          // process the agreement template
73          //
74          AgreementType processed = processTemplate( template );
75  
76          //
77          // initialize the quote
78          //
79          initialize( processed );
80      }
81  
82      /**
83       * Constructs a quote for the given negotiation offer.
84       * 
85       * @param negotiationOffer
86       *            the negotiation offer
87       */
88      public AgreementOfferType( org.ogf.graap.wsag.api.types.NegotiationOfferTypeImpl negotiationOffer )
89      {
90          this( negotiationOffer.getXMLObject() );
91      }
92  
93      /**
94       * Initializes an agreement offer from a negotiation offer.
95       * 
96       * @param negotiationOffer
97       *            the negotiation offer that is used to create the agreement offer
98       */
99      public AgreementOfferType( NegotiationOfferType negotiationOffer )
100     {
101         AgreementOfferDocument offerDoc = AgreementOfferDocument.Factory.newInstance();
102         offerDoc.addNewAgreementOffer();
103 
104         if ( negotiationOffer.isSetAgreementId() )
105         {
106             offerDoc.getAgreementOffer().setAgreementId( negotiationOffer.getAgreementId() );
107         }
108 
109         if ( negotiationOffer.isSetName() )
110         {
111             offerDoc.getAgreementOffer().setName( negotiationOffer.getName() );
112         }
113 
114         if ( negotiationOffer.getContext() != null )
115         {
116             offerDoc.getAgreementOffer().addNewContext().set( negotiationOffer.getContext().copy() );
117         }
118 
119         if ( negotiationOffer.getTerms() != null )
120         {
121             offerDoc.getAgreementOffer().addNewTerms().set( negotiationOffer.getTerms().copy() );
122         }
123 
124         initialize( offerDoc.getAgreementOffer() );
125     }
126 
127     /**
128      * Initializes an agreement offer from a agreement type.
129      * 
130      * @param offer
131      *            the offer that is used to create this object
132      */
133     public AgreementOfferType( AgreementType offer )
134     {
135         initialize( (AgreementType) offer.copy() );
136     }
137 
138     private void initialize( AgreementType offerTemplate )
139     {
140         if ( offerTemplate == null )
141         {
142             throw new IllegalStateException( "Parameter Template must not be null." );
143         }
144 
145         if ( offerTemplate.getContext() == null )
146         {
147             throw new IllegalStateException( "Parameter Template#Context must not be null." );
148         }
149 
150         if ( offerTemplate.getTerms() == null )
151         {
152             throw new IllegalStateException( "Parameter Template#Terms must not be null." );
153         }
154 
155         //
156         // initialize the offer object
157         //
158         CreateAgreementInputType input = agreementOfferDocument.addNewCreateAgreementInput();
159         offer = (AgreementType) input.addNewAgreementOffer().set( offerTemplate );
160 
161         String agreementId =
162             ( offerTemplate.getAgreementId() != null ) ? offerTemplate.getAgreementId() : "1";
163         String agreementName =
164             ( offerTemplate.getName() != null ) ? offerTemplate.getName() : "AGREEMENT_OFFER";
165 
166         setAgreementId( agreementId );
167         setName( agreementName );
168     }
169 
170     /**
171      * {@inheritDoc}
172      */
173     public EndpointReferenceType getInitiatorEPR()
174     {
175         return getAgreementInput().getInitiatorAgreementEPR();
176     }
177 
178     /**
179      * {@inheritDoc}
180      */
181     public NoncriticalExtensionType[] getNoncriticalExtensions()
182     {
183         return getAgreementInput().getNoncriticalExtensionArray();
184     }
185 
186     /**
187      * {@inheritDoc}
188      */
189     public void setInitiatorEPR( EndpointReferenceType initiatorAgreementEPR )
190     {
191         getAgreementInput().setInitiatorAgreementEPR( initiatorAgreementEPR );
192     }
193 
194     /**
195      * {@inheritDoc}
196      */
197     public void setNoncriticalExtensions( NoncriticalExtensionType[] noncriticalExtensionArray )
198     {
199         getAgreementInput().setNoncriticalExtensionArray( noncriticalExtensionArray );
200     }
201 
202     /**
203      * {@inheritDoc}
204      */
205     public String getAgreementId()
206     {
207         return getAgreementOffer().getAgreementId();
208     }
209 
210     /**
211      * {@inheritDoc}
212      */
213     public AgreementContextType getContext()
214     {
215         return getAgreementOffer().getContext();
216     }
217 
218     /**
219      * {@inheritDoc}
220      */
221     public String getName()
222     {
223         return getAgreementOffer().getName();
224     }
225 
226     /**
227      * {@inheritDoc}
228      */
229     public TermTreeType getTerms()
230     {
231         return getAgreementOffer().getTerms();
232     }
233 
234     /**
235      * {@inheritDoc}
236      */
237     public void setAgreementId( String agreementId )
238     {
239         getAgreementOffer().setAgreementId( agreementId );
240     }
241 
242     /**
243      * {@inheritDoc}
244      */
245     public void setContext( AgreementContextType context )
246     {
247         getAgreementOffer().setContext( context );
248     }
249 
250     /**
251      * {@inheritDoc}
252      */
253     public void setName( String name )
254     {
255         getAgreementOffer().setName( name );
256     }
257 
258     /**
259      * {@inheritDoc}
260      */
261     public void setTerms( TermTreeType terms )
262     {
263         getAgreementOffer().setTerms( terms );
264     }
265 
266     /**
267      * {@inheritDoc}
268      */
269     @Override
270     public boolean validate()
271     {
272         return validate( agreementOfferDocument );
273     }
274 
275     private CreateAgreementInputType getAgreementInput()
276     {
277         return agreementOfferDocument.getCreateAgreementInput();
278     }
279 
280     private AgreementType getAgreementOffer()
281     {
282         return offer;
283     }
284 
285     /**
286      * {@inheritDoc}
287      */
288     public AgreementType getXMLObject()
289     {
290         return offer;
291     }
292 
293 }