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 3 of the License. * * 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/>. */ package com.autentia.tnt.bean.admin; import java.math.*; import java.text.NumberFormat; import java.util.*; import javax.faces.component.UIComponent; import javax.faces.component.UIData; import javax.faces.component.html.HtmlDataTable; import javax.faces.model.*; import org.apache.commons.logging.*; import com.autentia.tnt.bean.*; import com.autentia.tnt.businessobject.*; import com.autentia.tnt.dao.*; import com.autentia.tnt.dao.search.*; import com.autentia.tnt.manager.admin.ProjectCostManager; import com.autentia.tnt.manager.admin.ProjectManager; import com.autentia.tnt.manager.admin.ProjectRoleManager; import com.autentia.tnt.manager.contacts.OrganizationManager; import com.autentia.tnt.manager.security.Permission; import com.autentia.tnt.upload.*; import com.autentia.tnt.util.*; import org.acegisecurity.acls.domain.BasePermission; /** * UI bean for Project objects. * @author stajanov code generator */ public class ProjectBean extends BaseBean { /** Serial version field */ private static final long serialVersionUID = -1L; public BigDecimal getTotalCost() { return project.getTotalCost(); } /** * Go to edit page * @return forward to EDIT page */ public String copy() { Integer id = Integer.parseInt(FacesUtils.getRequestParameter(ROW_ID)); Offer oferta = manager.copyFromOffer(id); project = new Project(); project.setClient(oferta.getOrganization()); project.setStartDate(new Date()); project.setOpen(true); HashSet<ProjectRole> roles = new HashSet<ProjectRole>(); HashSet<ProjectCost> costs = new HashSet<ProjectCost>(); for (OfferRole role : oferta.getRoles()) { ProjectRole pr = new ProjectRole(); pr.setName(role.getName()); pr.setCostPerHour(role.getCostPerHour()); pr.setExpectedHours(role.getExpectedHours()); roles.add(pr); } for (OfferCost cost : oferta.getCosts()) { ProjectCost pc = new ProjectCost(); pc.setName(cost.getName()); pc.setBillable(cost.isBillable()); pc.setCost(cost.getCost()); costs.add(pc); } project.setRoles(roles); project.setCosts(costs); return NavigationResults.COPY_FROM_OFFER; } /* project - generated by stajanov (do not edit/delete) */ /** Logger */ private static final Log log = LogFactory.getLog(ProjectBean.class); /** Active search object */ private ProjectSearch search = new ProjectSearch(); /** Manager */ private static ProjectManager manager = ProjectManager.getDefault(); /** Upload service */ private static final Uploader uploader = UploaderFactory.getInstance("project"); /** Active Project object */ private Project project; /** Default sort column */ private String sortColumn = "name"; /** Default sort order */ private boolean sortAscending = true; /** Quick search letter for ABC pager control */ private Character letter; public ProjectBean() { search.setOpen(new Boolean(true)); } /** * List projects. Order depends on Faces parameter sort. * @return the list of all projects sorted by requested criterion */ public List<Project> getAll() { return manager.getAllEntities(search, new SortCriteria(sortColumn, sortAscending)); } // Getters to list possible values of related entities /** * Get the list of all clients * @return the list of all clients */ public List<SelectItem> getClients() { List<Organization> refs = OrganizationManager.getDefault().getAllEntities(null, new SortCriteria("name")); ArrayList<SelectItem> ret = new ArrayList<SelectItem>(); for (Organization ref : refs) { ret.add(new SelectItem(ref, ref.getName())); } return ret; } /** * Get the list of all roless * @return the list of all roless */ public List<SelectItem> getRoless() { List<ProjectRole> refs = ProjectRoleManager.getDefault().getAllEntities(null, new SortCriteria("name")); ArrayList<SelectItem> ret = new ArrayList<SelectItem>(); for (ProjectRole ref : refs) { ret.add(new SelectItem(ref, ref.getName())); } return ret; } /** * Get the list of all costss * @return the list of all costss */ public List<SelectItem> getCostss() { List<ProjectCost> refs = ProjectCostManager.getDefault().getAllEntities(null, new SortCriteria("name")); ArrayList<SelectItem> ret = new ArrayList<SelectItem>(); for (ProjectCost ref : refs) { ret.add(new SelectItem(ref, ref.getName())); } return ret; } // Getters to list possible values of enum fields // Methods to create/remove instances of one-to-many entities (slave entities) /** * Create a new empty instance of the one-to-many field * @return forward to the same page */ public String createRoles() { ProjectRole item = new ProjectRole(); item.setProject(project); if (project.getRoles() == null) { project.setRoles(new HashSet()); } project.getRoles().add(item); return null; } /** * Create a new empty instance of the one-to-many field * @return forward to the same page */ public String editRoles() { ProjectRole item = new ProjectRole(); item.setProject(project); if (project.getRoles() == null) { project.setRoles(new HashSet()); } project.getRoles().add(item); return null; } /** * Delete selected instance of the one-to-many field * @return forward to the same page */ public String deleteRoles() { UIData table = (UIData) FacesUtils.getComponent("project:roles"); project.getRoles().remove(table.getRowData()); return null; } /** * Create a new empty instance of the one-to-many field * @return forward to the same page */ public String createCosts() { ProjectCost item = new ProjectCost(); item.setProject(project); if (project.getCosts() == null) { project.setCosts(new HashSet()); } project.getCosts().add(item); return null; } /** * Create a new empty instance of the one-to-many field * @return forward to the same page */ public String editCosts() { ProjectCost item = new ProjectCost(); item.setProject(project); if (project.getCosts() == null) { project.setCosts(new HashSet()); } project.getCosts().add(item); return null; } /** * Delete selected instance of the one-to-many field * @return forward to the same page */ public String deleteCosts() { UIData table = (UIData) FacesUtils.getComponent("project:costs"); project.getCosts().remove(table.getRowData()); return null; } /** * Whether or not create button is available for user * @return true if user can create objects of type Project */ public boolean isCreateAvailable() { return SpringUtils.isRolePermissionGranted(Permission.Entity_Create(Project.class)); } /** * Whether or not edit button is available for user * @return true if user can edit current object */ public boolean isEditAvailable() { return SpringUtils.isAclPermissionGranted(project, BasePermission.WRITE); } /** * Whether or not delete button is available for user * @return true if user can delete current object */ public boolean isDeleteAvailable() { return (project.getId() != null) && SpringUtils.isAclPermissionGranted(project, BasePermission.DELETE); } /** * Reset search criteria * @return forward to LIST page */ public String reset() { search.reset(); return list(); } /** * Go to create page * @return forward to CREATE page */ public String create() { project = new Project(); project.setStartDate(new Date()); project.setBillable(true); return NavigationResults.CREATE; } /** * Go to detail page * @return forward to DETAIL page */ public String detail() { Integer id = Integer.parseInt(FacesUtils.getRequestParameter(ROW_ID)); project = manager.getEntityById(id); return SpringUtils.isAclPermissionGranted(project, BasePermission.WRITE) ? NavigationResults.EDIT : NavigationResults.DETAIL; } /** * Save bean and stay on it * @return forward to list page */ public String save() { doBeforeSave(); if (project.getId() == null) { manager.insertEntity(project); } else { manager.updateEntity(project); } // Calls an after save action String result = doAfterSave(NavigationResults.LIST); // Unselect object project = null; return result; } /** * Delete bean and go back to beans list * @return forward to LIST page */ public String delete() { manager.deleteEntity(project); project = 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 isProjectSelected() { return project != 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 ProjectSearch getSearch() { return search; } public Date getSearchStartStartDate() { return search.getStartStartDate(); } public void setSearchStartStartDate(Date val) { if (val != null) { search.setStartStartDate(val); } else { search.unsetStartStartDate(); } } public boolean isSearchStartStartDateValid() { return search.isStartStartDateSet(); } public void setSearchStartStartDateValid(boolean val) { if (val) { search.setStartStartDate(search.getStartStartDate()); } else { search.unsetStartStartDate(); } } public Date getSearchEndStartDate() { return search.getEndStartDate(); } public void setSearchEndStartDate(Date val) { if (val != null) { search.setEndStartDate(val); } else { search.unsetEndStartDate(); } } public boolean isSearchEndStartDateValid() { return search.isEndStartDateSet(); } public void setSearchEndStartDateValid(boolean val) { if (val) { search.setEndStartDate(search.getEndStartDate()); } else { search.unsetEndStartDate(); } } public Date getSearchStartEndDate() { return search.getStartEndDate(); } public void setSearchStartEndDate(Date val) { if (val != null) { search.setStartEndDate(val); } else { search.unsetStartEndDate(); } } public boolean isSearchStartEndDateValid() { return search.isStartEndDateSet(); } public void setSearchStartEndDateValid(boolean val) { if (val) { search.setStartEndDate(search.getStartEndDate()); } else { search.unsetStartEndDate(); } } public Date getSearchEndEndDate() { return search.getEndEndDate(); } public void setSearchEndEndDate(Date val) { if (val != null) { search.setEndEndDate(val); } else { search.unsetEndEndDate(); } } public boolean isSearchEndEndDateValid() { return search.isEndEndDateSet(); } public void setSearchEndEndDateValid(boolean val) { if (val) { search.setEndEndDate(search.getEndEndDate()); } else { search.unsetEndEndDate(); } } public Boolean getSearchOpen() { return search.getOpen(); } public void setSearchOpen(Boolean val) { if (search.isOpenSet()) { search.setOpen(val); } } public boolean isSearchOpenValid() { return search.isOpenSet(); } public void setSearchOpenValid(boolean val) { if (val) { search.setOpen(search.getOpen()); } else { search.unsetOpen(); } } public String getSearchName() { return search.getName(); } public void setSearchName(String val) { if (search.isNameSet()) { search.setName(val); } } public boolean isSearchNameValid() { return search.isNameSet(); } public void setSearchNameValid(boolean val) { if (val) { search.setName(search.getName()); } else { search.unsetName(); } } 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 Boolean getSearchBillable() { return search.getBillable(); } public void setSearchBillable(Boolean val) { if (search.isBillableSet()) { search.setBillable(val); } } public boolean isSearchBillableValid() { return search.isBillableSet(); } public void setSearchBillableValid(boolean val) { if (val) { search.setBillable(search.getBillable()); } else { search.unsetBillable(); } } 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(); } } public Organization getSearchClient() { return search.getClient(); } public void setSearchClient(Organization val) { if (search.isClientSet()) { search.setClient(val); } } public boolean isSearchClientValid() { return search.isClientSet(); } public void setSearchClientValid(boolean val) { if (val) { search.setClient(search.getClient()); } else { search.unsetClient(); } } public List<ProjectRole> getSearchRoles() { return search.getRoles(); } public void setSearchRoles(List<ProjectRole> val) { if (search.isRolesSet()) { search.setRoles(val); } } public boolean isSearchRolesValid() { return search.isRolesSet(); } public void setSearchRolesValid(boolean val) { if (val) { search.setRoles(search.getRoles()); } else { search.unsetRoles(); } } public List<ProjectCost> getSearchCosts() { return search.getCosts(); } public void setSearchCosts(List<ProjectCost> val) { if (search.isCostsSet()) { search.setCosts(val); } } public boolean isSearchCostsValid() { return search.isCostsSet(); } public void setSearchCostsValid(boolean val) { if (val) { search.setCosts(search.getCosts()); } else { search.unsetCosts(); } } /** Handle an ABC pager letter click: filter objects by specified starting letter */ public void letterClicked() { if (letter != null) { UIComponent comp = FacesUtils.getComponent("projects:list"); HtmlDataTable tabla = (HtmlDataTable) comp; tabla.setFirst(0); search.setName(letter + "%"); } else { search.unsetName(); } } 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 Project object public java.lang.Integer getId() { return project.getId(); } public Date getStartDate() { return project.getStartDate(); } public void setStartDate(Date startDate) { project.setStartDate(startDate); } public Date getEndDate() { return project.getEndDate(); } public void setEndDate(Date endDate) { project.setEndDate(endDate); } public Boolean getOpen() { return project.getOpen(); } public void setOpen(Boolean open) { project.setOpen(open); } public String getName() { return project.getName(); } public void setName(String name) { project.setName(name); } public String getDescription() { return project.getDescription(); } public void setDescription(String description) { project.setDescription(description); } public Boolean getBillable() { return project.getBillable(); } public void setBillable(Boolean billable) { project.setBillable(billable); } public Integer getOwnerId() { return project.getOwnerId(); } public void setOwnerId(Integer ownerId) { project.setOwnerId(ownerId); } public Integer getDepartmentId() { return project.getDepartmentId(); } public void setDepartmentId(Integer departmentId) { project.setDepartmentId(departmentId); } public Date getInsertDate() { return project.getInsertDate(); } public void setInsertDate(Date insertDate) { project.setInsertDate(insertDate); } public Date getUpdateDate() { return project.getUpdateDate(); } public void setUpdateDate(Date updateDate) { project.setUpdateDate(updateDate); } public Organization getClient() { return project.getClient(); } public void setClient(Organization client) { project.setClient(client); } public Set<ProjectRole> getRoles() { return project.getRoles(); } public void setRoles(Set<ProjectRole> roles) { project.setRoles(roles); } public Set<ProjectCost> getCosts() { return project.getCosts(); } public void setCosts(Set<ProjectCost> costs) { project.setCosts(costs); } /* project - generated by stajanov (do not edit/delete) */ // Para arreglar un problema que surge en copy from Offer public String doBeforeSave() { if (project != null) { if (project.getRoles() != null) { for (ProjectRole pr : project.getRoles()) { if (pr.getProject() == null) pr.setProject(project); } } if (project.getCosts() != null) { for (ProjectCost pc : project.getCosts()) { if (pc.getProject() == null) pc.setProject(project); } } } return null; } public String getWorkedHours() { long total = project.getWorkedHours(); String totalWorked = "0"; if (total != 0) { NumberFormat format = NumberFormat.getInstance(); format.setMaximumFractionDigits(2); totalWorked = format.format(total / 60); // Convertimos a horas } return totalWorked; } public long getExpectedHours() { return project.getExpectedHours(); } public double getPercentageWorked() { return project.getPercentageWorked(); } public double getPercentageCosts() { return project.getPercentageCosts(); } public String getCostPerProject() { BigDecimal total = project.getCostPerProject(); String totalCost = null; NumberFormat format = NumberFormat.getInstance(); format.setMaximumFractionDigits(2); totalCost = format.format(total.doubleValue()); return totalCost; } /** Fuerza refresh de pantalla */ public String reload() { return null; } }