Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • simonrose/naming-backend
  • anderslindh1/naming-backend
  • andersharrisson/naming-backend
3 results
Show changes
Showing
with 2127 additions and 1152 deletions
/*
* Copyright (C) 2021 European Spallation Source ERIC.
* Copyright (C) 2024 European Spallation Source ERIC.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
......@@ -26,13 +26,18 @@ import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.openepics.names.repository.model.DeviceType;
import org.openepics.names.repository.model.NameStructure;
import org.openepics.names.repository.model.Persistable;
import org.openepics.names.repository.model.Structure;
import org.openepics.names.rest.beans.FieldStructure;
import org.openepics.names.rest.beans.Status;
import org.openepics.names.util.RepositoryUtil;
import org.springframework.stereotype.Repository;
/**
......@@ -43,42 +48,28 @@ import org.springframework.stereotype.Repository;
@Repository
public class DeviceTypeRepository {
private static final String PERCENT = "%";
@PersistenceContext
private EntityManager em;
/**
* Count device types.
*
* @param statuses statuses
* @param deleted deleted
* @param queryFields query fields
* @param queryValues query values
* @return count of device types
*/
public Long countDeviceTypes(
Status[] statuses, Boolean deleted, FieldStructure[] queryFields, String[] queryValues) {
return countDeviceTypes(statuses, deleted, queryFields, queryValues, Boolean.FALSE);
}
/**
* Count device types.
*
* @param statuses statuses
* @param deleted deleted
* @param queryFields query fields
* @param queryValues query values
* @param includeHistory include history
* @param uuid uuid
* @param parentUuid parent uuid
* @param mnemonic mnemonic
* @param mnemonicEquivalence mnemonic equivalence
* @param mnemonicPath mnemonic path
* @param description description
* @param who who
* @return count of device types
*/
public Long countDeviceTypes(
Status[] statuses, Boolean deleted, FieldStructure[] queryFields, String[] queryValues, Boolean includeHistory) {
public Long countDeviceTypes(Boolean deleted,
String uuid, String parentUuid, String mnemonic, String mnemonicEquivalence, String mnemonicPath, String description, String who) {
// note
// use of function for mnemonic path
// where
// statuses
// deleted
// queryFields, queryValues
......@@ -86,7 +77,8 @@ public class DeviceTypeRepository {
CriteriaQuery<Long> cq = cb.createQuery(Long.class);
Root<DeviceType> from = cq.from(DeviceType.class);
cq.where(cb.and(preparePredicatesDeviceTypes(cb, from, statuses, deleted, queryFields, queryValues, includeHistory).toArray(new Predicate[0])));
cq.where(cb.and(preparePredicatesDeviceTypes(cb, cq, from, deleted,
uuid, parentUuid, mnemonic, mnemonicEquivalence, mnemonicPath, description, who).toArray(new Predicate[0])));
cq.select(cb.count(from));
return em.createQuery(cq).getSingleResult();
......@@ -95,67 +87,48 @@ public class DeviceTypeRepository {
/**
* Find device types.
*
* @param status status
* @param deleted deleted
* @param queryField query field
* @param queryValue query value
* @param uuid uuid
* @param parentUuid parent uuid
* @param mnemonic mnemonic
* @param mnemonicEquivalence mnemonic equivalence
* @param mnemonicPath mnemonic path
* @param description description
* @param who who
* @return list of device types
*/
public List<DeviceType> readDeviceTypes(
Status status, Boolean deleted, FieldStructure queryField, String queryValue) {
public List<DeviceType> readDeviceTypes(Boolean deleted,
String uuid, String parentUuid, String mnemonic, String mnemonicEquivalence, String mnemonicPath, String description, String who) {
return readDeviceTypes(
status != null ? new Status[] {status} : null,
deleted,
queryField != null ? new FieldStructure[] {queryField} : null,
queryValue != null ? new String[] {queryValue} : null,
Boolean.FALSE,
return readDeviceTypes(deleted,
uuid, parentUuid, mnemonic, mnemonicEquivalence, mnemonicPath, description, who,
null, null, null, null);
}
/**
* Find device types.
*
* @param statuses statuses
* @param deleted deleted
* @param queryFields query fields
* @param queryValues query values
* @param uuid uuid
* @param parentUuid parent uuid
* @param mnemonic mnemonic
* @param mnemonicEquivalence mnemonic equivalence
* @param mnemonicPath mnemonic path
* @param description description
* @param who who
* @param orderBy order by
* @param isAsc is ascending
* @param offset offset
* @param limit limit
* @return list of device types
*/
public List<DeviceType> readDeviceTypes(
Status[] statuses, Boolean deleted, FieldStructure[] queryFields, String[] queryValues,
FieldStructure orderBy, Boolean isAsc, Integer offset, Integer limit) {
return readDeviceTypes(
statuses, deleted, queryFields, queryValues, Boolean.FALSE,
orderBy, isAsc, offset, limit);
}
/**
* Find device types.
*
* @param statuses statuses
* @param deleted deleted
* @param queryFields query fields
* @param queryValues query values
* @param includeHistory include history
* @param orderBy order by
* @param isAsc is ascending
* @param offset offset
* @param limit limit
* @return list of device types
*/
public List<DeviceType> readDeviceTypes(
Status[] statuses, Boolean deleted, FieldStructure[] queryFields, String[] queryValues, Boolean includeHistory,
public List<DeviceType> readDeviceTypes(Boolean deleted,
String uuid, String parentUuid, String mnemonic, String mnemonicEquivalence, String mnemonicPath, String description, String who,
FieldStructure orderBy, Boolean isAsc, Integer offset, Integer limit) {
// note
// use of function for mnemonic path
// where
// statuses
// deleted
// queryFields, queryValues
// order
......@@ -167,38 +140,16 @@ public class DeviceTypeRepository {
CriteriaQuery<DeviceType> cq = cb.createQuery(DeviceType.class);
Root<DeviceType> from = cq.from(DeviceType.class);
cq.where(cb.and(preparePredicatesDeviceTypes(cb, from, statuses, deleted, queryFields, queryValues, includeHistory).toArray(new Predicate[0])));
cq.where(cb.and(preparePredicatesDeviceTypes(cb, cq, from, deleted,
uuid, parentUuid, mnemonic, mnemonicEquivalence, mnemonicPath, description, who).toArray(new Predicate[0])));
cq.select(from);
if (orderBy != null) {
Expression<String> exp = orderBy(from, orderBy);
if (BooleanUtils.toBoolean(isAsc)) {
if (FieldStructure.NAME.equals(orderBy)) {
cq.orderBy(cb.asc(from.get("name")));
} else if (FieldStructure.MNEMONIC.equals(orderBy)) {
cq.orderBy(cb.asc(from.get("mnemonic")));
} else if (FieldStructure.MNEMONICEQUIVALENCE.equals(orderBy)) {
cq.orderBy(cb.asc(from.get("mnemonic_equivalence")));
} else if (FieldStructure.MNEMONICPATH.equals(orderBy)) {
cq.orderBy(cb.asc(cb.function("get_mnemonic_path_devicetype", String.class, from.get("uuid"))));
} else if (FieldStructure.DESCRIPTION.equals(orderBy)) {
cq.orderBy(cb.asc(from.get("description")));
} else {
cq.orderBy(cb.asc(from.get("convention_name")));
}
cq.orderBy(cb.asc(exp));
} else {
if (FieldStructure.NAME.equals(orderBy)) {
cq.orderBy(cb.desc(from.get("name")));
} else if (FieldStructure.MNEMONIC.equals(orderBy)) {
cq.orderBy(cb.desc(from.get("mnemonic")));
} else if (FieldStructure.MNEMONICEQUIVALENCE.equals(orderBy)) {
cq.orderBy(cb.desc(from.get("mnemonic_equivalence")));
} else if (FieldStructure.MNEMONICPATH.equals(orderBy)) {
cq.orderBy(cb.desc(cb.function("get_mnemonic_path_devicetype", String.class, from.get("uuid"))));
} else if (FieldStructure.DESCRIPTION.equals(orderBy)) {
cq.orderBy(cb.desc(from.get("description")));
} else {
cq.orderBy(cb.desc(from.get("name")));
}
cq.orderBy(cb.desc(exp));
}
}
......@@ -215,88 +166,83 @@ public class DeviceTypeRepository {
* Prepare predicates for device types.
*
* @param cb criteria builder
* @param cq criteria query
* @param from criteria query root
* @param statuses statuses
* @param deleted deleted
* @param queryFields query fields
* @param queryValues query values
* @param includeHistory include history
* @param uuid uuid
* @param parentUuid parent uuid
* @param mnemonic mnemonic
* @param mnemonicEquivalence mnemonic equivalence
* @param mnemonicPath mnemonic path
* @param description description
* @param who who
* @return list of predicates
*/
private List<Predicate> preparePredicatesDeviceTypes(
CriteriaBuilder cb, Root<DeviceType> from,
Status[] statuses, Boolean deleted, FieldStructure[] queryFields, String[] queryValues, Boolean includeHistory) {
private List<Predicate> preparePredicatesDeviceTypes(CriteriaBuilder cb, CriteriaQuery cq, Root<DeviceType> from, Boolean deleted,
String uuid, String parentUuid, String mnemonic, String mnemonicEquivalence, String mnemonicPath, String description, String who) {
List<Predicate> predicates = new ArrayList<>();
if (!Boolean.TRUE.equals(includeHistory)) {
// exclude (approved and not latest)
Predicate predicateApproved = cb.equal(from.get("status"), Status.APPROVED);
Predicate predicateNotLatest = cb.equal(from.get("latest"), Boolean.FALSE);
Predicate predicateExclude = cb.not(cb.and(predicateApproved, predicateNotLatest));
predicates.add(predicateExclude);
}
if (statuses != null) {
List<Predicate> predicatesStatus = new ArrayList<>();
for (Status status : statuses) {
predicatesStatus.add(cb.equal(from.get("status"), status));
}
predicates.add(cb.or((Predicate[]) predicatesStatus.toArray(new Predicate[0])));
}
if (deleted != null) {
predicates.add(cb.equal(from.get("deleted"), deleted));
predicates.add(cb.equal(from.get(NameStructure.FIELD_DELETED), deleted));
}
if (queryFields != null) {
for (int i=0; i<queryFields.length; i++) {
String queryValue = queryValues[i];
// jpa query characters % and _
// remove excess % characters
if (queryValue.startsWith(PERCENT)) {
while (queryValue.startsWith(PERCENT)) {
queryValue = queryValue.substring(1);
}
queryValue = PERCENT + queryValue;
}
if (queryValue.endsWith(PERCENT)) {
while (queryValue.endsWith(PERCENT)) {
queryValue = queryValue.substring(0, queryValue.length()-1);
}
queryValue = queryValue + PERCENT;
}
// prepare pattern
// jpa query characters % and _
// remove excess % characters
switch (queryFields[i]) {
case UUID:
predicates.add(cb.and(cb.equal(from.get("uuid"), queryValue)));
break;
case PARENT:
predicates.add(cb.and(cb.equal(from.get("parent_uuid"), queryValue)));
break;
case NAME:
predicates.add(cb.and(cb.like(from.get("name"), queryValue)));
break;
case MNEMONIC:
predicates.add(cb.and(cb.like(from.get("mnemonic"), queryValue)));
break;
case MNEMONICEQUIVALENCE:
predicates.add(cb.and(cb.like(from.get("mnemonic_equivalence"), queryValue)));
break;
case MNEMONICPATH:
predicates.add(cb.and(cb.like(cb.function("get_mnemonic_path_devicetype", String.class, from.get("uuid")), queryValue)));
break;
case DESCRIPTION:
predicates.add(cb.and(cb.like(from.get("description"), queryValue)));
break;
default:
continue;
}
}
if (!StringUtils.isEmpty(uuid)) {
predicates.add(cb.and(cb.equal(from.get(NameStructure.FIELD_UUID), RepositoryUtil.preparePattern(uuid))));
}
if (!StringUtils.isEmpty(parentUuid)) {
predicates.add(cb.and(cb.equal(cb.function(DeviceType.FUNCTION_GET_PARENT_UUID_DEVICETYPE, String.class, from.get(Persistable.FIELD_ID)), RepositoryUtil.preparePattern(parentUuid))));
}
if (!StringUtils.isEmpty(mnemonic)) {
predicates.add(cb.and(cb.like(from.get(Structure.FIELD_MNEMONIC), RepositoryUtil.preparePattern(mnemonic))));
}
if (!StringUtils.isEmpty(mnemonicEquivalence)) {
predicates.add(cb.and(cb.like(from.get(Structure.FIELD_MNEMONIC_EQUIVALENCE), RepositoryUtil.preparePattern(mnemonicEquivalence))));
}
if (!StringUtils.isEmpty(mnemonicPath)) {
predicates.add(cb.and(cb.like(cb.function(DeviceType.FUNCTION_GET_MNEMONIC_PATH_DEVICETYPE, String.class, from.get(Persistable.FIELD_ID)), RepositoryUtil.preparePattern(mnemonicPath))));
}
if (!StringUtils.isEmpty(description)) {
predicates.add(cb.and(cb.like(from.get(NameStructure.FIELD_DESCRIPTION), RepositoryUtil.preparePattern(description))));
}
if (!StringUtils.isEmpty(who)) {
predicates.add(cb.and(cb.like(from.get(NameStructure.FIELD_PROCESSED_BY), RepositoryUtil.preparePattern(who))));
}
return predicates;
}
/**
* Prepare order by query expression.
*
* @param root root type in the from clause
* @param orderBy field on which to order by
* @return order by query expression
*/
private Expression<String> orderBy(Root<DeviceType> root, FieldStructure orderBy) {
if (FieldStructure.UUID.equals(orderBy)) {
return root.get(NameStructure.FIELD_UUID);
} else if (FieldStructure.PARENT.equals(orderBy)) {
return root.get(DeviceType.FIELD_PARENT_ID);
} else if (FieldStructure.MNEMONIC.equals(orderBy)) {
return root.get(Structure.FIELD_MNEMONIC);
} else if (FieldStructure.MNEMONICPATH.equals(orderBy)) {
return root.get(Structure.FIELD_MNEMONIC);
} else if (FieldStructure.ORDERING.equals(orderBy)) {
return root.get(Structure.FIELD_ORDERING);
} else if (FieldStructure.DESCRIPTION.equals(orderBy)) {
return root.get(NameStructure.FIELD_DESCRIPTION);
} else if (FieldStructure.WHEN.equals(orderBy)) {
return root.get(NameStructure.FIELD_PROCESSED);
} else {
return root.get(Structure.FIELD_MNEMONIC);
}
}
/**
* Persist device type into persistence context.
*
......
/*
* Copyright (C) 2021 European Spallation Source ERIC.
* Copyright (C) 2024 European Spallation Source ERIC.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
......@@ -26,13 +26,17 @@ import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.openepics.names.repository.model.Discipline;
import org.openepics.names.repository.model.NameStructure;
import org.openepics.names.repository.model.Structure;
import org.openepics.names.rest.beans.FieldStructure;
import org.openepics.names.rest.beans.Status;
import org.openepics.names.util.RepositoryUtil;
import org.springframework.stereotype.Repository;
/**
......@@ -43,42 +47,27 @@ import org.springframework.stereotype.Repository;
@Repository
public class DisciplineRepository {
private static final String PERCENT = "%";
@PersistenceContext
private EntityManager em;
/**
* Count disciplines.
*
* @param statuses statuses
* @param deleted deleted
* @param queryFields query fields
* @param queryValues query values
* @return count of disciplines
*/
public Long countDisciplines(
Status[] statuses, Boolean deleted, FieldStructure[] queryFields, String[] queryValues) {
return countDisciplines(statuses, deleted, queryFields, queryValues, Boolean.FALSE);
}
/**
* Count disciplines.
*
* @param statuses statuses
* @param deleted deleted
* @param queryFields query fields
* @param queryValues query values
* @param includeHistory include history
* @param uuid uuid
* @param mnemonic mnemonic
* @param mnemonicEquivalence mnemonic equivalence
* @param mnemonicPath mnemonic path
* @param description description
* @param who who
* @return count of disciplines
*/
public Long countDisciplines(
Status[] statuses, Boolean deleted, FieldStructure[] queryFields, String[] queryValues, Boolean includeHistory) {
public Long countDisciplines(Boolean deleted,
String uuid, String mnemonic, String mnemonicEquivalence, String mnemonicPath, String description, String who) {
// note
// use of function for mnemonic path
// where
// statuses
// deleted
// queryFields, queryValues
......@@ -86,77 +75,57 @@ public class DisciplineRepository {
CriteriaQuery<Long> cq = cb.createQuery(Long.class);
Root<Discipline> from = cq.from(Discipline.class);
cq.where(cb.and(preparePredicatesDisciplines(cb, from, statuses, deleted, queryFields, queryValues, includeHistory).toArray(new Predicate[0])));
cq.where(cb.and(preparePredicatesDisciplines(cb, cq, from, deleted,
uuid, mnemonic, mnemonicEquivalence, mnemonicPath, description, who).toArray(new Predicate[0])));
cq.select(cb.count(from));
return em.createQuery(cq).getSingleResult();
}
/**
*Find disciplines.
* Find disciplines.
*
* @param status status
* @param deleted deleted
* @param queryField query field
* @param queryValue query value
* @param uuid uuid
* @param mnemonic mnemonic
* @param mnemonicEquivalence mnemonic equivalence
* @param mnemonicPath mnemonic path
* @param description description
* @param who who
* @return list of disciplines
*/
public List<Discipline> readDisciplines(
Status status, Boolean deleted, FieldStructure queryField, String queryValue) {
public List<Discipline> readDisciplines(Boolean deleted,
String uuid, String mnemonic, String mnemonicEquivalence, String mnemonicPath, String description, String who) {
return readDisciplines(
status != null ? new Status[] {status} : null,
deleted,
queryField != null ? new FieldStructure[] {queryField} : null,
queryValue != null ? new String[] {queryValue} : null,
Boolean.FALSE,
return readDisciplines(deleted,
uuid, mnemonic, mnemonicEquivalence, mnemonicPath, description, who,
null, null, null, null);
}
/**
* Find disciplines.
*
* @param statuses statuses
* @param deleted deleted
* @param queryFields query fields
* @param queryValues query values
* @param orderBy order by
* @param isAsc is ascending
* @param offset offset
* @param limit limit
* @return list of disciplines
*/
public List<Discipline> readDisciplines(
Status[] statuses, Boolean deleted, FieldStructure[] queryFields, String[] queryValues,
FieldStructure orderBy, Boolean isAsc, Integer offset, Integer limit) {
return readDisciplines(
statuses, deleted, queryFields, queryValues, Boolean.FALSE,
orderBy, isAsc, offset, limit);
}
/**
* Find disciplines.
*
* @param statuses statuses
* @param deleted deleted
* @param queryFields query fields
* @param queryValues query values
* @param includeHistory include history
* @param uuid uuid
* @param mnemonic mnemonic
* @param mnemonicEquivalence mnemonic equivalence
* @param mnemonicPath mnemonic path
* @param description description
* @param who who
* @param orderBy order by
* @param isAsc is ascending
* @param offset offset
* @param limit limit
* @return list of disciplines
*/
public List<Discipline> readDisciplines(
Status[] statuses, Boolean deleted, FieldStructure[] queryFields, String[] queryValues, Boolean includeHistory,
public List<Discipline> readDisciplines(Boolean deleted,
String uuid, String mnemonic, String mnemonicEquivalence, String mnemonicPath, String description, String who,
FieldStructure orderBy, Boolean isAsc, Integer offset, Integer limit) {
// note
// use of function for mnemonic path
// where
// statuses
// latest, deleted
// deleted
// queryFields, queryValues
// order
// orderBy, isAsc
......@@ -167,38 +136,16 @@ public class DisciplineRepository {
CriteriaQuery<Discipline> cq = cb.createQuery(Discipline.class);
Root<Discipline> from = cq.from(Discipline.class);
cq.where(cb.and(preparePredicatesDisciplines(cb, from, statuses, deleted, queryFields, queryValues, includeHistory).toArray(new Predicate[0])));
cq.where(cb.and(preparePredicatesDisciplines(cb, cq, from, deleted,
uuid, mnemonic, mnemonicEquivalence, mnemonicPath, description, who).toArray(new Predicate[0])));
cq.select(from);
if (orderBy != null) {
Expression<String> exp = orderBy(from, orderBy);
if (BooleanUtils.toBoolean(isAsc)) {
if (FieldStructure.NAME.equals(orderBy)) {
cq.orderBy(cb.asc(from.get("name")));
} else if (FieldStructure.MNEMONIC.equals(orderBy)) {
cq.orderBy(cb.asc(from.get("mnemonic")));
} else if (FieldStructure.MNEMONICEQUIVALENCE.equals(orderBy)) {
cq.orderBy(cb.asc(from.get("mnemonic_equivalence")));
} else if (FieldStructure.MNEMONICPATH.equals(orderBy)) {
cq.orderBy(cb.asc(from.get("mnemonic")));
} else if (FieldStructure.DESCRIPTION.equals(orderBy)) {
cq.orderBy(cb.asc(from.get("description")));
} else {
cq.orderBy(cb.asc(from.get("convention_name")));
}
cq.orderBy(cb.asc(exp));
} else {
if (FieldStructure.NAME.equals(orderBy)) {
cq.orderBy(cb.desc(from.get("name")));
} else if (FieldStructure.MNEMONIC.equals(orderBy)) {
cq.orderBy(cb.desc(from.get("mnemonic")));
} else if (FieldStructure.MNEMONICEQUIVALENCE.equals(orderBy)) {
cq.orderBy(cb.desc(from.get("mnemonic_equivalence")));
} else if (FieldStructure.MNEMONICPATH.equals(orderBy)) {
cq.orderBy(cb.asc(from.get("mnemonic")));
} else if (FieldStructure.DESCRIPTION.equals(orderBy)) {
cq.orderBy(cb.desc(from.get("description")));
} else {
cq.orderBy(cb.desc(from.get("name")));
}
cq.orderBy(cb.desc(exp));
}
}
......@@ -215,85 +162,79 @@ public class DisciplineRepository {
* Prepare predicates for disciplines.
*
* @param cb criteria builder
* @param cq criteria query
* @param from criteria query root
* @param statuses statuses
* @param deleted deleted
* @param queryFields query fields
* @param queryValues query values
* @param includeHistory include history
* @param uuid uuid
* @param mnemonic mnemonic
* @param mnemonicEquivalence mnemonic equivalence
* @param mnemonicPath mnemonic path
* @param description description
* @param who who
* @return list of predicates
*/
private List<Predicate> preparePredicatesDisciplines(
CriteriaBuilder cb, Root<Discipline> from,
Status[] statuses, Boolean deleted, FieldStructure[] queryFields, String[] queryValues, Boolean includeHistory) {
private List<Predicate> preparePredicatesDisciplines(CriteriaBuilder cb, CriteriaQuery cq, Root<Discipline> from, Boolean deleted,
String uuid, String mnemonic, String mnemonicEquivalence, String mnemonicPath, String description, String who) {
List<Predicate> predicates = new ArrayList<>();
if (!Boolean.TRUE.equals(includeHistory)) {
// exclude (approved and not latest)
Predicate predicateApproved = cb.equal(from.get("status"), Status.APPROVED);
Predicate predicateNotLatest = cb.equal(from.get("latest"), Boolean.FALSE);
Predicate predicateExclude = cb.not(cb.and(predicateApproved, predicateNotLatest));
predicates.add(predicateExclude);
}
if (statuses != null) {
List<Predicate> predicatesStatus = new ArrayList<>();
for (Status status : statuses) {
predicatesStatus.add(cb.equal(from.get("status"), status));
}
predicates.add(cb.or((Predicate[]) predicatesStatus.toArray(new Predicate[0])));
}
if (deleted != null) {
predicates.add(cb.equal(from.get("deleted"), deleted));
predicates.add(cb.equal(from.get(NameStructure.FIELD_DELETED), deleted));
}
if (queryFields != null) {
for (int i=0; i<queryFields.length; i++) {
String queryValue = queryValues[i];
// jpa query characters % and _
// remove excess % characters
if (queryValue.startsWith(PERCENT)) {
while (queryValue.startsWith(PERCENT)) {
queryValue = queryValue.substring(1);
}
queryValue = PERCENT + queryValue;
}
if (queryValue.endsWith(PERCENT)) {
while (queryValue.endsWith(PERCENT)) {
queryValue = queryValue.substring(0, queryValue.length()-1);
}
queryValue = queryValue + PERCENT;
}
// prepare pattern
// jpa query characters % and _
// remove excess % characters
switch (queryFields[i]) {
case UUID:
predicates.add(cb.and(cb.equal(from.get("uuid"), queryValue)));
break;
case NAME:
predicates.add(cb.and(cb.like(from.get("name"), queryValue)));
break;
case MNEMONIC:
predicates.add(cb.and(cb.like(from.get("mnemonic"), queryValue)));
break;
case MNEMONICEQUIVALENCE:
predicates.add(cb.and(cb.like(from.get("mnemonic_equivalence"), queryValue)));
break;
case MNEMONICPATH:
predicates.add(cb.and(cb.like(from.get("mnemonic"), queryValue)));
break;
case DESCRIPTION:
predicates.add(cb.and(cb.like(from.get("description"), queryValue)));
break;
default:
continue;
}
}
if (!StringUtils.isEmpty(uuid)) {
predicates.add(cb.and(cb.equal(from.get(NameStructure.FIELD_UUID), RepositoryUtil.preparePattern(uuid))));
}
if (!StringUtils.isEmpty(mnemonic)) {
predicates.add(cb.and(cb.like(from.get(Structure.FIELD_MNEMONIC), RepositoryUtil.preparePattern(mnemonic))));
}
if (!StringUtils.isEmpty(mnemonicEquivalence)) {
predicates.add(cb.and(cb.like(from.get(Structure.FIELD_MNEMONIC_EQUIVALENCE), RepositoryUtil.preparePattern(mnemonicEquivalence))));
}
if (!StringUtils.isEmpty(mnemonicPath)) {
predicates.add(cb.and(cb.like(from.get(Structure.FIELD_MNEMONIC), RepositoryUtil.preparePattern(mnemonicPath))));
}
if (!StringUtils.isEmpty(description)) {
predicates.add(cb.and(cb.like(from.get(NameStructure.FIELD_DESCRIPTION), RepositoryUtil.preparePattern(description))));
}
if (!StringUtils.isEmpty(who)) {
predicates.add(cb.and(cb.like(from.get(NameStructure.FIELD_PROCESSED_BY), RepositoryUtil.preparePattern(who))));
}
return predicates;
}
/**
* Prepare order by query expression.
*
* @param root root type in the from clause
* @param orderBy field on which to order by
* @return order by query expression
*/
private Expression<String> orderBy(Root<Discipline> root, FieldStructure orderBy) {
// no parent for discipline
if (FieldStructure.UUID.equals(orderBy)) {
return root.get(NameStructure.FIELD_UUID);
} else if (FieldStructure.MNEMONIC.equals(orderBy)) {
return root.get(Structure.FIELD_MNEMONIC);
} else if (FieldStructure.MNEMONICPATH.equals(orderBy)) {
return root.get(Structure.FIELD_MNEMONIC);
} else if (FieldStructure.ORDERING.equals(orderBy)) {
return root.get(Structure.FIELD_ORDERING);
} else if (FieldStructure.DESCRIPTION.equals(orderBy)) {
return root.get(NameStructure.FIELD_DESCRIPTION);
} else if (FieldStructure.WHEN.equals(orderBy)) {
return root.get(NameStructure.FIELD_PROCESSED);
} else {
return root.get(Structure.FIELD_MNEMONIC);
}
}
/**
* Persist discipline into persistence context.
*
......
/*
* Copyright (C) 2024 European Spallation Source ERIC.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.openepics.names.repository;
import java.util.Date;
import java.util.List;
import org.openepics.names.repository.model.AuditName;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
/**
* Find audit name (device) information in JPA.
*
* @author Lars Johansson
*/
@Repository
public interface IAuditNameRepository extends JpaRepository<AuditName, Long> {
@Query("SELECT aun FROM AuditName aun "
+ "WHERE aun.uuid = ?1 "
+ "AND aun.status = 'APPROVED' "
+ "AND aun.auditId = (select max(aun2.auditId) from AuditName aun2 where aun2.uuid = aun.uuid and aun2.auditId < ?2)")
AuditName findPreviousByUuidAndAuditId(String uuid, Long id);
@Query("FROM AuditName aun WHERE aun.requested >= ?1 AND aun.requested <= ?2")
List<AuditName> findByRequestedBetween(Date from, Date to);
@Query("FROM AuditName aun WHERE aun.uuid = ?1")
List<AuditName> findByUuid(String uuid);
}
/*
* Copyright (C) 2024 European Spallation Source ERIC.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package org.openepics.names.repository;
import org.openepics.names.repository.model.AuditStructure;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
/**
* Find audit structure (name part) information in JPA.
*
* @author Lars Johansson
*/
@Repository
public interface IAuditStructureRepository extends JpaRepository<AuditStructure, Long> {
@Query("SELECT aus FROM AuditStructure aus "
+ "WHERE aus.auditTable = ?1 "
+ "AND aus.uuid = ?2 "
+ "AND aus.status = 'APPROVED' "
+ "AND aus.auditId = (select max(aus2.auditId) from AuditStructure aus2 where aus2.uuid = aus.uuid and aus2.auditId < ?3)")
AuditStructure findPreviousByAuditTableAndUuidAndAuditId(String auditTable, String uuid, Long id);
}