2 * Copyright (C) 2012 Evergreen Open-ILS
3 * @author Daniel-Octavian Rizea
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.
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.
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
20 package org.evergreen.android.accountAccess.checkout;
22 import java.util.ArrayList;
23 import java.util.Date;
24 import java.util.List;
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;
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;
55 public class ItemsCheckOutListView extends Activity {
57 private String TAG = ItemsCheckOutListView.class.getName();
59 private AccountAccess accountAccess = null;
63 private CheckOutArrayAdapter listAdapter = null;
65 private ArrayList<CircRecord> circRecords = null;
67 private Context context;
69 private ProgressDialog progressDialog;
71 private Button homeButton;
73 private Button myAccountButton;
75 private TextView headerTitle;
77 private TextView itemsNo;
79 private Activity thisActivity;
81 private TextView overdueItems;
83 private Date currentDate;
86 public void onCreate(Bundle savedInstanceState) {
87 super.onCreate(savedInstanceState);
88 if (!SplashActivity.isAppInitialized()) {
89 SplashActivity.restartApp(this);
94 setContentView(R.layout.checkout_list);
95 setTitle(R.string.checkout_items_title);
97 currentDate = new Date(System.currentTimeMillis());
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);
105 myAccountButton.setOnClickListener(new OnClickListener() {
107 public void onClick(View v) {
108 Intent intent = new Intent(getApplicationContext(),
109 AccountScreenDashboard.class);
110 startActivity(intent);
114 homeButton.setOnClickListener(new OnClickListener() {
116 public void onClick(View v) {
117 Intent intent = new Intent(getApplicationContext(),
118 SearchCatalogListView.class);
119 startActivity(intent);
122 // end header portion actions
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);
134 Thread getCirc = new Thread(new Runnable() {
140 circRecords = accountAccess.getItemsCheckedOut();
141 } catch (SessionNotFoundException e) {
143 if (accountAccess.reauthenticate(ItemsCheckOutListView.this))
144 circRecords = accountAccess.getItemsCheckedOut();
145 } catch (Exception eauth) {
146 Log.d(TAG, "Exception in reauth", eauth);
150 runOnUiThread(new Runnable() {
154 for (int i = 0; i < circRecords.size(); i++)
155 listAdapter.add(circRecords.get(i));
157 itemsNo.setText(" " + circRecords.size() + " ");
160 // find overdue items
162 for (int i = 0; i < circRecords.size(); i++) {
163 CircRecord circ = circRecords.get(i);
165 if (circ.getDueDateObject().compareTo(currentDate) < 0)
169 overdueItems.setText(" " + overdueNo);
171 progressDialog.dismiss();
173 if (circRecords.size() == 0)
174 Toast.makeText(context, "No records",
177 listAdapter.notifyDataSetChanged();
183 if (accountAccess.isAuthenticated()) {
184 progressDialog = new ProgressDialog(context);
185 progressDialog.setMessage("Retrieving circulation data");
186 progressDialog.show();
190 Toast.makeText(context,
191 "You must be authenticated to retrieve circulation records",
197 public boolean onCreateOptionsMenu(Menu menu) {
198 MenuInflater menuInflater = getMenuInflater();
199 menuInflater.inflate(R.menu.checkout_menu, menu);
200 return super.onCreateOptionsMenu(menu);
205 public boolean onOptionsItemSelected(MenuItem item) {
207 return super.onOptionsItemSelected(item);
210 class CheckOutArrayAdapter extends ArrayAdapter<CircRecord> {
211 private static final String tag = "CheckoutArrayAdapter";
213 private TextView recordTitle;
214 private TextView recordAuthor;
215 private TextView recordDueDate;
216 private TextView recordRenewals;
217 private TextView renewButton;
219 private List<CircRecord> records = new ArrayList<CircRecord>();
221 public CheckOutArrayAdapter(Context context, int textViewResourceId,
222 List<CircRecord> objects) {
223 super(context, textViewResourceId, objects);
224 this.records = objects;
227 public int getCount() {
228 return this.records.size();
231 public CircRecord getItem(int index) {
232 return this.records.get(index);
235 public View getView(int position, View convertView, ViewGroup parent) {
236 View row = convertView;
239 final CircRecord record = getItem(position);
241 // if it is the right type of view
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,
250 Log.d(tag, "Successfully completed XML Row Inflation!");
254 // Get reference to TextView - title
255 recordTitle = (TextView) row
256 .findViewById(R.id.checkout_record_title);
258 // Get reference to TextView - author
259 recordAuthor = (TextView) row
260 .findViewById(R.id.checkout_record_author);
262 // Get reference to TextView - record Publisher date+publisher
263 recordDueDate = (TextView) row
264 .findViewById(R.id.checkout_due_date);
266 renewButton = (TextView) row.findViewById(R.id.renew_button);
268 renewButton.setText("renew : " + record.getRenewals());
270 renewButton.setOnClickListener(new OnClickListener() {
272 public void onClick(View v) {
273 Thread renew = new Thread(new Runnable() {
277 boolean refresh = true;
278 AccountAccess ac = AccountAccess
281 runOnUiThread(new Runnable() {
284 progressDialog = new ProgressDialog(
287 .setMessage("Renewing item");
288 progressDialog.show();
293 ac.renewCirc(record.getTargetCopy());
294 } catch (MaxRenewalsException e1) {
295 runOnUiThread(new Runnable() {
299 progressDialog.dismiss();
300 Toast.makeText(context,
301 "Max renewals reached",
302 Toast.LENGTH_LONG).show();
307 } catch (ServerErrorMessage error) {
308 final String errorMessage = error.message;
309 runOnUiThread(new Runnable() {
313 progressDialog.dismiss();
314 Toast.makeText(context,
316 Toast.LENGTH_LONG).show();
319 } catch (SessionNotFoundException e1) {
321 if (accountAccess.reauthenticate(ItemsCheckOutListView.this))
322 ac.renewCirc(record.getTargetCopy());
323 } catch (Exception eauth) {
324 Log.d(TAG, "Exception in reauth", eauth);
330 circRecords = accountAccess.getItemsCheckedOut();
331 } catch (SessionNotFoundException e) {
333 if (accountAccess.reauthenticate(ItemsCheckOutListView.this))
334 circRecords = accountAccess.getItemsCheckedOut();
335 } catch (Exception eauth) {
336 Log.d(TAG, "Exception in reauth", eauth);
340 runOnUiThread(new Runnable() {
344 for (int i = 0; i < circRecords.size(); i++) {
345 listAdapter.add(circRecords.get(i));
347 progressDialog.dismiss();
348 listAdapter.notifyDataSetChanged();
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());