Revision 6ae5d8db src/com/rackspacecloud/android/ViewLoadBalancerActivity.java

b/src/com/rackspacecloud/android/ViewLoadBalancerActivity.java
108 108
				startActivityForResult(editLoadBalancerIntent, EDIT_NODES_CODE);
109 109
			}
110 110
		});
111

  
112
		Button logs = (Button)findViewById(R.id.connection_log_button);
113
		setLogButtonText();
114
		logs.setOnClickListener(new OnClickListener() {
115

  
116
			@Override
117
			public void onClick(View v) {
118
				showDialog(R.id.connection_log_button);	
119
			}
120
		});
121
		
122
		Button persist = (Button)findViewById(R.id.session_persistence_button);
123
		setSessionButtonText();
124
		persist.setOnClickListener(new OnClickListener() {
125
			@Override
126
			public void onClick(View v) {
127
				if(!loadBalancer.getProtocol().equals("HTTP")){
128
					showAlert("Error", "Session Persistence cannot be enabled for protocols other than HTTP.");
129
				} else {
130
					showDialog(R.id.session_persistence_button);
131
				}
132
			}
133
		});
111 134
	}
112 135
	
136
	private void setLogButtonText(){
137
		Button logs = (Button)findViewById(R.id.connection_log_button);
138
		if(loadBalancer.getIsConnectionLoggingEnabled().equals("true")){
139
			logs.setText("Disable Logs");
140
		} else {
141
			logs.setText("Enable Logs");
142
		}
143
	}
144
	
145
	private void setSessionButtonText(){
146
		Button sessionPersistence = (Button)findViewById(R.id.session_persistence_button);
147
		if(loadBalancer.getSessionPersistence() != null){
148
			sessionPersistence.setText("Disable Session Persistence");
149
		} else {
150
			sessionPersistence.setText("Enable Session Persistence");
151
		}
152
	}
153

  
154
	/*
155
	 * bad buttons are used when the load balancer
156
	 * in a delete status, prevents load balancer 
157
	 * from being referenced when it doesnt exist
158
	 */
113 159
	private void setUpBadButtons(){
114 160
		Button editLoadBalancer = (Button)findViewById(R.id.edit_loadbalancer_button);
115 161
		editLoadBalancer.setOnClickListener(new OnClickListener() {
......
140 186
			}
141 187
		});
142 188

  
189
		Button logs = (Button)findViewById(R.id.connection_log_button);
190
		logs.setOnClickListener(new OnClickListener() {
191

  
192
			@Override
193
			public void onClick(View v) {
194
				showAlert(loadBalancer.getStatus(), "Log settings cannot be edited.");		
195
			}
196
		});
197
		
198
		Button sessionPersistence = (Button)findViewById(R.id.session_persistence_button);
199
		sessionPersistence.setOnClickListener(new OnClickListener() {
200

  
201
			@Override
202
			public void onClick(View v) {
203
				showAlert(loadBalancer.getStatus(), "Session Persistence cannot be edited.");		
204
			}
205
		});
206

  
143 207

  
144 208
	}
145 209

  
......
163 227
				}
164 228
			})
165 229
			.create();
230
		case R.id.connection_log_button:
231
			return new AlertDialog.Builder(ViewLoadBalancerActivity.this)
232
			.setIcon(R.drawable.alert_dialog_icon)
233
			.setTitle("Disable Logs")
234
			.setMessage("Are you sure you want to disable logs for this Load Balancer?")
235
			.setPositiveButton("Enable", new DialogInterface.OnClickListener() {
236
				public void onClick(DialogInterface dialog, int whichButton) {
237
					// User clicked OK so do some stuff
238
					new SetLoggingTask().execute();	
239
				}
240
			})
241
			.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
242
				public void onClick(DialogInterface dialog, int whichButton) {
243
					// User clicked Cancel so do some stuff
244
				}
245
			})
246
			.create();
247
		case R.id.session_persistence_button:
248
			return new AlertDialog.Builder(ViewLoadBalancerActivity.this)
249
			.setIcon(R.drawable.alert_dialog_icon)
250
			.setTitle("Session Persistence")
251
			.setMessage("Are you sure you want to disable session persistence for this Load Balancer?")
