Commit bffa4240 authored by Roberto Loayza's avatar Roberto Loayza

Creación de artefactos : WEEK_SCHEDULER y CALENDAR_EXCEPTION

parent dfca8e2e
package com.bytesw.bytebot.bean;
import com.google.gson.annotations.Expose;
import lombok.Getter;
import lombok.Setter;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.OffsetDateTime;
@Getter
@Setter
public class CalendarExceptionBean {
@Expose
private BigInteger id;
@Expose private long version;
@Expose private String description;
//@Expose private CalendarBean calendar;
@Expose private String calendar;
@Expose private String calendarExceptionType;
//Frecuencia
@Expose private String frequencyType;
//annualCalendar y UniqueWeekly
@Expose private int month;
@Expose private int dayOfMonth;
@Expose private int weekOfMonth;
@Expose private int dayOfWeek;
//UniqueCalendar
@Expose private LocalDate date;
//rangeCalendar
@Expose private OffsetDateTime from;
@Expose private OffsetDateTime to;
@Expose private boolean delete;
}
package com.bytesw.bytebot.bean;
import com.google.gson.annotations.Expose;
import lombok.Getter;
import lombok.Setter;
import java.math.BigInteger;
import java.time.OffsetTime;
@Getter
@Setter
public class WeekSchedulerBean {
@Expose
private BigInteger id;
@Expose private long version;
@Expose private int dayOfWeek;
@Expose private OffsetTime from;
@Expose private OffsetTime to;
//@Expose private CalendarBean calendar;
@Expose private String calendar;
}
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.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.NamedQuery;
@Audited
@Entity
@DiscriminatorValue(value=AnnualCalendarException.DISCRIMINATOR_VALUE)
@NamedQuery(name = "AnnualCalendarException.findByPK", query = "Select u from AnnualCalendarException u where u.id = ?1")
@Getter
@Setter
@EqualsAndHashCode
@ToString
public class AnnualCalendarException extends CalendarException {
public static final String DISCRIMINATOR_VALUE = "Y";
@Column(name = "CALE_EXC_MONTH", nullable = false)
private int month;
@Column(name = "CALE_EXC_DAYMO", nullable = false)
private int dayOfMonth;
}
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.math.BigInteger;
@Audited
@Entity
@Inheritance(
strategy = InheritanceType.SINGLE_TABLE
)
@DiscriminatorColumn(name="CAEX_FRECU", discriminatorType = DiscriminatorType.STRING)
@Table(name = "AVB_CALENDAR_EXCEPTION")
@Getter
@Setter
@EqualsAndHashCode
@ToString(exclude = "id")
public abstract class CalendarException implements Serializable {
@Id
@Column(name = "CALE_EXC_ID")
@SequenceGenerator(name = "AVB_CALENDAR_EXCEPTION_GENERATOR", sequenceName = "AVB_CALENDAR_EXCEPTION_SEQ", initialValue = 1, allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "AVB_CALENDAR_EXCEPTION_GENERATOR")
private BigInteger id;
@Version
@Column(name = "CALE_EXC_VERSION")
@Basic(optional = false)
private long version;
@Column(name = "CALE_EXC_TYPE")
//@Convert(converter = CalendarExceptionTypeConverter.class)
//private CalendarExceptionType calendarExceptionType;
private String calendarExceptionType;
@Column(name = "CALE_EXC_FRECU", insertable = false, updatable = false)
private String frequencyType;
@Column(name = "CALE_EXC_DESCR")
private String description;
@ManyToOne(optional = false)
@JoinColumn(name = "CALE_ID", referencedColumnName = "CALE_ID", nullable = false)
//private Calendar calendar;
private String calendar;
}
\ No newline at end of file
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.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.NamedQuery;
import java.time.OffsetDateTime;
@Audited
@Entity
@DiscriminatorValue(value= RangeCalendarException.DISCRIMINATOR_VALUE)
@NamedQuery(name = "RangeCalendarException.findByPK", query = "Select u from RangeCalendarException u where u.id = ?1")
@Getter
@Setter
@EqualsAndHashCode
@ToString
public class RangeCalendarException extends CalendarException {
public static final String DISCRIMINATOR_VALUE = "R";
@Column(name = "CALE_EXC_FROM", nullable = false)
private OffsetDateTime from;
@Column(name = "CALE_EXC_TO", nullable = false)
private OffsetDateTime to;
}
\ No newline at end of file
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.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.NamedQuery;
import java.time.LocalDate;
@Audited
@Entity
@DiscriminatorValue(value= UniqueCalendarException.DISCRIMINATOR_VALUE)
@NamedQuery(name = "UniqueCalendarException.findByPK", query = "Select u from UniqueCalendarException u where u.id = ?1")
@Getter
@Setter
@EqualsAndHashCode
@ToString
public class UniqueCalendarException extends CalendarException {
public static final String DISCRIMINATOR_VALUE = "U";
@Column(name = "CALE_EXC_DATE", nullable = true)
private LocalDate date;
}
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.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.NamedQuery;
@Audited
@Entity
@DiscriminatorValue(value= UniqueWeeklyCalendarException.DISCRIMINATOR_VALUE)
@NamedQuery(name = "UniqueWeeklyCalendarException.findByPK", query = "Select u from UniqueWeeklyCalendarException u where u.id = ?1")
@Getter
@Setter
@EqualsAndHashCode
@ToString
public class UniqueWeeklyCalendarException extends CalendarException {
public static final String DISCRIMINATOR_VALUE = "W";
@Column(name = "CALE_EXC_MONTH", nullable = false)
private int month;
@Column(name = "CALE_EXC_WEEK", nullable = false)
private int weekOfMonth;
@Column(name = "CALE_EXC_DAYWE", nullable = false)
private int dayOfWeek;
}
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.math.BigInteger;
import java.time.OffsetTime;
@Audited
@Entity
@Table(name = "AVB_WEEK_SCHEDULER")
@NamedQuery(name = "WeekScheduler.findByPK", query = "Select u from WeekScheduler u where u.id = ?1")
@Getter
@Setter
@EqualsAndHashCode
@ToString(exclude = "calendar")
public class WeekScheduler implements Serializable {
@Id
@Column(name = "WESC_ID")
@SequenceGenerator(name = "AVB_WEEK_SCHEDULER_GENERATOR", sequenceName = "AVB_WEEK_SCHEDULER_SEQ", initialValue = 1, allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "AVB_WEEK_SCHEDULER_GENERATOR")
private BigInteger id;
@Version
@Column(name = "WESC_VERSION")
@Basic(optional = false)
private long version;
@Column(name = "WESC_DAY", nullable = false)
private int dayOfWeek;
@Column(name = "WESC_FROM", nullable = false)
private OffsetTime from;
@Column(name = "WESC_TO", nullable = false)
private OffsetTime to;
@ManyToOne(optional = false)
@JoinColumn(name = "CALE_ID", referencedColumnName = "CALE_ID", nullable = false)
//private Calendar calendar;
private String calendar;
}
package com.bytesw.bytebot.model.enums;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;
public enum CalendarExceptionType {
HOLIDAY("H"), WORK_EXCEPTION("W"), NO_WORK_EXCEPTION("N");
private final String name;
private static final Map<String, CalendarExceptionType> map = new HashMap<>();
static {
for (CalendarExceptionType type : CalendarExceptionType.values()) {
map.put(type.name, type);
}
}
CalendarExceptionType(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static CalendarExceptionType fromString(String name) {
if (map.containsKey(name)) {
return map.get(name);
}
throw new NoSuchElementException(name + " not found");
}
}
\ No newline at end of file
package com.bytesw.bytebot.repository;
import com.bytesw.bytebot.model.CalendarException;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.Optional;
public interface CalendarExceptionRepository extends CrudRepository<CalendarException, BigInteger>, JpaSpecificationExecutor<CalendarException> {
/*@Query("select s from RangeCalendarException s where s.calendar.id = :calendarId and s.from < :date and s.to > :date and s.calendarExceptionType = :type")
Optional<CalendarException> findByRangeExceptionByCalendarIdAndDateAndType(@Param("calendarId") String calendarId, @Param("date") OffsetDateTime date, @Param("type") CalendarExceptionType type);
@Query("select s from AnnualCalendarException s where s.calendar.id = :calendarId and s.month = :month and s.dayOfMonth = :dayOfMonth")
Optional<CalendarException> findByAnnualExceptionByCalendarIdAndYearAndDay(@Param("calendarId") String calendarId, @Param("month") int month, @Param("dayOfMonth") int dayOfMonth);
@Query("select s from UniqueCalendarException s where s.calendar.id = :calendarId and s.date = :date")
Optional<CalendarException> findByUniqueExceptionByCalendarIdAndDate(@Param("calendarId") String calendarId, @Param("date") LocalDate date);
@Query("select s from UniqueWeeklyCalendarException s where s.calendar.id = :calendarId and s.month = :month and s.dayOfWeek = :dayOfWeek and s.weekOfMonth = :weekOfMonth")
Optional<CalendarException> findByUniqueWeeklyExceptionByCalendarIdAndDayOfWeekAndWeekOfMonth(@Param("calendarId") String calendarId, @Param("month") int month, @Param("dayOfWeek") int dayOfWeek, @Param("weekOfMonth") int weekOfMonth);
@Query("select s from RangeCalendarException s where s.calendar.id = :calendarId and s.from >= :date and s.calendarExceptionType = :type")
Optional<CalendarException> findByRangeExceptionByCalendarIdFromDateAndType(@Param("calendarId") String calendarId, @Param("date") OffsetDateTime date, @Param("type") CalendarExceptionType type);
@Query("select s from CalendarException s where s.calendar.id = :calendarId")
Optional<List<CalendarException>> findByCalendarId(@Param("calendarId") String calendarId);*/
}
package com.bytesw.bytebot.repository;
import com.bytesw.bytebot.model.WeekScheduler;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import java.math.BigInteger;
import java.util.List;
import java.util.Optional;
public interface WeekSchedulerRepository extends CrudRepository<WeekScheduler, BigInteger>, JpaSpecificationExecutor<WeekScheduler> {
//@Query("select b from WeekScheduler b where b.calendar.id = :caleId")
//Optional<List<WeekScheduler>> findByCalendarId(@Param("caleId") String caleId);
}
package com.bytesw.bytebot.service;
import com.bytesw.bytebot.bean.CalendarExceptionBean;
import com.bytesw.bytebot.model.*;
import com.bytesw.bytebot.model.enums.CalendarExceptionType;
import com.bytesw.bytebot.repository.CalendarExceptionRepository;
import com.bytesw.xdf.dao.rsql.CustomRsqlVisitor;
import com.bytesw.xdf.exception.NotFoundException;
import com.bytesw.xdf.service.XDFService;
import cz.jirutka.rsql.parser.RSQLParser;
import cz.jirutka.rsql.parser.ast.Node;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigInteger;
import java.util.*;
@Service
@Transactional
public class CalendarExceptionService extends XDFService<CalendarException, CalendarExceptionBean, BigInteger> {
//@Autowired
//CalendarRepository calendarRepository;
//@Autowired
//CalendarService gatewayCalendarService;
@Autowired
CalendarExceptionRepository calendarExceptionRepository;
//@Autowired
//BusinessFlowService businessFlowService;
protected CalendarExceptionService(CalendarExceptionRepository repository) {
super(repository);
}
@Override
protected CalendarException toModel(CalendarException model, CalendarExceptionBean bean) {
if (model == null) {
model = getCalendarException(bean);
}
BeanUtils.copyProperties(bean, model);
if (bean.getCalendar() == null) {
throw new NotFoundException("Calendar can not be null");
}
Optional<Calendar> calendarOptional = calendarRepository.findById(bean.getCalendar().getId());
if (!calendarOptional.isPresent()) {
throw new NotFoundException("Calendar not found " + bean.getCalendar().getId());
}
if (bean.getCalendarExceptionType() == null) {
throw new NotFoundException("Calendar Exception Type can not be null");
}
model.setCalendar(calendarOptional.get());
model.setCalendarExceptionType(CalendarExceptionType.fromString(bean.getCalendarExceptionType()));
return model;
}
public static CalendarException getCalendarException(CalendarExceptionBean bean) {
CalendarException model;
switch (bean.getFrequencyType()) {
case UniqueCalendarException.DISCRIMINATOR_VALUE:
model = new UniqueCalendarException();
break;
case AnnualCalendarException.DISCRIMINATOR_VALUE:
model = new AnnualCalendarException();
break;
case RangeCalendarException.DISCRIMINATOR_VALUE:
model = new RangeCalendarException();
break;
default:
model = new UniqueWeeklyCalendarException();
}
return model;
}
@Override
protected CalendarExceptionBean toBean(CalendarException model) {
CalendarExceptionBean bean = new CalendarExceptionBean();
BeanUtils.copyProperties(model, bean);
bean.setCalendar(this.calendarService.toBean(model.getCalendar()));
bean.setCalendarExceptionType(model.getCalendarExceptionType().getName());
bean.setFrequencyType(model.getFrequencyType());
return bean;
}
public List<CalendarExceptionBean> getCalendarExceptionbyID(Map<String, String> parametersCalendar) {
String sortDirection = parametersCalendar.get("sortDirection");
String columnName = parametersCalendar.get("columnName");
String caleId = parametersCalendar.get("caleId");
String filterExpresion = parametersCalendar.get("filterExpresion");
Specification<CalendarException> calendarExceptionSpecification = null;
Sort sort;
switch (sortDirection) {
case "desc":
sort = Sort.by(Sort.Direction.DESC, columnName);
break;
case "asc":
sort = Sort.by(Sort.Direction.ASC, columnName);
break;
default:
sort = Sort.by(Sort.Direction.ASC, "description");
}
if (filterExpresion != null) {
if (!filterExpresion.isEmpty()) {
filterExpresion = filterExpresion + ";";
}
filterExpresion = filterExpresion + String.format("calendar.id=='%s'", caleId);
calendarExceptionSpecification = createSpecification(filterExpresion);
}
List<CalendarException> optionalCalendarExceptionList = calendarExceptionRepository.findAll(calendarExceptionSpecification, sort);
List<CalendarExceptionBean> calendarExceptionBeanList = new ArrayList<>();
for (int i = 0; i < optionalCalendarExceptionList.size(); i++) {
CalendarExceptionBean aux = new CalendarExceptionBean();
BeanUtils.copyProperties(optionalCalendarExceptionList.get(i), aux);
calendarExceptionBeanList.add(i, toBean(optionalCalendarExceptionList.get(i)));
}
return calendarExceptionBeanList;
}
private Specification<CalendarException> createSpecification(String filterExpression) {
Node rootNode = (new RSQLParser()).parse(filterExpression);
return (Specification) rootNode.accept(new CustomRsqlVisitor<>());
}
}
package com.bytesw.bytebot.service;
import com.bytesw.bytebot.bean.WeekSchedulerBean;
import com.bytesw.bytebot.model.WeekScheduler;
import com.bytesw.bytebot.repository.WeekSchedulerRepository;
import com.bytesw.xdf.exception.NotFoundException;
import com.bytesw.xdf.service.XDFService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@Service
@Transactional
public class WeekSchedulerService extends XDFService<WeekScheduler, WeekSchedulerBean, BigInteger> {
@Autowired
private WeekSchedulerRepository weekSchedulerRepository;
@Autowired
private CalendarService calendarService;
@Autowired
private CalendarRepository calendarRepository;
protected WeekSchedulerService(WeekSchedulerRepository repository) {
super(repository);
}
@Override
protected WeekScheduler toModel(WeekScheduler model, WeekSchedulerBean bean) {
if (model == null) {
model = new WeekScheduler();
}
BeanUtils.copyProperties(bean, model);
if (bean.getCalendar() == null) {
throw new NotFoundException("Calendar can not be null");
}
Optional<Calendar> calendarOptional = calendarRepository.findById(bean.getCalendar().getId());
if (!calendarOptional.isPresent()) {
throw new NotFoundException("Calendar not found " + bean.getCalendar().getId());
}
model.setCalendar(calendarOptional.get());
return model;
}
// @Override
// protected WeekSchedulerBean toBean(WeekScheduler model) {
// WeekSchedulerBean bean = new WeekSchedulerBean();
// BeanUtils.copyProperties(model, bean);
// bean.setCalendar(gatewayCalendarService.toBean(model.getCalendar()));
// return bean;
// }
public List<WeekSchedulerBean> getWeekSchedulerByCalId(String caleID) {
List<WeekSchedulerBean> weekSchedulerBeanList = new ArrayList<>(7);
Optional<List<WeekScheduler>> weekSchedulerOptional = weekSchedulerRepository.findByCalendarId(caleID);
if (!weekSchedulerOptional.isPresent()) {
for (int i = 0; i < 7; i++) {
weekSchedulerBeanList.add(i, new WeekSchedulerBean());
}
} else {
for (int i = 0; i < 7; i++) {
WeekSchedulerBean aux = new WeekSchedulerBean();
for (int j = 0; j < weekSchedulerOptional.get().size(); j++){
aux.setDayOfWeek(i+1);
if(weekSchedulerOptional.get().get(j).getDayOfWeek() == i+1){
BeanUtils.copyProperties(weekSchedulerOptional.get().get(j), aux);
}
}
weekSchedulerBeanList.add(i, aux);
}
}
return weekSchedulerBeanList;
}
}
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