Revision b722cab3 src/com/rackspacecloud/android/AddLoadBalancerActivity.java

b/src/com/rackspacecloud/android/AddLoadBalancerActivity.java
1 1
package com.rackspacecloud.android;
2 2

  
3
import java.io.IOException;
4
import java.io.StringReader;
5 3
import java.util.ArrayList;
6 4

  
7
import javax.xml.parsers.FactoryConfigurationError;
8
import javax.xml.parsers.ParserConfigurationException;
9
import javax.xml.parsers.SAXParser;
10
import javax.xml.parsers.SAXParserFactory;
11

  
12 5
import org.apache.http.HttpResponse;
13
import org.apache.http.client.ClientProtocolException;
14
import org.apache.http.impl.client.BasicResponseHandler;
15
import org.xml.sax.InputSource;
16
import org.xml.sax.SAXException;
17
import org.xml.sax.XMLReader;
18 6

  
19 7
import com.rackspace.cloud.loadbalancer.api.client.Algorithm;
20 8
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancer;
21 9
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancerManager;
22 10
import com.rackspace.cloud.loadbalancer.api.client.Node;
23 11
import com.rackspace.cloud.loadbalancer.api.client.Protocol;
24
import com.rackspace.cloud.servers.api.client.Account;
25 12
import com.rackspace.cloud.servers.api.client.CloudServersException;
26 13
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
27
import com.rackspace.cloud.servers.api.client.parsers.CloudServersFaultXMLParser;
28 14

  
29 15
import android.app.Activity;
30
import android.app.AlertDialog;
31
import android.app.ProgressDialog;
32
import android.content.Context;
33
import android.content.DialogInterface;
34 16
import android.content.Intent;
35 17
import android.os.AsyncTask;
36 18
import android.os.Bundle;
37
import android.util.Log;
38 19
import android.view.View;
39
import android.view.WindowManager;
40 20
import android.view.View.OnClickListener;
41
import android.view.ViewGroup.LayoutParams;
42 21
import android.widget.AdapterView;
43 22
import android.widget.AdapterView.OnItemSelectedListener;
44 23
import android.widget.ArrayAdapter;
45 24
import android.widget.Button;
46 25
import android.widget.EditText;
47
import android.widget.ProgressBar;
48 26
import android.widget.Spinner;
49 27

  
50
public class AddLoadBalancerActivity extends Activity implements OnItemSelectedListener {
28
public class AddLoadBalancerActivity extends CloudActivity implements OnItemSelectedListener {
51 29

  
52 30
	//TODO Shared Virtual IP (not in API though?)
53 31
	private static final String[] VIP_TYPES = {"Public", "ServiceNet"};
......
69 47
	private String selectedName;
70 48
	private String selectedPort;
71 49
	private ArrayList<Node> nodes;
72
	private Context context;
73 50
	private LoadBalancer loadBalancer;
74
	ProgressDialog pDialog;
75 51

  
76 52
	@Override
77 53
	public void onCreate(Bundle savedInstanceState) {
......
81 57
	}
82 58

  
83 59
	@SuppressWarnings("unchecked")
84
	private void restoreState(Bundle state) {
85
		
86
		context = getApplicationContext();
60
	protected void restoreState(Bundle state) {
61
		super.restoreState(state);
87 62
		
88 63
		portText = (EditText)findViewById(R.id.edit_port_text);
89 64

  
......
110 85
	public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
111 86
		if (parent == protocolSpinner) {
112 87
			selectedProtocol = protocols[position];
113
			portText.setText(protocols[position].getDefaultPort());
88
			//portText.setText(protocols[position].getDefaultPort());
114 89
		} 
115 90
		else if (parent == algorithmSpinner){
116 91
			selectedAlgorithm = algorithms[position];
......
133 108

  
134 109
			@Override
135 110
			public void onClick(View v) {
136
				/*
137
				selectedName = ((EditText)findViewById(R.id.edit_lb_name_text)).getText().toString();
138
				selectedPort = ((EditText)findViewById(R.id.edit_port_text)).getText().toString();
139
				if(!validName()){
140
					showAlert("Error", "Load balancer name cannot be blank.");
141
				}
142
				else if(!validPort()){
143
					showAlert("Error", "Must have a protocol port number that is between 1 and 65535.");
144
				}
145
				 */
146
				//else{
147 111
				Intent viewIntent = new Intent(getApplicationContext(), AddNodesActivity.class);
148
				/*viewIntent.putExtra("name", selectedName);
149
					viewIntent.putExtra("protocol", selectedProtocol);
150
					viewIntent.putExtra("port", selectedPort);
151
					viewIntent.putExtra("vip", selectedVipType);
152
					viewIntent.putExtra("algorithm", selectedAlgorithm);
153
					viewIntent.putExtra("region", selectedRegion);
154
				 */
155 112
				viewIntent.putExtra("nodes", nodes);
156 113
				startActivityForResult(viewIntent, ADD_NODES_ACTIVITY_CODE);
157
				//}
158 114
			}
159 115
		});
160 116

  
......
172 128
					showAlert("Error", "Must have a protocol port number that is between 1 and 65535.");
173 129
				}
174 130
				else{
175
					/*
176
					Intent viewIntent = new Intent(getApplicationContext(), AddNodesActivity.class);
177
					viewIntent.putExtra("name", selectedName);
178
					viewIntent.putExtra("protocol", selectedProtocol);
179
					viewIntent.putExtra("port", selectedPort);
180
					viewIntent.putExtra("vip", selectedVipType);
181
					viewIntent.putExtra("algorithm", selectedAlgorithm);
182
					viewIntent.putExtra("region", selectedRegion);
183
					viewIntent.putExtra("nodes", nodes);
184
					 */
185 131
					loadBalancer = new LoadBalancer();
186 132
					loadBalancer.setName(selectedName);
187 133
					loadBalancer.setProtocol(selectedProtocol.getName());
......
189 135
					loadBalancer.setVirtualIpType(selectedVipType);
190 136
					loadBalancer.setAlgorithm(selectedAlgorithm.getName());
191 137
					loadBalancer.setNodes(nodes);
192
					
193 138
					new AddLoadBalancerTask().execute();
194 139
				}
195 140
			}
......
254 199
		algorithmSpinner.setAdapter(algorithmAdapter);
255 200
	}
