source: EDIS/trunk/java/tracking-server-core/src/main/java/gov/va/med/edp/dao/rpc/VistaLinkSessionDao.java@ 1256

Last change on this file since 1256 was 1256, checked in by Solomon Blaz, 13 years ago

added guard clauses to VistaLinkSessionDao in case initUser RPC fails

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