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() + " ");
157 // find overdue items
159 for (int i = 0; i < circRecords.size(); i++) {
160 CircRecord circ = circRecords.get(i);
161 if (circ.getDueDateObject().compareTo(currentDate) < 0)
164 overdueItems.setText(" " + overdueNo);
166 progressDialog.dismiss();
168 if (circRecords.size() == 0)
169 Toast.makeText(context, "No records",
172 listAdapter.notifyDataSetChanged();
178 progressDialog = new ProgressDialog(context);
179 progressDialog.setMessage("Retrieving circulation data");
180 progressDialog.show();
184 private void dismissProgress() {
185 if (progressDialog != null && progressDialog.isShowing())
186 progressDialog.dismiss();
190 public boolean onCreateOptionsMenu(Menu menu) {
191 MenuInflater menuInflater = getMenuInflater();
192 menuInflater.inflate(R.menu.checkout_menu, menu);
193 return super.onCreateOptionsMenu(menu);
198 public boolean onOptionsItemSelected(MenuItem item) {
200 return super.onOptionsItemSelected(item);
203 class CheckOutArrayAdapter extends ArrayAdapter<CircRecord> {
204 private static final String tag = "CheckoutArrayAdapter";
206 private TextView recordTitle;
207 private TextView recordAuthor;
208 private TextView recordDueDate;
209 private TextView recordRenewals;
210 private TextView renewButton;
212 private List<CircRecord> records = new ArrayList<CircRecord>();
214 public CheckOutArrayAdapter(Context context, int textViewResourceId,
215 List<CircRecord> objects) {
216 super(context, textViewResourceId, objects);
217 this.records = objects;
220 public int getCount() {
221 return this.records.size();
224 public CircRecord getItem(int index) {
225 return this.records.get(index);
228 public View getView(int position, View convertView, ViewGroup parent) {
229 View row = convertView;
232 final CircRecord record = getItem(position);
234 // if it is the right type of view
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,
243 Log.d(tag, "Successfully completed XML Row Inflation!");
247 // Get reference to TextView - title
248 recordTitle = (TextView) row.findViewById(R.id.checkout_record_title);
250 // Get reference to TextView - author
251 recordAuthor = (TextView) row.findViewById(R.id.checkout_record_author);
253 // Get reference to TextView - record Publisher date+publisher
254 recordDueDate = (TextView) row.findViewById(R.id.checkout_due_date);
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() {
262 public void onClick(View v) {
265 Thread renew = new Thread(new Runnable() {
269 boolean refresh = true;
270 AccountAccess ac = AccountAccess
273 runOnUiThread(new Runnable() {
276 progressDialog = new ProgressDialog(context);
277 progressDialog.setMessage("Renewing item");
278 progressDialog.show();
283 ac.renewCirc(record.getTargetCopy());
284 } catch (MaxRenewalsException e1) {
285 runOnUiThread(new Runnable() {
289 progressDialog.dismiss();
290 Toast.makeText(context,
291 "Max renewals reached",
292 Toast.LENGTH_LONG).show();
297 } catch (ServerErrorMessage error) {
298 final String errorMessage = error.message;
299 runOnUiThread(new Runnable() {
303 progressDialog.dismiss();
304 Toast.makeText(context,
306 Toast.LENGTH_LONG).show();
309 } catch (SessionNotFoundException e1) {
311 if (accountAccess.reauthenticate(ItemsCheckOutListView.this))
312 ac.renewCirc(record.getTargetCopy());
313 } catch (Exception eauth) {
314 Log.d(TAG, "Exception in reauth", eauth);
317 runOnUiThread(new Runnable() {
320 Toast.makeText(context, getString(R.string.item_renewed), Toast.LENGTH_SHORT).show();
326 circRecords = accountAccess.getItemsCheckedOut();
327 } catch (SessionNotFoundException e) {
329 if (accountAccess.reauthenticate(ItemsCheckOutListView.this))
330 circRecords = accountAccess.getItemsCheckedOut();
331 } catch (Exception eauth) {
332 Log.d(TAG, "Exception in reauth", eauth);
336 runOnUiThread(new Runnable() {
340 for (int i = 0; i < circRecords.size(); i++) {
341 listAdapter.add(circRecords.get(i));
343 progressDialog.dismiss();
344 listAdapter.notifyDataSetChanged();
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());