Guest User

Untitled

a guest
Feb 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.49 KB | None | 0 0
  1.     private final Map<Key, Map<Integer, NapeduvDataMap>> cache = new HashMap<Key, Map<Integer, NapeduvDataMap>>();
  2.  
  3.     @PersistenceContext(unitName = "AAMSMessages")
  4.     protected Session session;
  5.  
  6.     private AtomicWorkService workService;
  7.  
  8.     public TaxPeriodServiceImpl(@Reference AtomicWorkService workService) {
  9.         this.workService = workService;
  10.     }
  11.  
  12.     @Init
  13.     public void init() throws AAMSException {
  14.         initializeCache();
  15.     }
  16.  
  17.     public Map<Integer, NapeduvDataMap> getInformation(int messageType, Date eventPeriod) {
  18.         Key key = new Key(messageType, getYear(eventPeriod));
  19.         synchronized (cache) {
  20.             return cache.get(key);
  21.         }
  22.     }
  23.  
  24.     /**
  25.      * Populates the tax data cache from a backing table.
  26.      *
  27.      * @throws AAMSException if the table cannot be read
  28.      */
  29.     @SuppressWarnings({"unchecked"})
  30.     @ManagementOperation(path = "refresh", type = OperationType.GET)
  31.     private void initializeCache() throws AAMSException {
  32.         workService.execute(new UnitOfWork() {
  33.             public void execute() {
  34.                 // synchronize to ensure the cache is not being repopulated while processing a request
  35.                 synchronized (cache) {
  36.                     cache.clear();
  37.                     Query query = session.createQuery("from NapeduvDefinitionEntity e");
  38.  
  39.                     List<NapeduvDefinitionEntity> definitions = query.list();
  40.                     if (definitions.isEmpty()) {
  41.                         return;
  42.                         // TODO issue a warning
  43.                     }
  44.                     Map<Integer, NapeduvDataMap> map = new HashMap<Integer, NapeduvDataMap>();
  45.                     for (NapeduvDefinitionEntity definition : definitions) {
  46.                         NapeduvDataMap dataMap = new NapeduvDataMap();
  47.                         dataMap.setDeclarationFieldName(definition.getDeclFieldName());
  48.                         dataMap.setUiOrder(definition.getUiOrder());
  49.                         map.put(definition.getNapNumber(), dataMap);
  50.                     }
  51.  
  52.                     NapeduvDefinitionEntity definition = definitions.get(0);
  53.                     Key key = createKey(definition);
  54.                     cache.put(key, map);
  55.  
  56.                 }
  57.             }
  58.         });
  59.     }
  60.  
  61.     private Key createKey(NapeduvDefinitionEntity definition) {
  62.         int year = getYear(definition.getTaxPeriod());
  63.         int messageType = Integer.parseInt(definition.getMsgType());
  64.         return new Key(messageType, year);
  65.     }
  66.  
  67.     private int getYear(Date date) {
  68.         Calendar calendar = Calendar.getInstance();
  69.         calendar.setTime(date);
  70.         return calendar.get(Calendar.YEAR);
  71.     }
  72.  
  73.     /**
  74.      * Key used to match on message type and event period (year).
  75.      */
  76.     private class Key {
  77.         private int messageType;
  78.         private int eventPeriod;
  79.  
  80.         private Key(int messageType, int eventPeriod) {
  81.             this.messageType = messageType;
  82.             this.eventPeriod = eventPeriod;
  83.         }
  84.  
  85.         public boolean equals(Object o) {
  86.             if (this == o) return true;
  87.             if (o == null || getClass() != o.getClass()) return false;
  88.  
  89.             Key key = (Key) o;
  90.  
  91.             return eventPeriod == key.eventPeriod && messageType == key.messageType;
  92.  
  93.         }
  94.  
  95.         public int hashCode() {
  96.             int result = messageType;
  97.             result = 31 * result + eventPeriod;
  98.             return result;
  99.         }
  100.     }
  101. }
Add Comment
Please, Sign In to add comment