de.forsthaus.webui.security.group.SecGroupDialogCtrl.java Source code

Java tutorial

Introduction

Here is the source code for de.forsthaus.webui.security.group.SecGroupDialogCtrl.java

Source

/**
 * Copyright 2010 the original author or authors.
 * 
 * This file is part of Zksample2. http://zksample2.sourceforge.net/
 *
 * Zksample2 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.
 * 
 * Zksample2 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 Zksample2.  If not, see <http://www.gnu.org/licenses/gpl.html>.
 */
package de.forsthaus.webui.security.group;

import java.io.Serializable;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.zkoss.util.resource.Labels;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Button;
import org.zkoss.zul.ListModelList;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

import de.forsthaus.backend.model.SecGroup;
import de.forsthaus.backend.service.SecurityService;
import de.forsthaus.webui.util.ButtonStatusCtrl;
import de.forsthaus.webui.util.GFCBaseCtrl;
import de.forsthaus.webui.util.MultiLineMessageBox;
import de.forsthaus.webui.util.ZksampleMessageUtils;

/**
 * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<br>
 * This is the controller class for the
 * /WEB-INF/pages/sec_group/secGroupDialog.zul file.<br>
 * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++<br>
 * 
 * @changes 05/15/2009: sge Migrating the list models for paging. <br>
 *          07/24/2009: sge changes for clustering.<br>
 *          10/12/2009: sge changings in the saving routine.<br>
 *          11/07/2009: bbr changed to extending from GFCBaseCtrl<br>
 *          (GenericForwardComposer) for spring-managed creation.<br>
 * 
 * @author bbruhns
 * @author sgerth
 */
public class SecGroupDialogCtrl extends GFCBaseCtrl implements Serializable {

    private static final long serialVersionUID = -546886879998950467L;
    private static final Logger logger = Logger.getLogger(SecGroupDialogCtrl.class);

    /*
     * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     * All the components that are defined here and have a corresponding
     * component with the same 'id' in the zul-file are getting autowired by our
     * 'extends GFCBaseCtrl' GenericForwardComposer.
     * ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
     */
    protected Window secGroupDialogWindow; // autowired
    protected Textbox grpShortdescription; // autowired
    protected Textbox grpLongdescription; // autowired

    // overhanded vars per params
    private transient Listbox listBoxSecGroups; // overhanded
    private transient SecGroup group; // overhanded

    // old value vars for edit mode. that we can check if something
    // on the values are edited since the last init.
    private transient String oldVar_grpShortdescription;
    private transient String oldVar_grpLongdescription;

    private transient boolean validationOn;

    // Button controller for the CRUD buttons
    private transient final String btnCtroller_ClassPrefix = "button_SecGroupDialog_";
    private transient ButtonStatusCtrl btnCtrl;
    protected Button btnNew; // autowire
    protected Button btnEdit; // autowire
    protected Button btnDelete; // autowire
    protected Button btnSave; // autowire
    protected Button btnCancel; // autowire
    protected Button btnClose; // autowire

    // ServiceDAOs / Domain Classes
    private transient SecurityService securityService;

    /**
     * default constructor.<br>
     */
    public SecGroupDialogCtrl() {
        super();
    }

    /**
     * Before binding the data and calling the dialog window we check, if the
     * zul-file is called with a parameter for a selected user object in a Map.
     * 
     * @param event
     * @throws Exception
     */
    public void onCreate$secGroupDialogWindow(Event event) throws Exception {
        // create the Button Controller. Disable not used buttons during working
        btnCtrl = new ButtonStatusCtrl(getUserWorkspace(), btnCtroller_ClassPrefix, true, btnNew, btnEdit,
                btnDelete, btnSave, btnCancel, btnClose);

        // get the params map that are overhanded by creation.
        Map<String, Object> args = getCreationArgsMap(event);

        if (args.containsKey("group")) {
            group = (SecGroup) args.get("group");
            setGroup(group);
        } else {
            setGroup(null);
        }

        // we get the listBox Object for the users list. So we have access
        // to it and can synchronize the shown data when we do insert, edit or
        // delete users here.
        if (args.containsKey("listBoxSecGroups")) {
            listBoxSecGroups = (Listbox) args.get("listBoxSecGroups");
        } else {
            listBoxSecGroups = null;
        }

        // set Field Properties
        doSetFieldProperties();

        doShowDialog(getGroup());

    }

    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // +++++++++++++++++++++++ Components events +++++++++++++++++++++++
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    /**
     * If we close the dialog window. <br>
     * 
     * @param event
     * @throws Exception
     */
    public void onClose$secGroupDialogWindow(Event event) throws Exception {
        // logger.debug(event.toString());

        doClose();
    }