256 201

  
257
	private void showAlert(String title, String message) {
258
		AlertDialog alert = new AlertDialog.Builder(this).create();
259
		alert.setTitle(title);
260
		alert.setMessage(message);
261
		alert.setButton("OK", new DialogInterface.OnClickListener() {
262
			public void onClick(DialogInterface dialog, int which) {
263
				return;
264
			} }); 
265
		alert.show();
266
	}
267
	
268
	private void startLoadBalancerError(String message, HttpBundle bundle){
269
		Intent viewIntent = new Intent(getApplicationContext(), ServerErrorActivity.class);
270
		viewIntent.putExtra("errorMessage", message);
271
		viewIntent.putExtra("response", bundle.getResponseText());
272
		viewIntent.putExtra("request", bundle.getCurlRequest());
273
		startActivity(viewIntent);
274
	}
275
	
276
	//using cloudServersException, it works for us too
277
	private CloudServersException parseCloudServersException(HttpResponse response) {
278
		CloudServersException cse = new CloudServersException();
279
		try {
280
		    BasicResponseHandler responseHandler = new BasicResponseHandler();
281
		    String body = responseHandler.handleResponse(response);
282
	    	CloudServersFaultXMLParser parser = new CloudServersFaultXMLParser();
283
	    	SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
284
	    	XMLReader xmlReader = saxParser.getXMLReader();
285
	    	xmlReader.setContentHandler(parser);
286
	    	xmlReader.parse(new InputSource(new StringReader(body)));		    	
287
	    	cse = parser.getException();		    	
288
		} catch (ClientProtocolException e) {
289
			cse = new CloudServersException();
290
			cse.setMessage(e.getLocalizedMessage());
291
		} catch (IOException e) {
292
			cse = new CloudServersException();
293
			cse.setMessage(e.getLocalizedMessage());
294
		} catch (ParserConfigurationException e) {
295
			cse = new CloudServersException();
296
			cse.setMessage(e.getLocalizedMessage());
297
		} catch (SAXException e) {
298
			cse = new CloudServersException();
299
			cse.setMessage(e.getLocalizedMessage());
300
		} catch (FactoryConfigurationError e) {
301
			cse = new CloudServersException();
302
			cse.setMessage(e.getLocalizedMessage());
303
		}
304
		return cse;
305
	}
