Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / EditNodeActivity.java @ b722cab3

History | View | Annotate | Download (7.6 kB)

1
package com.rackspacecloud.android;
2

    
3
import org.apache.http.HttpResponse;
4

    
5
import com.rackspace.cloud.loadbalancer.api.client.LoadBalancer;
6
import com.rackspace.cloud.loadbalancer.api.client.Node;
7
import com.rackspace.cloud.loadbalancer.api.client.NodeManager;
8
import com.rackspace.cloud.servers.api.client.CloudServersException;
9
import com.rackspace.cloud.servers.api.client.http.HttpBundle;
10
import android.app.Activity;
11
import android.app.AlertDialog;
12
import android.app.Dialog;
13
import android.content.DialogInterface;
14
import android.content.Intent;
15
import android.os.AsyncTask;
16
import android.os.Bundle;
17
import android.view.View;
18
import android.view.View.OnClickListener;
19
import android.view.Window;
20
import android.widget.AdapterView;
21
import android.widget.AdapterView.OnItemSelectedListener;
22
import android.widget.ArrayAdapter;
23
import android.widget.Button;
24
import android.widget.EditText;
25
import android.widget.Spinner;
26
import android.widget.TextView;
27

    
28
public class EditNodeActivity extends CloudActivity{
29

    
30
        private final String[] CONDITIONS = {"Enabled", "Disabled", "Draining"};
31
        private final int NODE_DELETED_CODE = 389;
32
        
33
        private Spinner conditionSpinner;
34
        private EditText weightText;
35
        private String selectedCondition;
36
        private String selectedWeight;
37
        private LoadBalancer loadBalancer;
38
        private Node node;
39

    
40
        public void onCreate(Bundle savedInstanceState) {
41
                super.onCreate(savedInstanceState);
42
                requestWindowFeature(Window.FEATURE_NO_TITLE);
43
                setContentView(R.layout.editnode);
44
                node = (Node) this.getIntent().getExtras().get("node");
45
                loadBalancer = (LoadBalancer) this.getIntent().getExtras().get("loadBalancer");
46
                restoreState(savedInstanceState);
47
        } 
48
        
49
        protected void restoreState(Bundle state){
50
                super.restoreState(state);
51
                loadData();
52
                setUpButtons();
53
        }
54
        
55
        private void setUpButtons(){
56
                Button submit = (Button) findViewById(R.id.edit_node_button);
57
                submit.setOnClickListener(new OnClickListener() {
58
                        
59
                        @Override
60
                        public void onClick(View v) {
61
                                selectedWeight = weightText.getText().toString();
62
                                if(weightText.getVisibility() == View.GONE || (weightText.getVisibility() != View.GONE && validWeight(selectedWeight))){
63
                                        new ModifyNodeTask().execute();
64
                                }
65
                                else{
66
                                        showAlert("Error", "Weight must be between 1 and 100.");
67
                                }
68
                        }
69
                });
70
                
71
                Button delete = (Button) findViewById(R.id.delete_node_button);
72
                delete.setOnClickListener(new OnClickListener() {
73
                        
74
                        @Override
75
                        public void onClick(View v) {
76
                                showDialog(R.id.delete_node_button);
77
                        }
78
                });
79
        }
80
        
81
        @Override
82
        protected Dialog onCreateDialog(int id) {
83
                        switch (id) {
84
                        case R.id.delete_node_button:
85
                                return new AlertDialog.Builder(EditNodeActivity.this)
86
                                .setIcon(R.drawable.alert_dialog_icon)
87
                                .setTitle("Remove Node")
88
                                .setMessage("Are you sure you want to remove this node?")
89
                                .setPositiveButton("Remove", new DialogInterface.OnClickListener() {
90
                                        public void onClick(DialogInterface dialog, int whichButton) {
91
                                                // User clicked OK so do some stuff
92
                                                new DeleteNodeTask().execute((Void[]) null);
93
                                        }
94
                                })
95
                                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
96
                                        public void onClick(DialogInterface dialog, int whichButton) {
97
                                                // User clicked Cancel so do some stuff
98
                                        }
99
                                })
100
                                .create();
101
                        }
102
                        return null;
103
        }
104

    
105
        public void onBackPressed(){
106
                setResult(RESULT_CANCELED);
107
                finish();
108
        }
109
        
110
        private void loadData(){
111
                loadConditionSpinner();
112
                
113
                TextView ipText = (TextView) findViewById(R.id.node_ip_text);
114
                ipText.setText(node.getAddress());
115
                
116
                TextView portText = (TextView) findViewById(R.id.node_port_text);
117
                portText.setText(node.getPort());
118
                
119
                weightText = (EditText) findViewById(R.id.node_weight_text);
120
                //if algorithm is not weighted then then node's weight will be null
121
                if(node.getWeight() == null){
122
                        TextView weightLabel = (TextView) findViewById(R.id.node_weight_label);
123
                        weightLabel.setVisibility(View.GONE);
124
                        weightText.setVisibility(View.GONE);
125
                }
126
                else{
127
                        weightText.setText(node.getWeight());
128
                }
129
        }