    /**
     * when the "save" button is clicked. <br>
     * 
     * @param event
     * @throws InterruptedException
     */
    public void onClick$btnSave(Event event) throws InterruptedException {
        // logger.debug(event.toString());

        doSave();
    }

    /**
     * when the "edit" button is clicked. <br>
     * 
     * @param event
     */
    public void onClick$btnEdit(Event event) {
        // logger.debug(event.toString());

        doEdit();
    }

    /**
     * when the "help" button is clicked. <br>
     * 
     * @param event
     * @throws InterruptedException
     */
    public void onClick$btnHelp(Event event) throws InterruptedException {
        // logger.debug(event.toString());

        ZksampleMessageUtils.doShowNotImplementedMessage();
    }

    /**
     * when the "new" button is clicked. <br>
     * 
     * @param event
     */
    public void onClick$btnNew(Event event) {
        // logger.debug(event.toString());

        doNew();
    }

    /**
     * when the "delete" button is clicked. <br>
     * 
     * @param event
     * @throws InterruptedException
     */
    public void onClick$btnDelete(Event event) throws InterruptedException {
        // logger.debug(event.toString());

        doDelete();
    }

    /**
     * when the "cancel" button is clicked. <br>
     * 
     * @param event
     */
    public void onClick$btnCancel(Event event) {
        // logger.debug(event.toString());

        doCancel();
    }

    /**
     * when the "close" button is clicked. <br>
     * 
     * @param event
     * @throws InterruptedException
     */
    public void onClick$btnClose(Event event) throws InterruptedException {
        // logger.debug(event.toString());

        try {
            doClose();
        } catch (final Exception e) {
            // close anyway
            secGroupDialogWindow.onClose();
        }
    }

    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // ++++++++++++++++++++++++ GUI operations +++++++++++++++++++++++++
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    /**
     * Closes the dialog window. <br>
     * <br>
     * Before closing we check if there are unsaved changes in <br>
     * the components and ask the user if saving the modifications. <br>
     * 
     */
    private void doClose() throws Exception {

        if (isDataChanged()) {

            // Show a confirm box
            String msg = Labels.getLabel("message_Data_Modified_Save_Data_YesNo");
            String title = Labels.getLabel("message.Information");

            MultiLineMessageBox.doSetTemplate();
            if (MultiLineMessageBox.show(msg, title, MultiLineMessageBox.YES | MultiLineMessageBox.NO,
                    MultiLineMessageBox.QUESTION, true, new EventListener() {
                        @Override
                        public void onEvent(Event evt) {
                            switch (((Integer) evt.getData()).intValue()) {
                            case MultiLineMessageBox.YES:
                                try {
                                    doSave();
                                } catch (final InterruptedException e) {
                                    throw new RuntimeException(e);
                                }
                            case MultiLineMessageBox.NO:
                                break; //
                            }
                        }
                    }

            ) == MultiLineMessageBox.YES) {
            }
        }
        secGroupDialogWindow.onClose();
    }

    /**
     * Cancel the actual operation. <br>
     * <br>
     * Resets to the original status.<br>
     * 
     */
    private void doCancel() {
        doResetInitValues();
        doReadOnly();
        btnCtrl.setInitEdit();
    }

    /**
     * Writes the bean data to the components.<br>
     * 
     * @param aGroup
     *            SecGroup
     */
    public void doWriteBeanToComponents(SecGroup aGroup) {

        grpShortdescription.setValue(aGroup.getGrpShortdescription());
        grpLongdescription.setValue(aGroup.getGrpLongdescription());

    }