252
			.setPositiveButton("Enable", new DialogInterface.OnClickListener() {
253
				public void onClick(DialogInterface dialog, int whichButton) {
254
					// User clicked OK so do some stuff
255
					new SessionPersistenceTask().execute();
256
				}
257
			})
258
			.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
259
				public void onClick(DialogInterface dialog, int whichButton) {
260
					// User clicked Cancel so do some stuff
261
				}
262
			})
263
			.create();
166 264
		}
167 265
		return null;
168 266
	}
169 267

  
268
	@Override
269
	protected void onPrepareDialog(int id, Dialog dialog){
270
		switch (id) {
271
		case R.id.connection_log_button:
272
			String logTitle;
273
			String logMessage;
274
			String logButton;
275
			if(loadBalancer.getIsConnectionLoggingEnabled().equals("true")){
276
				logTitle = "Disable Logs";
277
				logMessage = "Are you sure you want to disable logs for this Load Balancer?";
278
				logButton = "Disable";
279
			} else {
280
				logTitle = "Enable Logs";
281
				logMessage = "Log files will be processed every hour and stored in your Cloud Files account. " +
282
						"Standard Cloud Files storage and transfer fees will be accessed for the use of this feature." +
283
						"\n\nAre you sure you want to enable logs for this Load Balancer?";
284
				logButton = "Enable";
285
			}
286
			((AlertDialog)dialog).setTitle(logTitle);
287
			((AlertDialog)dialog).setMessage(logMessage);
288
			Button sessionLogButton = ((AlertDialog)dialog).getButton(AlertDialog.BUTTON1);
289
			sessionLogButton.setText(logButton);
290
			sessionLogButton.invalidate();
291
			break;
292
		case R.id.session_persistence_button:
293
			String sessionMessage;
294
			String sessionButton;
295
			if(loadBalancer.getSessionPersistence() != null){
296
				Log.d("info", "in sessionpersistence != null");
297
				sessionMessage = "Are you sure you want to disable session persistence for this Load Balancer?";
298
				sessionButton = "Disable";
299
			} else {
300
				Log.d("info", "in sessionpersistence == null");
301
				sessionMessage = "Are you sure you want to enable session persistence for this Load Balancer?";
302
				sessionButton = "Enable";
303
			}
304
			((AlertDialog)dialog).setMessage(sessionMessage);
305
			Button sessionPersistButton = ((AlertDialog)dialog).getButton(AlertDialog.BUTTON1);
306
			sessionPersistButton.setText(sessionButton);
307
			sessionPersistButton.invalidate();
308
			break;
309
		}
310
	}
311

  
170 312
	private void loadLoadBalancerData() {
171 313
		if(loadBalancer != null){
314

  
315
			/*
316
			 * need to update the text on button if 
317
			 * it has changed
318
			 */
319
			setLogButtonText();
320
			setSessionButtonText();
321
			
322

  
172 323
			TextView name = (TextView) findViewById(R.id.view_name);
173 324
			name.setText(loadBalancer.getName());
174 325

  
......
193 344
				status.setText(loadBalancer.getStatus());
194 345
			}
195 346

  
196

  
197 347
			status.setText(loadBalancer.getStatus());
198 348

  
199 349
			TextView connectionLogging = (TextView) findViewById(R.id.view_islogging);
200
			connectionLogging.setText(loadBalancer.getIsConnectionLoggingEnabled());
350
			if(loadBalancer.getIsConnectionLoggingEnabled().equals("true")){
351
				connectionLogging.setText("Enabled");
352
			} else {
353
				connectionLogging.setText("Disabled");
354
			}
201 355

  
202 356
			loadVirutalIpData();
203 357
		}
......
232 386
		if(nodeIps == null){
233 387
			nodeIps = new ArrayList<Node>();
234 388
		}
235
		
