Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private final Map<Key, Map<Integer, NapeduvDataMap>> cache = new HashMap<Key, Map<Integer, NapeduvDataMap>>();
- @PersistenceContext(unitName = "AAMSMessages")
- protected Session session;
- private AtomicWorkService workService;
- public TaxPeriodServiceImpl(@Reference AtomicWorkService workService) {
- this.workService = workService;
- }
- @Init
- public void init() throws AAMSException {
- initializeCache();
- }
- public Map<Integer, NapeduvDataMap> getInformation(int messageType, Date eventPeriod) {
- Key key = new Key(messageType, getYear(eventPeriod));
- synchronized (cache) {
- return cache.get(key);
- }
- }
- /**
- * Populates the tax data cache from a backing table.
- *
- * @throws AAMSException if the table cannot be read
- */
- @SuppressWarnings({"unchecked"})
- @ManagementOperation(path = "refresh", type = OperationType.GET)
- private void initializeCache() throws AAMSException {
- workService.execute(new UnitOfWork() {
- public void execute() {
- // synchronize to ensure the cache is not being repopulated while processing a request
- synchronized (cache) {
- cache.clear();
- Query query = session.createQuery("from NapeduvDefinitionEntity e");
- List<NapeduvDefinitionEntity> definitions = query.list();
- if (definitions.isEmpty()) {
- return;
- // TODO issue a warning
- }
- Map<Integer, NapeduvDataMap> map = new HashMap<Integer, NapeduvDataMap>();
- for (NapeduvDefinitionEntity definition : definitions) {
- NapeduvDataMap dataMap = new NapeduvDataMap();
- dataMap.setDeclarationFieldName(definition.getDeclFieldName());
- dataMap.setUiOrder(definition.getUiOrder());
- map.put(definition.getNapNumber(), dataMap);
- }
- NapeduvDefinitionEntity definition = definitions.get(0);
- Key key = createKey(definition);
- cache.put(key, map);
- }
- }
- });
- }
- private Key createKey(NapeduvDefinitionEntity definition) {
- int year = getYear(definition.getTaxPeriod());
- int messageType = Integer.parseInt(definition.getMsgType());
- return new Key(messageType, year);
- }
- private int getYear(Date date) {
- Calendar calendar = Calendar.getInstance();
- calendar.setTime(date);
- return calendar.get(Calendar.YEAR);
- }
- /**
- * Key used to match on message type and event period (year).
- */
- private class Key {
- private int messageType;
- private int eventPeriod;
- private Key(int messageType, int eventPeriod) {
- this.messageType = messageType;
- this.eventPeriod = eventPeriod;
- }
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- Key key = (Key) o;
- return eventPeriod == key.eventPeriod && messageType == key.messageType;
- }
- public int hashCode() {
- int result = messageType;
- result = 31 * result + eventPeriod;
- return result;
- }
- }
- }
Add Comment
Please, Sign In to add comment