    /**
     * Writes the components values to the bean.<br>
     * 
     * @param aGroup
     */
    public void doWriteComponentsToBean(SecGroup aGroup) {

        aGroup.setGrpShortdescription(grpShortdescription.getValue());
        aGroup.setGrpLongdescription(grpLongdescription.getValue());

    }

    /**
     * Opens the Dialog window modal.
     * 
     * It checks if the dialog opens with a new or existing object, if so they
     * set the readOnly mode accordingly.
     * 
     * @param aGroup
     * @throws InterruptedException
     */
    public void doShowDialog(SecGroup aGroup) throws InterruptedException {

        // if aGroup == null then we opened the Dialog without
        // args for a given entity, so we get a new Obj().
        if (aGroup == null) {

            /** !!! DO NOT BREAK THE TIERS !!! */
            // We don't create a new DomainObject() in the frontend.
            // We GET it from the backend.
            aGroup = getSecurityService().getNewSecGroup();
        }

        // set Readonly mode accordingly if the object is new or not.
        if (aGroup.isNew()) {
            doEdit();
            btnCtrl.setInitNew();
        } else {
            btnCtrl.setInitEdit();
            doReadOnly();
        }

        try {
            // fill the components with the data
            doWriteBeanToComponents(aGroup);

            // stores the inital data for comparing if they are changed
            // during user action.
            doStoreInitValues();

            secGroupDialogWindow.doModal(); // open the dialog in modal
            // mode
        } catch (final Exception e) {
            Messagebox.show(e.toString());
        }
    }

    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // ++++++++++++++++++++++++++++++ helpers ++++++++++++++++++++++++++
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    /**
     * Set the properties of the fields, like maxLength.<br>
     */
    private void doSetFieldProperties() {
        grpShortdescription.setMaxlength(40);
        grpLongdescription.setMaxlength(1000);
    }

    /**
     * Stores the init values in mem vars. <br>
     */
    private void doStoreInitValues() {
        oldVar_grpShortdescription = grpShortdescription.getValue();
        oldVar_grpLongdescription = grpLongdescription.getValue();
    }

    /**
     * Resets the init values from mem vars. <br>
     */
    private void doResetInitValues() {
        grpShortdescription.setValue(oldVar_grpShortdescription);
        grpLongdescription.setValue(oldVar_grpLongdescription);
    }

    /**
     * Checks, if data are changed since the last call of <br>
     * doStoreInitData() . <br>
     * 
     * @return true, if data are changed, otherwise false
     */
    private boolean isDataChanged() {
        boolean changed = false;

        if (oldVar_grpShortdescription != grpShortdescription.getValue()) {
            changed = true;
        }
        if (oldVar_grpLongdescription != grpLongdescription.getValue()) {
            changed = true;
        }

        return changed;
    }

    /**
     * Sets the Validation by setting the accordingly constraints to the fields.
     */
    private void doSetValidation() {

        setValidationOn(true);

        grpShortdescription.setConstraint("NO EMPTY");
    }

    /**
     * Disables the Validation by setting empty constraints.
     */
    private void doRemoveValidation() {

        setValidationOn(false);

        grpShortdescription.setConstraint("");
    }

    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // +++++++++++++++++++++++++ crud operations +++++++++++++++++++++++
    // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

