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.contacts; import com.autentia.intra.bean.BaseBean; import com.autentia.intra.bean.NavigationResults; import com.autentia.intra.businessobject.Albaran; import com.autentia.intra.businessobject.Bill; import com.autentia.intra.businessobject.Organization; import com.autentia.intra.businessobject.Project; import com.autentia.intra.dao.SortCriteria; import com.autentia.intra.dao.hibernate.AlbaranDAO; import com.autentia.intra.dao.hibernate.ProjectDAO; import com.autentia.intra.dao.search.AlbaranSearch; import com.autentia.intra.dao.search.ProjectSearch; import com.autentia.intra.manager.billing.BillManager; import com.autentia.intra.manager.contacts.AlbaranManager; import com.autentia.intra.manager.contacts.OrganizationManager; import com.autentia.intra.manager.security.Permission; import com.autentia.intra.upload.Uploader; import com.autentia.intra.upload.UploaderFactory; import com.autentia.intra.util.ConfigurationUtil; 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.application.FacesMessage; import javax.faces.component.UIComponent; import javax.faces.component.UIData; import javax.faces.component.html.HtmlDataTable; import javax.faces.context.FacesContext; import javax.faces.event.ValueChangeEvent; import javax.faces.model.SelectItem; import javax.faces.validator.ValidatorException; import java.text.NumberFormat; import java.util.*; /** * UI bean for Albaran objects. * * @author stajanov code generator */ public class AlbaranBean extends BaseBean { private AlbaranDAO albaranDAO = AlbaranDAO.getDefault(); public void nameValidate(FacesContext context, UIComponent toValidate, Object value) throws ValidatorException { if (value == null) return; String name = (String) value; List<Albaran> albarans = albaranDAO.search(null); if (albarans == null || albarans.isEmpty()) return; for (Albaran o : albarans) { if (o.getName() != null && o.getName().equals(name) && !o.getId().equals(albaran.getId())) { //((UIInput) toValidate).setValid(false); //context.addMessage(toValidate.getClientId(context), new FacesMessage("Ya existe otra oferta con este nmero")); throw new ValidatorException(new FacesMessage("Ya existe otro albarn con este nmero")); } } } public String siguienteNumero() { int maximo = 0; NumberFormat nf = NumberFormat.getInstance(); nf.setMinimumIntegerDigits(2); String regex = "A\\d+/"; String anio = nf.format(new Date().getYear() - 100); regex = regex.concat(anio); List<Albaran> list = albaranDAO.search(null); for (Albaran o : list) { String numero = o.getName(); if (numero != null && numero.matches(regex)) { int x = Integer.parseInt(numero.substring(1, numero.indexOf('/'))); if (x > maximo) maximo = x; } } nf.setMinimumIntegerDigits(3); return "A".concat(nf.format(maximo + 1)).concat("/").concat(anio); } /** * Serial version field */ private static final long serialVersionUID = -1L; /* albaran - generated by stajanov (do not edit/delete) */ /** * Logger */ private static final Log log = LogFactory.getLog(AlbaranBean.class); /** * Active search object */ private AlbaranSearch search = new AlbaranSearch(); /** * Manager */ private static AlbaranManager manager = AlbaranManager.getDefault(); /** * Upload service */ private static final Uploader uploader = UploaderFactory.getInstance("albaran"); /** * Active Albaran object */ private Albaran albaran; /** * Default sort column */ private String sortColumn = "name"; /** * Default sort order */ private boolean sortAscending = true; /** * Quick search letter for ABC pager control */ private Character letter; /** * List albarans. Order depends on Faces parameter sort. * * @return the list of all albarans sorted by requested criterion */ public List<Albaran> getAll() { return manager.getAllEntities(search, new SortCriteria(sortColumn, sortAscending)); } // 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 Albaran */ public boolean isCreateAvailable() { return SpringUtils.isRolePermissionGranted(Permission.Entity_Create(Albaran.class)); } /** * Whether or not edit button is available for user * * @return true if user can edit current object */ public boolean isEditAvailable() { return SpringUtils.isAclPermissionGranted(albaran, BasePermission.WRITE); } /** * Whether or not delete button is available for user * * @return true if user can delete current object */ public boolean isDeleteAvailable() { return (albaran.getId() != null) && SpringUtils.isAclPermissionGranted(albaran, 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() { albaran = new Albaran(); albaran.setName(siguienteNumero()); albaran.setFecha(new Date()); return NavigationResults.CREATE; } /** * Go to detail page * * @return forward to DETAIL page */ public String detail() { Integer id = Integer.parseInt(FacesUtils.getRequestParameter("id")); albaran = manager.getEntityById(id); return SpringUtils.isAclPermissionGranted(albaran, BasePermission.WRITE) ? NavigationResults.EDIT : NavigationResults.DETAIL; } /** * Save bean and stay on it * * @return forward to list page */ public String save() { doBeforeSave(); if (albaran.getId() == null) { manager.insertEntity(albaran); } else { manager.updateEntity(albaran); } // Calls an after save action String result = doAfterSave(NavigationResults.LIST); // Unselect object //albaran = null; return null; } /** * Delete bean and go back to beans list * * @return forward to LIST page */ public String delete() { manager.deleteEntity(albaran); albaran = null; return NavigationResults.LIST; } public boolean isPuedoBorrar() { return albaran != null && (albaran.getProjects() == null || albaran.getProjects().isEmpty()) && albaran.getBill() == null; } /** * 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 isAlbaranSelected() { return albaran != 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 AlbaranSearch getSearch() { return search; } public Set<Project> getSearchProjects() { return search.getProjects(); } public void setSearchProjects(Set<Project> val) { if (search.isProjectsSet()) { search.setProjects(val); } } public boolean isSearchProjectsValid() { return search.isProjectsSet(); } public void setSearchProjectsValid(boolean val) { if (val) { search.setProjects(search.getProjects()); } else { search.unsetProjects(); } } public Date getSearchStartInsertDate() { return search.getStartInsertDate(); } public void setSearchStartInsertDate(Date val) { if (search.isStartInsertDateSet()) { search.setStartInsertDate(val); } } 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 (search.isEndInsertDateSet()) { search.setEndInsertDate(val); } } public boolean isSearchEndInsertDateValid() { return search.isEndInsertDateSet(); } public void setSearchEndInsertDateValid(boolean val) { if (val) { search.setEndInsertDate(search.getEndInsertDate()); } else { search.unsetEndInsertDate(); } } 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 Boolean getSearchDevuelto() { return search.getDevuelto(); } public void setSearchDevuelto(Boolean val) { if (search.isDevueltoSet()) { search.setDevuelto(val); } } public boolean isSearchDevueltoValid() { return search.isDevueltoSet(); } public void setSearchDevueltoValid(boolean val) { if (val) { search.setDevuelto(search.getDevuelto()); } else { search.unsetDevuelto(); } } public Date getSearchStartUpdateDate() { return search.getStartUpdateDate(); } public void setSearchStartUpdateDate(Date val) { if (search.isStartUpdateDateSet()) { search.setStartUpdateDate(val); } } 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 (search.isEndUpdateDateSet()) { search.setEndUpdateDate(val); } } public boolean isSearchEndUpdateDateValid() { return search.isEndUpdateDateSet(); } public void setSearchEndUpdateDateValid(boolean val) { if (val) { search.setEndUpdateDate(search.getEndUpdateDate()); } else { search.unsetEndUpdateDate(); } } 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 getSearchInforme() { return search.getInforme(); } public void setSearchInforme(String val) { if (search.isInformeSet()) { search.setInforme(val); } } public boolean isSearchInformeValid() { return search.isInformeSet(); } public void setSearchInformeValid(boolean val) { if (val) { search.setInforme(search.getInforme()); } else { search.unsetInforme(); } } public Date getSearchStartFecha() { return search.getStartFecha(); } public void setSearchStartFecha(Date val) { if (search.isStartFechaSet()) { search.setStartFecha(val); } } public boolean isSearchStartFechaValid() { return search.isStartFechaSet(); } public void setSearchStartFechaValid(boolean val) { if (val) { search.setStartFecha(search.getStartFecha()); } else { search.unsetStartFecha(); } } public Date getSearchEndFecha() { return search.getEndFecha(); } public void setSearchEndFecha(Date val) { if (search.isEndFechaSet()) { search.setEndFecha(val); } } public boolean isSearchEndFechaValid() { return search.isEndFechaSet(); } public void setSearchEndFechaValid(boolean val) { if (val) { search.setEndFecha(search.getEndFecha()); } else { search.unsetEndFecha(); } } 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 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(); } } /** * Handle an ABC pager letter click: filter objects by specified starting letter */ public void letterClicked() { if (letter != null) { UIComponent comp = FacesUtils.getComponent("albarans: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 Albaran object public void setProjects(Set<Project> projects) { getAlbaran().setProjects(projects); } public Set<Project> getProjects() { return getAlbaran().getProjects(); } public void setProjectsLista(List<Project> projects) { albaran.setProjects(new HashSet<Project>(projects)); } public List<Project> getProjectsLista() { return new ArrayList<Project>(albaran.getProjects()); } class comparaProyectos implements Comparator<Project> { public int compare(Project o1, Project o2) { return o1.getNameDescriptivo().compareTo(o2.getNameDescriptivo()); } } public void seleccionfoItem(ValueChangeEvent vce) { Project p = (Project) vce.getNewValue(); p.setAlbaran(albaran); FacesUtils.renderResponse(); } public List<Project> getAllMyProjects() { ProjectDAO pdao = ProjectDAO.getDefault(); List<Project> ret = new ArrayList<Project>(); if (albaran.getClient() != null) { ProjectSearch ps = new ProjectSearch(); ps.setClient(albaran.getClient()); ps.setBillable(true); ps.setAlbaran(null); for (Project p : pdao.search(ps, null)) { ret.add(p); } ps.unsetClient(); ps.setAlbaran(albaran); if (albaran.getId() != null) { for (Project p : pdao.search(ps, null)) { ret.add(p); } } } Collections.sort(ret, new comparaProyectos()); return ret; } public Boolean getDevuelto() { return albaran.getDevuelto(); } public void setDevuelto(Boolean devuelto) { albaran.setDevuelto(devuelto); } public Date getUpdateDate() { return albaran.getUpdateDate(); } public void setUpdateDate(Date updateDate) { albaran.setUpdateDate(updateDate); } public String getName() { return albaran.getName(); } public void setName(String name) { albaran.setName(name); } public Date getFecha() { return albaran.getFecha(); } public void setFecha(Date fecha) { albaran.setFecha(fecha); } public Integer getId() { return albaran.getId(); } public Integer getOwnerId() { return albaran.getOwnerId(); } public void setOwnerId(Integer ownerId) { albaran.setOwnerId(ownerId); } /* albaran - generated by stajanov (do not edit/delete) */ private Integer idSelectedOrganization = 0; public Integer getIdSelectedOrganization() { idSelectedOrganization = ConfigurationUtil.getDefault().getIdOurCompany(); return idSelectedOrganization; } public void setIdSelectedOrganization(Integer idOrganization) { idSelectedOrganization = idOrganization; } public String quitaProject() { UIData table = (UIData) FacesUtils.getComponent("albaran:projects"); Project p = (Project) table.getRowData(); albaran.getProjects().remove(p); p.setAlbaran(null); return null; } public String anadeProject() { save(); UIData table = (UIData) FacesUtils.getComponent("albaran:projects"); Project p = (Project) table.getRowData(); if (albaran.getProjects() == null) albaran.setProjects(new HashSet<Project>()); albaran.getProjects().add(p); p.setAlbaran(albaran); return null; } public String doBeforeSave() { return null; } public Albaran getAlbaran() { return albaran; } public void setAlbaran(Albaran albaran) { this.albaran = albaran; } /** * 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) { if (ref.isEsCliente()) { ret.add(new SelectItem(ref, ref.getNameAcro())); } } ret.add(0, new SelectItem("")); return ret; } public List<SelectItem> getBills() { List<Bill> refs = new ArrayList<Bill>(); try { refs = BillManager.getDefault().getAllEntities(null, new SortCriteria("number")); } catch (Exception e) { e.printStackTrace(); } ArrayList<SelectItem> ret = new ArrayList<SelectItem>(); for (Bill ref : refs) { ret.add(new SelectItem(ref, ref.getNumber())); } ret.add(0, new SelectItem("")); return ret; } public Organization getClient() { return albaran.getClient(); } public void setClient(Organization client) { albaran.setClient(client); } public void onSelectedOrganizationChanged(ValueChangeEvent event) { setClient((Organization) event.getNewValue()); FacesUtils.renderResponse(); } public String detail_desde_fuera() { return "albaran_" + detail(); } }