Skip to content

Commit

Permalink
archive-mgr: jpql query with the parameter code extended
Browse files Browse the repository at this point in the history
  • Loading branch information
DenChaykovskiy committed Apr 19, 2024
1 parent 7e78125 commit 46e315a
Showing 1 changed file with 87 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,9 @@
import de.dlr.proseo.model.service.RepositoryService;
import de.dlr.proseo.model.service.SecurityService;


/**
* Service methods required to create, modify and delete product archives in the prosEO database, and to query the database about
* such archives
* Service methods required to create, modify and delete product archives in the
* prosEO database, and to query the database about such archives
*
* @author Denys Chaykovskiy
*/
Expand All @@ -58,8 +57,7 @@ public class ProductArchiveManager {
/** Utility class for user authorizations */
@Autowired
private SecurityService securityService;



/** The product archive manager configuration */
@Autowired
ProductArchiveManagerConfiguration config;
Expand All @@ -85,7 +83,8 @@ public RestProductArchive createArchive(RestProductArchive restArchive) throws I
}

if (archiveExistsByCode(restArchive.getCode())) {
throw new IllegalArgumentException(logger.log(ProductArchiveMgrMessage.DUPLICATED_ARCHIVE, restArchive.getCode()));
throw new IllegalArgumentException(
logger.log(ProductArchiveMgrMessage.DUPLICATED_ARCHIVE, restArchive.getCode()));
}