    /**
     * Deletes a secGroup object from database.<br>
     * 
     * @throws InterruptedException
     */
    private void doDelete() throws InterruptedException {

        final SecGroup aGroup = getGroup();

        // Show a confirm box
        String msg = Labels.getLabel("message.Question.Are_you_sure_to_delete_this_record") + "\n\n --> "
                + aGroup.getGrpShortdescription();
        String title = Labels.getLabel("message.Deleting.Record");

        MultiLineMessageBox.doSetTemplate();
        if (MultiLineMessageBox.show(msg, title, MultiLineMessageBox.YES | MultiLineMessageBox.NO,
                MultiLineMessageBox.QUESTION, true, new EventListener() {
                    @Override
                    public void onEvent(Event evt) {
                        switch (((Integer) evt.getData()).intValue()) {
                        case MultiLineMessageBox.YES:
                            try {
                                delete();
                            } catch (InterruptedException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                        case MultiLineMessageBox.NO:
                            break; //
                        }
                    }

                    // delete from database
                    private void delete() throws InterruptedException {

                        try {
                            getSecurityService().delete(aGroup);
                        } catch (DataAccessException e) {
                            ZksampleMessageUtils.showErrorMessage(e.getMostSpecificCause().toString());
                        }

                        // now synchronize the listBox
                        ListModelList lml = (ListModelList) listBoxSecGroups.getListModel();

                        // Check if the object is new or updated
                        // -1 means that the obj is not in the list, so it's
                        // new..
                        if (lml.indexOf(aGroup) == -1) {
                        } else {
                            lml.remove(lml.indexOf(aGroup));
                        }

                        secGroupDialogWindow.onClose(); // close
                    }
                }

        ) == MultiLineMessageBox.YES) {
        }

    }

    /**
     * Create a new SecGroup object. <br>
     */
    private void doNew() {

        // remember the old vars
        doStoreInitValues();

        /** !!! DO NOT BREAK THE TIERS !!! */
        // We don't create a new DomainObject() in the frontend.
        // We GET it from the backend.
        setGroup(getSecurityService().getNewSecGroup());

        doClear(); // clear all commponents
        doEdit(); // edit mode

        btnCtrl.setBtnStatus_New();

    }

    /**
     * Set the components for edit mode. <br>
     */
    private void doEdit() {

        grpShortdescription.setReadonly(false);
        grpLongdescription.setReadonly(false);

        btnCtrl.setBtnStatus_Edit();

        // remember the old vars
        doStoreInitValues();
    }

    /**
     * Set the components to ReadOnly. <br>
     */
    public void doReadOnly() {

        grpShortdescription.setReadonly(true);
        grpLongdescription.setReadonly(true);
    }

    /**
     * Clears the components values. <br>
     */
    public void doClear() {

        // temporarely disable the validation to allow the field's clearing
        doRemoveValidation();

        grpShortdescription.setValue("");
        grpLongdescription.setValue("");
    }

    /**
     * Saves the components to table. <br>
     * 
     * @throws InterruptedException
     */
    public void doSave() throws InterruptedException {

        final SecGroup aGroup = getGroup();

        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        // force validation, if on, than execute by component.getValue()
        // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        if (!isValidationOn()) {
            doSetValidation();
        }

        // fill the object with the components data
        doWriteComponentsToBean(aGroup);

        // save it to database
        try {
            getSecurityService().saveOrUpdate(aGroup);
        } catch (DataAccessException e) {
            ZksampleMessageUtils.showErrorMessage(e.getMostSpecificCause().toString());

            // Reset to init values
            doResetInitValues();

            doReadOnly();
            btnCtrl.setBtnStatus_Save();
            return;
        }

        // now synchronize the listBox
        ListModelList lml = (ListModelList) listBoxSecGroups.getListModel();

        // Check if the object is new or updated
        // -1 means that the obj is not in the list, so it's new.
        if (lml.indexOf(aGroup) == -1) {
            lml.add(aGroup);
        } else {
            lml.set(lml.indexOf(aGroup), aGroup);
        }

        doReadOnly();
        btnCtrl.setBtnStatus_Save();

        // init the old values vars new
        doStoreInitValues();
    }

    // ++++++++++++++++++++++++++++++++++++++++++++++++++++++//
    // ++++++++++++++++++ getter / setter +++++++++++++++++++//
    // ++++++++++++++++++++++++++++++++++++++++++++++++++++++//
    public SecGroup getGroup() {
        return this.group;
    }

    public void setGroup(SecGroup group) {
        this.group = group;
    }

    public void setValidationOn(boolean validationOn) {
        this.validationOn = validationOn;
    }

    public boolean isValidationOn() {
        return this.validationOn;
    }

    public SecurityService getSecurityService() {
        return this.securityService;
    }

    public void setSecurityService(SecurityService securityService) {
        this.securityService = securityService;
    }

}