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 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;
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 final 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 myAccountButton = (Button) findViewById(R.id.my_account_button);
101 myAccountButton.setOnClickListener(new OnClickListener() {
103 public void onClick(View v) {
104 Intent intent = new Intent(getApplicationContext(),
105 AccountScreenDashboard.class);
106 startActivity(intent);
110 homeButton = (Button) findViewById(R.id.action_bar_home_button);
111 homeButton.setText(R.string.checkout_items_title);
112 homeButton.setOnClickListener(new OnClickListener() {
114 public void onClick(View v) {
115 Intent intent = new Intent(getApplicationContext(),
116 SearchCatalogListView.class);
117 startActivity(intent);
120 // end header portion actions
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);
132 Thread getCirc = new Thread(new Runnable() {
138 circRecords = accountAccess.getItemsCheckedOut();
139 } catch (SessionNotFoundException e) {
141 if (accountAccess.reauthenticate(ItemsCheckOutListView.this))
142 circRecords = accountAccess.getItemsCheckedOut();
143 } catch (Exception eauth) {
144 Log.d(TAG, "Exception in reauth", eauth);
148 runOnUiThread(new Runnable() {
152 for (int i = 0; i < circRecords.size(); i++)
153 listAdapter.add(circRecords.get(i));
155 itemsNo.setText(" " + circRecords.size() + " ");
158 // find overdue items
160 for (int i = 0; i < circRecords.size(); i++) {
161 CircRecord circ = circRecords.get(i);
163 if (circ.getDueDateObject().compareTo(currentDate) < 0)
167 overdueItems.setText(" " + overdueNo);
169 progressDialog.dismiss();
171 if (circRecords.size() == 0)
172 Toast.makeText(context, "No records",
175 listAdapter.notifyDataSetChanged();
181 if (accountAccess.isAuthenticated()) {
182 progressDialog = new ProgressDialog(context);
183 progressDialog.setMessage("Retrieving circulation data");
184 progressDialog.show();
188 Toast.makeText(context,
189 "You must be authenticated to retrieve circulation records",
195 public boolean onCreateOptionsMenu(Menu menu) {
196 MenuInflater menuInflater = getMenuInflater();
197 menuInflater.inflate(R.menu.checkout_menu, menu);
198 return super.onCreateOptionsMenu(menu);
203 public boolean onOptionsItemSelected(MenuItem item) {
205 return super.onOptionsItemSelected(item);
208 class CheckOutArrayAdapter extends ArrayAdapter<CircRecord> {
209 private static final String tag = "CheckoutArrayAdapter";
211 private TextView recordTitle;
212 private TextView recordAuthor;
213 private TextView recordDueDate;
214 private TextView recordRenewals;
215 private TextView renewButton;
217 private List<CircRecord> records = new ArrayList<CircRecord>();
219 public CheckOutArrayAdapter(Context context, int textViewResourceId,
220 List<CircRecord> objects) {
221 super(context, textViewResourceId, objects);
222 this.records = objects;
225 public int getCount() {
226 return this.records.size();
229 public CircRecord getItem(int index) {
230 return this.records.get(index);
233 public View getView(int position, View convertView, ViewGroup parent) {
234 View row = convertView;
237 final CircRecord record = getItem(position);
239 // if it is the right type of view
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,
248 Log.d(tag, "Successfully completed XML Row Inflation!");
252 // Get reference to TextView - title
253 recordTitle = (TextView) row.findViewById(R.id.checkout_record_title);
255 // Get reference to TextView - author
256 recordAuthor = (TextView) row.findViewById(R.id.checkout_record_author);
258 // Get reference to TextView - record Publisher date+publisher
259 recordDueDate = (TextView) row.findViewById(R.id.checkout_due_date);
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() {
267 public void onClick(View v) {
270 Thread renew = new Thread(new Runnable() {
274 boolean refresh = true;
275 AccountAccess ac = AccountAccess
278 runOnUiThread(new Runnable() {
281 progressDialog = new ProgressDialog(
284 .setMessage("Renewing item");
285 progressDialog.show();
290 ac.renewCirc(record.getTargetCopy());
291 } catch (MaxRenewalsException e1) {
292 runOnUiThread(new Runnable() {
296 progressDialog.dismiss();
297 Toast.makeText(context,
298 "Max renewals reached",
299 Toast.LENGTH_LONG).show();
304 } catch (ServerErrorMessage error) {
305 final String errorMessage = error.message;
306 runOnUiThread(new Runnable() {
310 progressDialog.dismiss();
311 Toast.makeText(context,
313 Toast.LENGTH_LONG).show();
316 } catch (SessionNotFoundException e1) {
318 if (accountAccess.reauthenticate(ItemsCheckOutListView.this))
319 ac.renewCirc(record.getTargetCopy());
320 } catch (Exception eauth) {
321 Log.d(TAG, "Exception in reauth", eauth);
324 runOnUiThread(new Runnable() {
327 Toast.makeText(context, getString(R.string.item_renewed), Toast.LENGTH_SHORT).show();
333 circRecords = accountAccess.getItemsCheckedOut();
334 } catch (SessionNotFoundException e) {
336 if (accountAccess.reauthenticate(ItemsCheckOutListView.this))
337 circRecords = accountAccess.getItemsCheckedOut();
338 } catch (Exception eauth) {
339 Log.d(TAG, "Exception in reauth", eauth);
343 runOnUiThread(new Runnable() {
347 for (int i = 0; i < circRecords.size(); i++) {
348 listAdapter.add(circRecords.get(i));
350 progressDialog.dismiss();
351 listAdapter.notifyDataSetChanged();
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());