package nc.noumea.mairie.organigramme.viewmodel;
/*
* #%L
* Logiciel de Gestion des Organigrammes de la Ville de Nouméa
* $Id:$
* $HeadURL:$
* %%
* Copyright (C) 2015 Mairie de Nouméa
* %%
* 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.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import nc.noumea.mairie.organigramme.core.utility.DateUtil;
import nc.noumea.mairie.organigramme.core.viewmodel.AbstractPopupViewModel;
import nc.noumea.mairie.organigramme.core.ws.IAdsWSConsumer;
import nc.noumea.mairie.organigramme.dto.EntiteDto;
import nc.noumea.mairie.organigramme.enums.Statut;
import nc.noumea.mairie.organigramme.enums.Transition;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;
import org.zkoss.bind.BindUtils;
import org.zkoss.bind.annotation.AfterCompose;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.ContextParam;
import org.zkoss.bind.annotation.ContextType;
import org.zkoss.bind.annotation.GlobalCommand;
import org.zkoss.bind.annotation.Init;
import org.zkoss.bind.annotation.NotifyChange;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.select.Selectors;
import org.zkoss.zk.ui.select.annotation.VariableResolver;
import org.zkoss.zk.ui.select.annotation.WireVariable;
import org.zkoss.zkplus.spring.DelegatingVariableResolver;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;
@VariableResolver(DelegatingVariableResolver.class)
@Init(superclass = true)
public class PopupStatutWithRefAndDateViewModel extends AbstractPopupViewModel<EntiteDto> implements Serializable {
private static final long serialVersionUID = 1L;
@WireVariable
IAdsWSConsumer adsWSConsumer;
protected Transition transition;
private String refDeliberation;
private Date dateDeliberation;
private boolean majEntitesEnfant = false;
private String nfa;
private boolean entiteAS400;
@AfterCompose
public void afterCompose(@ContextParam(ContextType.VIEW) Component view) {
setPopup((Window) Selectors.iterable(view, "#popupStatutWithRefAndDate").iterator().next());
}
public Transition getTransition() {
return transition;
}
public void setTransition(Transition transition) {
this.transition = transition;
}
public String getRefDeliberation() {
return refDeliberation;
}
public void setRefDeliberation(String refDeliberation) {
this.refDeliberation = refDeliberation;
}
public Date getDateDeliberation() {
return dateDeliberation;
}
public void setDateDeliberation(Date dateDeliberation) {
this.dateDeliberation = dateDeliberation;
}
public boolean isMajEntitesEnfant() {
return majEntitesEnfant;
}
public void setMajEntitesEnfant(boolean majEntitesEnfant) {
this.majEntitesEnfant = majEntitesEnfant;
}
public String getNfa() {
return nfa;
}
public void setNfa(String nfa) {
this.nfa = nfa;
}
public boolean isEntiteAS400() {
return entiteAS400;
}
public void setEntiteAS400(boolean entiteAS400) {
this.entiteAS400 = entiteAS400;
}
@GlobalCommand
@NotifyChange("*")
public void ouvrirPopupStatutWithRefAndDate(@BindingParam("entity") EntiteDto entity, @BindingParam("transition") Transition transition) throws InstantiationException, IllegalAccessException {
// On va chercher en base la valeur de la NFA si elle est renseignée
EntiteDto entityBdd = adsWSConsumer.getEntite(entity.getIdEntite());
setEntity(entity);
if (transition.equals(Transition.ACTIF_APRES_PREVISION)) {
if (null != entityBdd.getRefDeliberationActif() && !"".equals(entityBdd.getRefDeliberationActif())) {
setRefDeliberation(entityBdd.getRefDeliberationActif());
}
if (null != entityBdd.getDateDeliberationActif()) {
setDateDeliberation(entityBdd.getDateDeliberationActif());
}
}
if (transition.equals(Transition.INACTIF_APRES_TRANSITOIRE)) {
if (null != entityBdd.getRefDeliberationInactif() && !"".equals(entityBdd.getRefDeliberationInactif())) {
setRefDeliberation(entityBdd.getRefDeliberationInactif());
}
if (null != entityBdd.getDateDeliberationInactif()) {
setDateDeliberation(entityBdd.getDateDeliberationInactif());
}
}
setNfa(entityBdd.getNfa());
setTransition(transition);
getPopup().doModal();
}
@Command
public void saveStatutWithRefAndDate() {
if (!validerChampObligatoire()) {
return;
}
entity.setNfa(nfa);
entity.setEntiteAs400(entiteAS400);
if (transition.getStatut() == Statut.ACTIF) {
entity.setDateDeliberationActif(dateDeliberation);
entity.setRefDeliberationActif(refDeliberation);
} else if (dateDeliberation != null && (transition.getStatut() == Statut.INACTIF || transition.getStatut() == Statut.TRANSITOIRE)) {
if (DateUtil.compare(dateDeliberation, entity.getDateDeliberationActif()) < 0) {
Messagebox.show(
"La date de délibération / CTP d'inactivation doit être supérieure ou égale à la " + "date de délibération / CTP d'activation ("
+ DateUtil.formatDate(entity.getDateDeliberationActif()) + ")", "Erreur", Messagebox.OK, Messagebox.EXCLAMATION);
return;
}
entity.setDateDeliberationInactif(dateDeliberation);
entity.setRefDeliberationInactif(refDeliberation);
}
Map<String, Object> args = new HashMap<>();
args.put("entity", entity);
args.put("transition", transition);
args.put("popup", getPopup());
args.put("majEntitesEnfant", majEntitesEnfant);
BindUtils.postGlobalCommand(null, null, "saveStatutWithRefAndDateGenerique", args);
}
private boolean validerChampObligatoire() {
List<String> listeMessageErreur = new ArrayList<String>();
if (transition.getStatut() == Statut.ACTIF) {
if (StringUtils.isBlank(nfa)) {
listeMessageErreur.add("La NFA est obligatoire");
}
}
if (transition.getStatut() != Statut.TRANSITOIRE) {
// Lors d'un passage en statut transitoire la date et la référence
// ne sont pas obligatoires
if (StringUtils.isBlank(refDeliberation)) {
listeMessageErreur.add("La référence de la délibération est obligatoire");
}
if (dateDeliberation == null) {
listeMessageErreur.add("La date de la délibération est obligatoire");
}
Date demain = new DateTime().plusDays(1).withHourOfDay(0).withMinuteOfHour(0).withMillisOfDay(0).toDate();
if (DateUtil.compare(demain, dateDeliberation) <= 0) {
listeMessageErreur.add("La date de délibération ne peut pas être postérieure à la date du jour");
}
if (!CollectionUtils.isEmpty(listeMessageErreur)) {
Messagebox.show(StringUtils.join(listeMessageErreur, "\n"), "Erreur", Messagebox.OK, Messagebox.ERROR);
return false;
}
}
return true;
}
public String getCssLabelAndDate() {
if (transition == null) {
return "";
}
if (transition.getStatut() == Statut.ACTIF || transition.getStatut() == Statut.INACTIF) {
return "mandatory";
}
return "";
}
}