<commit_before>package com.oracle.poco.bbhelper;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import com.fasterxml.jackson.databind.JsonNode;
import com.oracle.poco.bbhelper.exception.BbhelperBeehive4jException;
import com.oracle.poco.bbhelper.exception.BbhelperException;
import com.oracle.poco.bbhelper.exception.BbhelperUnauthorizedException;
import com.oracle.poco.bbhelper.exception.ErrorDescription;
import com.oracle.poco.bbhelper.model.Invitation;
import com.oracle.poco.bbhelper.model.Person;
import com.oracle.poco.bbhelper.model.Resource;

import jp.gr.java_conf.hhayakawa_jp.beehive4j.BeehiveApiDefinitions;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.BeehiveContext;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.BeehiveResponse;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.InvtCreateInvoker;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.InvtListByRangeInvoker;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.InvtReadBatchInvoker;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.InvtReadInvoker;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.MyWorkspaceInvoker;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.exception.BeehiveApiFaultException;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.model.BeeId;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.model.BeeIdList;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.model.CalendarRange;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.model.MeetingCreator;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.model.MeetingParticipantUpdater;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.model.MeetingParticipantUpdaterOperation;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.model.MeetingUpdater;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.model.OccurrenceParticipantStatus;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.model.OccurrenceStatus;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.model.OccurrenceType;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.model.Priority;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.model.Transparency;

class Session {

    @Autowired
    private ResourceCache resourceCache;

    @Autowired
    private ApplicationProperties properties;

    private long lastUsed;

    private final BeehiveContext context;

    private String calendar_id = null;

    Session(BeehiveContext context) {
        super();
        this.lastUsed = System.currentTimeMillis();
        this.context = context;
    }

    boolean isActive() {
        return (System.currentTimeMillis() - lastUsed) < properties.getSessionTimeout();
    }

    private void update() {
        lastUsed = System.currentTimeMillis();
    }

    Collection<Invitation> listConflictedInvitaitons(
            ZonedDateTime start, ZonedDateTime end, FloorCategory floorCategory)
                    throws BbhelperException {
        // TODO update()が新規メソッドでも確実に実行されるような工夫が必要
        update();
        List<String> calendar_ids = resourceCache.getCalendarIds(floorCategory);
        List<String> invitation_ids = new ArrayList<String>();
        List<BbhelperException> bbhe = new ArrayList<BbhelperException>();
        calendar_ids.stream().parallel().forEach(c -> {
            CalendarRange range = new CalendarRange.Builder()
                    .beeId(new BeeId.Builder().id(c).build())
                    .start(start)
                    .end(end)
                    .build();
            try {
                InvtListByRangeInvoker invoker = context.getInvoker(
                        BeehiveApiDefinitions.TYPEDEF_INVT_LIST_BY_RANGE);
                invoker.setRequestPayload(range);
                ResponseEntity<BeehiveResponse> response = invoker.invoke();
                BeehiveResponse body = response.getBody();
                if (body != null) {
                    Iterable<JsonNode> elements = body.getJson().get("elements");
                    if (elements != null) {
                        for (JsonNode element : elements) {
                            invitation_ids.add(getNodeAsText(element, "collabId", "id"));
                        }
                    }
                }
            } catch (BeehiveApiFaultException e) {
                if (HttpStatus.UNAUTHORIZED.equals(e.getHttpStatus())) {
                    bbhe.add(new BbhelperUnauthorizedException(
                            ErrorDescription.UNAUTORIZED, e, e.getHttpStatus()));
                } else {
                    bbhe.add(new BbhelperBeehive4jException(
                            ErrorDescription.BEEHIVE4J_FAULT, e, e.getHttpStatus()));
                }

            }
        });
        if (bbhe.size() > 0) { 
            throw bbhe.get(0);
        }

        if (invitation_ids.size() == 0) {
            return null;
        }
        List<BeeId> beeIds = new ArrayList<BeeId>(invitation_ids.size());
        invitation_ids.stream().forEach(i -> {
            beeIds.add(new BeeId.Builder().id(i).build());
        });

        BeeIdList beeIdList = new BeeIdList(beeIds);
        ResponseEntity<BeehiveResponse> response = null;
        try {
            InvtReadBatchInvoker invoker = context.getInvoker(
                    BeehiveApiDefinitions.TYPEDEF_INVT_READ_BATCH);
            invoker.setRequestPayload(beeIdList);
            response = invoker.invoke();
        } catch (BeehiveApiFaultException e) {
            BbhelperException be = null;
            if (HttpStatus.UNAUTHORIZED.equals(e.getHttpStatus())) {
                be = new BbhelperUnauthorizedException(
                        ErrorDescription.UNAUTORIZED, e, e.getHttpStatus());
            } else {
                be = new BbhelperBeehive4jException(
                        ErrorDescription.BEEHIVE4J_FAULT, e, e.getHttpStatus());
            }
            throw be;
        }
        BeehiveResponse body = response.getBody();
        if (body == null) {
            return null;
        }
        return parseInvtReadBatchResult(body.getJson());
    }

