47f76a22d43306bfbaf13ad5f9b736e460e3995c
[working/Evergreen.git] /
1 /*
2  * Copyright (C) 2012 Evergreen Open-ILS
3  * @author Daniel-Octavian Rizea
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * or the License, or (at your option) any later version.
9  * 
10  * This program is distributed in the hope that it will be usefull,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  * 
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software 
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
18  * 
19  */
20 package org.evergreen.android.accountAccess.checkout;
21
22 import java.util.ArrayList;
23 import java.util.Date;
24 import java.util.List;
25
26 import android.opengl.Visibility;
27 import org.evergreen.android.R;
28 import org.evergreen.android.accountAccess.AccountAccess;
29 import org.evergreen.android.accountAccess.MaxRenewalsException;
30 import org.evergreen.android.accountAccess.ServerErrorMessage;
31 import org.evergreen.android.accountAccess.SessionNotFoundException;
32 import org.evergreen.android.searchCatalog.SearchCatalogListView;
33 import org.evergreen.android.views.AccountScreenDashboard;
34 import org.evergreen.android.views.splashscreen.SplashActivity;
35
36 import android.app.Activity;
37 import android.app.ProgressDialog;
38 import android.content.Context;
39 import android.content.Intent;
40 import android.os.Bundle;
41 import android.util.Log;
42 import android.view.LayoutInflater;
43 import android.view.Menu;
44 import android.view.MenuInflater;
45 import android.view.MenuItem;
46 import android.view.View;
47 import android.view.View.OnClickListener;
48 import android.view.ViewGroup;
49 import android.widget.ArrayAdapter;
50 import android.widget.Button;
51 import android.widget.ListView;
52 import android.widget.TextView;
53 import android.widget.Toast;
54
55 public class ItemsCheckOutListView extends Activity {
56
57     private final String TAG = ItemsCheckOutListView.class.getName();
58
59     private AccountAccess accountAccess = null;
60
61     private ListView lv;
62
63     private CheckOutArrayAdapter listAdapter = null;
64
65     private ArrayList<CircRecord> circRecords = null;
66
67     private Context context;
68
69     private ProgressDialog progressDialog;
70
71     private Button homeButton;
72
73     private Button myAccountButton;
74
75     private TextView headerTitle;
76
77     private TextView itemsNo;
78
79     private Activity thisActivity;
80
81     private TextView overdueItems;
82
83     private Date currentDate;
84
85     @Override
86     public void onCreate(Bundle savedInstanceState) {
87         super.onCreate(savedInstanceState);
88         if (!SplashActivity.isAppInitialized()) {
89             SplashActivity.restartApp(this);
90             return;
91         }
92
93         thisActivity = this;
94         setContentView(R.layout.checkout_list);
95         setTitle(R.string.checkout_items_title);
96
97         currentDate = new Date(System.currentTimeMillis());
98
99         // header portion actions
100         myAccountButton = (Button) findViewById(R.id.my_account_button);
101         myAccountButton.setOnClickListener(new OnClickListener() {
102             @Override
103             public void onClick(View v) {
104                 Intent intent = new Intent(getApplicationContext(),
105                         AccountScreenDashboard.class);
106                 startActivity(intent);
107             }
108         });
109
110         homeButton = (Button) findViewById(R.id.action_bar_home_button);
111         homeButton.setText(R.string.checkout_items_title);
112         homeButton.setOnClickListener(new OnClickListener() {
113             @Override
114             public void onClick(View v) {
115                 Intent intent = new Intent(getApplicationContext(),
116                         SearchCatalogListView.class);
117                 startActivity(intent);
118             }
119         });
120         // end header portion actions
121
122         context = this;
123         itemsNo = (TextView) findViewById(R.id.checkout_items_number);
124         overdueItems = (TextView) findViewById(R.id.checkout_items_overdue);
125         accountAccess = AccountAccess.getAccountAccess();
126         lv = (ListView) findViewById(R.id.checkout_items_list);
127         circRecords = new ArrayList<CircRecord>();
128         listAdapter = new CheckOutArrayAdapter(context,
129                 R.layout.checkout_list_item, circRecords);
130         lv.setAdapter(listAdapter);
131
132         Thread getCirc = new Thread(new Runnable() {
133
134             @Override
135             public void run() {
136
137                 try {
138                     circRecords = accountAccess.getItemsCheckedOut();
139                 } catch (SessionNotFoundException e) {
140                     try {
141                         if (accountAccess.reauthenticate(ItemsCheckOutListView.this))
142                             circRecords = accountAccess.getItemsCheckedOut();
143                     } catch (Exception eauth) {
144                         Log.d(TAG, "Exception in reauth", eauth);
145                     }
146                 }
147
148                 runOnUiThread(new Runnable() {
149
150                     @Override
151                     public void run() {
152                         for (int i = 0; i < circRecords.size(); i++)
153                             listAdapter.add(circRecords.get(i));
154
155                         itemsNo.setText(" " + circRecords.size() + " ");
156
157                         int overdueNo = 0;
158                         // find overdue items
159
160                         for (int i = 0; i < circRecords.size(); i++) {
161                             CircRecord circ = circRecords.get(i);
162
163                             if (circ.getDueDateObject().compareTo(currentDate) < 0)
164                                 overdueNo++;
165                         }
166
167                         overdueItems.setText(" " + overdueNo);
168
169                         progressDialog.dismiss();
170
171                         if (circRecords.size() == 0)
172                             Toast.makeText(context, "No records",
173                                     Toast.LENGTH_LONG);
174
175                         listAdapter.notifyDataSetChanged();
176                     }
177                 });
178             }
179         });
180
181         if (accountAccess.isAuthenticated()) {
182             progressDialog = new ProgressDialog(context);
183             progressDialog.setMessage("Retrieving circulation data");
184             progressDialog.show();
185             getCirc.start();
186
187         } else
188             Toast.makeText(context,
189                     "You must be authenticated to retrieve circulation records",
190                     Toast.LENGTH_LONG);
191
192     }
193
194     @Override
195     public boolean onCreateOptionsMenu(Menu menu) {
196         MenuInflater menuInflater = getMenuInflater();
197         menuInflater.inflate(R.menu.checkout_menu, menu);
198         return super.onCreateOptionsMenu(menu);
199
200     }
201
202     @Override
203     public boolean onOptionsItemSelected(MenuItem item) {
204
205         return super.onOptionsItemSelected(item);
206     }
207
208     class CheckOutArrayAdapter extends ArrayAdapter<CircRecord> {
209         private static final String tag = "CheckoutArrayAdapter";
210
211         private TextView recordTitle;
212         private TextView recordAuthor;
213         private TextView recordDueDate;
214         private TextView recordRenewals;
215         private TextView renewButton;
216
217         private List<CircRecord> records = new ArrayList<CircRecord>();
218
219         public CheckOutArrayAdapter(Context context, int textViewResourceId,
220                 List<CircRecord> objects) {
221             super(context, textViewResourceId, objects);
222             this.records = objects;
223         }
224
225         public int getCount() {
226             return this.records.size();
227         }
228
229         public CircRecord getItem(int index) {
230             return this.records.get(index);
231         }
232
233         public View getView(int position, View convertView, ViewGroup parent) {
234             View row = convertView;
235
236             // Get item
237             final CircRecord record = getItem(position);
238
239             // if it is the right type of view
240             if (row == null) {
241
242                 Log.d(tag, "Starting XML Row Inflation ... ");
243                 LayoutInflater inflater = (LayoutInflater) this
244                         .getContext().getSystemService(
245                                 Context.LAYOUT_INFLATER_SERVICE);
246                 row = inflater.inflate(R.layout.checkout_list_item, parent,
247                         false);
248                 Log.d(tag, "Successfully completed XML Row Inflation!");
249
250             }
251
252             // Get reference to TextView - title
253             recordTitle = (TextView) row.findViewById(R.id.checkout_record_title);
254
255             // Get reference to TextView - author
256             recordAuthor = (TextView) row.findViewById(R.id.checkout_record_author);
257
258             // Get reference to TextView - record Publisher date+publisher
259             recordDueDate = (TextView) row.findViewById(R.id.checkout_due_date);
260
261             renewButton = (TextView) row.findViewById(R.id.renew_button);
262             final boolean renewable = record.getRenewals() > 0;
263             renewButton.setVisibility(renewable ? View.VISIBLE : View.GONE);
264             renewButton.setEnabled(renewable);
265             renewButton.setOnClickListener(new OnClickListener() {
266                 @Override
267                 public void onClick(View v) {
268                     if (!renewable)
269                         return;
270                     Thread renew = new Thread(new Runnable() {
271
272                         @Override
273                         public void run() {
274                             boolean refresh = true;
275                             AccountAccess ac = AccountAccess
276                                     .getAccountAccess();
277
278                             runOnUiThread(new Runnable() {
279                                 @Override
280                                 public void run() {
281                                     progressDialog = new ProgressDialog(
282                                             context);
283                                     progressDialog
284                                             .setMessage("Renewing item");
285                                     progressDialog.show();
286                                 }
287                             });
288
289                             try {
290                                 ac.renewCirc(record.getTargetCopy());
291                             } catch (MaxRenewalsException e1) {
292                                 runOnUiThread(new Runnable() {
293
294                                     @Override
295                                     public void run() {
296                                         progressDialog.dismiss();
297                                         Toast.makeText(context,
298                                                 "Max renewals reached",
299                                                 Toast.LENGTH_LONG).show();
300                                     }
301                                 });
302
303                                 refresh = false;
304                             } catch (ServerErrorMessage error) {
305                                 final String errorMessage = error.message;
306                                 runOnUiThread(new Runnable() {
307
308                                     @Override
309                                     public void run() {
310                                         progressDialog.dismiss();
311                                         Toast.makeText(context,
312                                                 errorMessage,
313                                                 Toast.LENGTH_LONG).show();
314                                     }
315                                 });
316                             } catch (SessionNotFoundException e1) {
317                                 try {
318                                     if (accountAccess.reauthenticate(ItemsCheckOutListView.this))
319                                         ac.renewCirc(record.getTargetCopy());
320                                 } catch (Exception eauth) {
321                                     Log.d(TAG, "Exception in reauth", eauth);
322                                 }
323                             }
324                             runOnUiThread(new Runnable() {
325                                 @Override
326                                 public void run() {
327                                     Toast.makeText(context, getString(R.string.item_renewed), Toast.LENGTH_SHORT).show();
328                                 }
329                             });
330
331                             if (refresh) {
332                                 try {
333                                     circRecords = accountAccess.getItemsCheckedOut();
334                                 } catch (SessionNotFoundException e) {
335                                     try {
336                                         if (accountAccess.reauthenticate(ItemsCheckOutListView.this))
337                                             circRecords = accountAccess.getItemsCheckedOut();
338                                     } catch (Exception eauth) {
339                                         Log.d(TAG, "Exception in reauth", eauth);
340                                     }
341                                 }
342
343                                 runOnUiThread(new Runnable() {
344                                     @Override
345                                     public void run() {
346                                         listAdapter.clear();
347                                         for (int i = 0; i < circRecords.size(); i++) {
348                                             listAdapter.add(circRecords.get(i));
349                                         }
350                                         progressDialog.dismiss();
351                                         listAdapter.notifyDataSetChanged();
352                                     }
353                                 });
354                             }
355                         }
356                     });
357
358                     renew.start();
359                 }
360             });
361
362             // set text
363             recordTitle.setText(record.getTitle());
364             recordAuthor.setText(record.getAuthor());
365             recordDueDate.setText(getString(R.string.due) + ": " + record.getDueDate());
366             Log.d(TAG, "title:  " + record.getTitle());
367             Log.d(TAG, "author: " + record.getAuthor());
368             Log.d(TAG, "due:    " + record.getDueDate());
369             Log.d(TAG, "renew:  " + record.getRenewals());
370
371             return row;
372         }
373     }
374 }