389

  
236 390
		/*
237 391
		 * need to sort the addresses because during polling
238 392
		 * their order can change and the display becomes
......
242 396
		for(Node n : nodeIps){
243 397
			addresses.add(n.getAddress());
244 398
		}
245
		
399

  
246 400
		Collections.sort(addresses);
247
				
401

  
248 402
		//may be null if lb has been deleted
249 403
		if(nodeIps != null){
250 404
			for (int i = 0; i < nodeIps.size(); i++) {
......
314 468
		private String loadBalancerId;
315 469

  
316 470
		protected void onPreExecute() {
317
			Log.d("info", "load called");
318 471
			loadBalancerId = loadBalancer.getId();
319 472
			/*
320 473
			 * set to null, so if config change occurs
......
337 490

  
338 491
		@Override
339 492
		protected void onPostExecute(LoadBalancer result) {
340
			Log.d("info", "in post");
341 493
			hideDialog();
342 494
			if (exception != null) {
343 495
				showAlert("Error", exception.getMessage());
......
398 550
	}
399 551

  
400 552

  
553
	private class SetLoggingTask extends AsyncTask<Void, Void, HttpBundle> {
554

  
555
		private CloudServersException exception;
556

  
557
		@Override
558
		protected void onPreExecute(){
559
			showDialog();
560
		}
561

  
562
		@Override
563
		protected HttpBundle doInBackground(Void... arg0) {
564
			HttpBundle bundle = null;	
565
			try {
566
				bundle = (new LoadBalancerManager(context)).setLogging(loadBalancer, !Boolean.valueOf(loadBalancer.getIsConnectionLoggingEnabled()));
567
			} catch (CloudServersException e) {
568
				exception = e;
569
			}
570
			return bundle;
571
		}
572

  
573
		@Override
574
		protected void onPostExecute(HttpBundle bundle) {
575
			hideDialog();
576
			HttpResponse response = bundle.getResponse();
577
			if (response != null) {
578
				int statusCode = response.getStatusLine().getStatusCode();			
579
				if (statusCode == 202 || statusCode == 204) {
580
					pollLoadBalancerTask = new PollLoadBalancerTask();
581
					pollLoadBalancerTask.execute((Void[]) null);
582
				} else {					
583
					CloudServersException cse = parseCloudServersException(response);
584
					if ("".equals(cse.getMessage())) {
585
						showError("There was a problem changing your log settings.", bundle);
586
					} else {
587
						showError("There was a problem changing your log settings: " + cse.getMessage(), bundle);
588
					}					
589
				}
590
			} else if (exception != null) {
591
				showError("There was a problem changing your log settings: " + exception.getMessage(), bundle);
592

  
593
			}
594

  
595
		}
596
	}
597
	
598
	private class SessionPersistenceTask extends AsyncTask<Void, Void, HttpBundle> {
599

  
600
		private CloudServersException exception;
601

  
602
		@Override
603
		protected void onPreExecute(){
604
			showDialog();
605
		}
606

  
607
		@Override
608
		protected HttpBundle doInBackground(Void... arg0) {
609
			HttpBundle bundle = null;	
610
			try {
611
				String currentSetting = loadBalancer.getSessionPersistence();
612
				if(currentSetting == null){
613
					bundle = (new LoadBalancerManager(context)).setSessionPersistence(loadBalancer, "HTTP_COOKIE");
614
				} else {
615
					bundle = (new LoadBalancerManager(context)).disableSessionPersistence(loadBalancer);
616
				}
617
			} catch (CloudServersException e) {
618
				exception = e;
619
			}
620
			return bundle;
621
		}
622

  
623
		@Override
624
		protected void onPostExecute(HttpBundle bundle) {
625
			hideDialog();
626
			HttpResponse response = bundle.getResponse();
627
			if (response != null) {
628
				int statusCode = response.getStatusLine().getStatusCode();			
629
				if (statusCode == 202 || statusCode == 200) {
630
					pollLoadBalancerTask = new PollLoadBalancerTask();
631
					pollLoadBalancerTask.execute((Void[]) null);
632
				} else {					
633
					CloudServersException cse = parseCloudServersException(response);
634
					if ("".equals(cse.getMessage())) {
635
						showError("There was a problem changing your session persistence settings.", bundle);
636
					} else {
637
						showError("There was a problem changing your session persistence settings: " + cse.getMessage(), bundle);
638
					}					
639
				}
640
			} else if (exception != null) {
641
				showError("There was a problem changing your session persistence settings: " + exception.getMessage(), bundle);
642

  
643
			}
644

  
645
		}
646
	}
647

  
648

  
401 649
}

Also available in: Unified diff