001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 *
019 */
020 package org.apache.directory.shared.dsmlv2.request;
021
022
023 import java.util.ArrayList;
024 import java.util.List;
025
026 import org.apache.directory.shared.dsmlv2.DsmlDecorator;
027 import org.apache.directory.shared.dsmlv2.ParserUtils;
028 import org.apache.directory.shared.dsmlv2.request.BatchRequest.OnError;
029 import org.apache.directory.shared.dsmlv2.request.BatchRequest.Processing;
030 import org.apache.directory.shared.dsmlv2.request.BatchRequest.ResponseOrder;
031 import org.dom4j.Document;
032 import org.dom4j.DocumentHelper;
033 import org.dom4j.Element;
034
035
036 /**
037 * This class represents the Batch Request. It can be used to generate an the XML String of a BatchRequest.
038 *
039 * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
040 * @version $Rev$, $Date$
041 */
042 public class BatchRequestDsml
043 {
044 /** The Requests list */
045 private List<DsmlDecorator> requests;
046
047 /** The ID of the request */
048 private int requestID;
049
050 /** The type of processing of the Batch Request */
051 private Processing processing;
052
053 /** The type of on error handling */
054 private OnError onError;
055
056 /** The response order */
057 private ResponseOrder responseOrder;
058
059
060 /**
061 * Creates a new instance of BatchResponseDsml.
062 */
063 public BatchRequestDsml()
064 {
065 requests = new ArrayList<DsmlDecorator>();
066 responseOrder = ResponseOrder.SEQUENTIAL;
067 processing = Processing.SEQUENTIAL;
068 onError = OnError.EXIT;
069 }
070
071
072 /**
073 * Adds a request to the Batch Request DSML.
074 *
075 * @param request
076 * the request to add
077 * @return
078 * true (as per the general contract of the Collection.add method).
079 */
080 public boolean addRequest( DsmlDecorator request )
081 {
082 return requests.add( request );
083 }
084
085
086 /**
087 * Removes a request from the Batch Request DSML.
088 *
089 * @param request
090 * the request to remove
091 * @return
092 * true if this list contained the specified element.
093 */
094 public boolean removeRequest( DsmlDecorator request )
095 {
096 return requests.remove( request );
097 }
098
099
100 /**
101 * Gets the ID of the request
102 *
103 * @return
104 * the ID of the request
105 */
106 public int getRequestID()
107 {
108 return requestID;
109 }
110
111
112 /**
113 * Sets the ID of the request
114 *
115 * @param requestID
116 * the ID to set
117 */
118 public void setRequestID( int requestID )
119 {
120 this.requestID = requestID;
121 }
122
123
124 /**
125 * Gets the processing type of the request
126 *
127 * @return
128 * the processing type of the request
129 */
130 public Processing getProcessing()
131 {
132 return processing;
133 }
134
135
136 /**
137 * Sets the processing type of the request
138 *
139 * @param processing
140 * the processing type to set
141 */
142 public void setProcessing( Processing processing )
143 {
144 this.processing = processing;
145 }
146
147
148 /**
149 * Gets the on error handling type of the request
150 *
151 * @return
152 * the on error handling type of the request
153 */
154 public OnError getOnError()
155 {
156 return onError;
157 }
158
159
160 /**
161 * Sets the on error handling type of the request
162 *
163 * @param onError
164 * the on error handling type to set
165 */
166 public void setOnError( OnError onError )
167 {
168 this.onError = onError;
169 }
170
171
172 /**
173 * Gets the reponse order type of the request
174 *
175 * @return
176 * the reponse order type of the request
177 */
178 public ResponseOrder getResponseOrder()
179 {
180 return responseOrder;
181 }
182
183
184 /**
185 * Sets the reponse order type of the request
186 *
187 * @param responseOrder
188 * the reponse order type to set
189 */
190 public void setResponseOrder( ResponseOrder responseOrder )
191 {
192 this.responseOrder = responseOrder;
193 }
194
195
196 /**
197 * Converts the Batch Request to its XML representation in the DSMLv2 format.
198 */
199 public String toDsml()
200 {
201 Document document = DocumentHelper.createDocument();
202 Element element = document.addElement( "batchRequest" );
203
204 // RequestID
205 if ( requestID != 0 )
206 {
207 element.addAttribute( "requestID", "" + requestID );
208 }
209
210 // ResponseOrder
211 if ( responseOrder == ResponseOrder.UNORDERED )
212 {
213 element.addAttribute( "responseOrder", "unordered" );
214 }
215
216 // Processing
217 if ( processing == Processing.PARALLEL )
218 {
219 element.addAttribute( "processing", "parallel" );
220 }
221
222 // On Error
223 if ( onError == OnError.RESUME )
224 {
225 element.addAttribute( "onError", "resume" );
226 }
227
228 // Requests
229 for ( DsmlDecorator request : requests )
230 {
231 request.toDsml( element );
232 }
233
234 return ParserUtils.styleDocument( document ).asXML();
235 }
236 }