1 package org.wcb.resources;
2
3 import org.wcb.model.vo.LogImportVO;
4 import org.wcb.model.vo.hibernate.Logbook;
5 import org.wcb.exception.ServiceException;
6
7 import java.io.Reader;
8 import java.io.IOException;
9 import java.util.Date;
10 import java.util.List;
11 import java.util.ArrayList;
12 import java.text.SimpleDateFormat;
13 import java.text.ParseException;
14
15 import com.Ostermiller.util.CSVParser;
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 public class ImportLogbookDataFile {
43
44 public List<Logbook> process(Reader sValue, LogImportVO vo) throws IOException, ServiceException {
45 String[][] result = CSVParser.parse(sValue, ',');
46 return this.marshall(result, vo);
47 }
48
49 private List<Logbook> marshall(String[][] result, LogImportVO vo) throws ServiceException {
50 String[] lineItem;
51 List<Logbook> logbookEntries = new ArrayList<Logbook>();
52 for (int i = 0; i < result.length; i++) {
53 lineItem = result[i];
54 if (lineItem.length >= vo.getLength()) {
55 logbookEntries.add(processLine(lineItem, vo));
56 }
57 else {
58 throw new ServiceException("Line " + i + " could not be processed!");
59 }
60 }
61 return logbookEntries;
62 }
63
64 private Logbook processLine(String[] line, LogImportVO vo) throws ServiceException {
65 Logbook lineItem = new Logbook();
66 if (!isNegative(vo.getEntryDatePosition())) {
67 lineItem.setEntryDate(parseDate(line[vo.getEntryDatePosition()]));
68 }
69 if (!isNegative(vo.getRegistrationPosition())) {
70 lineItem.setRegistration(line[vo.getRegistrationPosition()]);
71 }
72 if (!isNegative(vo.getFAAFromPosition())) {
73 lineItem.setFaaFrom(line[vo.getFAAFromPosition()]);
74 }
75 if (!isNegative(vo.getViaPosition())) {
76 lineItem.setFaaVia(line[vo.getViaPosition()]);
77 }
78 if (!isNegative(vo.getFAAToPosition())) {
79 lineItem.setFaaTo(line[vo.getFAAToPosition()]);
80 }
81 if (!isNegative(vo.getFlightDurationPosition())) {
82 lineItem.setFlightDuration(parseDouble(line[vo.getFlightDurationPosition()]));
83 }
84 if (!isNegative(vo.getPICPosition())) {
85 lineItem.setPic(parseDouble(line[vo.getPICPosition()]));
86 }
87 if (!isNegative(vo.getSICPosition())) {
88 lineItem.setSic(parseDouble(line[vo.getSICPosition()]));
89 }
90 if (!isNegative(vo.getFlightInstructingPosition())) {
91 lineItem.setFlightInstructing(parseDouble(line[vo.getFlightInstructingPosition()]));
92 }
93 if (!isNegative(vo.getCrossCountryPosition())) {
94 lineItem.setCrossCountry(parseDouble(line[vo.getCrossCountryPosition()]));
95 }
96 if (!isNegative(vo.getSafetyPilotPosition())) {
97 lineItem.setSafetyPilot(parseDouble(line[vo.getSafetyPilotPosition()]));
98 }
99 if (!isNegative(vo.getDualReceivedPosition())) {
100 lineItem.setDualReceived(parseDouble(line[vo.getDualReceivedPosition()]));
101 }
102 if (!isNegative(vo.getSoloPosition())) {
103 lineItem.setSolo(parseDouble(line[vo.getSoloPosition()]));
104 }
105 if (!isNegative(vo.getDayTakoffPosition())) {
106 lineItem.setDayTakeoffs(parseInteger(line[vo.getDayTakoffPosition()]));
107 }
108 if (!isNegative(vo.getDayLandingPosition())) {
109 lineItem.setDayLandings(parseInteger(line[vo.getDayLandingPosition()]));
110 }
111 if (!isNegative(vo.getNightTakeoffPosition())) {
112 lineItem.setNightTakeoffs(parseInteger(line[vo.getNightTakeoffPosition()]));
113 }
114 if (!isNegative(vo.getNightLandingPosition())) {
115 lineItem.setNightLandings(parseInteger(line[vo.getNightLandingPosition()]));
116 }
117 if (!isNegative(vo.getInstrumentApproachesPosition())) {
118 lineItem.setInstrumentApproaches(parseInteger(line[vo.getInstrumentApproachesPosition()]));
119 }
120 if (!isNegative(vo.getConditionNightPosition())) {
121 lineItem.setConditionNight(parseDouble(line[vo.getConditionNightPosition()]));
122 }
123 if (!isNegative(vo.getActualIMCPosition())) {
124 lineItem.setConditionActualImc(parseDouble(line[vo.getActualIMCPosition()]));
125 }
126 if (!isNegative(vo.getSimulatedIMCPosition())) {
127 lineItem.setConditionSimulatedImc(parseDouble(line[vo.getSimulatedIMCPosition()]));
128 }
129 if (!isNegative(vo.getFlightSimPosition())) {
130 lineItem.setConditionFlightSim(parseDouble(line[vo.getFlightSimPosition()]));
131 }
132
133 return lineItem;
134 }
135
136 private boolean isNegative(int value) {
137 return value < 0;
138 }
139
140 private Integer parseInteger(String sVal) {
141 try
142 {
143 return Integer.parseInt(sVal);
144 }
145 catch (NumberFormatException nfe) {
146 return 0;
147 }
148 }
149
150 private Double parseDouble(String sVal) {
151 try
152 {
153 return Double.parseDouble(sVal);
154 }
155 catch (NumberFormatException nfe) {
156 return 0.0;
157 }
158
159 }
160
161 private Date parseDate(String sValue) {
162 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-M-dd");
163 try {
164 return sdf.parse(sValue);
165 }
166 catch (ParseException pse) {
167 return new Date();
168 }
169 }
170 }