    private List<Invitation> parseInvtReadBatchResult(JsonNode node) {
        Iterable<JsonNode> elements = node.get("elements");
        if (elements == null) {
            return null;
        }
        List<Invitation> retval = new ArrayList<Invitation>();
        for (JsonNode element : elements) {
            retval.add(parseInvitationJsonNode(element));
        }
        return retval;
    }

    Invitation createInvitaion(Invitation invitation) throws BbhelperException {
        // TODO update()が新規メソッドでも確実に実行されるような工夫が必要
        update();
        if (calendar_id == null || calendar_id.length() == 0) {
            calendar_id = getDefaultCalendar();
        }
        // BeeId of user's calendar
        BeeId calendar = new BeeId.Builder().id(calendar_id).build();
        // MeetingUpdater
        Resource resource =
                resourceCache.getResource(invitation.getResource_id());
        List<MeetingParticipantUpdater> participantUpdaters = 
                new ArrayList<MeetingParticipantUpdater>(1);
        BeeId resourceCalendarId = new BeeId.Builder()
                .id(resource.getCalendar_id())
                .build();
        participantUpdaters.add(new MeetingParticipantUpdater.Builder()
                .operation(MeetingParticipantUpdaterOperation.ADD)
                .beeId(resourceCalendarId)
                .build());
        MeetingUpdater meetingUpdater = new MeetingUpdater.Builder()
                .name(invitation.getName())
                .start(invitation.getStart())
                .end(invitation.getEnd())
                .status(OccurrenceStatus.TENTATIVE)
                .participantUpdaters(participantUpdaters)
                .locationName(resource.getName())
                .includeOnlineConference(false)
                .inviteeParticipantStatus(OccurrenceParticipantStatus.ACCEPTED)
                .inviteePriority(Priority.MEDIUM)
                .inviteeTransparency(Transparency.TRANSPARENT)
                .build();
        MeetingCreator meetingCreater = new MeetingCreator.Builder()
                .calendar(calendar)
                .meetingUpdater(meetingUpdater)<commit_msg>会議室予約に失敗するバグを修正<commit_after>package com.oracle.poco.bbhelper;

import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import com.fasterxml.jackson.databind.JsonNode;
import com.oracle.poco.bbhelper.exception.BbhelperBeehive4jException;
import com.oracle.poco.bbhelper.exception.BbhelperException;
import com.oracle.poco.bbhelper.exception.BbhelperUnauthorizedException;
import com.oracle.poco.bbhelper.exception.ErrorDescription;
import com.oracle.poco.bbhelper.model.Invitation;
import com.oracle.poco.bbhelper.model.Person;
import com.oracle.poco.bbhelper.model.Resource;

import jp.gr.java_conf.hhayakawa_jp.beehive4j.BeehiveApiDefinitions;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.BeehiveContext;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.BeehiveResponse;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.InvtCreateInvoker;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.InvtListByRangeInvoker;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.InvtReadBatchInvoker;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.InvtReadInvoker;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.MyWorkspaceInvoker;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.exception.BeehiveApiFaultException;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.model.BeeId;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.model.BeeIdList;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.model.CalendarRange;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.model.MeetingCreator;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.model.MeetingParticipantUpdater;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.model.MeetingParticipantUpdaterOperation;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.model.MeetingUpdater;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.model.OccurrenceParticipantStatus;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.model.OccurrenceStatus;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.model.OccurrenceType;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.model.Priority;
import jp.gr.java_conf.hhayakawa_jp.beehive4j.model.Transparency;

class Session {

    @Autowired
    private ResourceCache resourceCache;

    @Autowired
    private ApplicationProperties properties;

    private long lastUsed;

    private final BeehiveContext context;

    private String calendar_id = null;

    Session(BeehiveContext context) {
        super();
        this.lastUsed = System.currentTimeMillis();
        this.context = context;
    }

    boolean isActive() {
        return (System.currentTimeMillis() - lastUsed) < properties.getSessionTimeout();
    }

    private void update() {
        lastUsed = System.currentTimeMillis();
    }

    Collection<Invitation> listConflictedInvitaitons(
            ZonedDateTime start, ZonedDateTime end, FloorCategory floorCategory)
                    throws BbhelperException {
        // TODO update()が新規メソッドでも確実に実行されるような工夫が必要
        update();
        List<String> calendar_ids = resourceCache.getCalendarIds(floorCategory);
        List<String> invitation_ids = new ArrayList<String>();
        List<BbhelperException> bbhe = new ArrayList<BbhelperException>();
        calendar_ids.stream().parallel().forEach(c -> {
            CalendarRange range = new CalendarRange.Builder()
                    .beeId(new BeeId.Builder().id(c).build())
                    .start(start)
                    .end(end)
                    .build();
            try {
                InvtListByRangeInvoker invoker = context.getInvoker(
                        BeehiveApiDefinitions.TYPEDEF_INVT_LIST_BY_RANGE);
                invoker.setRequestPayload(range);
                ResponseEntity<BeehiveResponse> response = invoker.invoke();
                BeehiveResponse body = response.getBody();
                if (body != null) {
                    Iterable<JsonNode> elements = body.getJson().get("elements");
                    if (elements != null) {
                        for (JsonNode element : elements) {
                            invitation_ids.add(getNodeAsText(element, "collabId", "id"));
                        }
                    }
                }
            } catch (BeehiveApiFaultException e) {
                if (HttpStatus.UNAUTHORIZED.equals(e.getHttpStatus())) {
                    bbhe.add(new BbhelperUnauthorizedException(
                            ErrorDescription.UNAUTORIZED, e, e.getHttpStatus()));
                } else {
                    bbhe.add(new BbhelperBeehive4jException(
                            ErrorDescription.BEEHIVE4J_FAULT, e, e.getHttpStatus()));
                }

            }
        });
        if (bbhe.size() > 0) { 
            throw bbhe.get(0);
        }

        if (invitation_ids.size() == 0) {
            return null;
        }
        List<BeeId> beeIds = new ArrayList<BeeId>(invitation_ids.size());
        invitation_ids.stream().forEach(i -> {
            beeIds.add(new BeeId.Builder().id(i).build());
        });

        BeeIdList beeIdList = new BeeIdList(beeIds);
        ResponseEntity<BeehiveResponse> response = null;
        try {
            InvtReadBatchInvoker invoker = context.getInvoker(
                    BeehiveApiDefinitions.TYPEDEF_INVT_READ_BATCH);
            invoker.setRequestPayload(beeIdList);
            response = invoker.invoke();
        } catch (BeehiveApiFaultException e) {
            BbhelperException be = null;
            if (HttpStatus.UNAUTHORIZED.equals(e.getHttpStatus())) {
                be = new BbhelperUnauthorizedException(
                        ErrorDescription.UNAUTORIZED, e, e.getHttpStatus());
            } else {
                be = new BbhelperBeehive4jException(
                        ErrorDescription.BEEHIVE4J_FAULT, e, e.getHttpStatus());
            }
            throw be;
        }
        BeehiveResponse body = response.getBody();
        if (body == null) {
            return null;
        }
        return parseInvtReadBatchResult(body.getJson());
    }

    private List<Invitation> parseInvtReadBatchResult(JsonNode node) {
        Iterable<JsonNode> elements = node.get("elements");
        if (elements == null) {
            return null;
        }
        List<Invitation> retval = new ArrayList<Invitation>();
        for (JsonNode element : elements) {
            retval.add(parseInvitationJsonNode(element));
        }
        return retval;
    }

    Invitation createInvitaion(Invitation invitation) throws BbhelperException {
        // TODO update()が新規メソッドでも確実に実行されるような工夫が必要
        update();
        if (calendar_id == null || calendar_id.length() == 0) {
            calendar_id = getDefaultCalendar();
        }
        // BeeId of user's calendar
        BeeId calendar = new BeeId.Builder().id(calendar_id).build();
        // MeetingUpdater
        Resource resource =
                resourceCache.getResource(invitation.getResource_id());
        List<MeetingParticipantUpdater> participantUpdaters = 
                new ArrayList<MeetingParticipantUpdater>(1);
        BeeId resourceId = new BeeId.Builder()
                .id(resource.getResource_id())
                .build();
        participantUpdaters.add(new MeetingParticipantUpdater.Builder()
                .operation(MeetingParticipantUpdaterOperation.ADD)
                .beeId(resourceId)
                .build());
        MeetingUpdater meetingUpdater = new MeetingUpdater.Builder()
                .name(invitation.getName())
                .start(invitation.getStart())
                .end(invitation.getEnd())
                .status(OccurrenceStatus.TENTATIVE)
                .participantUpdaters(participantUpdaters)
                .locationName(resource.getName())
                .includeOnlineConference(false)
                .inviteeParticipantStatus(OccurrenceParticipantStatus.ACCEPTED)
                .inviteePriority(Priority.MEDIUM)
                .inviteeTransparency(Transparency.TRANSPARENT)
                .build();
        MeetingCreator meetingCreater = new MeetingCreator.Builder()
                .calendar(calendar)
                .meetingUpdater(meetingUpdater)
{'fraction_non_alphanumeric': 0.095078031212485, 'fraction_numerical': 0.003721488595438175, 'mean_word_length': 2.5127556398903645, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5901139', 'n_tokens_mistral': 5209, 'n_tokens_neox': 5039, 'n_words': 687}