1 | package gov.va.med.edp.dao.rpc;
|
---|
2 |
|
---|
3 | import gov.va.med.edp.dao.SessionDao;
|
---|
4 | import gov.va.med.edp.vo.SessionVO;
|
---|
5 | import org.springframework.dao.DataAccessException;
|
---|
6 | import org.springframework.dao.DataRetrievalFailureException;
|
---|
7 | import org.w3c.dom.Document;
|
---|
8 | import org.w3c.dom.Element;
|
---|
9 | import org.xml.sax.InputSource;
|
---|
10 | import org.xml.sax.SAXException;
|
---|
11 |
|
---|
12 | import javax.xml.parsers.DocumentBuilder;
|
---|
13 | import javax.xml.parsers.DocumentBuilderFactory;
|
---|
14 | import javax.xml.parsers.ParserConfigurationException;
|
---|
15 | import java.io.IOException;
|
---|
16 | import java.io.StringReader;
|
---|
17 | import java.util.HashMap;
|
---|
18 | import java.util.Map;
|
---|
19 |
|
---|
20 | public class VistaLinkSessionDao extends VistaLinkTrackingDao implements SessionDao {
|
---|
21 | private static final String UNABLE_TO_GET_USER_INFO = "unable to get user info";
|
---|
22 |
|
---|
23 | public SessionVO getSessionInfo(String stationNumber, String duz) throws DataAccessException {
|
---|
24 | Map params = new HashMap();
|
---|
25 | params.put("command", "initUser");
|
---|
26 | String result = executeCommand(stationNumber, duz, params);
|
---|
27 | return createSessionInfo(result);
|
---|
28 | }
|
---|
29 |
|
---|
30 | private SessionVO createSessionInfo(String result) {
|
---|
31 | Document doc = buildDocument(result);
|
---|
32 |
|
---|
33 | SessionVO session = new SessionVO();
|
---|
34 | session.setServerPackageVersion(getServerPackageVersion(doc));
|
---|
35 | session.setTimeOutMillis(Integer.parseInt(getTimeout(doc)));
|
---|
36 | session.setCountDownMillis(Integer.parseInt(getCountdown(doc)));
|
---|
37 | return session;
|
---|
38 | }
|
---|
39 |
|
---|
40 | private String getServerPackageVersion(Document doc) {
|
---|
41 | return getUserElement(doc).getAttribute("version");
|
---|
42 | }
|
---|
43 |
|
---|
44 | private String getTimeout(Document doc) {
|
---|
45 | return getUserElement(doc).getAttribute("timeOut");
|
---|
46 | }
|
---|
47 |
|
---|
48 | private Element getUserElement(Document doc) {
|
---|
49 | return ((Element) doc.getDocumentElement().getElementsByTagName("user").item(0));
|
---|
50 | }
|
---|
51 |
|
---|
52 | private String getCountdown(Document doc) {
|
---|
53 | return getUserElement(doc).getAttribute("countDown");
|
---|
54 | }
|
---|
55 |
|
---|
56 | private Document buildDocument(String result) {
|
---|
57 | try {
|
---|
58 | DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
|
---|
59 | return builder.parse(new InputSource(new StringReader("<results>" + result + "</results>")));
|
---|
60 | } catch (ParserConfigurationException e) {
|
---|
61 | throw new DataRetrievalFailureException(UNABLE_TO_GET_USER_INFO, e);
|
---|
62 | } catch (IOException e) {
|
---|
63 | throw new DataRetrievalFailureException(UNABLE_TO_GET_USER_INFO, e);
|
---|
64 | } catch (SAXException e) {
|
---|
65 | throw new DataRetrievalFailureException(UNABLE_TO_GET_USER_INFO, e);
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 |
|
---|
70 | }
|
---|