Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

116 nel calendario ical turni rendere opzionale type e year #117

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
### Changed
- Rimossa redirect in caso di cancellazione di un orario di lavoro particolare che generava loop infinito
- Resi opzionali parametri type e year nel ical dei turni

## [2.12.0] - 2024-01-15
### Added
Expand Down
3 changes: 2 additions & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
2.12.1
2.12.1

2 changes: 1 addition & 1 deletion app/controllers/Calendar.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ public static void events(long activityId, LocalDate start, LocalDate end)
private static List<ShiftEvent> shiftEvents(ShiftType shiftType, Person person, LocalDate start,
LocalDate end, EventColor color) {

return shiftDao.getPersonShiftDaysByPeriodAndType(start, end, shiftType, person).stream()
return shiftDao.getPersonShiftDaysByPeriodAndType(start, end, Optional.of(shiftType), person).stream()
.map(shiftDay -> {
LocalTime begin = null;
LocalTime finish = null;
Expand Down
59 changes: 36 additions & 23 deletions app/controllers/Shift.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.inject.Inject;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -64,7 +65,10 @@
import net.fortuna.ical4j.data.CalendarOutputter;
import net.fortuna.ical4j.model.Calendar;
import org.allcolor.yahp.converter.IHtmlToPdfTransformer;
import org.assertj.core.util.Sets;
import org.joda.time.LocalDate;
import org.testng.collections.Lists;

import play.data.binding.As;
import play.data.validation.Required;
import play.data.validation.Validation;
Expand Down Expand Up @@ -188,7 +192,7 @@ public static void find(

// get the cancelled shifts of type shiftType
List<ShiftCancelled> shiftCancelled =
shiftDao.getShiftCancelledByPeriodAndType(from, to, shiftType);
shiftDao.getShiftCancelledByPeriodAndType(from, to, Optional.of(shiftType));
log.debug("ShiftCancelled find called from {} to {}, type {} - found {} shift days",
from, to, shiftType.getType(), shiftCancelled.size());

Expand Down Expand Up @@ -512,27 +516,38 @@ public static void absence(
* Restituisce la informazioni sul turno in formato iCal.
*/
@BasicAuth
public static void ical(@Required String type, @Required int year) {
if (Validation.hasErrors()) {
badRequest("Parametri mancanti. " + Validation.errors());
public static void ical(String type, Integer year) {
if (year == null) {
year = LocalDate.now().getYear();
}
Optional<User> currentUser = Security.getUser();
log.debug("Shift::ical type={}, year={}, currentUser={}", type, year, currentUser);

response.accessControl("*");

ShiftType shiftType = shiftDao.getShiftTypeByType(type);

if (shiftType == null) {
notFound(String.format("ShiftType type = %s doesn't exist", type));
Set<ShiftType> shiftTypes = Sets.newHashSet();
if (type != null) {
ShiftType shiftType = shiftDao.getShiftTypeByType(type);
if (shiftType == null) {
log.info("ShiftType = {} not found", shiftType);
notFound(String.format("ShiftType type = %s doesn't exist", type));
} else {
shiftTypes.add(shiftType);
}
} else {
if (!currentUser.isPresent() || currentUser.get().getPerson() == null) {
notFound(String.format("L'utente corrente %s non ha associato turni", currentUser.get().getUsername()));
} else {
shiftTypes.addAll(shiftDao.getShiftTypesForPerson(currentUser.get().getPerson()));
}
}

ImmutableList<Person> canAccess =
ImmutableList.<Person>builder()
.addAll(personDao.getPersonForShift(type,
LocalDate.now().withYear(year).monthOfYear().withMinimumValue()
.dayOfMonth().withMinimumValue()))
.add(shiftType.getShiftCategories().getSupervisor()).build();
log.debug("Shift::ical types={}, year={}, currentUser={}", shiftTypes, year, currentUser);

response.accessControl("*");
final Integer shiftYear = year;
List<Person> canAccess = Lists.newArrayList();
shiftTypes.forEach(st ->{
canAccess.addAll(personDao.getPersonForShift(st.getType(),
LocalDate.now().withYear(shiftYear).monthOfYear().withMinimumValue()
.dayOfMonth().withMinimumValue()));
});

if (!currentUser.isPresent() || currentUser.get().getPerson() == null
|| !canAccess.contains(currentUser.get().getPerson())) {
Expand All @@ -545,12 +560,12 @@ public static void ical(@Required String type, @Required int year) {
Long personId = currentUser.get().getPerson().getId();
try {
Optional<Calendar> calendar =
shiftManager.createCalendar(type, Optional.fromNullable(personId), year);
shiftManager.createCalendar(personId, year);
if (!calendar.isPresent()) {
log.warn("Impossible to create shift calendar for personId = {}, type = {}, year = {}",
personId, type, year);
notFound(
String.format("Person id = %d is not associated to a shift of type = %s",
String.format("Person id = %d is not associated to a shift of type = %s",
personId, type));
}

Expand All @@ -560,14 +575,12 @@ public static void ical(@Required String type, @Required int year) {

response.setHeader("Content-Type", "application/ics");
InputStream is = new ByteArrayInputStream(bos.toByteArray());
renderBinary(is, "reperibilitaRegistro.ics");
renderBinary(is, "turni.ics");
bos.close();
is.close();
} catch (IOException ex) {
log.error("Io exception building ical", ex);
}
}



}
56 changes: 42 additions & 14 deletions app/dao/ShiftDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,19 @@ public List<PersonShiftDay> getShiftDaysByPeriodAndType(
* periodo tra 'begin' e 'to'.
*/
public List<PersonShiftDay> getPersonShiftDaysByPeriodAndType(
LocalDate begin, LocalDate to, ShiftType type, Person person) {
LocalDate begin, LocalDate to, Optional<ShiftType> type, Person person) {
final QPersonShiftDay psd = QPersonShiftDay.personShiftDay;
BooleanBuilder conditions = new BooleanBuilder(psd.date.between(begin, to));
conditions.and(psd.personShift.person.eq(person));
if (type.isPresent()) {
conditions.and(psd.shiftType.eq(type.get()));
}
return getQueryFactory().selectFrom(psd)
.where(psd.date.between(begin, to)
.and(psd.shiftType.eq(type))
.and(psd.personShift.person.eq(person)))
.where(conditions)
.orderBy(psd.organizationShiftSlot.name.asc(), psd.date.asc())
.fetch();
}


/**
* La lista dei turni cancellati tra from e to dell'attività type.
*
Expand All @@ -142,11 +144,13 @@ public List<PersonShiftDay> getPersonShiftDaysByPeriodAndType(
* 'to'.
*/
public List<ShiftCancelled> getShiftCancelledByPeriodAndType(
LocalDate from, LocalDate to, ShiftType type) {
LocalDate from, LocalDate to, Optional<ShiftType> type) {
final QShiftCancelled sc = QShiftCancelled.shiftCancelled;
return getQueryFactory().selectFrom(sc)
.where(sc.date.between(from, to).and(sc.type.eq(type))).orderBy(sc.date.asc())
.fetch();
BooleanBuilder conditions = new BooleanBuilder(sc.date.between(from, to));
if (type.isPresent()) {
conditions.and(sc.type.eq(type.get()));
}
return getQueryFactory().selectFrom(sc).where(conditions).orderBy(sc.date.asc()).fetch();
}

/**
Expand Down Expand Up @@ -190,6 +194,24 @@ public Long deletePersonShiftDay(PersonShiftDay personShiftDay) {
}


/**
* L'associazione persona/attività se esiste sul db.
*
* @param personId l'id della persona
* @param type il nome dell'attività
* @return il PersonShift relativo alla persona person e al tipo type passati come parametro.
*/
public List<PersonShift> getPersonShiftByPerson(Long personId) {
final QPersonShiftShiftType psst = QPersonShiftShiftType.personShiftShiftType;

BooleanBuilder conditions =
new BooleanBuilder(psst.personShift.person.id.eq(personId));
conditions.and(psst.beginDate.isNull().or(psst.beginDate.loe(LocalDate.now())));
conditions.and(psst.endDate.isNull().or(psst.endDate.goe(LocalDate.now())));
return getQueryFactory().select(psst.personShift).from(psst)
.where(conditions).fetch();
}

/**
* L'associazione persona/attività se esiste sul db.
*
Expand All @@ -200,12 +222,13 @@ public Long deletePersonShiftDay(PersonShiftDay personShiftDay) {
public PersonShift getPersonShiftByPersonAndType(Long personId, String type) {
final QPersonShiftShiftType psst = QPersonShiftShiftType.personShiftShiftType;

BooleanBuilder conditions =
new BooleanBuilder(psst.personShift.person.id.eq(personId));
conditions.and(psst.beginDate.isNull().or(psst.beginDate.loe(LocalDate.now())));
conditions.and(psst.endDate.isNull().or(psst.endDate.goe(LocalDate.now())));
conditions.and(psst.shiftType.type.eq(type));
return getQueryFactory().select(psst.personShift).from(psst)
.where(psst.personShift.person.id.eq(personId)
.and(psst.shiftType.type.eq(type))
.and(psst.beginDate.isNull().or(psst.beginDate.loe(LocalDate.now())))
.and(psst.endDate.isNull().or(psst.endDate.goe(LocalDate.now())))
).fetchOne();
.where(conditions).fetchOne();
}

/**
Expand Down Expand Up @@ -394,5 +417,10 @@ public List<PersonShiftShiftType> getByPersonShiftAndDate(
.fetch();
}

public List<ShiftType> getShiftTypesForPerson(Person person) {
final QPersonShiftShiftType psst = QPersonShiftShiftType.personShiftShiftType;
return getQueryFactory().selectFrom(psst).select(psst.shiftType)
.where(psst.personShift.person.eq(person)).fetch();
}

}
Loading
Loading