Commit 532d1d70 authored by Cristian Aguirre's avatar Cristian Aguirre

Calendar

parent dfca8e2e
package com.bytesw.bytebot.bean;
import com.google.gson.annotations.Expose;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
import java.util.List;
@Getter
@Setter
public class CalendarBean implements Serializable {
@Expose
private String id;
@Expose private long version;
@Expose private String name;
// @TODO: Revisar List<WeekScheduler> weekSchedulerList de la clase Calendar
// @Expose private List<WeekSchedulerBean> weekSchedulerBeanList;
// @Expose private List<CalendarExceptionBean> calendarExceptionBeanList;
@Expose private List<String> weekSchedulerBeanList;
@Expose private List<String> calendarExceptionBeanList;
}
\ No newline at end of file
package com.bytesw.bytebot.controller;
import com.bytesw.bytebot.bean.CalendarBean;
import com.bytesw.xdf.annotation.ProgramSecurity;
import com.bytesw.xdf.controller.XDFController;
import com.bytesw.bytebot.service.CalendarService;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController()
@RequestMapping("/service/settings/calendar")
@ProgramSecurity("CALENDAR")
@Log4j2
public class CalendarController extends XDFController<CalendarBean, String> {
public CalendarController(CalendarService service) {
super(service);
}
}
package com.bytesw.bytebot.model;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.hibernate.envers.Audited;
import javax.persistence.*;
import java.io.Serializable;
import java.util.List;
@Audited
@Entity
@Table(name = "avb_calendar")
@NamedQuery(name = "Calendar.findByPK", query = "Select u from Calendar u where u.id = ?1")
@Getter
@Setter
@EqualsAndHashCode
@ToString(exclude = "id")
public class Calendar implements Serializable {
@Id
@Column(name = "cale_id")
private String id;
@Version
@Column(name = "cale_version")
@Basic(optional = false)
private long version;
@Column(name = "cale_name", nullable = false)
private String name;
// @OneToMany(mappedBy = "calendar")
// private List<WeekScheduler> weekSchedulerList;
@OneToMany(mappedBy = "calendar")
private List<String> weekSchedulerList;
}
package com.bytesw.bytebot.repository;
import com.bytesw.bytebot.model.Calendar;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.repository.CrudRepository;
public interface CalendarRepository extends CrudRepository<Calendar, String>, JpaSpecificationExecutor<Calendar> {
}
\ No newline at end of file
package com.bytesw.bytebot.service;
import com.bytesw.bytebot.bean.CalendarBean;
import com.bytesw.bytebot.model.Calendar;
import com.bytesw.bytebot.repository.CalendarRepository;
import com.bytesw.xdf.exception.AlreadyExistsException;
import com.bytesw.xdf.exception.NotFoundException;
import com.bytesw.xdf.exception.ReferentialIntegrityException;
import com.bytesw.xdf.service.XDFService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.transaction.Transactional;
import java.util.Optional;
@Service
@Transactional
public class CalendarService extends XDFService<Calendar, CalendarBean, String> {
@Autowired
private WeekSchedulerService weekSchedulerService;
@Autowired
private WeekSchedulerRepository weekSchedulerRepository;
@Autowired
private SchedulerTaskRepository schedulerTaskRepository;
@Autowired
private CalendarExceptionRepository calendarExceptionRepository;
@Autowired
private CalendarRepository calendarRepository;
@Autowired
private CalendarExceptionService calendarExceptionService;
protected CalendarService(CalendarRepository repository) {
super(repository);
}
@Override
protected Calendar toModel(Calendar model, CalendarBean bean) {
if (model == null) {
model = new Calendar();
}
BeanUtils.copyProperties(bean, model);
return model;
}
@Override
protected CalendarBean toBean(Calendar model) {
CalendarBean bean = new CalendarBean();
BeanUtils.copyProperties(model, bean);
bean.setWeekSchedulerBeanList(this.weekSchedulerService.getWeekSchedulerByCalId(model.getId()));
return bean;
}
@Override
public void delete(String id) {
Optional<Calendar> found = this.calendarRepository.findById(id);
if (!found.isPresent()) {
throw new NotFoundException();
}
Optional<List<SchedulerTask>> schedulerTaskList = this.schedulerTaskRepository.findByCalendarId(id);
if (schedulerTaskList.isPresent()) {
throw new ReferentialIntegrityException("CALENDAR_EXCEPTION_DELETE_SCHEDULER_TASK");
}
Optional<List<CalendarException>> calendarExceptionList = this.calendarExceptionRepository.findByCalendarId(id);
calendarExceptionList.ifPresent(list -> this.calendarExceptionRepository.deleteAll(list));
Optional<List<WeekScheduler>> weekSchedulerList = this.weekSchedulerRepository.findByCalendarId(id);
weekSchedulerList.ifPresent(list -> this.weekSchedulerRepository.deleteAll(list));
this.calendarRepository.delete(found.get());
}
@Override
public CalendarBean create(CalendarBean bean) {
if( this.calendarRepository.existsById(bean.getId())) {
throw new AlreadyExistsException("errors.form.duplicated.id");
}
Calendar model = new Calendar();
model = this.calendarRepository.save(this.toModel(model, bean));
saveWeekScheduler(bean, model);
saveCalendarException(bean, model);
return this.toBean(model);
}
@Override
public CalendarBean update(CalendarBean bean, String id) {
Optional<Calendar> found = this.calendarRepository.findById(id);
if (!found.isPresent()) {
throw new NotFoundException();
}
Calendar calendar = this.toModel(found.get(), bean);
BeanUtils.copyProperties(calendar, found.get());
calendar = this.calendarRepository.save(found.get());
saveWeekScheduler(bean, calendar);
saveCalendarException(bean, calendar);
return this.toBean(calendar);
}
private void saveWeekScheduler(CalendarBean bean, Calendar calendar) {
List<WeekSchedulerBean> weekSchedulerBeanList = bean.getWeekSchedulerBeanList();
for (WeekSchedulerBean weekSchedulerBean : weekSchedulerBeanList) {
WeekScheduler weekScheduler;
boolean delete = weekSchedulerBean.getFrom() == null;
if (weekSchedulerBean.getId() != null) {
Optional<WeekScheduler> weekSchedulerOptional = this.weekSchedulerRepository.findById(weekSchedulerBean.getId());
if (weekSchedulerOptional.isPresent()) {
if(delete){
this.weekSchedulerRepository.delete(weekSchedulerOptional.get());
continue;
}
weekScheduler = weekSchedulerOptional.get();
} else {
weekScheduler = new WeekScheduler();
weekScheduler.setDayOfWeek(weekSchedulerBean.getDayOfWeek());
}
weekScheduler.setCalendar(calendar);
weekScheduler.setFrom(weekSchedulerBean.getFrom());
weekScheduler.setTo(weekSchedulerBean.getTo());
this.weekSchedulerRepository.save(weekScheduler);
} else if (!delete) {
weekScheduler = new WeekScheduler();
weekScheduler.setCalendar(calendar);
weekScheduler.setDayOfWeek(weekSchedulerBean.getDayOfWeek());
weekScheduler.setFrom(weekSchedulerBean.getFrom());
weekScheduler.setTo(weekSchedulerBean.getTo());
this.weekSchedulerRepository.save(weekScheduler);
}
}
}
private void saveCalendarException(CalendarBean bean, Calendar calendar) {
List<CalendarExceptionBean> calendarExceptionBeanList = bean.getCalendarExceptionBeanList();
CalendarException calendarException = null;
for (CalendarExceptionBean calendarExceptionBean : calendarExceptionBeanList) {
if (calendarExceptionBean.getId() != null) {
Optional<CalendarException> calendarExceptionOptional = calendarExceptionRepository.findById(calendarExceptionBean.getId());
if (calendarExceptionBean.isDelete()) {
calendarExceptionRepository.delete(calendarExceptionOptional.get());
continue;
}
// modificar
if (!calendarExceptionOptional.get().getFrequencyType().equals(calendarExceptionBean.getFrequencyType())) {
calendarExceptionRepository.delete(calendarExceptionOptional.get());
} else {
calendarExceptionBean.setCalendar(bean);
calendarException = calendarExceptionService.toModel(calendarExceptionOptional.get(), calendarExceptionBean);
calendarExceptionRepository.save(calendarException);
continue;
}
}
// Nueva excepción
calendarException = calendarExceptionService.getCalendarException(calendarExceptionBean);
calendarExceptionBean.setCalendar(bean);
calendarException = calendarExceptionService.toModel(calendarException, calendarExceptionBean);
calendarException.setCalendar(calendar);
calendarExceptionRepository.save(calendarException);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment