/* * Created on Jan 12, 2005 * */ package net.sourceforge.fenixedu.domain.projectsManagement; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import net.sourceforge.fenixedu.domain.Identification; import net.sourceforge.fenixedu.domain.Login; import net.sourceforge.fenixedu.domain.Person; import net.sourceforge.fenixedu.domain.RootDomainObject; import org.joda.time.DateTime; import org.joda.time.Interval; import com.linkare.commons.metainfo.Linkare; /** * @author Susana Fernandes * */ public class ProjectAccess extends ProjectAccess_Base { public ProjectAccess() { super(); setRootDomainObject(RootDomainObject.getInstance()); } /** * @return Returns the beginDate. */ public Calendar getBeginDate() { if (this.getBegin() != null) { Calendar result = Calendar.getInstance(); result.setTime(this.getBegin()); return result; } return null; } /** * @param beginDate * The beginDate to set. */ public void setBeginDate(Calendar beginDate) { if (beginDate != null) { this.setBegin(beginDate.getTime()); } else { this.setBegin(null); } } /** * @return Returns the endDate. */ public Calendar getEndDate() { if (this.getEnd() != null) { Calendar result = Calendar.getInstance(); result.setTime(this.getEnd()); return result; } return null; } /** * @param endDate * The endDate to set. */ public void setEndDate(Calendar endDate) { if (endDate != null) { this.setEnd(endDate.getTime()); } else { this.setEnd(null); } } public Interval getProjectAccessInterval() { return new Interval(getBeginDateTime(), getEndDateTime()); } public void delete() { removePerson(); removeRootDomainObject(); deleteDomainObject(); } private static List getAllByPersonUsernameAndCoordinatorOrCostCenterOrProject(String username, Integer coordinatorCode, Integer costCenter, Integer projectCode, boolean all, boolean it) { List result = new ArrayList(); Date currentDate = Calendar.getInstance().getTime(); outter: for (ProjectAccess projectAccess : RootDomainObject.getInstance().getProjectAccesss()) { if (it != projectAccess.getItProject().booleanValue()) { continue; } if (projectCode != null && !projectCode.equals(projectAccess.getKeyProject())) { continue; } if (coordinatorCode != null && !coordinatorCode.equals(projectAccess.getKeyProjectCoordinator())) { continue; } if (costCenter != null) { if (!costCenter.equals(projectAccess.getKeyProjectCoordinator())) { continue; } if (!projectAccess.getCostCenter()) { continue; } } else { if (coordinatorCode == null && projectCode == null && projectAccess.getCostCenter()) { continue; } } if (projectAccess.getEnd().before(currentDate)) { continue; } // skip projects accesses that begin in the future if (!all && projectAccess.getBegin().after(currentDate)) { continue; } for (Identification identification : projectAccess.getPerson().getUser().getIdentifications()) { if (identification instanceof Login) { Login login = (Login) identification; if (!login.hasUsername(username)) { continue outter; } } } result.add(projectAccess); } return result; } public static List getAllByPersonUsernameAndCoordinator(String username, Integer coordinatorCode, boolean all, boolean it) { return getAllByPersonUsernameAndCoordinatorOrCostCenterOrProject(username, coordinatorCode, null, null, all, it); } @Linkare(author = "Paulo Zenida", comments = "Used Integer.valueOf() instead of new Integer()") public static List getAllByPersonUsernameAndDatesAndCostCenter(String username, String costCenter, boolean it) { Integer costCenterCode = costCenter == null || costCenter.length() == 0 ? null : Integer.valueOf(costCenter); return getAllByPersonUsernameAndCoordinatorOrCostCenterOrProject(username, null, costCenterCode, null, false, it); } public static ProjectAccess getByPersonAndProject(Person person, Integer projectCode, boolean it) { List projectAccesses = person.getProjectAccesses(); for (ProjectAccess access : projectAccesses) { if (projectCode.equals(access.getKeyProject()) && access.getItProject().equals(it)) { return access; } } return null; } public static ProjectAccess getByUsernameAndProjectCode(String username, Integer projectCode, boolean it) { List result = getAllByPersonUsernameAndCoordinatorOrCostCenterOrProject(username, null, null, projectCode, false, it); if (result.isEmpty()) { return null; } return result.get(0); } public static List getAllByCoordinator(Integer coordinatorCode, boolean it) { List result = new ArrayList(); Date currentDate = Calendar.getInstance().getTime(); for (ProjectAccess access : RootDomainObject.getInstance().getProjectAccesss()) { if (!access.getItProject().equals(it)) { continue; } if (access.getEnd().before(currentDate)) { continue; } if (!access.getKeyProjectCoordinator().equals(coordinatorCode)) { continue; } result.add(access); } return result; } public static List getAllByPersonAndCostCenter(Person person, boolean isCostCenter, boolean limitDates, boolean it) { List result = new ArrayList(); Date currentDate = Calendar.getInstance().getTime(); for (ProjectAccess access : person.getProjectAccesses()) { if (limitDates) { if (access.getBegin().after(currentDate)) { continue; } if (access.getEnd().before(currentDate)) { continue; } } if (!access.getItProject().equals(it)) { continue; } if (!Boolean.valueOf(isCostCenter).equals(access.getCostCenter())) { continue; } result.add(access); } return result; } public static List getAllByPerson(Person person, boolean it) { List result = new ArrayList(); DateTime currentDate = new DateTime(); for (ProjectAccess projectAccess : RootDomainObject.getInstance().getProjectAccesss()) { if (projectAccess.getEndDateTime().isAfter(currentDate) && projectAccess.getBeginDateTime().isBefore(currentDate)) { if (projectAccess.getPerson().equals(person) && projectAccess.getItProject().equals(it)) { result.add(projectAccess); } } } return result; } public boolean isActive(DateTime date) { return ((!getBeginDateTime().isAfter(date)) && (!getEndDateTime().isBefore(date))); } }