// all checks inside
Expand Down Expand Up @@ -141,7 +140,8 @@ public boolean archiveExistsById(Long id) {
*
* @param code the code of the product archive
* @return product archive
* @throws IllegalArgumentException if no product archive matching the given code could be found
* @throws IllegalArgumentException if no product archive matching the given
* code could be found
*/
public RestProductArchive getArchiveByCode(String code) {

Expand All @@ -164,7 +164,8 @@ public RestProductArchive getArchiveByCode(String code) {
*
* @param code the code of the product archive
* @return a list of product archives, if code == null, returns all archives
* @throws NoResultException if no product archives matching the given search criteria could be found
* @throws NoResultException if no product archives matching the given search
* criteria could be found
*/
public List<RestProductArchive> getArchivesByCode(String code) {

Expand Down Expand Up @@ -205,7 +206,8 @@ public List<RestProductArchive> getArchivesByCode(String code) {
* @param id the ID to look for
* @return a Json object corresponding to the product archive found
* @throws IllegalArgumentException if no product archive ID was given
* @throws NoResultException if no product archive with the given ID exists
* @throws NoResultException if no product archive with the given ID
* exists
*/
public RestProductArchive getArchiveById(Long id) throws IllegalArgumentException, NoResultException {

Expand All @@ -226,74 +228,85 @@ public RestProductArchive getArchiveById(Long id) throws IllegalArgumentExceptio

return new ProductArchiveModelMapper(modelArchive).toRest();
}



/**
* Get product archives by name and archive type
*
* @param name the archive name
* @param archiveType the archive type
* @param recordFrom first record of filtered and ordered result to return
* @param recordTo last record of filtered and ordered result to return
* @return a list of Json objects representing product archives satisfying the search criteria
* @throws NoResultException if no product archives matching the given search criteria could be found
* @param code the archive code
* @param name the archive name
* @param archiveType the archive type
* @param recordFrom first record of filtered and ordered result to return
* @param recordTo last record of filtered and ordered result to return
* @return a list of Json objects representing product archives satisfying the
* search criteria
* @throws NoResultException if no product archives matching the given search
* criteria could be found
*/
public List<RestProductArchive> getArchives(String name, String archiveType, Integer recordFrom, Integer recordTo)
throws NoResultException {
public List<RestProductArchive> getArchives(String code, String name, String archiveType, Integer recordFrom,
Integer recordTo) throws NoResultException {

if (logger.isTraceEnabled())
logger.trace(">>> getArchives({}, {}, {}, {}})", name, archiveType, recordFrom, recordTo);
logger.trace(">>> getArchives({}, {}, {}, {}, {})", code, name, archiveType, recordFrom, recordTo);

if (recordFrom == null) {
recordFrom = 0;
}
if (recordTo == null) {
recordTo = Integer.MAX_VALUE;
}

Long numberOfResults = Long.parseLong(
this.countArchives(name, archiveType));
Long numberOfResults = Long.parseLong(this.countArchives(name, archiveType));
Integer maxResults = config.getMaxResults();
if (numberOfResults > maxResults && (recordTo - recordFrom) > maxResults && (numberOfResults - recordFrom) > maxResults) {
throw new HttpClientErrorException(HttpStatus.TOO_MANY_REQUESTS,
logger.log(GeneralMessage.TOO_MANY_RESULTS, "productArchives", numberOfResults, config.getMaxResults()));
if (numberOfResults > maxResults && (recordTo - recordFrom) > maxResults
&& (numberOfResults - recordFrom) > maxResults) {
throw new HttpClientErrorException(HttpStatus.TOO_MANY_REQUESTS, logger.log(GeneralMessage.TOO_MANY_RESULTS,
"productArchives", numberOfResults, config.getMaxResults()));
}

List<RestProductArchive> result = new ArrayList<>();

String jpqlQuery = "select w from ProductArchive w";

if ((null != name) || (null != archiveType)) {
jpqlQuery += " where";
}

if (null != name) {
jpqlQuery += " name = :name";
}

if ((null != name) && (null != archiveType)) {
jpqlQuery += " and";
}

// adds WHERE condition
if ((null != code) || (null != name) || (null != archiveType)) {

if (null != archiveType) {
jpqlQuery += " archiveType = :archiveType";
jpqlQuery += " where";

if (null != code) {
jpqlQuery += " code = :code and";
}

if (null != name) {
jpqlQuery += " name = :name and";
}

if (null != archiveType) {
jpqlQuery += " archiveType = :archiveType and";
}

jpqlQuery = jpqlQuery.substring(0, jpqlQuery.length() - 4); // removes last " and"
}

jpqlQuery += " ORDER BY w.id";

Query query = em.createQuery(jpqlQuery);

if (null != code) {
query.setParameter("code", code);
}

if (null != name) {
query.setParameter("name", name);
}

try {
if (null != archiveType) {
query.setParameter("archiveType", ArchiveType.valueOf(archiveType));
}
} catch (Exception e) {
throw new NoResultException(logger.log(ProductArchiveMgrMessage.ARCHIVE_NOT_FOUND, name, archiveType));
}

query.setFirstResult(recordFrom);
query.setMaxResults(recordTo - recordFrom);

Expand All @@ -311,17 +324,16 @@ public List<RestProductArchive> getArchives(String name, String archiveType, Int

return result;
}



/**
* Count the product archives matching the specified name and archive type
*
* @param name the product archive name
* @param archiveType the product archive type
* @param name the product archive name
* @param archiveType the product archive type
* @return the number of product archives found as string
*/
public String countArchives(String name, String archiveType) {

if (logger.isTraceEnabled())
logger.trace(">>> countArchives({}, {})", name, archiveType);

Expand All @@ -330,37 +342,40 @@ public String countArchives(String name, String archiveType) {
Root<ProductArchive> rootArchive = query.from(ProductArchive.class);

List<Predicate> predicates = new ArrayList<>();

if (name != null)
predicates.add(cb.equal(rootArchive.get("name"), name));

try {
if (archiveType != null)
predicates.add(cb.equal(rootArchive.get("archiveType"), ArchiveType.valueOf(archiveType)));
} catch (Exception e) {
logger.trace("... wrong archive type: " + archiveType);
return "0";
}
catch (Exception e) {
logger.trace("... wrong archive type: " + archiveType);
return "0";
}


query.select(cb.count(rootArchive)).where(predicates.toArray(new Predicate[predicates.size()]));
Long result = em.createQuery(query).getSingleResult();
Long result = em.createQuery(query).getSingleResult();
logger.trace("... product archive count: " + result);

return result.toString();
}


/**
* Update the product archive with the given ID with the attribute values of the given Json object. Unchanged values must be
* provided, too, or they will be changed to null.
* Update the product archive with the given ID with the attribute values of the
* given Json object. Unchanged values must be provided, too, or they will be
* changed to null.
*
* @param id the ID of the product archive to update
* @param restArchive a Json object containing the modified (and unmodified) attributes
* @return a Json object corresponding to the product archive after modification (with ID and version for all contained objects)
* @throws EntityNotFoundException if no product with the given ID exists
* @param restArchive a Json object containing the modified (and unmodified)
* attributes
* @return a Json object corresponding to the product archive after modification
* (with ID and version for all contained objects)
* @throws EntityNotFoundException if no product with the given ID
* exists
* @throws IllegalArgumentException if any of the input data was invalid
* @throws ConcurrentModificationException if the facility has been modified since retrieval by the client
* @throws ConcurrentModificationException if the facility has been modified
* since retrieval by the client
*/
public RestProductArchive modifyArchive(Long id, RestProductArchive restArchive) {

Expand All @@ -382,7 +397,8 @@ public RestProductArchive modifyArchive(Long id, RestProductArchive restArchive)
ProductArchive modelArchive = RepositoryService.getProductArchiveRepository().findById(id).get();
modelArchive = new ProductArchiveModelMapper(modelArchive).get();

ProductArchive changedArchive = new ProductArchiveRestMapper(restArchive, securityService.getMission()).toModel();
ProductArchive changedArchive = new ProductArchiveRestMapper(restArchive, securityService.getMission())
.toModel();

boolean archiveChanged = isArchiveChanged(modelArchive, changedArchive);

Expand All @@ -408,9 +424,12 @@ public RestProductArchive modifyArchive(Long id, RestProductArchive restArchive)
* Delete a product archive by ID
*
* @param id the ID of the product archive to delete
* @throws EntityNotFoundException if the product archive to delete does not exist in the database
* @throws IllegalArgumentException if the product archive to delete still has stored products
* @throws RuntimeException if the deletion was not performed as expected
* @throws EntityNotFoundException if the product archive to delete does not
* exist in the database
* @throws IllegalArgumentException if the product archive to delete still has
* stored products
* @throws RuntimeException if the deletion was not performed as
* expected
*/
public void deleteArchiveById(Long id) throws EntityNotFoundException, IllegalArgumentException, RuntimeException {

Expand Down

0 comments on commit 46e315a

Please sign in to comment.