|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * Copyright © 2024 Landeshauptstadt München | it@M |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | + * of this software and associated documentation files (the "Software"), to deal |
| 7 | + * in the Software without restriction, including without limitation the rights |
| 8 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | + * copies of the Software, and to permit persons to whom the Software is |
| 10 | + * furnished to do so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in |
| 13 | + * all copies or substantial portions of the Software. |
| 14 | + * |
| 15 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | + * THE SOFTWARE. |
| 22 | + */ |
| 23 | +package de.muenchen.mobidam.mdl; |
| 24 | + |
| 25 | +import com.opencsv.CSVWriter; |
| 26 | +import java.io.FileWriter; |
| 27 | +import java.io.IOException; |
| 28 | +import java.lang.reflect.Field; |
| 29 | +import java.util.List; |
| 30 | +import lombok.AllArgsConstructor; |
| 31 | +import lombok.extern.log4j.Log4j2; |
| 32 | +import org.apache.camel.Exchange; |
| 33 | +import org.apache.camel.Processor; |
| 34 | +import org.springframework.stereotype.Component; |
| 35 | + |
| 36 | +@Log4j2 |
| 37 | +@Component |
| 38 | +@AllArgsConstructor |
| 39 | +/* |
| 40 | + * TODO: |
| 41 | + * This class is the first attempt to process the MDS ModelsVehicle objects to a CSV file |
| 42 | + * with the possibility to define the fields the should be included. |
| 43 | + * The first next step should be to implement this processor into the EAI route, as the current |
| 44 | + * version is meant for local testing. |
| 45 | + * Further criteria could be found in the ticket MDAS-1583. |
| 46 | + * TODO: |
| 47 | + * If the project is continued, it would be worth considering how the CSV conversion |
| 48 | + * can be implemented with Camel Bindy and without the intermediate step of file output and |
| 49 | + * instead in-memory. |
| 50 | + */ |
| 51 | +public class ProcessToCsv implements Processor { |
| 52 | + |
| 53 | + private static final String[] INCLUDED_FIELDS = { |
| 54 | + "provider_id", "device_id", "vehicle_id", "vehicle_type", |
| 55 | + "propulsion_types", "accessibility_attributes", "battery_capacity", "maximum_speed" |
| 56 | + }; |
| 57 | + |
| 58 | + private final String outputFilePath = "."; |
| 59 | + |
| 60 | + @Override |
| 61 | + public void process(Exchange exchange) throws Exception { |
| 62 | + List<?> vehicles = exchange.getIn().getBody(List.class); |
| 63 | + |
| 64 | + if (vehicles == null || vehicles.isEmpty()) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + try (CSVWriter writer = new CSVWriter(new FileWriter(outputFilePath))) { |
| 69 | + // Write CSV header |
| 70 | + writer.writeNext(INCLUDED_FIELDS); |
| 71 | + |
| 72 | + for (Object vehicle : vehicles) { |
| 73 | + Class<?> clazz = vehicle.getClass(); |
| 74 | + String[] row = new String[INCLUDED_FIELDS.length]; |
| 75 | + |
| 76 | + for (int i = 0; i < INCLUDED_FIELDS.length; i++) { |
| 77 | + try { |
| 78 | + Field field = clazz.getDeclaredField(INCLUDED_FIELDS[i]); |
| 79 | + field.setAccessible(true); |
| 80 | + Object value = field.get(vehicle); |
| 81 | + row[i] = value != null ? value.toString() : ""; |
| 82 | + } catch (NoSuchFieldException e) { |
| 83 | + row[i] = ""; // Field not found, leave empty |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + writer.writeNext(row); |
| 88 | + } |
| 89 | + } catch (IOException e) { |
| 90 | + throw new RuntimeException("Fehler beim Schreiben der CSV-Datei", e); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments