|
37 | 37 | import org.springframework.dao.DuplicateKeyException;
|
38 | 38 | import java.util.Date;
|
39 | 39 | import javax.servlet.http.HttpSession;
|
40 |
| -// import java.util.logging.Logger; |
41 |
| -// import java.util.logging.Level; |
| 40 | +// improt MultipartFile class |
| 41 | +import org.springframework.web.multipart.MultipartFile; |
| 42 | +// import portMaooing class |
| 43 | +import org.springframework.web.bind.annotation.GetMapping; |
| 44 | +import org.springframework.web.bind.annotation.PostMapping; |
| 45 | +// bufferedReader |
| 46 | +import java.io.BufferedReader; |
| 47 | +import java.io.InputStreamReader; |
| 48 | +import java.util.ArrayList; |
| 49 | +import java.time.format.DateTimeFormatter; |
| 50 | +import java.time.LocalDateTime; |
| 51 | +import java.time.ZoneId; |
42 | 52 |
|
43 | 53 | @EnableJpaRepositories(basePackages = "net.codejava")
|
44 | 54 | @Controller
|
@@ -207,5 +217,56 @@ public void exportToCSV(HttpServletResponse response) throws IOException {
|
207 | 217 | writer.newLine();
|
208 | 218 | }
|
209 | 219 | writer.flush();
|
210 |
| -} |
| 220 | + } |
| 221 | + |
| 222 | + @PostMapping("/import") |
| 223 | + public String uploadFile(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) { |
| 224 | + try { |
| 225 | + // Parse the file |
| 226 | + BufferedReader reader = new BufferedReader(new InputStreamReader(file.getInputStream())); |
| 227 | + |
| 228 | + // Validate the header |
| 229 | + String line = reader.readLine(); // Read the first line |
| 230 | + if (line == null || !line.equals("Serial Number,Item Name,Amount,Quantity,Date")) { |
| 231 | + throw new IllegalArgumentException("Invalid header. Expected 'Serial Number,Item Name,Amount,Quantity,Date'"); |
| 232 | + } |
| 233 | + |
| 234 | + // Validate the rest of the file |
| 235 | + while ((line = reader.readLine()) != null) { |
| 236 | + String[] fields = line.split(","); |
| 237 | + if (fields.length != 5) { |
| 238 | + throw new IllegalArgumentException("Invalid line. Each line should contain exactly 5 fields separated by commas."); |
| 239 | + } |
| 240 | + } |
| 241 | + |
| 242 | + // If the file format is valid, convert it to a list of Sale objects |
| 243 | + List<Sale> sales = new ArrayList<>(); |
| 244 | + reader = new BufferedReader(new InputStreamReader(file.getInputStream())); // Reset the reader |
| 245 | + reader.readLine(); // Skip the header |
| 246 | + while ((line = reader.readLine()) != null) { |
| 247 | + String[] fields = line.split(","); |
| 248 | + Sale sale = new Sale(); |
| 249 | + sale.setSerialNumber(fields[0].trim()); |
| 250 | + sale.setItem(fields[1].trim()); |
| 251 | + |
| 252 | + // Convert LocalDate to Date |
| 253 | + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
| 254 | + LocalDate date = LocalDate.parse(fields[4].trim(), formatter); |
| 255 | + Date sqlDate = Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant()); |
| 256 | + sale.setDate(sqlDate); |
| 257 | + |
| 258 | + sale.setAmount((float) Double.parseDouble(fields[2].trim())); |
| 259 | + sale.setQuantity(Integer.parseInt(fields[3].trim())); |
| 260 | + sales.add(sale); |
| 261 | + } |
| 262 | + |
| 263 | + // If the file format is valid, call the upload method |
| 264 | + dao.saveAll(sales); |
| 265 | + redirectAttributes.addFlashAttribute("message", "Successfully saved the list of Sale objects to the database"); |
| 266 | + System.out.println("Successfully saved the list of Sale objects to the database"); |
| 267 | + } catch (Exception e) { |
| 268 | + System.out.println("Error calling dao.saveAll(sales): " + e.getMessage()); |
| 269 | + } |
| 270 | + return "redirect:/"; |
| 271 | + } |
211 | 272 | }
|
0 commit comments