1
1
package main .model .db .dao ;
2
2
3
3
import com .mysql .cj .core .conf .url .ConnectionUrlParser .Pair ;
4
+ import main .exceptions .AqualityParametersException ;
4
5
import main .exceptions .AqualitySQLException ;
5
6
import main .exceptions .AqualityException ;
6
7
import main .model .db .RS_Converter ;
@@ -82,30 +83,58 @@ public List<T> searchAll(T entity) throws AqualityException {
82
83
83
84
/**
84
85
* Get single entity by id
85
- * @param entity entity id
86
+ * @param id entity id
86
87
* @return entity
87
88
*/
88
- public T getEntityById (T entity ) throws AqualityException {
89
- List <T > all = searchAll (entity );
90
- if (all .size () > 0 ) {
91
- return all .get (0 );
92
- }
93
- else {
94
- throw new AqualityException ("No Entities was found by %s" , entity .getIDParameters ());
89
+ public T getEntityById (Integer id ) throws AqualityException {
90
+ T entity ;
91
+ try {
92
+ entity = type .newInstance ();
93
+ } catch (InstantiationException | IllegalAccessException e ) {
94
+ throw new AqualityException ("Cannot Create new Instance of entity" );
95
95
}
96
+
97
+ List <Pair <String , String >> parameters = entity .getIdSearchParameters (id );
98
+ List <T > all = dtoMapper .mapObjects (CallStoredProcedure (select , parameters ).toString ());
99
+
100
+ return getSingleResult (all , id );
96
101
}
97
102
103
+ /**
104
+ * Get single entity by id and project_id
105
+ * @param entity entity with id and project_id
106
+ * @return entity
107
+ */
108
+ public T getEntityById (T entity ) throws AqualityException {
109
+ List <Pair <String , String >> parameters = entity .getIdAndProjectIdSearchParameters ();
110
+ List <T > all = dtoMapper .mapObjects (CallStoredProcedure (select , parameters ).toString ());
111
+
112
+ return getSingleResult (all , entity .getIdOrOverrideId ());
113
+ }
98
114
99
115
/**
100
116
* Update entity
101
117
* @param entity with fields that should be updated (id is required)
102
118
* @return Updated entity
103
119
*/
104
120
public T update (T entity ) throws AqualityException {
105
- if (entity .getIDParameters ().size () > 0 ){
106
- return create (entity );
121
+ try {
122
+ if (entity .hasProjectId ()){
123
+ getEntityById (entity );
124
+ } else {
125
+ getEntityById (entity .getIdOrOverrideId ());
126
+ }
127
+ } catch (AqualityException e ) {
128
+ throw new AqualityParametersException ("Entity with specified '%s' id does not exist!" , entity .getIdOrOverrideId ());
129
+ }
130
+
131
+ List <Pair <String , String >> parameters = entity .getParameters ();
132
+ List <T > results = dtoMapper .mapObjects (CallStoredProcedure (insert , parameters ).toString ());
133
+ if (!results .isEmpty ()) {
134
+ return results .get (0 );
107
135
}
108
- throw new AqualityException ("Cannot update entity without id!" );
136
+
137
+ throw new AqualityException ("Was not able to update entity!" );
109
138
}
110
139
111
140
/**
@@ -114,7 +143,7 @@ public T update(T entity) throws AqualityException {
114
143
* @return true if was able to remove entity
115
144
*/
116
145
public boolean delete (T entity ) throws AqualityException {
117
- List <Pair <String , String >> parameters = entity .getIDParameters ();
146
+ List <Pair <String , String >> parameters = entity .getDataBaseIDParameters ();
118
147
119
148
CallStoredProcedure (remove , parameters );
120
149
return true ;
@@ -126,10 +155,22 @@ public boolean delete(T entity) throws AqualityException {
126
155
* @return created entity
127
156
*/
128
157
public T create (T entity ) throws AqualityException {
129
- List <Pair <String , String >> parameters = entity .getParameters ();
130
- List <T > results = dtoMapper .mapObjects (CallStoredProcedure (insert , parameters ).toString ());
131
- if (results .size () > 0 ){
132
- return results .get (0 );
158
+ Integer id = null ;
159
+ try {
160
+ id = entity .getIdOrOverrideId ();
161
+ } catch (AqualityException e ) {
162
+ // entity has no id
163
+ }
164
+
165
+ if (id == null ){
166
+ List <Pair <String , String >> parameters = entity .getParameters ();
167
+ List <T > results = dtoMapper .mapObjects (CallStoredProcedure (insert , parameters ).toString ());
168
+ if (!results .isEmpty ()){
169
+ return results .get (0 );
170
+ }
171
+ }
172
+ else {
173
+ return update (entity );
133
174
}
134
175
135
176
throw new AqualitySQLException (new SQLException ("Possible duplicate error." , "23505" ));
@@ -176,6 +217,15 @@ protected JSONArray CallStoredProcedure(String sql, List<Pair<String, String>> p
176
217
return json ;
177
218
}
178
219
220
+ private T getSingleResult (List <T > allResults , Integer id ) throws AqualityException {
221
+ if (!allResults .isEmpty ()) {
222
+ return allResults .get (0 );
223
+ }
224
+ else {
225
+ throw new AqualityException ("No Entities was found by '%s' id" , id );
226
+ }
227
+ }
228
+
179
229
private void getConnection () throws AqualityException {
180
230
InitialContext initialContext ;
181
231
try {
@@ -220,13 +270,22 @@ private CallableStatement executeCallableStatement(String sql, List<Pair<String,
220
270
}
221
271
}
222
272
223
- try {
224
- callableStatement .execute ();
225
- } catch (SQLException e ) {
226
- throw new AqualitySQLException (e );
227
- }
273
+ return tryExecute (callableStatement );
274
+ }
228
275
229
- return callableStatement ;
276
+ private CallableStatement tryExecute (CallableStatement callableStatement ) throws AqualitySQLException {
277
+ int counter = 0 ;
278
+ SQLException lastException = null ;
279
+ while (counter < 5 ) {
280
+ try {
281
+ callableStatement .execute ();
282
+ return callableStatement ;
283
+ } catch (SQLException e ) {
284
+ counter ++;
285
+ lastException = e ;
286
+ }
287
+ }
288
+ throw new AqualitySQLException (lastException );
230
289
}
231
290
232
291
private CallableStatement getCallableStatement (String sql ) throws AqualityException {
@@ -261,4 +320,14 @@ private String getSqlName(String storedProcedure){
261
320
}
262
321
return "" ;
263
322
}
323
+
324
+ private boolean areParametersEmpty (List <Pair <String , String >> parameters ) {
325
+ for (Pair <String , String > pair : parameters ) {
326
+ if (!pair .right .equals ("" )){
327
+ return false ;
328
+ }
329
+ }
330
+
331
+ return true ;
332
+ }
264
333
}
0 commit comments