nc.noumea.mairie.appock.viewmodel.CreateCatalogueViewModel.java Source code

Java tutorial

Introduction

Here is the source code for nc.noumea.mairie.appock.viewmodel.CreateCatalogueViewModel.java

Source

package nc.noumea.mairie.appock.viewmodel;

/*-
 * #%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.io.Serializable;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.zkoss.bind.annotation.*;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.select.annotation.VariableResolver;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zkplus.spring.DelegatingVariableResolver;

import nc.noumea.mairie.appock.core.viewmodel.AbstractCreateViewModel;
import nc.noumea.mairie.appock.entity.Catalogue;
import nc.noumea.mairie.appock.entity.Marche;
import nc.noumea.mairie.appock.enums.EtatCatalogue;
import nc.noumea.mairie.appock.repositories.CatalogueRepository;
import nc.noumea.mairie.appock.repositories.MarcheRepository;

@VariableResolver(DelegatingVariableResolver.class)
public class CreateCatalogueViewModel extends AbstractCreateViewModel<Catalogue> implements Serializable {
    private static final long serialVersionUID = 1L;

    @WireVariable
    CatalogueRepository catalogueRepository;

    @WireVariable
    MarcheRepository marcheRepository;

    @Override
    @AfterCompose
    public void afterCompose(@ContextParam(ContextType.VIEW) Component view) {
        super.afterCompose(view);
    }

    @Override
    @Init(superclass = true)
    public void init() throws InstantiationException, IllegalAccessException {
        entity = createEntity();
        if (catalogueRepository.findFirstByEtatCatalogue(EtatCatalogue.ACTIF) != null) {
            // Il existe dj un catalogue ACTIF
            entity.setEtatCatalogue(EtatCatalogue.EN_COURS_DE_PREPARATION);
        } else {
            entity.setEtatCatalogue(EtatCatalogue.ACTIF);
        }
    }

    /**
     * Vrifie que le libell est unique puis lance la sauvegarde gnrique
     */
    @Override
    @Command
    @NotifyChange({ "entity", "popupVisible" })
    public void save() {
        if (CollectionUtils.isNotEmpty(catalogueRepository.findAllByLibelle(entity.getLibelle()))) {
            showErrorPopup("Le libell " + entity.getLibelle() + " est dj utilis");
            return;
        }
        super.save();
    }

    /**
     * @return la liste des marchs proposs sur cration d'un catalogue
     */
    public List<Marche> getListeMarche() {
        List<Marche> result = marcheRepository.findAllByOrderByLibelle();
        result.add(0, null);
        return result;
    }
}