controllers.BackOffice.java Source code

Java tutorial

Introduction

Here is the source code for controllers.BackOffice.java

Source

/*
 * Copyright(c) 2010 Les Zindeps.
 *
 * The code source of this project is distributed
 * under the Affero GPL GNU AFFERO GENERAL PUBLIC LICENSE
 * Version 3, 19 November 2007
 *
 * This file is part of project LesZindeps. The source code is
 * hosted on GitHub. The initial project was launched by
 * Nicolas Martignole.
 *
 * LesZindeps 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.
 *
 * LesZindeps 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.
 *
 *
 * Please see COPYING.AGPL.txt for the full text license
 * or online http://www.gnu.org/licenses/agpl.html
 */

package controllers;

import models.Propal;
import models.Zindep;
import org.joda.time.DateMidnight;
import play.data.validation.Valid;
import play.mvc.Before;
import play.mvc.Controller;

import java.util.List;

/**
 * Un controleur different pour surveiller l'arrire-boutique.
 * Mon ide est que ce controller n'est pas indispensable. Il sert le temps de la mise en route du projet.
 * Ensuite, il faut que le reste des outils se suffisent  eux-mmes.
 *
 * @author Nicolas Martignole
 * @since 21 dc. 2010 14:21:41
 */
public class BackOffice extends Controller {

    /**
     * Methode appele  chaque fois pour vrifier si l'utilisateur est authentifi ou non.
     */
    @Before(unless = { "login" })
    static void checkLogin() {
        if (!session.contains("zindepId")) {
            flash.error("Merci de vous authentifier pour accder  cette partie.");
            Admin.index();
        }
    }

    /**
     * Affiche la page d'accueil.
     */
    public static void index() {
        render();
    }

    /**
     * Affiche la page de cration d'un indep.
     */
    public static void newZindep() {
        render();
    }

    /**
     * Valide et sauvegarde un nouvel indpendant
     *
     * @param zindep est le nouvel indpendant
     */
    public static void storeNewZindep(@Valid Zindep zindep) {
        // Handle errors
        if (validation.hasErrors()) {
            render("@newZindep", zindep);
        }

        Zindep existing = Zindep.findByMail(zindep.email);
        if (existing != null) {
            flash.error("Attention, un compte avec cet email existe dj.");
            render("@newZindep", zindep);
        }

        zindep.validateAndSave();
        flash.success("Nouvel indpendant enregistr");
        index();
    }

    /**
     * Retourne la liste des zindeps, le tag listOfZindeps est directement itr dans la page HTML
     * grace  Groovy.
     */
    public static void listZindeps() {
        List<Zindep> listOfZindeps = Zindep.findAll();
        render(listOfZindeps);
    }

    /**
     * Permet de rendre visible un compte.
     *
     * @param id est la cle primaire.
     */
    public static void setVisible(String id) {
        Zindep z = Zindep.findById(id);
        if (z == null) {
            flash.error("Compte non trouv");
            listZindeps(); // Cette methode coupe le flow d'execution, Play leve une exception et termine l'execution
        }
        // ... donc le "else" est implicite
        z.isVisible = Boolean.TRUE;
        z.save();
        flash.success("Le compte est maintenant visible sur le site des Zindeps.");
        listZindeps();
    }

    /**
     * Permet de rendre invisible un compte.
     *
     * @param id de l'utilisateur.
     */
    public static void setInvisible(String id) {
        Zindep z = Zindep.findById(id);
        if (z == null) {
            flash.error("Compte non trouv");
            listZindeps(); // Cette methode coupe le flow d'execution, Play leve une exception et termine l'execution
        }
        // ... donc le "else" est implicite
        z.isVisible = Boolean.FALSE;
        z.save();
        flash.success("Le compte est maintenant invisible sur le site.");
        listZindeps();

    }

    /**
     * Methode ajoutee temporairement afin que l'ensemble des Propals ait une expirationDate
     */
    public static void doUpdatePropals() {
        List<Propal> listOfPropals = Propal.findAll();
        for (Propal p : listOfPropals) {
            if (p.expirationDate == null) {
                p.expirationDate = new DateMidnight().plus(30L).toDate();
            }
            if (p.creationDate == null) {
                p.creationDate = new DateMidnight().toDate();
                p.nbDaysOfValidity = 30;
            }
            p.save();
        }
    }

}