source: ccr/trunk/nhin-vista/projects/NHINC/Current/Product/Production/Common/AssigningAuthoritiesToHomeCommunityMappingDAO/src/gov/hhs/fha/nhinc/common/connectionmanager/dao/AssigningAuthorityHomeCommunityMappingDAO.java@ 507

Last change on this file since 507 was 507, checked in by George Lilly, 15 years ago

NHIN gateway and adaptor for use on linux with VistA EHR and RPMS

File size: 9.8 KB
Line 
1/*
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
4 */
5package gov.hhs.fha.nhinc.common.connectionmanager.dao;
6
7import gov.hhs.fha.nhinc.common.connectionmanager.model.AssigningAuthorityToHomeCommunityMapping;
8import gov.hhs.fha.nhinc.common.connectionmanager.persistence.HibernateUtil;
9import java.util.ArrayList;
10import java.util.List;
11import org.apache.commons.logging.Log;
12import org.apache.commons.logging.LogFactory;
13import org.hibernate.Criteria;
14import org.hibernate.Session;
15import org.hibernate.SessionFactory;
16import org.hibernate.Transaction;
17import org.hibernate.criterion.Expression;
18
19/**
20 *
21 * @author svalluripalli
22 */
23public class AssigningAuthorityHomeCommunityMappingDAO {
24
25 private static Log log = LogFactory.getLog(AssigningAuthorityHomeCommunityMappingDAO.class);
26
27 /**
28 * This method retrieves and returns a AssigningAuthority for an Home Community...
29 * @param homeCommunityId
30 * @return String
31 */
32 public String getAssigningAuthority(String homeCommunityId) {
33 log.debug("--Begin AssigningAuthorityHomeCommunityMappingDAO.getACommunityIdForAssigningAuthority() ---");
34 Session sess = null;
35 String assigningAuthId = "";
36 if (homeCommunityId != null && !homeCommunityId.equals("")) {
37 String sql = "select assigningauthorityid from assigningauthoritytohomecommunitymapping where homecommunityid = '" + homeCommunityId +"'";
38 SessionFactory fact = HibernateUtil.getSessionFactory();
39 try {
40 sess = fact.openSession();
41 if (sess != null) {
42 Criteria criteria = sess.createCriteria(AssigningAuthorityToHomeCommunityMapping.class);
43 criteria.add(Expression.eq("homeCommunityId", homeCommunityId));
44 List<AssigningAuthorityToHomeCommunityMapping> l = criteria.list();
45 if (l != null && l.size() > 0) {
46 assigningAuthId = l.get(0).getAssigningAuthorityId();
47 }
48 } else {
49 log.error("Unable create Hibernate Sessions");
50 }
51 } finally {
52 if (sess != null) {
53 try {
54 sess.close();
55 } catch (Throwable t) {
56 log.error("Failed to close session: " + t.getMessage(), t);
57 }
58 }
59 }
60 } else {
61 log.error("Please provide a valid homeCommunityId");
62 return null;
63 }
64 log.debug("--End AssigningAuthorityHomeCommunityMappingDAO.getACommunityIdForAssigningAuthority() ---");
65 return assigningAuthId;
66 }
67
68 /**
69 * returns List of Assigning Authorities for a given Home Community Id
70 * @param homeCommId
71 * @return List
72 */
73 public List<String> getAssigningAuthoritiesByHomeCommunity(String homeCommunityId)
74 {
75 log.debug("-- Begin AssigningAuthorityHomeCommunityMappingDAO.getAssigningAuthoritiesByHomeCommunity() ---");
76 Session sess = null;
77 List<String> listOfAAs = null;
78 if (homeCommunityId != null && !homeCommunityId.equals("")) {
79 //String sql = "select assigningauthorityid from assigningauthoritytohomecommunitymapping where homecommunityid = '" + homeCommunityId +"'";
80 SessionFactory fact = HibernateUtil.getSessionFactory();
81 try {
82 sess = fact.openSession();
83 if (sess != null) {
84 Criteria criteria = sess.createCriteria(AssigningAuthorityToHomeCommunityMapping.class);
85 criteria.add(Expression.eq("homeCommunityId", homeCommunityId));
86 List<AssigningAuthorityToHomeCommunityMapping> l = criteria.list();
87 if(l != null && l.size() > 0)
88 {
89 listOfAAs = new ArrayList<String>();
90 int size = l.size();
91 String sAA = "";
92 for(int i = 0; i < size; i++)
93 {
94 sAA = l.get(i).getAssigningAuthorityId();
95 listOfAAs.add(sAA);
96 }
97 }
98 } else {
99 log.error("Unable create Hibernate Sessions");
100 }
101 } finally {
102 if (sess != null) {
103 try {
104 sess.close();
105 } catch (Throwable t) {
106 log.error("Failed to close session: " + t.getMessage(), t);
107 }
108 }
109 }
110 } else {
111 log.error("Please provide a valid homeCommunityId");
112 return null;
113 }
114 log.debug("-- End AssigningAuthorityHomeCommunityMappingDAO.getAssigningAuthoritiesByHomeCommunity() ---");
115 return listOfAAs;
116 }
117
118 /**
119 * This method retrieves Home Community for an Assigning Authority...
120 * @param assigningAuthority
121 */
122 public String getHomeCommunityId(String assigningAuthority) {
123 log.debug("--Begin AssigningAuthorityHomeCommunityMappingDAO.getAllCommunityIdsForAllAssigningAuthorities() ---");
124 String homeCommunity = "";
125 if (assigningAuthority != null && !assigningAuthority.equals("")) {
126 Session sess = null;
127 String sql = "select homecommunityid from assigningauthoritytohomecommunitymapping where assigningauthorityid = '" + assigningAuthority + "'";
128 SessionFactory fact = HibernateUtil.getSessionFactory();
129 try {
130 sess = fact.openSession();
131 if (sess != null) {
132 Criteria criteria = sess.createCriteria(AssigningAuthorityToHomeCommunityMapping.class);
133 criteria.add(Expression.eq("assigningAuthorityId", assigningAuthority));
134 List<AssigningAuthorityToHomeCommunityMapping> l = criteria.list();
135 if (l != null && l.size() > 0) {
136 homeCommunity = l.get(0).getHomeCommunityId();
137 }
138 } else {
139 log.error("Unable to create session");
140 }
141 } finally {
142 if (sess != null) {
143 try {
144 sess.close();
145 } catch (Throwable t) {
146 log.error("Failed to close session: " + t.getMessage(), t);
147 }
148 }
149 }
150 } else {
151 log.error("Enter correct assigning authority");
152 }
153 log.debug("--End AssigningAuthorityHomeCommunityMappingDAO.getAllCommunityIdsForAllAssigningAuthorities() ---");
154 return homeCommunity;
155 }
156
157 /**
158 * This method stores Assigning Authority To Home Community Mapping...
159 * @param homeCommunityId
160 * @param assigningAuthority
161 */
162 public boolean storeMapping(String homeCommunityId, String assigningAuthority) {
163 log.debug("--Begin AssigningAuthorityHomeCommunityMappingDAO.storeAssigningAuthorityAndHomeCommunity() ---");
164 System.out.println("--Begin AssigningAuthorityHomeCommunityMappingDAO.storeAssigningAuthorityAndHomeCommunity() ---");
165 boolean success = false;
166 AssigningAuthorityToHomeCommunityMapping mappingInfo = null;
167 Transaction trans = null;
168 Session sess = null;
169 if (homeCommunityId != null && !homeCommunityId.equals("") && assigningAuthority != null && !assigningAuthority.equals("")) {
170 String sql = "select * from assigningauthoritytohomecommunitymapping where assigningauthorityid='" + assigningAuthority + "' and homecommunityid='" + homeCommunityId + "'";
171 SessionFactory fact = HibernateUtil.getSessionFactory();
172 try {
173 sess = fact.openSession();
174 if (sess != null) {
175 Criteria criteria = sess.createCriteria(AssigningAuthorityToHomeCommunityMapping.class);
176 criteria.add(Expression.eq("assigningAuthorityId", assigningAuthority));
177 criteria.add(Expression.eq("homeCommunityId", homeCommunityId));
178 List<AssigningAuthorityToHomeCommunityMapping> l = criteria.list();
179 if (l != null && l.size() > 0) {
180 log.info("Assigning Authority and Home Community pair already present in the repository");
181 } else {
182 mappingInfo = new AssigningAuthorityToHomeCommunityMapping();
183 mappingInfo.setAssigningAuthorityId(assigningAuthority);
184 mappingInfo.setHomeCommunityId(homeCommunityId);
185 trans = sess.beginTransaction();
186 sess.saveOrUpdate(mappingInfo);
187 success = true;
188 }
189 } else {
190 log.error("Unable to create session information");
191 }
192 } finally {
193 if (trans != null) {
194 try {
195 trans.commit();
196 } catch (Throwable t) {
197 log.error("Failed to commit transaction: " + t.getMessage(), t);
198 }
199 }
200 if (sess != null) {
201 try {
202 sess.close();
203 } catch (Throwable t) {
204 log.error("Failed to close session: " + t.getMessage(), t);
205 }
206 }
207 }
208 } else {
209 log.error("Invalid data entered, Enter Valid data to store");
210 }
211 log.debug("--End AssigningAuthorityHomeCommunityMappingDAO.storeAssigningAuthorityAndHomeCommunity() ---");
212 System.out.println("--End AssigningAuthorityHomeCommunityMappingDAO.storeAssigningAuthorityAndHomeCommunity() ---");
213 return success;
214 }
215}
Note: See TracBrowser for help on using the repository browser.