Search in sources :

Example 1 with CalendarYearly

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);
}
Also used : DayInYearDto(org.meveo.api.dto.DayInYearDto) DayInYear(org.meveo.model.catalog.DayInYear) CalendarInterval(org.meveo.model.catalog.CalendarInterval) Calendar(org.meveo.model.catalog.Calendar) ArrayList(java.util.ArrayList) HourInDay(org.meveo.model.catalog.HourInDay) HourInDayDto(org.meveo.api.dto.HourInDayDto) CalendarDateInterval(org.meveo.model.catalog.CalendarDateInterval) CalendarJoin(org.meveo.model.catalog.CalendarJoin) InvalidParameterException(org.meveo.api.exception.InvalidParameterException) CalendarDateIntervalDto(org.meveo.api.dto.CalendarDateIntervalDto) EntityDoesNotExistsException(org.meveo.api.exception.EntityDoesNotExistsException) CalendarYearly(org.meveo.model.catalog.CalendarYearly) CalendarPeriod(org.meveo.model.catalog.CalendarPeriod) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with CalendarYearly

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;
}
Also used : ValidationException(org.meveo.admin.exception.ValidationException) CalendarYearly(org.meveo.model.catalog.CalendarYearly) CalendarDaily(org.meveo.model.catalog.CalendarDaily) CalendarInterval(org.meveo.model.catalog.CalendarInterval) CustomFieldTemplate(org.meveo.model.crm.CustomFieldTemplate) Date(java.util.Date)

Example 3 with CalendarYearly

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");
    }
}
Also used : DayInYearDto(org.meveo.api.dto.DayInYearDto) EntityAlreadyExistsException(org.meveo.api.exception.EntityAlreadyExistsException) DayInYear(org.meveo.model.catalog.DayInYear) CalendarDaily(org.meveo.model.catalog.CalendarDaily) CalendarInterval(org.meveo.model.catalog.CalendarInterval) Calendar(org.meveo.model.catalog.Calendar) BusinessApiException(org.meveo.api.exception.BusinessApiException) ArrayList(java.util.ArrayList) HourInDay(org.meveo.model.catalog.HourInDay) HourInDayDto(org.meveo.api.dto.HourInDayDto) CalendarDateInterval(org.meveo.model.catalog.CalendarDateInterval) CalendarJoin(org.meveo.model.catalog.CalendarJoin) InvalidParameterException(org.meveo.api.exception.InvalidParameterException) CalendarDateIntervalDto(org.meveo.api.dto.CalendarDateIntervalDto) CalendarYearly(org.meveo.model.catalog.CalendarYearly) CalendarPeriod(org.meveo.model.catalog.CalendarPeriod) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with CalendarYearly

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);
}
Also used : CalendarYearly(org.meveo.model.catalog.CalendarYearly) CalendarDaily(org.meveo.model.catalog.CalendarDaily) CalendarInterval(org.meveo.model.catalog.CalendarInterval) CustomFieldTemplate(org.meveo.model.crm.CustomFieldTemplate) TreeMap(java.util.TreeMap)

Example 5 with CalendarYearly

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();
    }
}
Also used : CalendarYearly(org.meveo.model.catalog.CalendarYearly) CalendarDaily(org.meveo.model.catalog.CalendarDaily) CalendarInterval(org.meveo.model.catalog.CalendarInterval) CustomFieldTemplate(org.meveo.model.crm.CustomFieldTemplate) TreeMap(java.util.TreeMap) Date(java.util.Date) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) Lock(java.util.concurrent.locks.Lock)

Aggregations

CalendarInterval (org.meveo.model.catalog.CalendarInterval)5 CalendarYearly (org.meveo.model.catalog.CalendarYearly)5 CalendarDaily (org.meveo.model.catalog.CalendarDaily)4 CustomFieldTemplate (org.meveo.model.crm.CustomFieldTemplate)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 List (java.util.List)2 TreeMap (java.util.TreeMap)2 CalendarDateIntervalDto (org.meveo.api.dto.CalendarDateIntervalDto)2 DayInYearDto (org.meveo.api.dto.DayInYearDto)2 HourInDayDto (org.meveo.api.dto.HourInDayDto)2 InvalidParameterException (org.meveo.api.exception.InvalidParameterException)2 Calendar (org.meveo.model.catalog.Calendar)2 CalendarDateInterval (org.meveo.model.catalog.CalendarDateInterval)2 CalendarJoin (org.meveo.model.catalog.CalendarJoin)2 CalendarPeriod (org.meveo.model.catalog.CalendarPeriod)2 DayInYear (org.meveo.model.catalog.DayInYear)2 HourInDay (org.meveo.model.catalog.HourInDay)2 Lock (java.util.concurrent.locks.Lock)1 ReadWriteLock (java.util.concurrent.locks.ReadWriteLock)1