View Javadoc

1   /* 
2    * Copyright (c) 2007, 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.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   * SampleGetTemplateAction
59   * 
60   * @author hrasheed
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       * {@inheritDoc}
83       */
84      public void initialize() throws ActionInitializationException
85      {
86  
87          try
88          {
89              // START SNIPPET: DynamicTemplateGeneration0
90              ImplementationConfigurationType handlerConfiguration =
91                  getHandlerContext().getHandlerConfiguration();
92              XmlObject[] children =
93                  handlerConfiguration.selectChildren( FileTemplateConfigurationDocument.type.getDocumentElementName() );
94              // END SNIPPET: DynamicTemplateGeneration0
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             // START SNIPPET: DynamicTemplateGeneration1
108             FileTemplateConfigurationType config = (FileTemplateConfigurationType) children[0];
109             setTemplateFilename( config.getFilename() );
110             fileTemplate = new FileTemplate( getTemplateFilename() );
111             // END SNIPPET: DynamicTemplateGeneration1
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             // START SNIPPET: DynamicTemplateGeneration2
129             double totalResources = 10;
130             fileTemplate.addParameter( "TOTALRESOURCES", totalResources );
131             // END SNIPPET: DynamicTemplateGeneration2
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      * {@inheritDoc}
166      */
167     public AgreementTemplateType getTemplate()
168     {
169         return template;
170     }
171 
172     /**
173      * 
174      * @return the template filename
175      */
176     public String getTemplateFilename()
177     {
178         return templateFilename;
179     }
180 
181     /**
182      * 
183      * @param templateFilename
184      *            the template filename
185      */
186     public void setTemplateFilename( String templateFilename )
187     {
188         this.templateFilename = templateFilename;
189     }
190 
191     /**
192      * @return returns a map with the template parameters used for template creation
193      */
194     protected Map<String, Object> getTemplateParameters()
195     {
196         return templateParameters;
197     }
198 }