d4e74b10fc634544d7731fc582a993687633ef2d
[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                         // find overdue items
158                         int overdueNo = 0;
159                         for (int i = 0; i < circRecords.size(); i++) {
160                             CircRecord circ = circRecords.get(i);
161                             if (circ.getDueDateObject().compareTo(currentDate) < 0)
162                                 overdueNo++;
163                         }
164                         overdueItems.setText(" " + overdueNo);
165
166                         progressDialog.dismiss();
167
168                         if (circRecords.size() == 0)
169                             Toast.makeText(context, "No records",
170                                     Toast.LENGTH_LONG);
171
172                         listAdapter.notifyDataSetChanged();
173                     }
174                 });
175             }
176         });
177
178         progressDialog = new ProgressDialog(context);
179         progressDialog.setMessage("Retrieving circulation data");
180         progressDialog.show();
181         getCirc.start();
182     }
183
184     private void dismissProgress() {
185         if (progressDialog != null && progressDialog.isShowing())
186             progressDialog.dismiss();
187     }
188
189     @Override
190     public boolean onCreateOptionsMenu(Menu menu) {
191         MenuInflater menuInflater = getMenuInflater();
192         menuInflater.inflate(R.menu.checkout_menu, menu);
193         return super.onCreateOptionsMenu(menu);
194
195     }
196
197     @Override
198     public boolean onOptionsItemSelected(MenuItem item) {
199
200         return super.onOptionsItemSelected(item);
201     }
202
203     class CheckOutArrayAdapter extends ArrayAdapter<CircRecord> {
204         private static final String tag = "CheckoutArrayAdapter";
205
206         private TextView recordTitle;
207         private TextView recordAuthor;
208         private TextView recordDueDate;
209         private TextView recordRenewals;
210         private TextView renewButton;
211
212         private List<CircRecord> records = new ArrayList<CircRecord>();
213
214         public CheckOutArrayAdapter(Context context, int textViewResourceId,
215                 List<CircRecord> objects) {
216             super(context, textViewResourceId, objects);
217             this.records = objects;
218         }
219
220         public int getCount() {
221             return this.records.size();
222         }
223
224         public CircRecord getItem(int index) {
225             return this.records.get(index);
226         }
227
228         public View getView(int position, View convertView, ViewGroup parent) {
229             View row = convertView;
230
231             // Get item
232             final CircRecord record = getItem(position);
233
234             // if it is the right type of view
235             if (row == null) {
236
237                 Log.d(tag, "Starting XML Row Inflation ... ");
238                 LayoutInflater inflater = (LayoutInflater) this
239                         .getContext().getSystemService(
240                                 Context.LAYOUT_INFLATER_SERVICE);
241                 row = inflater.inflate(R.layout.checkout_list_item, parent,
242                         false);
243                 Log.d(tag, "Successfully completed XML Row Inflation!");
244
245             }
246
247             // Get reference to TextView - title
248             recordTitle = (TextView) row.findViewById(R.id.checkout_record_title);
249
250             // Get reference to TextView - author
251             recordAuthor = (TextView) row.findViewById(R.id.checkout_record_author);
252
253             // Get reference to TextView - record Publisher date+publisher
254             recordDueDate = (TextView) row.findViewById(R.id.checkout_due_date);
255
256             renewButton = (TextView) row.findViewById(R.id.renew_button);
257             final boolean renewable = record.getRenewals() > 0;
258             renewButton.setVisibility(renewable ? View.VISIBLE : View.GONE);
259             renewButton.setEnabled(renewable);
260             renewButton.setOnClickListener(new OnClickListener() {
261                 @Override
262                 public void onClick(View v) {
263                     if (!renewable)
264                         return;
265                     Thread renew = new Thread(new Runnable() {
266
267                         @Override
268                         public void run() {
269                             boolean refresh = true;
270                             AccountAccess ac = AccountAccess
271                                     .getAccountAccess();
272
273                             runOnUiThread(new Runnable() {
274                                 @Override
275                                 public void run() {
276                                     progressDialog = new ProgressDialog(context);
277                                     progressDialog.setMessage("Renewing item");
278                                     progressDialog.show();
279                                 }
280                             });
281
282                             try {
283                                 ac.renewCirc(record.getTargetCopy());
284                             } catch (MaxRenewalsException e1) {
285                                 runOnUiThread(new Runnable() {
286
287                                     @Override
288                                     public void run() {
289                                         progressDialog.dismiss();
290                                         Toast.makeText(context,
291                                                 "Max renewals reached",
292                                                 Toast.LENGTH_LONG).show();
293                                     }
294                                 });
295
296                                 refresh = false;
297                             } catch (ServerErrorMessage error) {
298                                 final String errorMessage = error.message;
299                                 runOnUiThread(new Runnable() {
300
301                                     @Override
302                                     public void run() {
303                                         progressDialog.dismiss();
304                                         Toast.makeText(context,
305                                                 errorMessage,
306                                                 Toast.LENGTH_LONG).show();
307                                     }
308                                 });
309                             } catch (SessionNotFoundException e1) {
310                                 try {
311                                     if (accountAccess.reauthenticate(ItemsCheckOutListView.this))
312                                         ac.renewCirc(record.getTargetCopy());
313                                 } catch (Exception eauth) {
314                                     Log.d(TAG, "Exception in reauth", eauth);
315                                 }
316                             }
317                             runOnUiThread(new Runnable() {
318                                 @Override
319                                 public void run() {
320                                     Toast.makeText(context, getString(R.string.item_renewed), Toast.LENGTH_SHORT).show();
321                                 }
322                             });
323
324                             if (refresh) {
325                                 try {
326                                     circRecords = accountAccess.getItemsCheckedOut();
327                                 } catch (SessionNotFoundException e) {
328                                     try {
329                                         if (accountAccess.reauthenticate(ItemsCheckOutListView.this))
330                                             circRecords = accountAccess.getItemsCheckedOut();
331                                     } catch (Exception eauth) {
332                                         Log.d(TAG, "Exception in reauth", eauth);
333                                     }
334                                 }
335
336                                 runOnUiThread(new Runnable() {
337                                     @Override
338                                     public void run() {
339                                         listAdapter.clear();
340                                         for (int i = 0; i < circRecords.size(); i++) {
341                                             listAdapter.add(circRecords.get(i));
342                                         }
343                                         progressDialog.dismiss();
344                                         listAdapter.notifyDataSetChanged();
345                                     }
346                                 });
347                             }
348                         }
349                     });
350
351                     renew.start();
352                 }
353             });
354
355             // set text
356             recordTitle.setText(record.getTitle());
357             recordAuthor.setText(record.getAuthor());
358             recordDueDate.setText(getString(R.string.due) + ": " + record.getDueDate());
359             Log.d(TAG, "title:  " + record.getTitle());
360             Log.d(TAG, "author: " + record.getAuthor());
361             Log.d(TAG, "due:    " + record.getDueDate());
362             Log.d(TAG, "renew:  " + record.getRenewals());
363
364             return row;
365         }
366     }
367 }