aboutsummaryrefslogtreecommitdiff
path: root/old_native_project/src/com/example/surgerylog/FileOperationsFragment.java
blob: 512f02b516057c70c807d696ca3534879c719acd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package com.example.surgerylog;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import net.redroid.medlog.R;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import com.csvreader.CsvReader;
import com.csvreader.CsvWriter;

public class FileOperationsFragment extends Fragment {
	/**
	 * The fragment argument representing the section number for this fragment.
	 */
	public static final String ARG_SECTION_NUMBER = "section_number";

	MainActivity _activity;

	public FileOperationsFragment() {
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

		// This makes sure that the container activity has implemented
		// the callback interface. If not, it throws an exception
		try {
			_activity = (MainActivity) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement OnHeadlineSelectedListener");
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// get root view
		View rootView = inflater.inflate(R.layout.fragment_file_operations,
				container, false);

		return rootView;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// set import MedLog config button
		Button importFile = (Button) getView().findViewById(R.id.importButton);
		importFile.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				try {
					// create csv values
					CsvReader reader = new CsvReader(
							_activity.TemplateDescriptionFile);

					// read headers
					if (reader.readHeaders() == false) {
						// could not read headers
						//Log.e("Error", "Headers could not be read.");
					}

					// copy back headers
					String headers[] = reader.getHeaders();

					// check number of headers
					if (headers.length != _activity.ProcessDescriptionFileNumColumns) {
						// number of headers is incorrect
						//Log.e("Error", "CSV has incorrect number of headers.");
					} else {
						// everything seems nice.... lets rebuild the tree
						_activity.Root = new MedLogRoot();
					}

					// read records
					while (reader.readRecord() == true) {
						// get record info into
						MedLogInfo surgeryInfo = new MedLogInfo(reader
								.get(headers[0]), // med type
								reader.get(headers[1]), // med subtype
								reader.get(headers[2]), // med sub-subtype
								reader.get(headers[3]), // pathology
								reader.get(headers[4]) // intervention
						);

						surgeryInfo.FirstAider = reader.get(headers[5]); // first
																			// aider
						surgeryInfo.Anesthesia = reader.get(headers[6]); // anesthesia
						surgeryInfo.Place = reader.get(headers[7]); // place

						// add info to grow tree
						_activity.Root.GrowTreeWithInfo(surgeryInfo);
					}

					// close csv reading file
					reader.close();
				} catch (FileNotFoundException e) {
					//Log.e("Error", "Config file not found.");
				} catch (IOException e) {
					//Log.e("Error", "Config file IO error.");
				}

				_activity.SaveMedLogConfig();
			}
		});

		// set export data base button
		Button exportDbBtn = (Button) getView().findViewById(
				R.id.exportDataBase);
		exportDbBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {

				//Log.e("Trace Export", "Exporting data");

				CsvWriter writer = null;
				boolean success = false;
				try {

					//Log.e("Trace Export", "Trying to open file");

					writer = new CsvWriter(_activity.ExportFile);

					//Log.e("Trace Export", "Writing headers");

					// Write descriptive headers.
					String[] headers = { "process number", "surgery number",
							"date", "type", "subtype", "subsubtype",
							"pathology", "intervention", "first aider",
							"anesthesia", "place", "urgency", "notes" };
					writer.writeRecord(headers);

					//Log.e("Trace Export", "Writing records...");
					
					// write all the records to CSV
					String[] record = new String[headers.length];
					for (MedLogInfo log : _activity.DataBase) {
						record[0] = log.ProcessNumber.toString();
						record[1] = log.SurgeryNumber.toString();
						record[2] = log.GetFormatedDate();
						record[3] = log.Type;
						record[4] = log.SubType;
						record[5] = log.SubSubType;
						record[6] = log.Pathology;
						record[7] = log.Intervention;
						record[8] = log.FirstAider;
						record[9] = log.Anesthesia;
						record[10] = log.Place;
						record[11] = _activity.getString(log.Urgency == true ? R.string.yes_str : R.string.no_str);
						record[12] = log.Notes;
						writer.writeRecord(record);
					}

					String message = _activity.getString(R.string.exportMessage_str)
							+ "'"
							+ _activity.ExportFile + "'";
					_activity.ShowMessage(message);

					//Log.e("Trace Export", "Done");

					success = true;
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					writer.close();
					if (success == false) {
						_activity.ShowMessage(R.string.exportMessageError_str);
					}
				}
			}
		});

		super.onActivityCreated(savedInstanceState);
	}
}