Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
clezag committed Jan 24, 2024
1 parent 5ff1890 commit e01ba5a
Show file tree
Hide file tree
Showing 31 changed files with 134 additions and 599 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package com.opendatahub.rest;
package com.opendatahub;

import java.util.Arrays;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package com.opendatahub.scheduler;
package com.opendatahub;

import java.io.File;
import java.io.FileInputStream;
Expand All @@ -22,8 +22,6 @@
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -33,28 +31,27 @@

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.opendatahub.constantsClasses.Constants;
import com.opendatahub.dto.AgencyValues;
import com.opendatahub.dto.CalendarValues;
import com.opendatahub.dto.Calendar_DatesValues;
import com.opendatahub.constants.SkyalpsAgency;
import com.opendatahub.dto.Data;
import com.opendatahub.dto.Flights;
import com.opendatahub.dto.RoutesValues;
import com.opendatahub.dto.Stop_TimesValues;
import com.opendatahub.dto.StopsValue;
import com.opendatahub.dto.TripsValues;
import com.opendatahub.rest.FlightsRest;
import com.opendatahub.service.GTFSCsvFile;
import com.opendatahub.service.GTFSFile;
import com.opendatahub.service.GTFSFolder;
import com.opendatahub.service.GTFSStop_Times;
import com.opendatahub.service.GTFSWriteAgency;
import com.opendatahub.service.GTFSWriteCalendar;
import com.opendatahub.service.GTFSWriteCalendar_Dates;
import com.opendatahub.service.GTFSWriteRoutes;
import com.opendatahub.service.GTFSWriteStops;
import com.opendatahub.service.GTFSWriteTrips;
import com.opendatahub.utils.S3FileUtil;
import com.opendatahub.file.GTFSCsvFile;
import com.opendatahub.file.GTFSFile;
import com.opendatahub.file.GTFSFolder;
import com.opendatahub.file.GTFSStop_Times;
import com.opendatahub.file.GTFSWriteAgency;
import com.opendatahub.file.GTFSWriteCalendar;
import com.opendatahub.file.GTFSWriteCalendar_Dates;
import com.opendatahub.file.GTFSWriteRoutes;
import com.opendatahub.file.GTFSWriteStops;
import com.opendatahub.file.GTFSWriteTrips;
import com.opendatahub.gtfs.AgencyValues;
import com.opendatahub.gtfs.CalendarValues;
import com.opendatahub.gtfs.Calendar_DatesValues;
import com.opendatahub.gtfs.RoutesValues;
import com.opendatahub.gtfs.Stop_TimesValues;
import com.opendatahub.gtfs.StopsValue;
import com.opendatahub.gtfs.Timepoint;
import com.opendatahub.gtfs.TripsValues;

import jakarta.annotation.PostConstruct;

Expand Down Expand Up @@ -82,6 +79,7 @@ private record Week(boolean mon, boolean tue, boolean wed, boolean thu, boolean

private static class Flight {
String name;
String id;
String origin;
String code;
String fullCode;
Expand Down Expand Up @@ -111,28 +109,23 @@ public void calculateGtfs() throws Exception {
GTFSFile.writeFiles();

gtfsCalendarDates.add(new Calendar_DatesValues(null, null));
AgencyValues agencySkyalps = new AgencyValues(Constants.agencyName, Constants.agencyName, new URL(Constants.agencyUrl), Constants.agencyTimeZone);
AgencyValues agencySkyalps = new AgencyValues(SkyalpsAgency.agencyName, SkyalpsAgency.agencyName, new URL(SkyalpsAgency.agencyUrl), SkyalpsAgency.agencyTimeZone);
gtfsAgencies.add(agencySkyalps);

String flightsJsonStr = new ObjectMapper().writerWithDefaultPrettyPrinter().writeValueAsString(flightDtos.getData()).toString();
LOG.debug("Result Flights: " + flightsJsonStr);
JSONArray flightsJson = new JSONArray(flightsJsonStr);

List<Flight> flights = new ArrayList<>();

var airports = gtfsCsvFile.getCSVFile();

for (int i = 0; i < flightsJson.length(); i++) {
JSONObject flightJson = flightsJson.getJSONObject(i);
Flight flight = mapFlightFromJson(flights, flightJson);
for (Data flightDto : flightDtos.getData()) {
Flight flight = mapFlightFromJson(flights, flightDto);

var calendar = mapCalendarValues(flight);
gtfsCalendar.put(calendar.getService_id(), calendar);

boolean outboundDirection = flight.fromDestination.equals("BZO");

gtfsTrips.add(new TripsValues(
flight.name,
flight.id,
calendar.getService_id(),
flight.fullCode,
outboundDirection ? 0 : 1)
Expand All @@ -152,7 +145,7 @@ public void calculateGtfs() throws Exception {
flight.departureTime,
flight.fromDestination,
1,
Stop_TimesValues.TIMEPOINT_EXACT)
Timepoint.exact)
);

gtfsStopTimes.add(new Stop_TimesValues(
Expand All @@ -161,10 +154,10 @@ public void calculateGtfs() throws Exception {
flight.arrivalTime,
flight.toDestination,
2,
Stop_TimesValues.TIMEPOINT_EXACT)
Timepoint.exact)
);

gtfsRoutes.add(new RoutesValues(flight.name, flight.name, RoutesValues.ROUTE_TYPE_AIR_SERVICE, agencySkyalps.agency_id()));
gtfsRoutes.add(new RoutesValues(flight.id, flight.name, RoutesValues.ROUTE_TYPE_AIR_SERVICE, agencySkyalps.agency_id()));
}

GTFSWriteAgency.writeAgency(gtfsAgencies);
Expand Down Expand Up @@ -214,31 +207,32 @@ private void uploadToS3() throws Exception {
LOG.info("Uploading files to S3 done.");
}

private Flight mapFlightFromJson(List<Flight> flights, JSONObject flightJson) {
private Flight mapFlightFromJson(List<Flight> flights, Data flightDto) {
Flight flight = new Flight();

flight.name = flightJson.getString(Constants.stationName).replaceAll("-", "_");
flight.name = flightDto.getSname().replace("-", " - ");
flight.id = flightDto.getSname().replace("-", "_");

flight.origin = flightJson.getString(Constants.stationOrigin);
flight.fullCode = flightJson.getString(Constants.stationCode);
flight.origin = flightDto.getSorigin();
flight.fullCode = flightDto.getScode();
flight.code = flight.fullCode.substring(7);

JSONObject metadata = flightJson.getJSONObject(Constants.stationMetadata);
var meta = flightDto.getSmetadata();

flight.week = new Week(
metadata.getBoolean(Constants.monday),
metadata.getBoolean(Constants.tuesday),
metadata.getBoolean(Constants.wednesday),
metadata.getBoolean(Constants.thursday),
metadata.getBoolean(Constants.friday),
metadata.getBoolean(Constants.saturday),
metadata.getBoolean(Constants.sunday)
meta.isWeekdaymon(),
meta.isWeekdaytue(),
meta.isWeekdaywed(),
meta.isWeekdaythu(),
meta.isWeekdayfri(),
meta.isWeekdaysat(),
meta.isWeekdaysun()
);

flight.arrivalTime = metadata.getString(Constants.metaSta);
flight.departureTime = metadata.getString(Constants.metaStd);
flight.toDestination = metadata.getString(Constants.metaToDestination);
flight.fromDestination = metadata.getString(Constants.metaFromDestination);
flight.arrivalTime = meta.getSta();
flight.departureTime = meta.getStd();
flight.toDestination = meta.getTodestination();
flight.fromDestination = meta.getFromdestination();
flights.add(flight);
return flight;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package com.opendatahub.utils;
package com.opendatahub;

import java.io.File;
import java.io.InputStream;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
Expand All @@ -14,16 +18,10 @@
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.TransferManagerBuilder;
import com.amazonaws.services.s3.transfer.Upload;

import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
* To upload files to an S3 bucket
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package com.opendatahub.constantsClasses;
package com.opendatahub.constants;

public class GTFSFileNames {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: NOI Techpark <[email protected]>
//
// SPDX-License-Identifier: AGPL-3.0-or-later

package com.opendatahub.constants;

public class SkyalpsAgency {
public static final String agencyName = "SkyAlps";
public static final String agencyUrl = "http://www.skyalps.com";
public static final String agencyTimeZone = "Europe/Rome";
public static final String agencyLang = "English";
}

This file was deleted.

Loading

0 comments on commit e01ba5a

Please sign in to comment.