Example usage for javax.json JsonArray getJsonObject

List of usage examples for javax.json JsonArray getJsonObject

Introduction

In this page you can find the example usage for javax.json JsonArray getJsonObject.

Prototype

JsonObject getJsonObject(int index);

Source Link

Document

Returns the object value at the specified position in this array.

Usage

From source file:org.optaplanner.examples.conferencescheduling.persistence.ConferenceSchedulingCfpDevoxxImporter.java

private void importTimeslotList() {
    List<Timeslot> timeslotList = new ArrayList<>();
    Map<Timeslot, List<Room>> timeslotToAvailableRoomsMap = new HashMap<>();
    Map<Pair<LocalDateTime, LocalDateTime>, Timeslot> startAndEndTimeToTimeslotMap = new HashMap<>();

    Long timeSlotId = 0L;/*  w w  w  .  j  ava 2 s.c o m*/
    String schedulesUrl = conferenceBaseUrl + "/schedules/";
    LOGGER.debug("Sending a request to: " + schedulesUrl);
    JsonArray daysArray = readJson(schedulesUrl, JsonReader::readObject).getJsonArray("links");
    for (int i = 0; i < daysArray.size(); i++) {
        JsonObject dayObject = daysArray.getJsonObject(i);
        String dayUrl = dayObject.getString("href");

        LOGGER.debug("Sending a request to: " + dayUrl);
        JsonArray daySlotsArray = readJson(dayUrl, JsonReader::readObject).getJsonArray("slots");

        for (int j = 0; j < daySlotsArray.size(); j++) {
            JsonObject timeslotObject = daySlotsArray.getJsonObject(j);

            LocalDateTime startDateTime = LocalDateTime.ofInstant(
                    Instant.ofEpochMilli(timeslotObject.getJsonNumber("fromTimeMillis").longValue()),
                    ZoneId.of(ZONE_ID));
            LocalDateTime endDateTime = LocalDateTime.ofInstant(
                    Instant.ofEpochMilli(timeslotObject.getJsonNumber("toTimeMillis").longValue()),
                    ZoneId.of(ZONE_ID));

            Room room = roomIdToRoomMap.get(timeslotObject.getString("roomId"));
            if (room == null) {
                throw new IllegalStateException("The timeslot (" + timeslotObject.getString("slotId")
                        + ") has a roomId (" + timeslotObject.getString("roomId")
                        + ") that does not exist in the rooms list");
            }

            // Assuming slotId is of format: tia_room6_monday_12_.... take only "tia"
            String talkTypeName = timeslotObject.getString("slotId").split("_")[0];
            TalkType timeslotTalkType = talkTypeNameToTalkTypeMap.get(talkTypeName);
            if (Arrays.asList(IGNORED_TALK_TYPES).contains(talkTypeName)) {
                continue;
            }

            Timeslot timeslot;
            if (startAndEndTimeToTimeslotMap.keySet().contains(Pair.of(startDateTime, endDateTime))) {
                timeslot = startAndEndTimeToTimeslotMap.get(Pair.of(startDateTime, endDateTime));
                timeslotToAvailableRoomsMap.get(timeslot).add(room);
                if (timeslotTalkType != null) {
                    timeslot.getTalkTypeSet().add(timeslotTalkType);
                }
            } else {
                timeslot = new Timeslot(timeSlotId++);
                timeslot.withStartDateTime(startDateTime).withEndDateTime(endDateTime)
                        .withTalkTypeSet(timeslotTalkType == null ? new HashSet<>()
                                : new HashSet<>(Arrays.asList(timeslotTalkType)));
                timeslot.setTagSet(new HashSet<>());

                timeslotList.add(timeslot);
                timeslotToAvailableRoomsMap.put(timeslot, new ArrayList<>(Arrays.asList(room)));
                startAndEndTimeToTimeslotMap.put(Pair.of(startDateTime, endDateTime), timeslot);
            }

            if (!timeslotObject.isNull("talk")) {
                scheduleTalk(timeslotObject, room, timeslot);
            }

            for (TalkType talkType : timeslot.getTalkTypeSet()) {
                talkType.getCompatibleTimeslotSet().add(timeslot);
            }
            timeslotTalkTypeToTotalMap.merge(talkTypeName, 1, Integer::sum);
        }
    }

    for (Room room : solution.getRoomList()) {
        room.setUnavailableTimeslotSet(timeslotList.stream()
                .filter(timeslot -> !timeslotToAvailableRoomsMap.get(timeslot).contains(room))
                .collect(Collectors.toSet()));
    }

    solution.setTimeslotList(timeslotList);
}