03fffba39e62337631f5b4a460f7625bc4b8834c
[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 org.evergreen.android.R;
27 import org.evergreen.android.accountAccess.AccountAccess;
28 import org.evergreen.android.accountAccess.MaxRenewalsException;
29 import org.evergreen.android.accountAccess.ServerErrorMessage;
30 import org.evergreen.android.accountAccess.SessionNotFoundException;
31 import org.evergreen.android.globals.Utils;
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 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         homeButton = (Button) findViewById(R.id.library_logo);
101         myAccountButton = (Button) findViewById(R.id.my_account_button);
102         headerTitle = (TextView) findViewById(R.id.header_title);
103         headerTitle.setText(R.string.checkout_items_title);
104
105         myAccountButton.setOnClickListener(new OnClickListener() {
106             @Override
107             public void onClick(View v) {
108                 Intent intent = new Intent(getApplicationContext(),
109                         AccountScreenDashboard.class);
110                 startActivity(intent);
111             }
112         });
113
114         homeButton.setOnClickListener(new OnClickListener() {
115             @Override
116             public void onClick(View v) {
117                 Intent intent = new Intent(getApplicationContext(),
118                         SearchCatalogListView.class);
119                 startActivity(intent);
120             }
121         });
122         // end header portion actions
123
124         context = this;
125         itemsNo = (TextView) findViewById(R.id.checkout_items_number);
126         overdueItems = (TextView) findViewById(R.id.checkout_items_overdue);
127         accountAccess = AccountAccess.getAccountAccess();
128         lv = (ListView) findViewById(R.id.checkout_items_list);
129         circRecords = new ArrayList<CircRecord>();
130         listAdapter = new CheckOutArrayAdapter(context,
131                 R.layout.checkout_list_item, circRecords);
132         lv.setAdapter(listAdapter);
133
134         Thread getCirc = new Thread(new Runnable() {
135
136             @Override
137             public void run() {
138
139                 try {
140                     circRecords = accountAccess.getItemsCheckedOut();
141                 } catch (SessionNotFoundException e) {
142                     try {
143                         if (accountAccess.reauthenticate(ItemsCheckOutListView.this))
144                             circRecords = accountAccess.getItemsCheckedOut();
145                     } catch (Exception eauth) {
146                         Log.d(TAG, "Exception in reauth", eauth);
147                     }
148                 }
149
150                 runOnUiThread(new Runnable() {
151
152                     @Override
153                     public void run() {
154                         for (int i = 0; i < circRecords.size(); i++)
155                             listAdapter.add(circRecords.get(i));
156
157                         itemsNo.setText(" " + circRecords.size() + " ");
158
159                         int overdueNo = 0;
160                         // find overdue items
161
162                         for (int i = 0; i < circRecords.size(); i++) {
163                             CircRecord circ = circRecords.get(i);
164
165                             if (circ.getDueDateObject().compareTo(currentDate) < 0)
166                                 overdueNo++;
167                         }
168
169                         overdueItems.setText(" " + overdueNo);
170
171                         progressDialog.dismiss();
172
173                         if (circRecords.size() == 0)
174                             Toast.makeText(context, "No records",
175                                     Toast.LENGTH_LONG);
176
177                         listAdapter.notifyDataSetChanged();
178                     }
179                 });
180             }
181         });
182
183         if (accountAccess.isAuthenticated()) {
184             progressDialog = new ProgressDialog(context);
185             progressDialog.setMessage("Retrieving circulation data");
186             progressDialog.show();
187             getCirc.start();
188
189         } else
190             Toast.makeText(context,
191                     "You must be authenticated to retrieve circulation records",
192                     Toast.LENGTH_LONG);
193
194     }
195
196     @Override
197     public boolean onCreateOptionsMenu(Menu menu) {
198         MenuInflater menuInflater = getMenuInflater();
199         menuInflater.inflate(R.menu.checkout_menu, menu);
200         return super.onCreateOptionsMenu(menu);
201
202     }
203
204     @Override
205     public boolean onOptionsItemSelected(MenuItem item) {
206
207         return super.onOptionsItemSelected(item);
208     }
209
210     class CheckOutArrayAdapter extends ArrayAdapter<CircRecord> {
211         private static final String tag = "CheckoutArrayAdapter";
212
213         private TextView recordTitle;
214         private TextView recordAuthor;
215         private TextView recordDueDate;
216         private TextView recordRenewals;
217         private TextView renewButton;
218
219         private List<CircRecord> records = new ArrayList<CircRecord>();
220
221         public CheckOutArrayAdapter(Context context, int textViewResourceId,
222                 List<CircRecord> objects) {
223             super(context, textViewResourceId, objects);
224             this.records = objects;
225         }
226
227         public int getCount() {
228             return this.records.size();
229         }
230
231         public CircRecord getItem(int index) {
232             return this.records.get(index);
233         }
234
235         public View getView(int position, View convertView, ViewGroup parent) {
236             View row = convertView;
237
238             // Get item
239             final CircRecord record = getItem(position);
240
241             // if it is the right type of view
242             if (row == null) {
243
244                 Log.d(tag, "Starting XML Row Inflation ... ");
245                 LayoutInflater inflater = (LayoutInflater) this
246                         .getContext().getSystemService(
247                                 Context.LAYOUT_INFLATER_SERVICE);
248                 row = inflater.inflate(R.layout.checkout_list_item, parent,
249                         false);
250                 Log.d(tag, "Successfully completed XML Row Inflation!");
251
252             }
253
254             // Get reference to TextView - title
255             recordTitle = (TextView) row
256                     .findViewById(R.id.checkout_record_title);
257
258             // Get reference to TextView - author
259             recordAuthor = (TextView) row
260                     .findViewById(R.id.checkout_record_author);
261
262             // Get reference to TextView - record Publisher date+publisher
263             recordDueDate = (TextView) row
264                     .findViewById(R.id.checkout_due_date);
265
266             renewButton = (TextView) row.findViewById(R.id.renew_button);
267
268             renewButton.setText("renew : " + record.getRenewals());
269
270             renewButton.setOnClickListener(new OnClickListener() {
271                 @Override
272                 public void onClick(View v) {
273                     Thread renew = new Thread(new Runnable() {
274
275                         @Override
276                         public void run() {
277                             boolean refresh = true;
278                             AccountAccess ac = AccountAccess
279                                     .getAccountAccess();
280
281                             runOnUiThread(new Runnable() {
282                                 @Override
283                                 public void run() {
284                                     progressDialog = new ProgressDialog(
285                                             context);
286                                     progressDialog
287                                             .setMessage("Renewing item");
288                                     progressDialog.show();
289                                 }
290                             });
291
292                             try {
293                                 ac.renewCirc(record.getTargetCopy());
294                             } catch (MaxRenewalsException e1) {
295                                 runOnUiThread(new Runnable() {
296
297                                     @Override
298                                     public void run() {
299                                         progressDialog.dismiss();
300                                         Toast.makeText(context,
301                                                 "Max renewals reached",
302                                                 Toast.LENGTH_LONG).show();
303                                     }
304                                 });
305
306                                 refresh = false;
307                             } catch (ServerErrorMessage error) {
308                                 final String errorMessage = error.message;
309                                 runOnUiThread(new Runnable() {
310
311                                     @Override
312                                     public void run() {
313                                         progressDialog.dismiss();
314                                         Toast.makeText(context,
315                                                 errorMessage,
316                                                 Toast.LENGTH_LONG).show();
317                                     }
318                                 });
319                             } catch (SessionNotFoundException e1) {
320                                 try {
321                                     if (accountAccess.reauthenticate(ItemsCheckOutListView.this))
322                                         ac.renewCirc(record.getTargetCopy());
323                                 } catch (Exception eauth) {
324                                     Log.d(TAG, "Exception in reauth", eauth);
325                                 }
326                             }
327
328                             if (refresh) {
329                                 try {
330                                     circRecords = accountAccess.getItemsCheckedOut();
331                                 } catch (SessionNotFoundException e) {
332                                     try {
333                                         if (accountAccess.reauthenticate(ItemsCheckOutListView.this))
334                                             circRecords = accountAccess.getItemsCheckedOut();
335                                     } catch (Exception eauth) {
336                                         Log.d(TAG, "Exception in reauth", eauth);
337                                     }
338                                 }
339
340                                 runOnUiThread(new Runnable() {
341                                     @Override
342                                     public void run() {
343                                         listAdapter.clear();
344                                         for (int i = 0; i < circRecords.size(); i++) {
345                                             listAdapter.add(circRecords.get(i));
346                                         }
347                                         progressDialog.dismiss();
348                                         listAdapter.notifyDataSetChanged();
349                                     }
350                                 });
351                             }
352                         }
353                     });
354
355                     renew.start();
356                 }
357             });
358
359             // set text
360             recordTitle.setText(record.getTitle());
361             recordAuthor.setText(record.getAuthor());
362             recordDueDate.setText(record.getDueDate());
363             Log.d(TAG, "title:  " + record.getTitle());
364             Log.d(TAG, "author: " + record.getAuthor());
365             Log.d(TAG, "due:    " + record.getDueDate());
366             Log.d(TAG, "renew:  " + record.getRenewals());
367
368             return row;
369         }
370     }
371 }