Statistics
| Branch: | Revision:

root / src / com / rackspacecloud / android / EditNodeActivity.java @ 1b82ddb3

History | View | Annotate | Download (7.8 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
                        try{
138
                                int w = Integer.valueOf(weight);
139
                                return w >= 1 && w <= 100 ;
140
                        } catch (NumberFormatException nfe){
141
                                return false;
142
                        }
143
                }
144
        }
145

    
146
        private void loadConditionSpinner(){
147
                conditionSpinner = (Spinner) findViewById(R.id.node_condition_spinner);
148

    
149
                ArrayAdapter<String> conditionAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, CONDITIONS);
150
                conditionAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
151
                conditionSpinner.setAdapter(conditionAdapter);
152

    
153
                conditionSpinner.setSelection(getSpinnerLocation(node.getCondition()));
154
                
155
                conditionSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
156

    
157
                        @Override
158
                        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
159
                                selectedCondition = CONDITIONS[pos];        
160
                        }
161

    
162
                        @Override
163
                        public void onNothingSelected(AdapterView<?> arg0) {
164

    
165
                        }
166

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

    
181
                private CloudServersException exception;
182

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

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

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

    
242
                @Override
243
                protected void onPostExecute(HttpBundle bundle) {
244
                        hideDialog();
245
                        HttpResponse response = bundle.getResponse();
246
                        if (response != null) {
247
                                int statusCode = response.getStatusLine().getStatusCode();
248
                                if (statusCode == 202 || statusCode == 200) {
249
                                        Intent viewIntent = new Intent();
250
                                        viewIntent.putExtra("deletedNode", node);
251
                                        setResult(NODE_DELETED_CODE, viewIntent);
252
                                        finish();
253
                                } else {
254
                                        CloudServersException cse = parseCloudServersException(response);
255
                                        if ("".equals(cse.getMessage())) {
256
                                                showError("There was a problem modifying your load balancer.", bundle);
257
                                        } else {
258
                                                showError("There was a problem modifying your load balancer: " + cse.getMessage(), bundle);
259
                                        }
260
                                }
261
                        } else if (exception != null) {
262
                                showError("There was a problem modifying your load balancer: " + exception.getMessage(), bundle);                                
263
                        }                        
264
                }
265
        }
266
        
267
        /*
268
         * For testing
269
         */
270
        @SuppressWarnings("unused")
271
        private void setSelectedWeight(String s){
272
                selectedWeight = s;
273
        }
274
}