io.cfp.service.TalkAdminService.java Source code

Java tutorial

Introduction

Here is the source code for io.cfp.service.TalkAdminService.java

Source

/*
 * Copyright (c) 2016 BreizhCamp
 * [http://breizhcamp.org]
 *
 * This file is part of CFP.io.
 *
 * CFP.io is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

package io.cfp.service;

import io.cfp.dto.EventSched;
import io.cfp.dto.TalkAdmin;
import io.cfp.entity.Event;
import io.cfp.entity.Rate;
import io.cfp.entity.Talk;
import io.cfp.entity.User;
import io.cfp.repository.RateRepo;
import io.cfp.repository.TalkRepo;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.*;
import static org.apache.commons.collections4.CollectionUtils.isNotEmpty;

/**
 * Service for managing talks by the admins
 */
@Service
@Transactional
public class TalkAdminService {

    @Autowired
    private TalkRepo talkRepo;

    @Autowired
    private RateRepo rateRepo;

    @Autowired
    private MapperFacade mapper;

    /**
     * Retrieve all talks
     *
     * @param states List of states the talk must be
     * @return List of talks
     */
    public List<TalkAdmin> findAll(int userId, Talk.State... states) {
        List<TalkAdmin> talks = talkRepo.findByEventIdAndStatesFetch(Event.current(), Arrays.asList(states))
                .stream().map(TalkAdmin::new).collect(Collectors.toList());

        List<Rate> rates = rateRepo.findAllFetchAdmin(Event.current());

        Map<Integer, List<Rate>> reviewed = rates.stream().filter(r -> userId == r.getAdminUser().getId())
                .collect(groupingBy(r -> r.getTalk().getId()));

        Map<Integer, Double> averages = rates.stream().filter(r -> r.getRate() > 0)
                .collect(groupingBy(r -> r.getTalk().getId(), averagingInt(Rate::getRate)));

        Map<Integer, List<String>> voters = rates.stream()
                .collect(groupingBy(r -> r.getTalk().getId(), mapping(r -> r.getAdminUser().getEmail(), toList())));

        for (TalkAdmin talk : talks) {
            int talkId = talk.getId();

            talk.setReviewed(reviewed.get(talkId) != null);
            talk.setMean(averages.get(talkId));
            talk.setVoteUsersEmail(voters.get(talkId));
        }
        return talks;
    }

    /**
     * Delete a talk
     *
     * @param talkId Id of the talk to delete
     * @return Deleted talk
     */
    public TalkAdmin delete(int talkId) {
        Talk talk = talkRepo.findByIdAndEventId(talkId, Event.current());
        TalkAdmin deleted = mapper.map(talk, TalkAdmin.class);
        talkRepo.delete(talk);
        return deleted;
    }

    /**
     * Export talks list into sched.org format
     * @param states State list to export
     * @return DTO in sched format
     */
    public List<EventSched> exportSched(Talk.State... states) {
        return talkRepo.findByEventIdAndStatesFetch(Event.current(), Arrays.asList(states)).stream()
                .map(t -> new EventSched().toBuilder().id(String.valueOf(t.getId())).name(t.getName())
                        .description(t.getDescription()).speakers(buildSpeakersList(t)).language(t.getLanguage())
                        .eventType(t.getTrack().getLibelle()).format(t.getFormat().getName()).build())
                .collect(toList());
    }

    /**
     * Build the name of the speakers as Speaker FirstName Lastname + Cospeakers
     * @param t Talk to build the speakers
     * @return Speakers list (ex: John Doe, Jack Bauer)
     */
    private String buildSpeakersList(Talk t) {
        String res = t.getUser().getFirstname() + " " + t.getUser().getLastname();
        if (isNotEmpty(t.getCospeakers())) {
            for (User user : t.getCospeakers()) {
                res += ", " + user.getFirstname() + " " + user.getLastname();
            }
        }
        return res;
    }
}