306

  
307 202
	@SuppressWarnings("unchecked")
308 203
	@Override
309 204
	protected void onActivityResult (int requestCode, int resultCode, Intent data){
310 205
		if(requestCode == ADD_NODES_ACTIVITY_CODE && resultCode == RESULT_OK){
311 206
			//set node list
312
			Log.d("info", "Result ok");
313 207
			nodes = ((ArrayList<Node>)data.getSerializableExtra("nodes"));
314
			Log.d("info", "the length is " + nodes.size());
315 208
		}
316 209
		else if(requestCode == ADD_NODES_ACTIVITY_CODE && resultCode == RESULT_CANCELED){
317 210
			//don't change list
318
			Log.d("info", "Result cancel");
211
			
319 212
		}
320 213
	}
321 214

  
......
330 223
		protected HttpBundle doInBackground(Void... arg0) {
331 224
			HttpBundle bundle = null;
332 225
			try {
333
				bundle = (new LoadBalancerManager(context)).create(loadBalancer, LoadBalancer.getRegionUrl(selectedRegion));
226
				bundle = (new LoadBalancerManager(getContext())).create(loadBalancer, LoadBalancer.getRegionUrl(selectedRegion));
334 227
			} catch (CloudServersException e) {
335 228
				exception = e;
336 229
			}
......
339 232

  
340 233
		@Override
341 234
		protected void onPostExecute(HttpBundle bundle) {
342
			pDialog.dismiss();
235
			hideDialog();
343 236
			HttpResponse response = bundle.getResponse();
344 237
			if (response != null) {
345 238
				int statusCode = response.getStatusLine().getStatusCode();
......
349 242
				} else {
350 243
					CloudServersException cse = parseCloudServersException(response);
351 244
					if ("".equals(cse.getMessage())) {
352
						startLoadBalancerError("There was a problem creating your load balancer.", bundle);
245
						showError("There was a problem creating your load balancer.", bundle);
353 246
					} else {
354 247
						//if container with same name already exists
355
						startLoadBalancerError("There was a problem creating your load balancer: " + cse.getMessage() + "\n See details for more information", bundle);
248
						showError("There was a problem creating your load balancer: " + cse.getMessage() + "\n See details for more information", bundle);
356 249
					}
357 250
				}
358 251
			} else if (exception != null) {
359
				startLoadBalancerError("There was a problem creating your container: " + exception.getMessage()+"\n See details for more information.", bundle);				
252
				showError("There was a problem creating your container: " + exception.getMessage()+"\n See details for more information.", bundle);				
360 253
			}
361 254
			finish();
362 255
		}
363 256
	}
364
	
365
	protected void showDialog() {
366
		pDialog = new ProgressDialog(this, R.style.NewDialog);
367
		// // Set blur to background
368
		WindowManager.LayoutParams lp = pDialog.getWindow().getAttributes();
369
		lp.dimAmount = 0.0f;
370
		pDialog.getWindow().setAttributes(lp);
371
		pDialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
372
		pDialog.show();
373
		pDialog.setContentView(new ProgressBar(this), new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
374
	}
375

  
376 257
}

Also available in: Unified diff