/**
* 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.contacts;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.faces.component.UIData;
import javax.faces.model.SelectItem;
import org.acegisecurity.acls.domain.BasePermission;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.autentia.tnt.bean.BaseBean;
import com.autentia.tnt.bean.NavigationResults;
import com.autentia.tnt.businessobject.Position;
import com.autentia.tnt.businessobject.PositionChange;
import com.autentia.tnt.businessobject.Province;
import com.autentia.tnt.businessobject.Tag;
import com.autentia.tnt.dao.SortCriteria;
import com.autentia.tnt.dao.search.PositionSearch;
import com.autentia.tnt.manager.admin.ProvinceManager;
import com.autentia.tnt.manager.contacts.PositionManager;
import com.autentia.tnt.manager.security.AuthenticationManager;
import com.autentia.tnt.manager.security.Permission;
import com.autentia.tnt.manager.tags.TagManager;
import com.autentia.tnt.upload.Uploader;
import com.autentia.tnt.upload.UploaderFactory;
import com.autentia.tnt.util.FacesUtils;
import com.autentia.tnt.util.SpringUtils;
public class PositionBean extends BaseBean {
/* Position - generated by stajanov (do not edit/delete) */
private static final long serialVersionUID = -7677005521837429777L;
/** Logger */
private static final Log log = LogFactory.getLog(PositionBean.class);
/** Active search object */
private PositionSearch search = new PositionSearch();
/** Manager */
private static PositionManager manager = PositionManager.getDefault();
/** Upload service */
private static final Uploader uploader = UploaderFactory.getInstance("position");
/** Active Position object */
private Position position;
/** Default sort column */
private String sortColumn = "name";
/** Default sort order */
private boolean sortAscending = true;
/** Manager */
private static AuthenticationManager authManager = AuthenticationManager.getDefault();
private Set<Tag> historicalTags = new HashSet<Tag>();
/**
* List positions. Order depends on Faces parameter sort.
*
* @return the list of all positions sorted by requested criterion
*/
public List<Position> getAll() {
final PositionSearch allNotDeletedSearch = new PositionSearch();
allNotDeletedSearch.setDeleted(false);
return manager.getAllEntities(allNotDeletedSearch, 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 Position
*/
public boolean isCreateAvailable() {
return SpringUtils.isRolePermissionGranted(Permission.Entity_Create(Position.class));
}
/**
* Whether or not edit button is available for user
*
* @return true if user can edit current object
*/
public boolean isEditAvailable() {
return SpringUtils.isAclPermissionGranted(position, BasePermission.WRITE);
}
/**
* Whether or not delete button is available for user
*
* @return true if user can delete current object
*/
public boolean isDeleteAvailable() {
return (position != null && position.getId() != null)
&& SpringUtils.isAclPermissionGranted(position, BasePermission.DELETE);
}
/**
* Go to create page
*
* @return forward to CREATE page
*/
public String create() {
position = new Position();
position.setInsertDate(new Date());
return NavigationResults.CREATE;
}
/**
* Go to detail page
*
* @return forward to DETAIL page
*/
public String detail() {
Integer id = Integer.parseInt(FacesUtils.getRequestParameter(ROW_ID));
position = manager.getEntityById(id);
return SpringUtils.isAclPermissionGranted(position, BasePermission.WRITE) ? NavigationResults.EDIT
: NavigationResults.DETAIL;
}
/**
* Save bean and stay on it
*
* @return forward to list page
*/
public String save() {
doBeforeSave();
if (position.getId() == null) {
manager.insertEntity(position);
} else {
manager.updateEntity(position);
}
// Calls an after save action
String result = doAfterSave(NavigationResults.LIST);
// Unselect object
position = null;
return result;
}
/**
* Delete bean and go back to beans list
*
* @return forward to LIST page
*/
public String delete() {
position.setDeleteDate(new Date());
manager.updateEntity(position);
position = null;
return NavigationResults.LIST;
}
/**
* Go back to beans list
*
* @return forward to LIST page
*/
public String list() {
return NavigationResults.LIST;
}
/**
* Reset search criteria
*
* @return forward to LIST page
*/
public String reset() {
search.reset();
return 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 isPositionSelected() {
return position != 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 PositionSearch getSearch() {
return search;
}
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 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 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 Date getSearchStartDeleteDate() {
return search.getStartDeleteDate();
}
public void setSearchStartDeleteDate(Date val) {
if (val != null) {
search.setStartDeleteDate(val);
} else {
search.unsetStartDeleteDate();
}
}
public boolean isSearchStartDeleteDateValid() {
return search.isStartDeleteDateSet();
}
public void setSearchStartDeleteDateValid(boolean val) {
if (val) {
search.setStartDeleteDate(search.getStartDeleteDate());
} else {
search.unsetStartDeleteDate();
}
}
public Date getSearchEndDeleteDate() {
return search.getEndDeleteDate();
}
public void setSearchEndDeleteDate(Date val) {
if (val != null) {
search.setEndDeleteDate(val);
} else {
search.unsetEndDeleteDate();
}
}
public boolean isSearchEndDeleteDateValid() {
return search.isEndDeleteDateSet();
}
public void setSearchEndDeleteDateValid(boolean val) {
if (val) {
search.setEndDeleteDate(search.getEndDeleteDate());
} else {
search.unsetEndDeleteDate();
}
}
// Getters and setters to handle uploads
// Getters and setters to manipulate active Position object
public java.lang.Integer getId() {
return position.getId();
}
public Integer getOwnerId() {
return position.getOwnerId();
}
public void setOwnerId(Integer ownerId) {
position.setOwnerId(ownerId);
}
public Integer getDepartmentId() {
return position.getDepartmentId();
}
public void setDepartmentId(Integer departmentId) {
position.setDepartmentId(departmentId);
}
public String getName() {
return position.getName();
}
public void setName(String name) {
position.setName(name);
}
public String getDescription() {
return position.getDescription();
}
public void setDescription(String description) {
position.setDescription(description);
}
public Date getInsertDate() {
return position.getInsertDate();
}
public void setInsertDate(Date insertDate) {
position.setInsertDate(insertDate);
}
public Date getUpdateDate() {
return position.getUpdateDate();
}
public void setUpdateDate(Date updateDate) {
position.setUpdateDate(updateDate);
}
public Date getDeleteDate() {
return position.getDeleteDate();
}
public void setDeleteDate(Date deleteDate) {
position.setDeleteDate(deleteDate);
}
/**
* @return
* @see com.autentia.tnt.dao.search.PositionSearch#getArguments()
*/
public Object[] getArguments() {
return search.getArguments();
}
/**
* @return
* @see com.autentia.tnt.dao.search.PositionSearch#getDeleted()
*/
public Boolean getDeleted() {
return search.getDeleted();
}
/**
* @return
* @see com.autentia.tnt.dao.search.PositionSearch#getEndDeleteDate()
*/
public Date getEndDeleteDate() {
return search.getEndDeleteDate();
}
/**
* @return
* @see com.autentia.tnt.dao.search.PositionSearch#getEndInsertDate()
*/
public Date getEndInsertDate() {
return search.getEndInsertDate();
}
/**
* @return
* @see com.autentia.tnt.dao.search.PositionSearch#getEndUpdateDate()
*/
public Date getEndUpdateDate() {
return search.getEndUpdateDate();
}
/**
* @return
* @see com.autentia.tnt.dao.search.PositionSearch#getStartDeleteDate()
*/
public Date getStartDeleteDate() {
return search.getStartDeleteDate();
}
/**
* @return
* @see com.autentia.tnt.dao.search.PositionSearch#getStartInsertDate()
*/
public Date getStartInsertDate() {
return search.getStartInsertDate();
}
/**
* @return
* @see com.autentia.tnt.dao.search.PositionSearch#getStartUpdateDate()
*/
public Date getStartUpdateDate() {
return search.getStartUpdateDate();
}
/**
* @return
* @see com.autentia.tnt.dao.search.PositionSearch#isDeletedSet()
*/
public boolean isDeletedSet() {
return search.isDeletedSet();
}
/**
* @return
* @see com.autentia.tnt.dao.search.PositionSearch#isDepartmentIdSet()
*/
public boolean isDepartmentIdSet() {
return search.isDepartmentIdSet();
}
/**
* @return
* @see com.autentia.tnt.dao.search.PositionSearch#isDescriptionSet()
*/
public boolean isDescriptionSet() {
return search.isDescriptionSet();
}
/**
* @return
* @see com.autentia.tnt.dao.search.PositionSearch#isEndDeleteDateSet()
*/
public boolean isEndDeleteDateSet() {
return search.isEndDeleteDateSet();
}
/**
* @return
* @see com.autentia.tnt.dao.search.PositionSearch#isEndInsertDateSet()
*/
public boolean isEndInsertDateSet() {
return search.isEndInsertDateSet();
}
/**
* @return
* @see com.autentia.tnt.dao.search.PositionSearch#isEndUpdateDateSet()
*/
public boolean isEndUpdateDateSet() {
return search.isEndUpdateDateSet();
}
/**
* @return
* @see com.autentia.tnt.dao.search.PositionSearch#isNameSet()
*/
public boolean isNameSet() {
return search.isNameSet();
}
/**
* @return
* @see com.autentia.tnt.dao.search.PositionSearch#isOwnerIdSet()
*/
public boolean isOwnerIdSet() {
return search.isOwnerIdSet();
}
/**
* @return
* @see com.autentia.tnt.dao.search.PositionSearch#isSearchActive()
*/
public boolean isSearchActive() {
return search.isSearchActive();
}
/**
* @return
* @see com.autentia.tnt.dao.search.PositionSearch#isStartDeleteDateSet()
*/
public boolean isStartDeleteDateSet() {
return search.isStartDeleteDateSet();
}
/**
* @return
* @see com.autentia.tnt.dao.search.PositionSearch#isStartInsertDateSet()
*/
public boolean isStartInsertDateSet() {
return search.isStartInsertDateSet();
}
/**
* @return
* @see com.autentia.tnt.dao.search.PositionSearch#isStartUpdateDateSet()
*/
public boolean isStartUpdateDateSet() {
return search.isStartUpdateDateSet();
}
/**
* @param deleted
* @see com.autentia.tnt.dao.search.PositionSearch#setDeleted(java.lang.Boolean)
*/
public void setDeleted(Boolean deleted) {
search.setDeleted(deleted);
}
/**
* @param endDeleteDate
* @see com.autentia.tnt.dao.search.PositionSearch#setEndDeleteDate(java.util.Date)
*/
public void setEndDeleteDate(Date endDeleteDate) {
search.setEndDeleteDate(endDeleteDate);
}
/**
* @param endInsertDate
* @see com.autentia.tnt.dao.search.PositionSearch#setEndInsertDate(java.util.Date)
*/
public void setEndInsertDate(Date endInsertDate) {
search.setEndInsertDate(endInsertDate);
}
/**
* @param endUpdateDate
* @see com.autentia.tnt.dao.search.PositionSearch#setEndUpdateDate(java.util.Date)
*/
public void setEndUpdateDate(Date endUpdateDate) {
search.setEndUpdateDate(endUpdateDate);
}
/**
* @param startDeleteDate
* @see com.autentia.tnt.dao.search.PositionSearch#setStartDeleteDate(java.util.Date)
*/
public void setStartDeleteDate(Date startDeleteDate) {
search.setStartDeleteDate(startDeleteDate);
}
/**
* @param startInsertDate
* @see com.autentia.tnt.dao.search.PositionSearch#setStartInsertDate(java.util.Date)
*/
public void setStartInsertDate(Date startInsertDate) {
search.setStartInsertDate(startInsertDate);
}
/**
* @param startUpdateDate
* @see com.autentia.tnt.dao.search.PositionSearch#setStartUpdateDate(java.util.Date)
*/
public void setStartUpdateDate(Date startUpdateDate) {
search.setStartUpdateDate(startUpdateDate);
}
/**
* @see com.autentia.tnt.dao.search.PositionSearch#unsetDeleted()
*/
public void unsetDeleted() {
search.unsetDeleted();
}
/**
* @see com.autentia.tnt.dao.search.PositionSearch#unsetDepartmentId()
*/
public void unsetDepartmentId() {
search.unsetDepartmentId();
}
/**
* @see com.autentia.tnt.dao.search.PositionSearch#unsetDescription()
*/
public void unsetDescription() {
search.unsetDescription();
}
/**
* @see com.autentia.tnt.dao.search.PositionSearch#unsetEndDeleteDate()
*/
public void unsetEndDeleteDate() {
search.unsetEndDeleteDate();
}
/**
* @see com.autentia.tnt.dao.search.PositionSearch#unsetEndInsertDate()
*/
public void unsetEndInsertDate() {
search.unsetEndInsertDate();
}
/**
* @see com.autentia.tnt.dao.search.PositionSearch#unsetEndUpdateDate()
*/
public void unsetEndUpdateDate() {
search.unsetEndUpdateDate();
}
/**
* @see com.autentia.tnt.dao.search.PositionSearch#unsetName()
*/
public void unsetName() {
search.unsetName();
}
/**
* @see com.autentia.tnt.dao.search.PositionSearch#unsetOwnerId()
*/
public void unsetOwnerId() {
search.unsetOwnerId();
}
/**
* @see com.autentia.tnt.dao.search.PositionSearch#unsetStartDeleteDate()
*/
public void unsetStartDeleteDate() {
search.unsetStartDeleteDate();
}
/**
* @see com.autentia.tnt.dao.search.PositionSearch#unsetStartInsertDate()
*/
public void unsetStartInsertDate() {
search.unsetStartInsertDate();
}
/**
* @see com.autentia.tnt.dao.search.PositionSearch#unsetStartUpdateDate()
*/
public void unsetStartUpdateDate() {
search.unsetStartUpdateDate();
}
public boolean isSearchDeletedValid() {
return search.isDeletedSet();
}
public void setSearchDeletedValid(boolean val) {
if (val) {
search.setDeleted(search.getDeleted());
} else {
search.unsetDeleted();
}
}
public java.lang.Boolean getSearchDeleted() {
return search.getDeleted();
}
public void setSearchDeleted(java.lang.Boolean val) {
if (search.isDeletedSet()) {
search.setDeleted(val);
}
}
/* Position - generated by stajanov (do not edit/delete) */
private Tag selectedTag;
public Tag getSelectedTag() {
return selectedTag;
}
public void setSelectedTag(Tag selectedTag) {
this.selectedTag = selectedTag;
}
public List<SelectItem> getAllTags() {
final List<Tag> refs = TagManager.getDefault().getAllEntities(null, new SortCriteria("id"));
final List<SelectItem> ret = new ArrayList<SelectItem>();
for (Tag ref : refs) {
ret.add(new SelectItem(ref, ref.getName()));
}
return ret;
}
public String addTag() {
position.getTags().add(getSelectedTag());
return NavigationResults.EDIT;
}
public Set<Tag> getDepartmentTags() {
return position.getTags();
}
public void setTags(Set<Tag> tags) {
position.setTags(new HashSet<Tag>(tags));
}
public String deleteTag() {
final UIData table = (UIData)FacesUtils.getComponent("position:tags");
final Tag toDelete = (Tag)table.getRowData();
position.getTags().remove(toDelete);
return NavigationResults.EDIT;
}
/**
* Devuelve el histórico del puesto
*/
public Set<PositionChange> getHistory() {
return position.getHistory();
}
/**
* Navega hacia la página de histórico
*/
public String showHistory() {
Integer id = Integer.parseInt(FacesUtils.getRequestParameter(ROW_ID));
position = manager.getEntityById(id);
return "changes";
}
public boolean isAreTagsToSelect() {
return !getAllTags().isEmpty();
}
public boolean isAreTagsSelected() {
return !position.getTags().isEmpty();
}
public String getEmail() {
return position.getEmail();
}
public void setEmail(String email) {
position.setEmail(email);
}
public String getPhone() {
return position.getPhone();
}
public void setPhone(String phone) {
position.setPhone(phone);
}
public String getFax() {
return position.getFax();
}
public void setFax(String fax) {
position.setFax(fax);
}
public String getCountry() {
return position.getCountry();
}
public void setCountry(String country) {
position.setCountry(country);
}
public String getCity() {
return position.getCity();
}
public void setCity(String city) {
position.setCity(city);
}
public Province getProvince() {
return position.getProvince();
}
public void setProvince(Province province) {
position.setProvince(province);
}
public void setPostalCode(String postalCode) {
position.setPostalCode(postalCode);
}
public String getPostalCode() {
return position.getPostalCode();
}
public void setAddress(String address) {
position.setAddress(address);
}
public String getAddress() {
return position.getAddress();
}
/**
* Gets all provinces
*
* @return
*/
public List<SelectItem> getProvinces() {
List<Province> refs = ProvinceManager.getDefault().getAllEntities(null, new SortCriteria("name"));
ArrayList<SelectItem> ret = new ArrayList<SelectItem>();
ret.add(new SelectItem(null, FacesUtils.getMessage("seleccion_sinSeleccion")));
for (Province ref : refs) {
ret.add(new SelectItem(ref, ref.getName()));
}
return ret;
}
}