130
        
131
        
132
        private Boolean validWeight(String weight){
133
                if(weight.equals("")){
134
                        return false;
135
                }
136
                else{
137
                        int w = Integer.valueOf(weight);
138
                        return w >= 1 && w <= 100 ;
139
                }
140
        }
141

    
142
        private void loadConditionSpinner(){
143
                conditionSpinner = (Spinner) findViewById(R.id.node_condition_spinner);
144

    
145
                ArrayAdapter<String> conditionAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, CONDITIONS);
146
                conditionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
147
                conditionSpinner.setAdapter(conditionAdapter);
148

    
149
                conditionSpinner.setSelection(getSpinnerLocation(node.getCondition()));
150
                
151
                conditionSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
152

    
153
                        @Override
154
                        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
155
                                selectedCondition = CONDITIONS[pos];        
156
                        }
157

    
158
                        @Override
159
                        public void onNothingSelected(AdapterView<?> arg0) {
160

    
161
                        }
162

    
163
                });
164
        }
165
        
166
        private int getSpinnerLocation(String condition){
167
                for(int i = 0; i < CONDITIONS.length; i++){
168
                        if(CONDITIONS[i].equalsIgnoreCase(condition)){
169
                                return i;
170
                        }
171
                }
172
                return 0;
173
        }
174
        
175
        public class ModifyNodeTask extends AsyncTask<Void, Void, HttpBundle> {
176

    
177
                private CloudServersException exception;
178

    
179
                @Override
180
                protected void onPreExecute(){
181
                        showDialog();
182
                }
183
                
184
                @Override
185
                protected HttpBundle doInBackground(Void... arg0) {
186
                        HttpBundle bundle = null;
187
                        try {
188
                                bundle = (new NodeManager(getContext())).update(loadBalancer, node, selectedCondition, selectedWeight);
189
                        } catch (CloudServersException e) {
190
                                exception = e;
191
                        }
192
                        return bundle;
193
                }
194

    
195
                @Override
196
                protected void onPostExecute(HttpBundle bundle) {
197
                        hideDialog();
198
                        HttpResponse response = bundle.getResponse();
199
                        if (response != null) {
200
                                int statusCode = response.getStatusLine().getStatusCode();
201
                                if (statusCode == 202 || statusCode == 200) {
202
                                        setResult(Activity.RESULT_OK);
203
                                        finish();
204
                                } else {
205
                                        CloudServersException cse = parseCloudServersException(response);
206
                                        if ("".equals(cse.getMessage())) {
207
                                                showError("There was a problem modifying your load balancer.", bundle);
208
                                        } else {
209
                                                showError("There was a problem modifying your load balancer: " + cse.getMessage(), bundle);
210
                                        }
211
                                }
212
                        } else if (exception != null) {
213
                                showError("There was a problem modifying your load balancer: " + exception.getMessage(), bundle);                                
214
                        }                        
215
                }
216
        }
217
        
218
        public class DeleteNodeTask extends AsyncTask<Void, Void, HttpBundle> {
219

    
220
                private CloudServersException exception;
221
                
222
                @Override
223
                protected void onPreExecute(){
224
                        showDialog();
225
                }
226
                
227
                @Override
228
                protected HttpBundle doInBackground(Void... arg0) {
229
                        HttpBundle bundle = null;
230
                        try {
231
                                bundle = (new NodeManager(getContext())).remove(loadBalancer, node);
232
                        } catch (CloudServersException e) {
233
                                exception = e;
234
                        }
235
                        return bundle;
236
                }
237

    
238
                @Override
239
                protected void onPostExecute(HttpBundle bundle) {
240
                        hideDialog();
241
                        HttpResponse response = bundle.getResponse();
242
                        if (response != null) {
243
                                int statusCode = response.getStatusLine().getStatusCode();
244
                                if (statusCode == 202 || statusCode == 200) {
245
                                        Intent viewIntent = new Intent();
246
                                        viewIntent.putExtra("deletedNode", node);
247
                                        setResult(NODE_DELETED_CODE, viewIntent);
248
                                        finish();
249
                                } else {
250
                                        CloudServersException cse = parseCloudServersException(response);
251
                                        if ("".equals(cse.getMessage())) {
252
                                                showError("There was a problem modifying your load balancer.", bundle);
253
                                        } else {
254
                                                showError("There was a problem modifying your load balancer: " + cse.getMessage(), bundle);
255
                                        }
256
                                }
257
                        } else if (exception != null) {
258
                                showError("There was a problem modifying your load balancer: " + exception.getMessage(), bundle);                                
259
                        }                        
260
                }
261
        }
262
}