nc.noumea.mairie.appock.services.impl.ServiceServiceImpl.java Source code

Java tutorial

Introduction

Here is the source code for nc.noumea.mairie.appock.services.impl.ServiceServiceImpl.java

Source

package nc.noumea.mairie.appock.services.impl;

/*-
 * #%L
 * Logiciel de Gestion des approvisionnements et des stocks des fournitures administratives de la Mairie de Nouma
 * %%
 * Copyright (C) 2017 Mairie de Nouma, Nouvelle-Caldonie
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Transactional;

import nc.noumea.mairie.appock.core.services.impl.GenericServiceImpl;
import nc.noumea.mairie.appock.core.utility.AppockUtil;
import nc.noumea.mairie.appock.entity.Service;
import nc.noumea.mairie.appock.repositories.ServiceRepository;
import nc.noumea.mairie.appock.services.ServiceService;

@org.springframework.stereotype.Service("serviceService")
@Scope(value = "singleton", proxyMode = ScopedProxyMode.TARGET_CLASS)
@Transactional
public class ServiceServiceImpl extends GenericServiceImpl<Service> implements ServiceService {

    @Autowired
    ServiceRepository serviceRepository;

    @Override
    public CrudRepository getRepository() {
        return serviceRepository;
    }

    @Override
    public Service findOne(Long id) {
        Service service = serviceRepository.findOne(id);
        chargeLazy(service);

        return service;
    }

    @Override
    public Service findOneAndChargeElementStock(Long id) {
        Service service = findOne(id);
        if (service.getStock() != null) {
            AppockUtil.chargeCollection(service.getStock().getListeArticleStock());
            AppockUtil.chargeCollection(service.getStock().getListeMouvementStock());
        }

        return service;
    }

    @Override
    public List<Service> findAllWithActifInactifOrderByLibelle(boolean avecNull) {
        List<Service> result = serviceRepository.findAllByOrderByLibelle();
        List<Service> listeServiceActif = new ArrayList<>();
        List<Service> listeServiceInactif = new ArrayList<>();
        for (Service service : result) {
            chargeLazy(service);
            if (service.isActif()) {
                listeServiceActif.add(service);
            } else {
                listeServiceInactif.add(service);
            }
        }
        result.clear();

        Comparator serviceComparator = Comparator
                .comparing((Service service) -> service.getDirection().getPole().getLibelle())
                .thenComparing(service -> service.getDirection().getLibelleCourt())
                .thenComparing(service -> service.getLibelleCourt());

        Collections.sort(listeServiceActif, serviceComparator);
        Collections.sort(listeServiceInactif, serviceComparator);

        result.addAll(listeServiceActif);
        result.addAll(listeServiceInactif);

        if (avecNull) {
            result.add(0, null);
        }

        return result;
    }

    @Override
    public List<Service> findAllActif() {
        List<Service> result = serviceRepository.findAllByActifOrderByLibelle(true);
        result.forEach(this::chargeLazy);

        return result;
    }

    private void chargeLazy(Service service) {
        AppockUtil.chargeElement(service.getDirection());
        AppockUtil.chargeElement(service.getDirection().getPole());
    }

    @Override
    public Service gereDesactivationService(Service service) {
        service.setActif(false);
        return service;
    }
}