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.samples.actions;
36
37 import java.io.ByteArrayInputStream;
38 import java.io.ByteArrayOutputStream;
39 import java.text.MessageFormat;
40 import java.util.Calendar;
41 import java.util.HashMap;
42 import java.util.Iterator;
43 import java.util.Map;
44
45 import org.apache.log4j.Logger;
46 import org.apache.xmlbeans.GDate;
47 import org.apache.xmlbeans.XmlObject;
48 import org.apache.xmlbeans.XmlOptions;
49 import org.ogf.graap.wsag.server.actions.AbstractGetTemplateAction;
50 import org.ogf.graap.wsag.server.actions.ActionInitializationException;
51 import org.ogf.graap.wsag.server.actions.impl.FileTemplate;
52 import org.ogf.graap.wsag4j.types.configuration.FileTemplateConfigurationDocument;
53 import org.ogf.graap.wsag4j.types.configuration.FileTemplateConfigurationType;
54 import org.ogf.graap.wsag4j.types.configuration.ImplementationConfigurationType;
55 import org.ogf.schemas.graap.wsAgreement.AgreementTemplateType;
56
57
58
59
60
61
62
63 public class SampleGetTemplateAction extends AbstractGetTemplateAction
64 {
65
66
67
68
69 private static final int ENDTIME_OFFSET = 60;
70
71 private static final Logger LOG = Logger.getLogger( SampleGetTemplateAction.class );
72
73 private FileTemplate fileTemplate = null;
74
75 private String templateFilename = "";
76
77 private Map<String, Object> templateParameters = new HashMap<String, Object>();
78
79 private AgreementTemplateType template;
80
81
82
83
84 public void initialize() throws ActionInitializationException
85 {
86
87 try
88 {
89
90 ImplementationConfigurationType handlerConfiguration =
91 getHandlerContext().getHandlerConfiguration();
92 XmlObject[] children =
93 handlerConfiguration.selectChildren( FileTemplateConfigurationDocument.type.getDocumentElementName() );
94
95
96 if ( children.length != 1 )
97 {
98 String error =
99 "Invalid configuration for action $1. Expected one configuration section of type $1, but found $3";
100 String message =
101 error + getClass().getName()
102 + FileTemplateConfigurationDocument.type.getDocumentElementName().toString()
103 + Integer.toString( children.length );
104 LOG.error( message );
105 throw new ActionInitializationException( message );
106 }
107
108 FileTemplateConfigurationType config = (FileTemplateConfigurationType) children[0];
109 setTemplateFilename( config.getFilename() );
110 fileTemplate = new FileTemplate( getTemplateFilename() );
111
112
113 Map<String, Object> parameters = getTemplateParameters();
114 Iterator<String> keys = parameters.keySet().iterator();
115
116 while ( keys.hasNext() )
117 {
118 try
119 {
120 String key = keys.next();
121 fileTemplate.addParameter( key, parameters.get( key ) );
122 }
123 catch ( ClassCastException e )
124 {
125 LOG.error( "Invalid template parameter. Key value must be of type String." );
126 }
127 }
128
129 double totalResources = 10;
130 fileTemplate.addParameter( "TOTALRESOURCES", totalResources );
131
132
133 Calendar startTime = Calendar.getInstance();
134 startTime.set( Calendar.SECOND, 0 );
135 startTime.set( Calendar.MILLISECOND, 0 );
136 Calendar endTime = (Calendar) startTime.clone();
137 endTime.add( Calendar.MINUTE, ENDTIME_OFFSET );
138
139 fileTemplate.addParameter( "STARTTIME", new GDate( startTime ) );
140
141 fileTemplate.addParameter( "ENDTIME", new GDate( endTime ) );
142
143 fileTemplate.addParameter( "DURATION", "60" );
144
145 ByteArrayOutputStream out = new ByteArrayOutputStream();
146 fileTemplate.processTemplate( out );
147 ByteArrayInputStream in = new ByteArrayInputStream( out.toByteArray() );
148
149 template =
150 AgreementTemplateType.Factory.parse( in,
151 new XmlOptions().setLoadReplaceDocumentElement( null ) );
152
153 }
154 catch ( Exception e )
155 {
156 e.printStackTrace();
157 Object[] filler = new Object[] { e.getMessage() };
158 String message = MessageFormat.format( "Error creating sample-template. Reason: {0}", filler );
159 LOG.error( message );
160 throw new ActionInitializationException( message );
161 }
162 }
163
164
165
166
167 public AgreementTemplateType getTemplate()
168 {
169 return template;
170 }
171
172
173
174
175
176 public String getTemplateFilename()
177 {
178 return templateFilename;
179 }
180
181
182
183
184
185
186 public void setTemplateFilename( String templateFilename )
187 {
188 this.templateFilename = templateFilename;
189 }
190
191
192
193
194 protected Map<String, Object> getTemplateParameters()
195 {
196 return templateParameters;
197 }
198 }