use of com.axelor.apps.base.db.ICalendarEvent in project axelor-open-suite by axelor.
the class ICalendarService method findOrCreateEvent.
@Transactional
protected ICalendarEvent findOrCreateEvent(VEvent vEvent, ICalendar calendar) {
String uid = vEvent.getUid().getValue();
DtStart dtStart = vEvent.getStartDate();
DtEnd dtEnd = vEvent.getEndDate();
ICalendarEvent event = iEventRepo.findByUid(uid);
if (event == null) {
event = ICalendarEventFactory.getNewIcalEvent(calendar);
event.setUid(uid);
event.setCalendar(calendar);
}
ZoneId zoneId = OffsetDateTime.now().getOffset();
if (dtStart.getDate() != null) {
if (dtStart.getTimeZone() != null) {
zoneId = dtStart.getTimeZone().toZoneId();
}
event.setStartDateTime(LocalDateTime.ofInstant(dtStart.getDate().toInstant(), zoneId));
}
if (dtEnd.getDate() != null) {
if (dtEnd.getTimeZone() != null) {
zoneId = dtEnd.getTimeZone().toZoneId();
}
event.setEndDateTime(LocalDateTime.ofInstant(dtEnd.getDate().toInstant(), zoneId));
}
event.setAllDay(!(dtStart.getDate() instanceof DateTime));
event.setSubject(getValue(vEvent, Property.SUMMARY));
event.setDescription(getValue(vEvent, Property.DESCRIPTION));
event.setLocation(getValue(vEvent, Property.LOCATION));
event.setGeo(getValue(vEvent, Property.GEO));
event.setUrl(getValue(vEvent, Property.URL));
event.setSubjectTeam(event.getSubject());
if (Clazz.PRIVATE.getValue().equals(getValue(vEvent, Property.CLASS))) {
event.setVisibilitySelect(ICalendarEventRepository.VISIBILITY_PRIVATE);
} else {
event.setVisibilitySelect(ICalendarEventRepository.VISIBILITY_PUBLIC);
}
if (Transp.TRANSPARENT.getValue().equals(getValue(vEvent, Property.TRANSP))) {
event.setDisponibilitySelect(ICalendarEventRepository.DISPONIBILITY_AVAILABLE);
} else {
event.setDisponibilitySelect(ICalendarEventRepository.DISPONIBILITY_BUSY);
}
if (event.getVisibilitySelect() == ICalendarEventRepository.VISIBILITY_PRIVATE) {
event.setSubjectTeam(I18n.get("Available"));
if (event.getDisponibilitySelect() == ICalendarEventRepository.DISPONIBILITY_BUSY) {
event.setSubjectTeam(I18n.get("Busy"));
}
}
ICalendarUser organizer = findOrCreateUser(vEvent.getOrganizer(), event);
if (organizer != null) {
event.setOrganizer(organizer);
iCalendarUserRepository.save(organizer);
}
for (Object item : vEvent.getProperties(Property.ATTENDEE)) {
ICalendarUser attendee = findOrCreateUser((Property) item, event);
if (attendee != null) {
event.addAttendee(attendee);
iCalendarUserRepository.save(attendee);
}
}
iEventRepo.save(event);
return event;
}
use of com.axelor.apps.base.db.ICalendarEvent in project axelor-open-suite by axelor.
the class ICalendarService method doSync.
@Transactional(rollbackOn = { Exception.class })
protected ICalendar doSync(ICalendar calendar, CalDavCalendarCollection collection, LocalDateTime startDate, LocalDateTime endDate) throws IOException, URISyntaxException, ParseException, ObjectStoreException, ConstraintViolationException, DavException, ParserConfigurationException, ParserException, AxelorException {
final boolean keepRemote = calendar.getKeepRemote() == Boolean.TRUE;
final Map<String, VEvent> modifiedRemoteEvents = new HashMap<>();
final List<ICalendarEvent> modifiedLocalEvents = getICalendarEvents(calendar);
final Set<String> allRemoteUids = new HashSet<>();
final Set<VEvent> updatedEvents = new HashSet<>();
List<VEvent> events = null;
Instant lastSynchro = null;
if (calendar.getLastSynchronizationDateT() != null) {
lastSynchro = calendar.getLastSynchronizationDateT().toInstant(OffsetDateTime.now().getOffset());
} else {
lastSynchro = LocalDateTime.MIN.toInstant(OffsetDateTime.now().getOffset());
}
if (startDate == null || endDate == null) {
events = ICalendarStore.getModifiedEvents(collection, null, allRemoteUids);
} else {
events = ICalendarStore.getModifiedEventsInRange(collection, lastSynchro, allRemoteUids, startDate, endDate);
}
if (CollectionUtils.isEmpty(events) && CollectionUtils.isEmpty(modifiedLocalEvents)) {
throw new AxelorException(TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.CALENDAR_NO_EVENTS_FOR_SYNC_ERROR));
}
if (events != null) {
for (VEvent item : events) {
modifiedRemoteEvents.put(item.getUid().getValue(), item);
}
}
for (ICalendarEvent item : modifiedLocalEvents) {
VEvent source = createVEvent(item);
VEvent target = modifiedRemoteEvents.get(source.getUid().getValue());
// If uid is empty, the event is new
if (StringUtils.isBlank(item.getUid())) {
item.setUid(source.getUid().getValue());
Calendar cal = newCalendar();
cal.getComponents().add(source);
collection.addCalendar(cal);
allRemoteUids.add(item.getUid());
} else // else it has been modified
{
// if target is null, then it hasn't been modified or it has been deleted
if (target == null) {
target = source;
} else {
updateEvent(source, target, keepRemote);
// modifiedRemoteEvents.remove(target.getUid().getValue());
}
updatedEvents.add(target);
}
}
// corresponding ICalendarEvent
for (Map.Entry<String, VEvent> entry : modifiedRemoteEvents.entrySet()) {
findOrCreateEvent(entry.getValue(), calendar);
}
// update remote events
for (VEvent item : updatedEvents) {
Calendar cal = newCalendar();
cal.getComponents().add(item);
// collection.updateCalendar(cal);
try {
collection.addCalendar(cal);
} catch (Exception e) {
TraceBackService.trace(e);
}
}
// remove deleted remote events
removeDeletedEventsInRange(allRemoteUids, calendar, startDate, endDate);
return calendar;
}
use of com.axelor.apps.base.db.ICalendarEvent in project axelor-open-suite by axelor.
the class LeaveServiceImpl method cancel.
@Override
@Transactional(rollbackOn = { Exception.class })
public void cancel(LeaveRequest leaveRequest) throws AxelorException {
if (leaveRequest.getLeaveReason().getManageAccumulation()) {
manageCancelLeaves(leaveRequest);
}
if (leaveRequest.getIcalendarEvent() != null) {
ICalendarEvent event = leaveRequest.getIcalendarEvent();
leaveRequest.setIcalendarEvent(null);
icalEventRepo.remove(icalEventRepo.find(event.getId()));
}
leaveRequest.setStatusSelect(LeaveRequestRepository.STATUS_CANCELED);
}
use of com.axelor.apps.base.db.ICalendarEvent in project axelor-open-suite by axelor.
the class LeaveServiceImpl method createEvents.
@Override
@Transactional(rollbackOn = { Exception.class })
public LeaveRequest createEvents(LeaveRequest leave) throws AxelorException {
Employee employee = leave.getUser().getEmployee();
if (employee == null) {
throw new AxelorException(leave, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.LEAVE_USER_EMPLOYEE), leave.getUser().getName());
}
WeeklyPlanning weeklyPlanning = employee.getWeeklyPlanning();
if (weeklyPlanning == null) {
throw new AxelorException(leave, TraceBackRepository.CATEGORY_CONFIGURATION_ERROR, I18n.get(IExceptionMessage.EMPLOYEE_PLANNING), employee.getName());
}
LocalDateTime fromDateTime;
LocalDateTime toDateTime;
if (leave.getLeaveReason().getUnitSelect() == LeaveReasonRepository.UNIT_SELECT_DAYS) {
fromDateTime = getDefaultStart(weeklyPlanning, leave);
toDateTime = getDefaultEnd(weeklyPlanning, leave);
} else {
fromDateTime = leave.getFromDateT();
toDateTime = leave.getToDateT();
}
ICalendarEvent event = icalendarService.createEvent(fromDateTime, toDateTime, leave.getUser(), leave.getComments(), 4, leave.getLeaveReason().getName() + " " + leave.getUser().getFullName());
icalEventRepo.save(event);
leave.setIcalendarEvent(event);
return leave;
}
use of com.axelor.apps.base.db.ICalendarEvent in project axelor-open-suite by axelor.
the class ICalendarService method export.
/**
* Export the calendar to the given output writer.
*
* @param calendar the source {@link ICalendar}
* @param writer the output writer
* @throws IOException
* @throws ParseException
* @throws ValidationException
*/
public void export(ICalendar calendar, Writer writer) throws IOException, ParseException, ValidationException {
Preconditions.checkNotNull(calendar, "calendar can't be null");
Preconditions.checkNotNull(writer, "writer can't be null");
Preconditions.checkNotNull(getICalendarEvents(calendar), "can't export empty calendar");
Calendar cal = newCalendar();
cal.getProperties().add(new XProperty(X_WR_CALNAME, calendar.getName()));
for (ICalendarEvent item : getICalendarEvents(calendar)) {
VEvent event = createVEvent(item);
cal.getComponents().add(event);
}
CalendarOutputter outputter = new CalendarOutputter();
outputter.output(cal, writer);
}
Aggregations