/** * 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.role; 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.SecRole; 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_role/secRoleDialogCtrl.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 SecRoleDialogCtrl extends GFCBaseCtrl implements Serializable { private static final long serialVersionUID = -546886879998950467L; private static final Logger logger = Logger.getLogger(SecRoleDialogCtrl.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 secRoleDialogWindow; // autowired protected Textbox rolShortdescription; // autowired protected Textbox rolLongdescription; // autowired // overhanded per param vars private transient Listbox listBoxSecRoles; // overhanded per param private transient SecRole role; // overhanded per param // 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_rolShortdescription; private transient String oldVar_rolLongdescription; private transient boolean validationOn; // Button controller for the CRUD buttons private transient final String btnCtroller_ClassPrefix = "button_SecRoleDialog_"; private transient ButtonStatusCtrl btnCtrl; protected Button btnNew; // autowired protected Button btnEdit; // autowired protected Button btnDelete; // autowired protected Button btnSave; // autowiredd protected Button btnCancel; // autowired protected Button btnClose; // autowired // ServiceDAOs / Domain Classes private transient SecurityService securityService; /** * default constructor.<br> */ public SecRoleDialogCtrl() { 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$secRoleDialogWindow(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("role")) { SecRole aRole = (SecRole) args.get("role"); setRole(aRole); } else { setRole(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("listBoxSecRoles")) { listBoxSecRoles = (Listbox) args.get("listBoxSecRoles"); } else { listBoxSecRoles = null; } // set Field Properties doSetFieldProperties(); doShowDialog(getRole()); } // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // +++++++++++++++++++++++ Components events +++++++++++++++++++++++ // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ /** * If we close the dialog window. <br> * * @param event * @throws Exception */ public void onClose$secRoleDialogWindow(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 secRoleDialogWindow.onClose(); // Messagebox.show(e.toString()); } } // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // ++++++++++++++++++++++++ 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) { } } secRoleDialogWindow.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 aRole */ public void doWriteBeanToComponents(SecRole aRole) { rolShortdescription.setValue(aRole.getRolShortdescription()); rolLongdescription.setValue(aRole.getRolLongdescription()); } /** * Writes the components values to the bean.<br> * * @param aRole */ public void doWriteComponentsToBean(SecRole aRole) { aRole.setRolShortdescription(rolShortdescription.getValue()); aRole.setRolLongdescription(rolLongdescription.getValue()); } /** * Opens the Dialog window modal. * * It checks if the dialog opens with a new or existing object and set the * readOnly mode accordingly. * * @param aBranche * @throws InterruptedException */ public void doShowDialog(SecRole aRole) throws InterruptedException { // if aRole == null then we opened the Dialog without // args for a given entity, so we get a new Obj(). if (aRole == null) { /** !!! DO NOT BREAK THE TIERS !!! */ // We don't create a new DomainObject() in the frontend. // We GET it from the backend. aRole = getSecurityService().getNewSecRole(); setRole(aRole); } else { setRole(aRole); } // set Readonly mode accordingly if the object is new or not. if (aRole.isNew()) { doEdit(); btnCtrl.setInitNew(); } else { btnCtrl.setInitEdit(); doReadOnly(); } try { // fill the components with the data doWriteBeanToComponents(aRole); // stores the inital data for comparing if they are changed // during user action. doStoreInitValues(); secRoleDialogWindow.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() { rolShortdescription.setMaxlength(30); rolLongdescription.setMaxlength(1000); } /** * Stores the init values in mem vars. <br> */ private void doStoreInitValues() { oldVar_rolShortdescription = rolShortdescription.getValue(); oldVar_rolLongdescription = rolLongdescription.getValue(); } /** * Resets the init values from mem vars. <br> */ private void doResetInitValues() { rolShortdescription.setValue(oldVar_rolShortdescription); rolLongdescription.setValue(oldVar_rolLongdescription); } /** * 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_rolShortdescription != rolShortdescription.getValue()) { changed = true; } if (oldVar_rolLongdescription != rolLongdescription.getValue()) { changed = true; } return changed; } /** * Sets the Validation by setting the accordingly constraints to the fields. */ private void doSetValidation() { setValidationOn(true); rolShortdescription.setConstraint("NO EMPTY"); } /** * Disables the Validation by setting empty constraints. */ private void doRemoveValidation() { setValidationOn(false); this.rolShortdescription.setConstraint(""); } /** * Set the components for edit mode. <br> */ private void doEdit() { rolShortdescription.setReadonly(false); rolLongdescription.setReadonly(false); btnCtrl.setBtnStatus_Edit(); // remember the old vars doStoreInitValues(); } /** * Set the components to ReadOnly. <br> */ public void doReadOnly() { rolShortdescription.setReadonly(true); rolLongdescription.setReadonly(true); } /** * Clears the components values. <br> */ public void doClear() { // temporarely disable the validation to allow the field's clearing doRemoveValidation(); rolShortdescription.setValue(""); rolLongdescription.setValue(""); } // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // +++++++++++++++++++++++++ crud operations +++++++++++++++++++++++ // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ /** * Deletes a secRole object from database.<br> * * @throws InterruptedException */ private void doDelete() throws InterruptedException { final SecRole aRole = getRole(); // Show a confirm box String msg = Labels.getLabel("message.Question.Are_you_sure_to_delete_this_record") + "\n\n --> " + aRole.getRolShortdescription(); 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(aRole); } catch (DataAccessException e) { ZksampleMessageUtils.showErrorMessage(e.getMostSpecificCause().toString()); } // now synchronize the listBox final ListModelList lml = (ListModelList) listBoxSecRoles.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(aRole) == -1) { } else { lml.remove(lml.indexOf(aRole)); } secRoleDialogWindow.onClose(); // close // the // dialog } } ) == MultiLineMessageBox.YES) { } } /** * Create a new secRole object. <br> */ private void doNew() { /** !!! DO NOT BREAK THE TIERS !!! */ // We don't create a new DomainObject() in the frontend. // We GET it from the backend. setRole(getSecurityService().getNewSecRole()); doClear(); // clear all commponents doEdit(); // edit mode btnCtrl.setBtnStatus_New(); // remember the old vars doStoreInitValues(); } /** * Saves the components to table. <br> * * @throws InterruptedException */ public void doSave() throws InterruptedException { final SecRole aRole = getRole(); // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ // force validation, if on, than execute by component.getValue() // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ if (!isValidationOn()) { doSetValidation(); } // fill the object with the components data doWriteComponentsToBean(aRole); // save it to database try { getSecurityService().saveOrUpdate(aRole); } 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) listBoxSecRoles.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(aRole) == -1) { lml.add(aRole); } else { lml.set(lml.indexOf(aRole), aRole); } doReadOnly(); btnCtrl.setBtnStatus_Save(); // init the old values vars new doStoreInitValues(); } // ++++++++++++++++++++++++++++++++++++++++++++++++++++++// // ++++++++++++++++++ getter / setter +++++++++++++++++++// // ++++++++++++++++++++++++++++++++++++++++++++++++++++++// public SecRole getRole() { return this.role; } public void setRole(SecRole role) { this.role = role; } 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; } }