use of org.meveo.model.catalog.CalendarYearly in project meveo by meveo-org.
the class CalendarApi method update.
public void update(CalendarDto postData) throws MeveoApiException, BusinessException {
if (StringUtils.isBlank(postData.getCode())) {
missingParameters.add("code");
}
if (StringUtils.isBlank(postData.getCalendarType())) {
missingParameters.add("calendarType");
}
handleMissingParametersAndValidate(postData);
Calendar calendar = calendarService.findByCode(postData.getCode());
if (calendar == null) {
throw new EntityDoesNotExistsException(Calendar.class, postData.getCode());
}
calendar.setCode(StringUtils.isBlank(postData.getUpdatedCode()) ? postData.getCode() : postData.getUpdatedCode());
calendar.setDescription(postData.getDescription());
if (calendar instanceof CalendarYearly) {
if (postData.getDays() != null && postData.getDays().size() > 0) {
List<DayInYear> days = new ArrayList<DayInYear>();
for (DayInYearDto d : postData.getDays()) {
DayInYear dayInYear = dayInYearService.findByMonthAndDay(d.getMonth(), d.getDay());
if (dayInYear != null) {
days.add(dayInYear);
}
}
((CalendarYearly) calendar).setDays(days);
}
} else if (calendar instanceof CalendarYearly) {
if (postData.getHours() != null && postData.getHours().size() > 0) {
List<HourInDay> hours = new ArrayList<HourInDay>();
for (HourInDayDto d : postData.getHours()) {
HourInDay hourInDay = hourInDayService.findByHourAndMin(d.getHour(), d.getMin());
if (hourInDay != null) {
hours.add(hourInDay);
}
}
((CalendarDaily) calendar).setHours(hours);
}
} else if (calendar instanceof CalendarPeriod) {
((CalendarPeriod) calendar).setPeriodLength(postData.getPeriodLength());
((CalendarPeriod) calendar).setNbPeriods(postData.getNbPeriods());
if (!StringUtils.isBlank(postData.getPeriodUnit())) {
((CalendarPeriod) calendar).setPeriodUnit(postData.getPeriodUnit().getUnitValue());
}
} else if (calendar instanceof CalendarInterval) {
CalendarInterval calendarInterval = (CalendarInterval) calendar;
calendarInterval.setIntervalType(postData.getIntervalType());
calendarInterval.getIntervals().clear();
if (postData.getIntervals() != null && postData.getIntervals().size() > 0) {
for (CalendarDateIntervalDto interval : postData.getIntervals()) {
calendarInterval.getIntervals().add(new CalendarDateInterval(calendarInterval, interval.getIntervalBegin(), interval.getIntervalEnd()));
}
}
} else if (calendar instanceof CalendarJoin) {
if (StringUtils.isBlank(postData.getJoinCalendar1Code())) {
missingParameters.add("joinCalendar1Code");
}
if (StringUtils.isBlank(postData.getJoinCalendar2Code())) {
missingParameters.add("joinCalendar2Code");
}
handleMissingParameters();
Calendar cal1 = calendarService.findByCode(postData.getJoinCalendar1Code());
Calendar cal2 = calendarService.findByCode(postData.getJoinCalendar2Code());
if (cal1 == null) {
throw new InvalidParameterException("joinCalendar1Code", postData.getJoinCalendar1Code());
}
if (cal2 == null) {
throw new InvalidParameterException("joinCalendar2Code", postData.getJoinCalendar2Code());
}
CalendarJoin calendarJoin = (CalendarJoin) calendar;
// Join type is expressed as Calendar type in DTO
calendarJoin.setJoinType(CalendarJoinTypeEnum.valueOf(postData.getCalendarType().name()));
calendarJoin.setJoinCalendar1(cal1);
calendarJoin.setJoinCalendar2(cal2);
}
calendarService.update(calendar);
}
use of org.meveo.model.catalog.CalendarYearly in project meveo by meveo-org.
the class CustomFieldTemplateService method copyCustomFieldTemplate.
/**
* Copy and associate a given custom field template to a given target entity type.
*
* @param cft Custom field template to copy
* @param targetAppliesTo Target CFT.appliesTo value associate custom field template with
* @return custom field template
* @throws BusinessException business exception.
*/
public CustomFieldTemplate copyCustomFieldTemplate(CustomFieldTemplate cft, String targetAppliesTo) throws BusinessException {
if (findByCodeAndAppliesTo(cft.getCode(), targetAppliesTo) != null) {
throw new ValidationException("Custom field template " + cft.getCode() + " already exists in targe entity " + targetAppliesTo, "customFieldTemplate.copyCFT.alreadyExists");
}
// Load calendar for lazy loading
if (cft.getCalendar() != null) {
cft.setCalendar(PersistenceUtils.initializeAndUnproxy(cft.getCalendar()));
if (cft.getCalendar() instanceof CalendarDaily) {
((CalendarDaily) cft.getCalendar()).setHours(PersistenceUtils.initializeAndUnproxy(((CalendarDaily) cft.getCalendar()).getHours()));
cft.getCalendar().nextCalendarDate(new Date());
} else if (cft.getCalendar() instanceof CalendarYearly) {
((CalendarYearly) cft.getCalendar()).setDays(PersistenceUtils.initializeAndUnproxy(((CalendarYearly) cft.getCalendar()).getDays()));
cft.getCalendar().nextCalendarDate(new Date());
} else if (cft.getCalendar() instanceof CalendarInterval) {
((CalendarInterval) cft.getCalendar()).setIntervals(PersistenceUtils.initializeAndUnproxy(((CalendarInterval) cft.getCalendar()).getIntervals()));
cft.getCalendar().nextCalendarDate(new Date());
}
}
if (cft.getListValues() != null) {
cft.getListValues().values().toArray(new String[] {});
}
if (cft.getMatrixColumns() != null) {
cft.getMatrixColumns().toArray(new CustomFieldMatrixColumn[] {});
}
detach(cft);
CustomFieldTemplate cftCopy = SerializationUtils.clone(cft);
cftCopy.setId(null);
cftCopy.setVersion(null);
cftCopy.setAppliesTo(targetAppliesTo);
if (cft.getListValues() != null) {
cftCopy.setListValues(new HashMap<>());
cftCopy.getListValues().putAll(cft.getListValues());
}
if (cft.getMatrixColumns() != null) {
cftCopy.setMatrixColumns(new ArrayList<>());
cftCopy.getMatrixColumns().addAll(cft.getMatrixColumns());
}
create(cftCopy);
return cftCopy;
}
use of org.meveo.model.catalog.CalendarYearly in project meveo by meveo-org.
the class CalendarApi method create.
public void create(CalendarDto postData) throws MeveoApiException, BusinessException {
if (StringUtils.isBlank(postData.getCode())) {
missingParameters.add("code");
}
if (StringUtils.isBlank(postData.getCalendarType())) {
missingParameters.add("calendarType");
}
handleMissingParametersAndValidate(postData);
if (calendarService.findByCode(postData.getCode()) != null) {
throw new EntityAlreadyExistsException(Calendar.class, postData.getCode());
}
if (postData.getCalendarType() == CalendarTypeEnum.YEARLY) {
CalendarYearly calendar = new CalendarYearly();
calendar.setCode(postData.getCode());
calendar.setDescription(postData.getDescription());
if (postData.getDays() != null && postData.getDays().size() > 0) {
List<DayInYear> days = new ArrayList<DayInYear>();
for (DayInYearDto d : postData.getDays()) {
DayInYear dayInYear = dayInYearService.findByMonthAndDay(d.getMonth(), d.getDay());
if (dayInYear != null) {
days.add(dayInYear);
}
}
calendar.setDays(days);
}
calendarService.create(calendar);
} else if (postData.getCalendarType() == CalendarTypeEnum.DAILY) {
CalendarDaily calendar = new CalendarDaily();
calendar.setCode(postData.getCode());
calendar.setDescription(postData.getDescription());
if (postData.getHours() != null && postData.getHours().size() > 0) {
List<HourInDay> hours = new ArrayList<HourInDay>();
for (HourInDayDto d : postData.getHours()) {
HourInDay hourInDay = hourInDayService.findByHourAndMin(d.getHour(), d.getMin());
if (hourInDay == null) {
hourInDay = new HourInDay(d.getHour(), d.getMin());
}
hours.add(hourInDay);
}
calendar.setHours(hours);
}
calendarService.create(calendar);
} else if (postData.getCalendarType() == CalendarTypeEnum.PERIOD) {
if (StringUtils.isBlank(postData.getPeriodUnit())) {
missingParameters.add("periodUnit");
handleMissingParameters();
}
CalendarPeriod calendar = new CalendarPeriod();
calendar.setCode(postData.getCode());
calendar.setDescription(postData.getDescription());
calendar.setPeriodLength(postData.getPeriodLength());
calendar.setNbPeriods(postData.getNbPeriods());
calendar.setPeriodUnit(postData.getPeriodUnit().getUnitValue());
calendarService.create(calendar);
} else if (postData.getCalendarType() == CalendarTypeEnum.INTERVAL) {
CalendarInterval calendar = new CalendarInterval();
calendar.setCode(postData.getCode());
calendar.setDescription(postData.getDescription());
calendar.setIntervalType(postData.getIntervalType());
if (postData.getIntervals() != null && postData.getIntervals().size() > 0) {
List<CalendarDateInterval> intervals = new ArrayList<CalendarDateInterval>();
for (CalendarDateIntervalDto interval : postData.getIntervals()) {
intervals.add(new CalendarDateInterval(calendar, interval.getIntervalBegin(), interval.getIntervalEnd()));
}
calendar.setIntervals(intervals);
}
calendarService.create(calendar);
} else if (postData.getCalendarType().isJoin()) {
if (StringUtils.isBlank(postData.getJoinCalendar1Code())) {
missingParameters.add("joinCalendar1Code");
}
if (StringUtils.isBlank(postData.getJoinCalendar2Code())) {
missingParameters.add("joinCalendar2Code");
}
handleMissingParameters();
Calendar cal1 = calendarService.findByCode(postData.getJoinCalendar1Code());
Calendar cal2 = calendarService.findByCode(postData.getJoinCalendar2Code());
if (cal1 == null) {
throw new InvalidParameterException("joinCalendar1Code", postData.getJoinCalendar1Code());
}
if (cal2 == null) {
throw new InvalidParameterException("joinCalendar2Code", postData.getJoinCalendar2Code());
}
CalendarJoin calendar = new CalendarJoin();
calendar.setCode(postData.getCode());
calendar.setDescription(postData.getDescription());
// Join type is expressed as Calendar type in DTO
calendar.setJoinType(CalendarJoinTypeEnum.valueOf(postData.getCalendarType().name()));
calendar.setJoinCalendar1(cal1);
calendar.setJoinCalendar2(cal2);
calendarService.create(calendar);
} else {
throw new BusinessApiException("invalid calendar type, possible values YEARLY, DAILY, PERIOD, INTERVAL, JOIN");
}
}
use of org.meveo.model.catalog.CalendarYearly in project meveo by meveo-org.
the class CustomFieldsCacheContainerProvider method populateCFTCache.
/**
* Populate custom field template cache.
*/
private void populateCFTCache() {
String currentProvider = currentUser.getProviderCode();
log.debug("Start to pre-populate CFT cache for provider {}", currentProvider);
CacheKeyStr lastAppliesTo = null;
Map<String, CustomFieldTemplate> cftsSameAppliesTo = null;
List<CustomFieldTemplate> cfts = customFieldTemplateService.getCFTForCache();
for (CustomFieldTemplate cft : cfts) {
CacheKeyStr cacheKeyByAppliesTo = getCFTCacheKeyByAppliesTo(cft);
if (lastAppliesTo == null) {
cftsSameAppliesTo = new TreeMap<>();
lastAppliesTo = cacheKeyByAppliesTo;
} else if (!lastAppliesTo.equals(cacheKeyByAppliesTo)) {
cftsByAppliesTo.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES).put(lastAppliesTo, cftsSameAppliesTo);
cftsSameAppliesTo = new TreeMap<>();
lastAppliesTo = cacheKeyByAppliesTo;
}
if (cft.getCalendar() != null) {
cft.setCalendar(PersistenceUtils.initializeAndUnproxy(cft.getCalendar()));
if (cft.getCalendar() instanceof CalendarDaily) {
((CalendarDaily) cft.getCalendar()).setHours(PersistenceUtils.initializeAndUnproxy(((CalendarDaily) cft.getCalendar()).getHours()));
} else if (cft.getCalendar() instanceof CalendarYearly) {
((CalendarYearly) cft.getCalendar()).setDays(PersistenceUtils.initializeAndUnproxy(((CalendarYearly) cft.getCalendar()).getDays()));
} else if (cft.getCalendar() instanceof CalendarInterval) {
((CalendarInterval) cft.getCalendar()).setIntervals(PersistenceUtils.initializeAndUnproxy(((CalendarInterval) cft.getCalendar()).getIntervals()));
}
}
if (cft.getListValues() != null) {
cft.getListValues().values().toArray(new String[] {});
}
customFieldTemplateService.detach(cft);
cftsSameAppliesTo.put(cft.getCode(), cft);
}
if (cftsSameAppliesTo != null && !cftsSameAppliesTo.isEmpty()) {
cftsByAppliesTo.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES).put(lastAppliesTo, cftsSameAppliesTo);
}
log.info("CFT cache populated with {} values of provider {}.", cfts.size(), currentProvider);
}
use of org.meveo.model.catalog.CalendarYearly in project meveo by meveo-org.
the class CustomFieldsCacheContainerProvider method addUpdateCustomFieldTemplate.
/**
* Store mapping between CF code and value storage in cache time period and cache by CFT appliesTo value.
*
* @param cft Custom field template definition
*/
public void addUpdateCustomFieldTemplate(CustomFieldTemplate cft) {
CacheKeyStr cacheKeyByAppliesTo = getCFTCacheKeyByAppliesTo(cft);
log.trace("Adding/updating custom field template {} for {} to CFT cache of Provider {}.", cft.getCode(), cacheKeyByAppliesTo, currentUser.getProviderCode());
Map<String, CustomFieldTemplate> cftsOld = cftsByAppliesTo.getAdvancedCache().withFlags(Flag.FORCE_WRITE_LOCK).get(cacheKeyByAppliesTo);
Map<String, CustomFieldTemplate> cfts = new TreeMap<>();
if (cftsOld != null) {
cfts.putAll(cftsOld);
}
// Load calendar for lazy loading
if (cft.getCalendar() != null) {
cft.setCalendar(PersistenceUtils.initializeAndUnproxy(cft.getCalendar()));
if (cft.getCalendar() instanceof CalendarDaily) {
((CalendarDaily) cft.getCalendar()).setHours(PersistenceUtils.initializeAndUnproxy(((CalendarDaily) cft.getCalendar()).getHours()));
cft.getCalendar().nextCalendarDate(new Date());
} else if (cft.getCalendar() instanceof CalendarYearly) {
((CalendarYearly) cft.getCalendar()).setDays(PersistenceUtils.initializeAndUnproxy(((CalendarYearly) cft.getCalendar()).getDays()));
cft.getCalendar().nextCalendarDate(new Date());
} else if (cft.getCalendar() instanceof CalendarInterval) {
((CalendarInterval) cft.getCalendar()).setIntervals(PersistenceUtils.initializeAndUnproxy(((CalendarInterval) cft.getCalendar()).getIntervals()));
cft.getCalendar().nextCalendarDate(new Date());
}
}
if (cft.getListValues() != null) {
cft.getListValues().values().toArray(new String[] {});
}
cft = SerializationUtils.clone(cft);
Lock lock = cacheLock.writeLock();
lock.lock();
try {
cfts.put(cft.getCode(), cft);
cftsByAppliesTo.getAdvancedCache().withFlags(Flag.IGNORE_RETURN_VALUES).put(cacheKeyByAppliesTo, cfts);
} finally {
lock.unlock();
}
}
Aggregations