Java tutorial
/* * TNTConcept Easy Enterprise Management by Autentia Real Bussiness Solution S.L. * Copyright (C) 2007 Autentia Real Bussiness Solution S.L. * * 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 2 * 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, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Autentia Real Bussiness Solution S.L. * Tlf: +34 91 675 33 06, +34 655 99 11 72 * Fax: +34 91 656 65 04 * info@autentia.com */ package com.autentia.intra.bean.holiday; import com.autentia.intra.bean.BaseBean; import com.autentia.intra.bean.NavigationResults; import com.autentia.intra.businessobject.Holiday; import com.autentia.intra.dao.SortCriteria; import com.autentia.intra.dao.search.HolidaySearch; import com.autentia.intra.manager.holiday.HolidayManager; import com.autentia.intra.manager.security.Permission; import com.autentia.intra.upload.Uploader; import com.autentia.intra.upload.UploaderFactory; import com.autentia.intra.util.FacesUtils; import com.autentia.intra.util.SpringUtils; import org.acegisecurity.acls.domain.BasePermission; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import javax.faces.component.UIComponent; import javax.faces.component.html.HtmlDataTable; import java.util.Calendar; import java.util.Date; import java.util.List; public class HolidayBean extends BaseBean { private Date selectedYear = null; public String reset() { search.reset(); return list(); } public Date getSelectedYear() { return selectedYear; } public void setSelectedYear(Date selectedYear) { this.selectedYear = selectedYear; } /** * List holidays. Order depends on Faces parameter sort. * * @return the list of all holidays sorted by requested criterion */ public List<Holiday> getAll() { if (selectedYear != null) { Calendar calMin = Calendar.getInstance(); calMin.setTime(selectedYear); calMin.set(Calendar.MONTH, calMin.getMinimum(Calendar.MONTH)); calMin.set(Calendar.DAY_OF_MONTH, calMin.getMinimum(Calendar.DAY_OF_MONTH)); calMin.set(Calendar.HOUR_OF_DAY, calMin.getMinimum(Calendar.HOUR_OF_DAY)); calMin.set(Calendar.MINUTE, calMin.getMinimum(Calendar.MINUTE)); calMin.set(Calendar.SECOND, calMin.getMinimum(Calendar.SECOND)); calMin.set(Calendar.MILLISECOND, calMin.getMinimum(Calendar.MILLISECOND)); Calendar calMax = Calendar.getInstance(); calMax.setTime(selectedYear); calMax.set(Calendar.MONTH, calMax.getMaximum(Calendar.MONTH)); calMax.set(Calendar.DAY_OF_MONTH, calMax.getMaximum(Calendar.DAY_OF_MONTH)); calMax.set(Calendar.HOUR_OF_DAY, calMax.getMaximum(Calendar.HOUR_OF_DAY)); calMax.set(Calendar.MINUTE, calMax.getMaximum(Calendar.MINUTE)); calMax.set(Calendar.SECOND, calMax.getMaximum(Calendar.SECOND)); calMax.set(Calendar.MILLISECOND, calMax.getMaximum(Calendar.MILLISECOND)); setSearchStartDate(calMin.getTime()); setSearchStartDateValid(true); setSearchEndDate(calMax.getTime()); setSearchEndDateValid(true); } return manager.getAllEntities(search, new SortCriteria(sortColumn, sortAscending)); } /* holiday - generated by stajanov (do not edit/delete) */ /** * Logger */ private static final Log log = LogFactory.getLog(HolidayBean.class); /** * Active search object */ private HolidaySearch search = new HolidaySearch(); /** * Manager */ private static HolidayManager manager = HolidayManager.getDefault(); /** * Upload service */ private static final Uploader uploader = UploaderFactory.getInstance("holiday"); /** * Active Holiday object */ private Holiday holiday; /** * Default sort column */ private String sortColumn = "date"; /** * Default sort order */ private boolean sortAscending = true; /** * Quick search letter for ABC pager control */ private Character letter; // Getters to list possible values of related entities // Getters to list possible values of enum fields // Methods to create/remove instances of one-to-many entities (slave entities) /** * Whether or not create button is available for user * * @return true if user can create objects of type Holiday */ public boolean isCreateAvailable() { return SpringUtils.isRolePermissionGranted(Permission.Entity_Create(Holiday.class)); } /** * Whether or not edit button is available for user * * @return true if user can edit current object */ public boolean isEditAvailable() { return SpringUtils.isAclPermissionGranted(holiday, BasePermission.WRITE); } /** * Whether or not delete button is available for user * * @return true if user can delete current object */ public boolean isDeleteAvailable() { return (holiday.getId() != null) && SpringUtils.isAclPermissionGranted(holiday, BasePermission.DELETE); } /** * Go to create page * * @return forward to CREATE page */ public String create() { holiday = new Holiday(); // return NavigationResults.CREATE; } /** * Go to detail page * * @return forward to DETAIL page */ public String detail() { Integer id = Integer.parseInt(FacesUtils.getRequestParameter("id")); holiday = manager.getEntityById(id); return SpringUtils.isAclPermissionGranted(holiday, BasePermission.WRITE) ? NavigationResults.EDIT : NavigationResults.DETAIL; } /** * Save bean and stay on it * * @return forward to list page */ public String save() { doBeforeSave(); if (holiday.getId() == null) { manager.insertEntity(holiday); } else { manager.updateEntity(holiday); } // Calls an after save action String result = doAfterSave(NavigationResults.LIST); // Unselect object holiday = null; return result; } /** * Delete bean and go back to beans list * * @return forward to LIST page */ public String delete() { manager.deleteEntity(holiday); holiday = null; return NavigationResults.LIST; } /** * Go back to beans list * * @return forward to LIST page */ public String list() { return NavigationResults.LIST; } /** * Go to search page * * @return forward to SEARCH page */ public String search() { return NavigationResults.SEARCH; } /** * Check if we have an active object. * * @return true is an object is selected */ public boolean isHolidaySelected() { return holiday != null; } // Getters and setters to manipulate sorting public boolean isSortAscending() { return sortAscending; } public void setSortAscending(boolean sortAscending) { this.sortAscending = sortAscending; } public String getSortColumn() { return sortColumn; } public void setSortColumn(String sortColumn) { this.sortColumn = sortColumn; } // Getters and setters to handle search public HolidaySearch getSearch() { return search; } public String getSearchDescription() { return search.getDescription(); } public void setSearchDescription(String val) { if (search.isDescriptionSet()) { search.setDescription(val); } } public boolean isSearchDescriptionValid() { return search.isDescriptionSet(); } public void setSearchDescriptionValid(boolean val) { if (val) { search.setDescription(search.getDescription()); } else { search.unsetDescription(); } } public Date getSearchStartDate() { return search.getStartDate(); } public void setSearchStartDate(Date val) { if (val != null) { search.setStartDate(val); } else { search.unsetStartDate(); } } public boolean isSearchStartDateValid() { return search.isStartDateSet(); } public void setSearchStartDateValid(boolean val) { if (val) { search.setStartDate(search.getStartDate()); } else { search.unsetStartDate(); } } public Date getSearchEndDate() { return search.getEndDate(); } public void setSearchEndDate(Date val) { if (val != null) { search.setEndDate(val); } else { search.unsetEndDate(); } } public boolean isSearchEndDateValid() { return search.isEndDateSet(); } public void setSearchEndDateValid(boolean val) { if (val) { search.setEndDate(search.getEndDate()); } else { search.unsetEndDate(); } } public Integer getSearchOwnerId() { return search.getOwnerId(); } public void setSearchOwnerId(Integer val) { if (search.isOwnerIdSet()) { search.setOwnerId(val); } } public boolean isSearchOwnerIdValid() { return search.isOwnerIdSet(); } public void setSearchOwnerIdValid(boolean val) { if (val) { search.setOwnerId(search.getOwnerId()); } else { search.unsetOwnerId(); } } public Integer getSearchDepartmentId() { return search.getDepartmentId(); } public void setSearchDepartmentId(Integer val) { if (search.isDepartmentIdSet()) { search.setDepartmentId(val); } } public boolean isSearchDepartmentIdValid() { return search.isDepartmentIdSet(); } public void setSearchDepartmentIdValid(boolean val) { if (val) { search.setDepartmentId(search.getDepartmentId()); } else { search.unsetDepartmentId(); } } public Date getSearchStartInsertDate() { return search.getStartInsertDate(); } public void setSearchStartInsertDate(Date val) { if (val != null) { search.setStartInsertDate(val); } else { search.unsetStartInsertDate(); } } public boolean isSearchStartInsertDateValid() { return search.isStartInsertDateSet(); } public void setSearchStartInsertDateValid(boolean val) { if (val) { search.setStartInsertDate(search.getStartInsertDate()); } else { search.unsetStartInsertDate(); } } public Date getSearchEndInsertDate() { return search.getEndInsertDate(); } public void setSearchEndInsertDate(Date val) { if (val != null) { search.setEndInsertDate(val); } else { search.unsetEndInsertDate(); } } public boolean isSearchEndInsertDateValid() { return search.isEndInsertDateSet(); } public void setSearchEndInsertDateValid(boolean val) { if (val) { search.setEndInsertDate(search.getEndInsertDate()); } else { search.unsetEndInsertDate(); } } public Date getSearchStartUpdateDate() { return search.getStartUpdateDate(); } public void setSearchStartUpdateDate(Date val) { if (val != null) { search.setStartUpdateDate(val); } else { search.unsetStartUpdateDate(); } } public boolean isSearchStartUpdateDateValid() { return search.isStartUpdateDateSet(); } public void setSearchStartUpdateDateValid(boolean val) { if (val) { search.setStartUpdateDate(search.getStartUpdateDate()); } else { search.unsetStartUpdateDate(); } } public Date getSearchEndUpdateDate() { return search.getEndUpdateDate(); } public void setSearchEndUpdateDate(Date val) { if (val != null) { search.setEndUpdateDate(val); } else { search.unsetEndUpdateDate(); } } public boolean isSearchEndUpdateDateValid() { return search.isEndUpdateDateSet(); } public void setSearchEndUpdateDateValid(boolean val) { if (val) { search.setEndUpdateDate(search.getEndUpdateDate()); } else { search.unsetEndUpdateDate(); } } /** * Handle an ABC pager letter click: filter objects by specified starting letter */ public void letterClicked() { if (letter != null) { UIComponent comp = FacesUtils.getComponent("holidays:list"); HtmlDataTable tabla = (HtmlDataTable) comp; tabla.setFirst(0); search.setDescription(letter + "%"); } else { search.unsetDescription(); } } public Character getLetter() { return letter; } public void setLetter(Character letter) { this.letter = letter; } // Getters and setters to handle uploads // Getters and setters to manipulate active Holiday object public java.lang.Integer getId() { return holiday.getId(); } public String getDescription() { return holiday.getDescription(); } public void setDescription(String description) { holiday.setDescription(description); } public Date getDate() { return holiday.getDate(); } public void setDate(Date date) { holiday.setDate(date); } public Integer getOwnerId() { return holiday.getOwnerId(); } public void setOwnerId(Integer ownerId) { holiday.setOwnerId(ownerId); } public Integer getDepartmentId() { return holiday.getDepartmentId(); } public void setDepartmentId(Integer departmentId) { holiday.setDepartmentId(departmentId); } public Date getInsertDate() { return holiday.getInsertDate(); } public void setInsertDate(Date insertDate) { holiday.setInsertDate(insertDate); } public Date getUpdateDate() { return holiday.getUpdateDate(); } public void setUpdateDate(Date updateDate) { holiday.setUpdateDate(updateDate); } /* holiday - generated by stajanov (do not edit/delete) */ }