-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
100 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
150 changes: 76 additions & 74 deletions
150
src/main/java/com/deloitte/elrr/datasync/jpa/service/CommonSvc.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,92 @@ | ||
package com.deloitte.elrr.datasync.jpa.service; | ||
|
||
import com.deloitte.elrr.datasync.exception.ResourceNotFoundException; | ||
|
||
import java.io.Serializable; | ||
import java.util.Optional; | ||
|
||
import org.springframework.data.repository.CrudRepository; | ||
|
||
import com.deloitte.elrr.datasync.exception.ResourceNotFoundException; | ||
|
||
/** | ||
* @author mnelakurti | ||
* @param <T> | ||
* @param <newId> | ||
*/ | ||
public interface CommonSvc<T, newId extends Serializable> { | ||
|
||
/** | ||
* | ||
* @return Iterable<T> | ||
*/ | ||
default Iterable<T> findAll() { | ||
return getRepository().findAll(); | ||
} | ||
/** | ||
* | ||
* @param id | ||
* @return Optional<T> | ||
*/ | ||
default Optional<T> get(newId id) { | ||
return getRepository().findById(id); | ||
} | ||
/** | ||
* | ||
* @param entity | ||
* @return T | ||
*/ | ||
default T save(T entity) { | ||
return getRepository().save(entity); | ||
} | ||
/** | ||
* | ||
* @param entities | ||
* @return Iterable<T> | ||
*/ | ||
default Iterable<T> saveAll(Iterable<T> entities) { | ||
return getRepository().saveAll(entities); | ||
} | ||
/** | ||
* | ||
* @param id | ||
* @throws ResourceNotFoundException | ||
*/ | ||
default void delete(newId id) throws ResourceNotFoundException { | ||
if (getRepository().existsById(id)) { | ||
getRepository().deleteById(id); | ||
} else { | ||
throw new ResourceNotFoundException( | ||
" Id not found for delete : " + id); | ||
} | ||
} | ||
/** | ||
* | ||
*/ | ||
default void deleteAll() { | ||
getRepository().deleteAll(); | ||
/** | ||
* @return Iterable<T> | ||
*/ | ||
default Iterable<T> findAll() { | ||
return getRepository().findAll(); | ||
} | ||
|
||
/** | ||
* @param id | ||
* @return Optional<T> | ||
*/ | ||
default Optional<T> get(newId id) { | ||
return getRepository().findById(id); | ||
} | ||
|
||
/** | ||
* @param entity | ||
* @return T | ||
*/ | ||
default T save(T entity) { | ||
return getRepository().save(entity); | ||
} | ||
|
||
/** | ||
* @param entities | ||
* @return Iterable<T> | ||
*/ | ||
default Iterable<T> saveAll(Iterable<T> entities) { | ||
return getRepository().saveAll(entities); | ||
} | ||
|
||
/** | ||
* @param id | ||
* @throws ResourceNotFoundException | ||
*/ | ||
default void delete(newId id) throws ResourceNotFoundException { | ||
try { | ||
if (getRepository().existsById(id)) { | ||
getRepository().deleteById(id); | ||
} else { | ||
throw new ResourceNotFoundException(" Id not found for delete : " + id); | ||
} | ||
} catch (IllegalArgumentException e) { | ||
e.printStackTrace(); | ||
return; | ||
} | ||
/** | ||
* | ||
* @param entity | ||
* @throws ResourceNotFoundException | ||
*/ | ||
default void update(T entity) throws ResourceNotFoundException { | ||
if (getRepository().existsById(getId(entity))) { | ||
getRepository().save(entity); | ||
} else { | ||
throw new ResourceNotFoundException( | ||
"Not found record in DB to update: " + entity); | ||
} | ||
} | ||
|
||
/** */ | ||
default void deleteAll() { | ||
getRepository().deleteAll(); | ||
} | ||
|
||
/** | ||
* @param entity | ||
* @throws ResourceNotFoundException | ||
*/ | ||
default void update(T entity) throws ResourceNotFoundException { | ||
if (getRepository().existsById(getId(entity))) { | ||
getRepository().save(entity); | ||
} else { | ||
throw new ResourceNotFoundException("Not found record in DB to update: " + entity); | ||
} | ||
/** | ||
* | ||
* @param entity | ||
* @return NewId | ||
*/ | ||
newId getId(T entity); | ||
/** | ||
* | ||
* @return CrudRepository<T, NewId> | ||
*/ | ||
CrudRepository<T, newId> getRepository(); | ||
} | ||
|
||
/** | ||
* @param entity | ||
* @return NewId | ||
*/ | ||
newId getId(T entity); | ||
|
||
/** | ||
* @return CrudRepository<T, NewId> | ||
*/ | ||
CrudRepository<T, newId> getRepository(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters