11package com .sage .dao ;
22
33import com .sage .model .vulnerability .VulnerabilityModel ;
4+ import com .sage .utility .JPAManager ;
5+ import jakarta .persistence .EntityManager ;
6+ import jakarta .persistence .EntityTransaction ;
47
58import java .sql .Clob ;
69import java .util .ArrayList ;
@@ -24,46 +27,60 @@ public static VulnerabilityDao instance() {
2427
2528 @ Override
2629 public boolean create (VulnerabilityModel entity ) {
30+ EntityManager entityManager = JPAManager .getEntityManager ();
31+ EntityTransaction transaction = entityManager .getTransaction ();
2732 try {
28- tx .begin ();
29- em .persist (entity );
30- tx .commit ();
33+ transaction .begin ();
34+ entityManager .persist (entity );
35+ transaction .commit ();
3136 return true ;
3237 } catch (Exception e ) {
33- if (tx .isActive ())
34- tx .rollback ();
38+ if (transaction .isActive ())
39+ transaction .rollback ();
3540
3641 LOGGER .severe ("[VulnerabilityDao] Error while inserting new vulnerability entity into database: " + e .getMessage ());
3742 return false ;
43+ } finally {
44+ entityManager .close ();
3845 }
3946 }
4047
4148 @ Override
4249 public VulnerabilityModel read (Integer key ) {
50+ EntityManager entityManager = JPAManager .getEntityManager ();
51+ EntityTransaction transaction = entityManager .getTransaction ();
4352 try {
44- tx .begin ();
45- VulnerabilityModel vulnerabilityModel = em .find (VulnerabilityModel .class , key );
46- tx .commit ();
53+ transaction .begin ();
54+ VulnerabilityModel vulnerabilityModel = entityManager .find (VulnerabilityModel .class , key );
55+ transaction .commit ();
4756 return vulnerabilityModel ;
4857 } catch (Exception e ) {
4958 LOGGER .warning ("[VulnerabilityDao] Error while trying to fetch entity with (id)=" + key );
5059 return null ;
60+ } finally {
61+ entityManager .close ();
5162 }
5263
5364 }
5465
5566 @ Override
5667 VulnerabilityModel update (Integer key , VulnerabilityModel newEntity ) {
57- tx .begin ();
58- VulnerabilityModel entity = em .find (VulnerabilityModel .class , key );
59- if (entity == null ) {
60- LOGGER .warning (String .format ("[VulnerabilityDao] Error while updating entity. Entity with id=(%d) could not be found." , key ));
61- return null ;
62- } else {
63- // update (actually not even necessary for this project :P
64- }
68+ EntityManager entityManager = JPAManager .getEntityManager ();
69+ EntityTransaction transaction = entityManager .getTransaction ();
6570
66- throw new UnsupportedOperationException ("Unimplemented method 'update'" );
71+ try {
72+ transaction .begin ();
73+ VulnerabilityModel entity = entityManager .find (VulnerabilityModel .class , key );
74+ if (entity == null ) {
75+ LOGGER .warning (String .format ("[VulnerabilityDao] Error while updating entity. Entity with id=(%d) could not be found." , key ));
76+ return null ;
77+ } else {
78+ return null ;
79+ // update (actually not even necessary for this project :P
80+ }
81+ } finally {
82+ entityManager .close ();
83+ }
6784 }
6885
6986 @ Override
@@ -74,41 +91,43 @@ boolean delete(Integer key) {
7491 }
7592
7693 public List <VulnerabilityModel > readAll () {
77- String query = "SELECT * FROM vulnerabilities;" ;
78-
79- List <Object []> results = em .createNativeQuery (query ).getResultList ();
80- List <VulnerabilityModel > vulnerabilityModels = new ArrayList <>();
81-
82- for (Object [] row : results ) {
83- VulnerabilityModel vulnerabilityModel = new VulnerabilityModel (
84- ((Number ) row [0 ]).intValue (),
85- (String ) row [1 ],
86- (String ) row [2 ],
87- ((Clob ) row [3 ]).toString (),
88- (String ) row [4 ],
89- (String ) row [5 ],
90- (String ) row [6 ],
91- (String ) row [7 ]
92- );
93- vulnerabilityModels .add (vulnerabilityModel );
94- }
94+ EntityManager entityManager = JPAManager .getEntityManager ();
95+
96+ String query = "SELECT v FROM VulnerabilityModel v" ;
9597
96- return vulnerabilityModels ;
98+ try {
99+ List <VulnerabilityModel > vulnerabilityModels = entityManager .createQuery (query , VulnerabilityModel .class ).getResultList ();
100+ return vulnerabilityModels ;
101+ } catch (Exception e ) {
102+ LOGGER .warning ("Error occured while fetching all vulnerabilities from database: " + e .getMessage ());
103+ return new ArrayList <>();
104+ } finally {
105+ entityManager .close ();
106+ }
97107 }
98108
99109 public HashMap <String , Integer > getSeverities () {
110+ EntityManager entityManager = JPAManager .getEntityManager ();
111+
100112 String query = "SELECT severity, COUNT(*) AS count FROM vulnerabilities GROUP BY severity" ;
101113
102- List <Object []> results = em .createNativeQuery (query ).getResultList ();
103- HashMap <String , Integer > severities = new HashMap <>();
114+ try {
115+ List <Object []> results = entityManager .createNativeQuery (query ).getResultList ();
116+ HashMap <String , Integer > severities = new HashMap <>();
104117
105- for (Object [] row : results ) {
106- String severity = (String ) row [0 ];
107- Number count = (Number ) row [1 ];
108- severities .put (severity , count .intValue ());
109- }
118+ for (Object [] row : results ) {
119+ String severity = (String ) row [0 ];
120+ Number count = (Number ) row [1 ];
121+ severities .put (severity , count .intValue ());
122+ }
110123
111- return severities ;
124+ return severities ;
125+ } catch (Exception e ) {
126+ LOGGER .warning ("Error occured while fetching severities from database: " + e .getMessage ());
127+ return new HashMap <>();
128+ } finally {
129+ entityManager .close ();
130+ }
112131 }
113132
114133}
0 commit comments