View Javadoc
1   package civitas.bboard.server.electioncache;
2   
3   import java.io.IOException;
4   import java.util.Optional;
5   
6   import org.springframework.beans.factory.annotation.Autowired;
7   import org.springframework.stereotype.Controller;
8   
9   import civitas.common.CommonConstants;
10  import civitas.common.ConvertFromXml;
11  import civitas.common.election.ElectionEvent;
12  import jakarta.xml.bind.JAXBException;
13  
14  @Controller
15  public class UpdateCache {
16  
17  	@Autowired
18  	ConvertFromXml convertFromXml;
19  
20  	@Autowired
21  	ElectionCacheRepository electionCacheRepository;
22  
23  	public void apply(final String bbid, final String meta, final String mesg, final long t)
24  			throws JAXBException, IOException {
25  
26  		Optional<ElectionCache> cachep = electionCacheRepository.findById(bbid);
27  		if (!cachep.isPresent()) {
28  			throw new IllegalArgumentException("no cache");
29  		}
30  
31  		ElectionCache cache = cachep.get();
32  
33  		if (CommonConstants.ELECTION_EVENT_META.equals(meta)) {
34  			ElectionEvent e = convertFromXml.apply(mesg, ElectionEvent.class);
35  			if (ElectionEvent.EVENT_KIND_FINALIZE.equals(e.kind())) {
36  				// FIXME: tally
37  				cache.electionFinalizeTime = t;
38  			} else if (ElectionEvent.EVENT_KIND_START.equals(e.kind())) {
39  				cache.electionStartTime = t;
40  			} else if (ElectionEvent.EVENT_KIND_STOP.equals(e.kind())) {
41  				cache.electionStopTime = t;
42  			}
43  		}
44  
45  		electionCacheRepository.save(cache);
46  	}
47  }