11package main .model .db .dao ;
22
33import com .mysql .cj .core .conf .url .ConnectionUrlParser .Pair ;
4+ import main .exceptions .AqualityParametersException ;
45import main .exceptions .AqualitySQLException ;
56import main .exceptions .AqualityException ;
67import main .model .db .RS_Converter ;
@@ -82,30 +83,58 @@ public List<T> searchAll(T entity) throws AqualityException {
8283
8384 /**
8485 * Get single entity by id
85- * @param entity entity id
86+ * @param id entity id
8687 * @return entity
8788 */
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" );
9595 }
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 );
96101 }
97102
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+ }
98114
99115 /**
100116 * Update entity
101117 * @param entity with fields that should be updated (id is required)
102118 * @return Updated entity
103119 */
104120 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 );
107135 }
108- throw new AqualityException ("Cannot update entity without id!" );
136+
137+ throw new AqualityException ("Was not able to update entity!" );
109138 }
110139
111140 /**
@@ -114,7 +143,7 @@ public T update(T entity) throws AqualityException {
114143 * @return true if was able to remove entity
115144 */
116145 public boolean delete (T entity ) throws AqualityException {
117- List <Pair <String , String >> parameters = entity .getIDParameters ();
146+ List <Pair <String , String >> parameters = entity .getDataBaseIDParameters ();
118147
119148 CallStoredProcedure (remove , parameters );
120149 return true ;
@@ -126,10 +155,22 @@ public boolean delete(T entity) throws AqualityException {
126155 * @return created entity
127156 */
128157 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 );
133174 }
134175
135176 throw new AqualitySQLException (new SQLException ("Possible duplicate error." , "23505" ));
@@ -176,6 +217,15 @@ protected JSONArray CallStoredProcedure(String sql, List<Pair<String, String>> p
176217 return json ;
177218 }
178219
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+
179229 private void getConnection () throws AqualityException {
180230 InitialContext initialContext ;
181231 try {
@@ -220,13 +270,22 @@ private CallableStatement executeCallableStatement(String sql, List<Pair<String,
220270 }
221271 }
222272
223- try {
224- callableStatement .execute ();
225- } catch (SQLException e ) {
226- throw new AqualitySQLException (e );
227- }
273+ return tryExecute (callableStatement );
274+ }
228275
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 );
230289 }
231290
232291 private CallableStatement getCallableStatement (String sql ) throws AqualityException {
@@ -261,4 +320,14 @@ private String getSqlName(String storedProcedure){
261320 }
262321 return "" ;
263322 }
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+ }
264333}
0 commit comments