Added Java Docs to the API for checking that application periods not overlap

This commit is contained in:
Tom Vahlman 2012-02-08 13:07:02 +01:00
parent 8fff1fb783
commit ae2165955e
2 changed files with 35 additions and 19 deletions
src/main/java/se/su/dsv/scipro

@ -86,11 +86,12 @@ public class ImporterFacade {
@PersistenceContext
private EntityManager entityManager;
private transient Logger logger = Logger.getLogger(ImporterFacade.class);
/**
/**
* If the supplied user is null, a new one will be created and persisted.
* Does not handle transient User instances, you probably have to do a dao-query for it before running this method.
* @param user
* @param dtoUser
* @param user hte user
* @param dtoUser the dtoUser
*/
@Transactional
public void mergeUser(final User user, final UserDTO dtoUser){

@ -14,18 +14,25 @@ import java.util.*;
@Service
public class ApplicationPeriodFacade {
@Autowired
@Autowired
private ApplicationPeriodDao applicationPeriodDao;
@Transactional
@Transactional
public void saveApplicationPeriod(final ApplicationPeriod period){
applicationPeriodDao.save(period);
}
@Transactional
/* returns true when the application period has been removed successfully */
public boolean removeApplicationPeriod(ApplicationPeriod appPeriod){
/**
* Returns true when the application period has been removed successfully,
* it should only be possible to remove/edit a period if the end date is after the present date,
* That is because the history should be kept.
* @param appPeriod the period that should be removed
* @return boolean the value is true when success false when failure
*/
@Transactional
public boolean removeApplicationPeriod(ApplicationPeriod appPeriod){
appPeriod = applicationPeriodDao.reLoad(appPeriod);
// only if the end date is after the present date, it should be possible to remove the period
if(appPeriod.getEndDate().after(Calendar.getInstance().getTime())) {
applicationPeriodDao.delete(appPeriod);
return true;
@ -34,7 +41,11 @@ public class ApplicationPeriodFacade {
}
}
/* returns true when an open application period exists for the submitted project class */
/**
* Returns true when an open application period exists for the submitted project class
* @param projectClass the project class that should be tested
* @return boolean the value is true when a period exists
*/
public boolean openApplicationPeriodsExists(final ProjectClass projectClass) {
Set<ProjectClass> projectClassSet = new HashSet<ProjectClass>();
projectClassSet.add(projectClass);
@ -50,38 +61,42 @@ public class ApplicationPeriodFacade {
return false;
}
/* returns true if the new period overlaps with some of the saved periods */
/**
* Returns true if the new period overlaps with some of the saved periods
* Different conditions that is handled is for example that a newPeriod that is edited cannot "overlap with itself" and
* that it should NOT be possible to create a period "within another period".
* @param newPeriod is the period that should be tested
* @return boolean the value is true when a period exists
*/
public boolean doesPeriodOverlap(final ApplicationPeriod newPeriod) {
for(ApplicationPeriod foundPeriod : applicationPeriodDao.findAll()) {
if(testForInvalidData(foundPeriod)) {
continue;
}
if(newPeriod.getId() != null && newPeriod.equals(foundPeriod)) { // a newPeriod that is edited cannot overlap with itself
if(newPeriod.getId() != null && newPeriod.equals(foundPeriod)) {
continue;
}
if(projectClassMatch(foundPeriod.getProjectClass(), newPeriod.getProjectClass()) &&
(newPeriod.getStartDate().equals(foundPeriod.getStartDate()) ||
(newPeriod.getStartDate().equals(foundPeriod.getStartDate()) || newPeriod.getEndDate().equals(foundPeriod.getEndDate()) ||
betweenDates(newPeriod.getStartDate(), foundPeriod.getStartDate(), foundPeriod.getEndDate()) ||
newPeriod.getEndDate().equals(foundPeriod.getEndDate()) ||
betweenDates(newPeriod.getEndDate(), foundPeriod.getStartDate(), foundPeriod.getEndDate()) ||
newPeriod.getStartDate().before(foundPeriod.getStartDate()) &&
newPeriod.getEndDate().after(foundPeriod.getEndDate()))) {
betweenDates(newPeriod.getEndDate(), foundPeriod.getStartDate(), foundPeriod.getEndDate()) ||
newPeriod.getStartDate().before(foundPeriod.getStartDate()) && newPeriod.getEndDate().after(foundPeriod.getEndDate()))) {
return true;
}
}
return false;
}
// to avoid NPEs when using faulty test data
// a helper method, to avoid NPEs when using faulty test data
private boolean testForInvalidData(final ApplicationPeriod applicationPeriod) {
return (applicationPeriod.getProjectClass() == null || applicationPeriod.getProjectClass().isEmpty() ||
applicationPeriod.getStartDate() == null || applicationPeriod.getEndDate() == null);
}
/* returns true if the new date is between the old periods start date and end date */
/* the method returns true if the new date is between the old periods start date and end date */
private boolean betweenDates(final Date date, final Date startDate, final Date endDate) {
return date.after(startDate) && date.before(endDate);
}