1 /*
2 * Copyright (c) 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.samples;
36
37 import org.apache.log4j.Logger;
38 import org.ogf.graap.wsag.api.AgreementOffer;
39 import org.ogf.graap.wsag.api.types.AbstractAgreementType;
40 import org.ogf.schemas.graap.wsAgreement.AgreementPropertiesType;
41 import org.ogf.schemas.graap.wsAgreement.AgreementStateDefinition;
42 import org.ogf.schemas.graap.wsAgreement.AgreementStateDocument;
43 import org.ogf.schemas.graap.wsAgreement.AgreementStateType;
44 import org.ogf.schemas.graap.wsAgreement.GuaranteeTermStateDefinition;
45 import org.ogf.schemas.graap.wsAgreement.GuaranteeTermStateType;
46 import org.ogf.schemas.graap.wsAgreement.ServiceTermStateDefinition;
47 import org.ogf.schemas.graap.wsAgreement.ServiceTermStateType;
48 import org.ogf.schemas.graap.wsAgreement.TerminateInputType;
49 import org.w3c.dom.Node;
50
51 /**
52 * Sample agreement implementation.
53 *
54 * @author Oliver Waeldrich
55 *
56 */
57 public class SampleAgreement extends AbstractAgreementType
58 {
59
60 /**
61 * constructs new agreement based on offer.
62 *
63 * @param offer
64 * offer object
65 */
66 public SampleAgreement( AgreementOffer offer )
67 {
68 super( offer );
69 setName( "SampleAgreement" );
70 }
71
72 private static Logger log = Logger.getLogger( SampleAgreement.class );
73
74 /**
75 * Creates an agreement with the given agreement properties document.
76 *
77 * @param properties
78 * the agreement properties document
79 */
80 public SampleAgreement( AgreementPropertiesType properties )
81 {
82 super();
83 this.agreementProperties = properties;
84 }
85
86 /**
87 * {@inheritDoc}
88 *
89 * @see org.ogf.graap.wsag.api.Agreement#terminate(org.ogf.schemas.graap.wsAgreement.TerminateInputType)
90 */
91 public void terminate( TerminateInputType reason )
92 {
93 Node terminateReson = reason.getDomNode().getFirstChild();
94 if ( terminateReson != null )
95 {
96 // find out what the reason is and take the appropriate action
97 if ( log.isInfoEnabled() )
98 {
99 log.info( "The agreement was terminated for the following reason: " + reason.xmlText() );
100 }
101 }
102 }
103
104 /**
105 * {@inheritDoc}
106 */
107 @Override
108 public ServiceTermStateType[] getServiceTermStates()
109 {
110 // in order to add specific behavior when a property is read,
111 // simply overwrite the method in question
112
113 // first create a new ServiceTermStateDocument result array
114 ServiceTermStateType[] state = new ServiceTermStateType[1];
115
116 // now you can add some specific functionality e.g. querying a sub system
117 // state[0] = mysystem.getState();
118
119 // here we simply initialize the our result array
120 state[0] = ServiceTermStateType.Factory.newInstance();
121
122 // and do some update action on the state object
123 for ( int i = 0; i < state.length; i++ )
124 {
125 state[i].setState( ServiceTermStateDefinition.READY );
126 }
127
128 // now we can return our state object;
129 return state;
130 }
131
132 /**
133 * {@inheritDoc}
134 */
135 @Override
136 public AgreementStateType getState()
137 {
138 // it is also possible to create a new state object, but in order to have
139 // automatic validity checks enabled, the local state holder should be updated
140 AgreementStateDocument stateDoc = AgreementStateDocument.Factory.newInstance();
141 AgreementStateType state = stateDoc.addNewAgreementState();
142
143 state.setState( AgreementStateDefinition.OBSERVED );
144
145 // now we can return our state object;
146 return stateDoc.getAgreementState();
147 }
148
149 /**
150 * {@inheritDoc}
151 */
152 // START SNIPPET: GetServiceTermStates
153 @Override
154 public GuaranteeTermStateType[] getGuaranteeTermStates()
155 {
156 GuaranteeTermStateType[] state = new GuaranteeTermStateType[2];
157
158 state[0] = GuaranteeTermStateType.Factory.newInstance();
159 state[1] = GuaranteeTermStateType.Factory.newInstance();
160
161 state[0].setState( GuaranteeTermStateDefinition.NOT_DETERMINED );
162 state[0].setTermName( "term_0" );
163
164 state[1].setState( GuaranteeTermStateDefinition.FULFILLED );
165 state[1].setTermName( "term_1" );
166
167 return state;
168 }
169 // END SNIPPET: GetServiceTermStates
170
171 }