package com.aperture_software.glados_wiki.services;
import com.aperture_software.glados_wiki.entities.User;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.util.*;
/**
* Created by jhyun on 14. 1. 4.
*/
@Service
public class TimeZoneService {
private static Logger LOG = LoggerFactory.getLogger(TimeZoneService.class);
@Autowired
private UserService userService;
public TimeZone getDefaultTimeZone() {
return TimeZone.getDefault();
}
@Cacheable(value = "UserTimeZones", key = "#p0")
public TimeZone getTimeZoneByUsername(final String username) {
TimeZone tz = getDefaultTimeZone();
Optional<User> u = userService.getByUsername(username);
if (u.isPresent() && !Strings.isNullOrEmpty(u.get().getTimezone())) {
tz = TimeZone.getTimeZone(u.get().getTimezone());
LOG.debug(String.format("USER [%s] -> TIMEZONE [%s]", username, tz));
}
return tz;
}
@CacheEvict(value = "UserTimeZones", key = "#p0")
public void evictUserTimeZoneCache(final String username) {
// NOTE: DO-NOTHING.
}
private static List<String> _sortedTimeZoneIds = null;
public List<String> sortedTimeZoneIds() {
if (_sortedTimeZoneIds == null) {
_sortedTimeZoneIds = new ArrayList<String>(Arrays.asList(TimeZone.getAvailableIDs()));
Collections.sort(_sortedTimeZoneIds);
}
//
return _sortedTimeZoneIds;
}
}