hawkBit repository uses Optional on single entity find/get requests (#435)

* Repo returns optionals.
* Improved exception handling for collection usage in repo queries.

Signed-off-by: kaizimmerm <kai.zimmermann@bosch-si.com>
This commit is contained in:
Kai Zimmermann
2017-02-16 10:09:14 +01:00
committed by GitHub
parent d21af83804
commit 804522f966
232 changed files with 2104 additions and 2377 deletions

View File

@@ -16,7 +16,6 @@ import org.eclipse.hawkbit.repository.jpa.model.JpaAction;
import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSet;
import org.eclipse.hawkbit.repository.jpa.model.JpaRollout;
import org.eclipse.hawkbit.repository.jpa.model.JpaRolloutGroup;
import org.eclipse.hawkbit.repository.jpa.model.JpaSoftwareModule;
import org.eclipse.hawkbit.repository.jpa.model.JpaTarget;
import org.eclipse.hawkbit.repository.model.Action;
import org.eclipse.hawkbit.repository.model.Action.Status;
@@ -53,7 +52,7 @@ public interface ActionRepository extends BaseEntityRepository<JpaAction, Long>,
* @return the found {@link Action}
*/
@EntityGraph(value = "Action.all", type = EntityGraphType.LOAD)
JpaAction findById(Long actionId);
Optional<Action> getById(Long actionId);
/**
* Retrieves all {@link Action}s which are referring the given
@@ -113,14 +112,14 @@ public interface ActionRepository extends BaseEntityRepository<JpaAction, Long>,
*
* @param targetId
* to search for
* @param module
* @param moduleId
* to search for
* @return action if there is one with assigned target and module is part of
* assigned {@link DistributionSet}.
*/
@Query("Select a from JpaAction a join a.distributionSet ds join ds.modules modul where a.target.controllerId = :target and modul = :module order by a.id desc")
@Query("Select a from JpaAction a join a.distributionSet ds join ds.modules modul where a.target.controllerId = :target and modul.id = :module order by a.id desc")
List<Action> findActionByTargetAndSoftwareModule(@Param("target") final String targetId,
@Param("module") JpaSoftwareModule module);
@Param("module") Long moduleId);
/**
* Retrieves all {@link Action}s which are referring the given

View File

@@ -9,8 +9,10 @@
package org.eclipse.hawkbit.repository.jpa;
import java.io.Serializable;
import java.util.Optional;
import org.eclipse.hawkbit.repository.jpa.model.AbstractJpaTenantAwareBaseEntity;
import org.eclipse.hawkbit.repository.model.BaseEntity;
import org.eclipse.hawkbit.repository.model.TenantAwareBaseEntity;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.repository.NoRepositoryBean;
@@ -41,4 +43,13 @@ public interface BaseEntityRepository<T extends AbstractJpaTenantAwareBaseEntity
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
void deleteByTenantIgnoreCase(String tenant);
/**
* Retrieves an {@link BaseEntity} by its id.
*
* @param id
* to search for
* @return {@link BaseEntity}
*/
Optional<T> findById(I id);
}

View File

@@ -12,10 +12,10 @@ import java.util.Collection;
import java.util.List;
import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSet;
import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSetTag;
import org.eclipse.hawkbit.repository.jpa.model.JpaSoftwareModule;
import org.eclipse.hawkbit.repository.model.Action;
import org.eclipse.hawkbit.repository.model.DistributionSet;
import org.eclipse.hawkbit.repository.model.DistributionSetTag;
import org.eclipse.hawkbit.repository.model.Rollout;
import org.eclipse.hawkbit.repository.model.SoftwareModule;
import org.eclipse.hawkbit.repository.model.Tag;
@@ -42,7 +42,7 @@ public interface DistributionSetRepository
* @return list of found {@link DistributionSet}s
*/
@Query(value = "Select Distinct ds from JpaDistributionSet ds join ds.tags dst where dst = :tag")
List<JpaDistributionSet> findByTag(@Param("tag") final JpaDistributionSetTag tag);
List<JpaDistributionSet> findByTag(@Param("tag") final DistributionSetTag tag);
/**
* deletes the {@link DistributionSet}s with the given IDs.

View File

@@ -9,6 +9,7 @@
package org.eclipse.hawkbit.repository.jpa;
import java.util.List;
import java.util.Optional;
import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSetTag;
import org.eclipse.hawkbit.repository.model.DistributionSet;
@@ -44,7 +45,7 @@ public interface DistributionSetTagRepository
* to filter on
* @return the {@link DistributionSetTag} if found, otherwise null
*/
JpaDistributionSetTag findByNameEquals(final String tagName);
Optional<DistributionSetTag> findByNameEquals(final String tagName);
/**
* Returns all instances of the type.

View File

@@ -9,7 +9,6 @@
package org.eclipse.hawkbit.repository.jpa;
import java.io.InputStream;
import java.util.List;
import java.util.Optional;
import org.eclipse.hawkbit.artifact.repository.ArtifactRepository;
@@ -105,9 +104,9 @@ public class JpaArtifactManagement implements ArtifactManagement {
@Override
@Modifying
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public boolean clearArtifactBinary(final Long existing) {
return clearArtifactBinary(Optional.ofNullable(localArtifactRepository.findOne(existing))
.orElseThrow(() -> new EntityNotFoundException("Artifact with given ID" + existing + " not found.")));
public boolean clearArtifactBinary(final Long artifactId) {
return clearArtifactBinary(localArtifactRepository.findById(artifactId)
.orElseThrow(() -> new EntityNotFoundException(Artifact.class, artifactId)));
}
private boolean clearArtifactBinary(final JpaArtifact existing) {
@@ -132,11 +131,8 @@ public class JpaArtifactManagement implements ArtifactManagement {
@Modifying
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public void deleteArtifact(final Long id) {
final JpaArtifact existing = localArtifactRepository.findOne(id);
if (null == existing) {
return;
}
final JpaArtifact existing = (JpaArtifact) findArtifact(id)
.orElseThrow(() -> new EntityNotFoundException(Artifact.class, id));
clearArtifactBinary(existing);
@@ -146,23 +142,23 @@ public class JpaArtifactManagement implements ArtifactManagement {
}
@Override
public Artifact findArtifact(final Long id) {
return localArtifactRepository.findOne(id);
public Optional<Artifact> findArtifact(final Long id) {
return Optional.ofNullable(localArtifactRepository.findOne(id));
}
@Override
public List<Artifact> findByFilenameAndSoftwareModule(final String filename, final Long softwareModuleId) {
return localArtifactRepository.findByFilenameAndSoftwareModuleId(filename, softwareModuleId);
public Optional<Artifact> findByFilenameAndSoftwareModule(final String filename, final Long softwareModuleId) {
return localArtifactRepository.findFirstByFilenameAndSoftwareModuleId(filename, softwareModuleId);
}
@Override
public Artifact findFirstArtifactBySHA1(final String sha1Hash) {
public Optional<Artifact> findFirstArtifactBySHA1(final String sha1Hash) {
return localArtifactRepository.findFirstBySha1Hash(sha1Hash);
}
@Override
public List<Artifact> findArtifactByFilename(final String filename) {
return localArtifactRepository.findByFilename(filename);
public Optional<Artifact> findArtifactByFilename(final String filename) {
return localArtifactRepository.findFirstByFilename(filename);
}
@Override
@@ -208,7 +204,7 @@ public class JpaArtifactManagement implements ArtifactManagement {
if (softwareModule == null) {
LOG.debug("no software module with ID {} exists", moduleId);
throw new EntityNotFoundException("Software Module: " + moduleId);
throw new EntityNotFoundException(SoftwareModule.class, moduleId);
}
return softwareModule;
}

View File

@@ -39,7 +39,6 @@ import org.eclipse.hawkbit.repository.jpa.model.JpaActionStatus;
import org.eclipse.hawkbit.repository.jpa.model.JpaActionStatus_;
import org.eclipse.hawkbit.repository.jpa.model.JpaAction_;
import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSet;
import org.eclipse.hawkbit.repository.jpa.model.JpaSoftwareModule;
import org.eclipse.hawkbit.repository.jpa.model.JpaTarget;
import org.eclipse.hawkbit.repository.jpa.model.JpaTargetInfo;
import org.eclipse.hawkbit.repository.jpa.model.JpaTarget_;
@@ -47,7 +46,6 @@ import org.eclipse.hawkbit.repository.jpa.specifications.ActionSpecifications;
import org.eclipse.hawkbit.repository.model.Action;
import org.eclipse.hawkbit.repository.model.Action.Status;
import org.eclipse.hawkbit.repository.model.ActionStatus;
import org.eclipse.hawkbit.repository.model.SoftwareModule;
import org.eclipse.hawkbit.repository.model.Target;
import org.eclipse.hawkbit.repository.model.TargetInfo;
import org.eclipse.hawkbit.repository.model.TargetUpdateStatus;
@@ -133,33 +131,31 @@ public class JpaControllerManagement implements ControllerManagement {
@Modifying
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public Target updateLastTargetQuery(final String controllerId, final URI address) {
final Target target = Optional.ofNullable(targetRepository.findByControllerId(controllerId))
.orElseThrow(() -> new EntityNotFoundException("Target with given ID " + controllerId + " not found"));
final Target target = targetRepository.findByControllerId(controllerId)
.orElseThrow(() -> new EntityNotFoundException(Target.class, controllerId));
return updateTargetStatus(target.getTargetInfo(), null, System.currentTimeMillis(), address).getTarget();
}
@Override
public Action getActionForDownloadByTargetAndSoftwareModule(final String controllerId,
final SoftwareModule module) {
final List<Action> action = actionRepository.findActionByTargetAndSoftwareModule(controllerId,
(JpaSoftwareModule) module);
public Optional<Action> getActionForDownloadByTargetAndSoftwareModule(final String controllerId,
final Long moduleId) {
final List<Action> action = actionRepository.findActionByTargetAndSoftwareModule(controllerId, moduleId);
if (action.isEmpty() || action.get(0).isCancelingOrCanceled()) {
throw new EntityNotFoundException(
"No assigment found for module " + module.getId() + " to target " + controllerId);
return Optional.empty();
}
return action.get(0);
return Optional.ofNullable(action.get(0));
}
@Override
public boolean hasTargetArtifactAssigned(final String controllerId, final String sha1Hash) {
final Target target = targetRepository.findByControllerId(controllerId);
if (target == null) {
final Optional<Target> target = targetRepository.findByControllerId(controllerId);
if (!target.isPresent()) {
return false;
}
return actionRepository.count(ActionSpecifications.hasTargetAssignedArtifact(target, sha1Hash)) > 0;
return actionRepository.count(ActionSpecifications.hasTargetAssignedArtifact(target.get(), sha1Hash)) > 0;
}
@Override
@@ -180,8 +176,8 @@ public class JpaControllerManagement implements ControllerManagement {
}
@Override
public Action findActionWithDetails(final Long actionId) {
return getActionAndThrowExceptionIfNotFound(actionId);
public Optional<Action> findActionWithDetails(final Long actionId) {
return actionRepository.getById(actionId);
}
@Override
@@ -388,11 +384,8 @@ public class JpaControllerManagement implements ControllerManagement {
@Modifying
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public Target updateControllerAttributes(final String controllerId, final Map<String, String> data) {
final JpaTarget target = targetRepository.findByControllerId(controllerId);
if (target == null) {
throw new EntityNotFoundException(controllerId);
}
final JpaTarget target = (JpaTarget) targetRepository.findByControllerId(controllerId)
.orElseThrow(() -> new EntityNotFoundException(Target.class, controllerId));
final JpaTargetInfo targetInfo = (JpaTargetInfo) target.getTargetInfo();
targetInfo.getControllerAttributes().putAll(data);
@@ -435,8 +428,7 @@ public class JpaControllerManagement implements ControllerManagement {
* {@link Status#RETRIEVED}
*/
private Action handleRegisterRetrieved(final Long actionId, final String message) {
final JpaAction action = Optional.ofNullable(actionRepository.findById(actionId)).orElseThrow(
() -> new EntityNotFoundException("Actionw ith given ID " + actionId + " doesn not exist."));
final JpaAction action = getActionAndThrowExceptionIfNotFound(actionId);
// do a manual query with CriteriaBuilder to avoid unnecessary field
// queries and an extra
// count query made by spring-data when using pageable requests, we
@@ -492,8 +484,8 @@ public class JpaControllerManagement implements ControllerManagement {
}
private JpaAction getActionAndThrowExceptionIfNotFound(final Long actionId) {
return Optional.ofNullable(actionRepository.findById(actionId))
.orElseThrow(() -> new EntityNotFoundException("Action with ID " + actionId + " not found!"));
return actionRepository.findById(actionId)
.orElseThrow(() -> new EntityNotFoundException(Action.class, actionId));
}
@Override
@@ -504,13 +496,13 @@ public class JpaControllerManagement implements ControllerManagement {
}
@Override
public Target findByControllerId(final String controllerId) {
public Optional<Target> findByControllerId(final String controllerId) {
return targetRepository.findByControllerId(controllerId);
}
@Override
public Target findByTargetId(final Long targetId) {
return targetRepository.findOne(targetId);
public Optional<Target> findByTargetId(final Long targetId) {
return Optional.ofNullable(targetRepository.findOne(targetId));
}
}

View File

@@ -174,8 +174,7 @@ public class JpaDeploymentManagement implements DeploymentManagement {
final Collection<TargetWithActionType> targets, final String actionMessage) {
final JpaDistributionSet set = distributoinSetRepository.findOne(dsID);
if (set == null) {
throw new EntityNotFoundException(
String.format("no %s with id %d found", DistributionSet.class.getSimpleName(), dsID));
throw new EntityNotFoundException(DistributionSet.class, dsID);
}
return assignDistributionSetToTargets(set, targets, null, null, actionMessage);
@@ -365,8 +364,8 @@ public class JpaDeploymentManagement implements DeploymentManagement {
public Action cancelAction(final Long actionId) {
LOG.debug("cancelAction({})", actionId);
final JpaAction action = Optional.ofNullable(actionRepository.findOne(actionId))
.orElseThrow(() -> new EntityNotFoundException("Action with given ID " + actionId + " not found"));
final JpaAction action = actionRepository.findById(actionId)
.orElseThrow(() -> new EntityNotFoundException(Action.class, actionId));
if (action.isCancelingOrCanceled()) {
throw new CancelActionNotAllowedException("Actions in canceling or canceled state cannot be canceled");
@@ -412,8 +411,8 @@ public class JpaDeploymentManagement implements DeploymentManagement {
@Modifying
@Transactional(isolation = Isolation.READ_COMMITTED)
public Action forceQuitAction(final Long actionId) {
final JpaAction action = Optional.ofNullable(actionRepository.findOne(actionId))
.orElseThrow(() -> new EntityNotFoundException("Action with given ID " + actionId + " not found"));
final JpaAction action = actionRepository.findById(actionId)
.orElseThrow(() -> new EntityNotFoundException(Action.class, actionId));
if (!action.isCancelingOrCanceled()) {
throw new ForceQuitActionNotAllowedException(
@@ -542,13 +541,13 @@ public class JpaDeploymentManagement implements DeploymentManagement {
}
@Override
public Action findAction(final Long actionId) {
return actionRepository.findOne(actionId);
public Optional<Action> findAction(final Long actionId) {
return Optional.ofNullable(actionRepository.findOne(actionId));
}
@Override
public Action findActionWithDetails(final Long actionId) {
return actionRepository.findById(actionId);
public Optional<Action> findActionWithDetails(final Long actionId) {
return actionRepository.getById(actionId);
}
@Override
@@ -622,8 +621,10 @@ public class JpaDeploymentManagement implements DeploymentManagement {
@Modifying
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public Action forceTargetAction(final Long actionId) {
final JpaAction action = actionRepository.findOne(actionId);
if (action != null && !action.isForced()) {
final JpaAction action = actionRepository.findById(actionId)
.orElseThrow(() -> new EntityNotFoundException(Action.class, actionId));
if (!action.isForced()) {
action.setActionType(ActionType.FORCED);
return actionRepository.save(action);
}
@@ -632,11 +633,19 @@ public class JpaDeploymentManagement implements DeploymentManagement {
@Override
public Page<ActionStatus> findActionStatusByAction(final Pageable pageReq, final Long actionId) {
if (!actionRepository.exists(actionId)) {
throw new EntityNotFoundException(Action.class, actionId);
}
return actionStatusRepository.findByActionId(pageReq, actionId);
}
@Override
public Page<ActionStatus> findActionStatusByActionWithMessages(final Pageable pageReq, final Long actionId) {
if (!actionRepository.exists(actionId)) {
throw new EntityNotFoundException(Action.class, actionId);
}
return actionStatusRepository.getByActionId(pageReq, actionId);
}

View File

@@ -48,6 +48,7 @@ import org.eclipse.hawkbit.repository.jpa.rsql.RSQLUtility;
import org.eclipse.hawkbit.repository.jpa.specifications.DistributionSetSpecification;
import org.eclipse.hawkbit.repository.jpa.specifications.DistributionSetTypeSpecification;
import org.eclipse.hawkbit.repository.jpa.specifications.SpecificationsBuilder;
import org.eclipse.hawkbit.repository.model.Action;
import org.eclipse.hawkbit.repository.model.DistributionSet;
import org.eclipse.hawkbit.repository.model.DistributionSetFilter;
import org.eclipse.hawkbit.repository.model.DistributionSetFilter.DistributionSetFilterBuilder;
@@ -56,6 +57,8 @@ import org.eclipse.hawkbit.repository.model.DistributionSetTag;
import org.eclipse.hawkbit.repository.model.DistributionSetTagAssignmentResult;
import org.eclipse.hawkbit.repository.model.DistributionSetType;
import org.eclipse.hawkbit.repository.model.MetaData;
import org.eclipse.hawkbit.repository.model.SoftwareModule;
import org.eclipse.hawkbit.repository.model.SoftwareModuleType;
import org.eclipse.hawkbit.repository.rsql.VirtualPropertyReplacer;
import org.eclipse.hawkbit.tenancy.TenantAware;
import org.springframework.beans.factory.annotation.Autowired;
@@ -125,22 +128,28 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
private SoftwareModuleTypeRepository softwareModuleTypeRepository;
@Override
public DistributionSet findDistributionSetByIdWithDetails(final Long distid) {
return distributionSetRepository.findOne(DistributionSetSpecification.byId(distid));
public Optional<DistributionSet> findDistributionSetByIdWithDetails(final Long distid) {
return Optional.ofNullable(distributionSetRepository.findOne(DistributionSetSpecification.byId(distid)));
}
@Override
public DistributionSet findDistributionSetById(final Long distid) {
return distributionSetRepository.findOne(distid);
public Optional<DistributionSet> findDistributionSetById(final Long distid) {
return Optional.ofNullable(distributionSetRepository.findOne(distid));
}
@Override
@Modifying
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public DistributionSetTagAssignmentResult toggleTagAssignment(final Collection<Long> dsIds, final String tagName) {
final List<JpaDistributionSet> sets = findDistributionSetListWithDetails(dsIds);
final DistributionSetTag myTag = tagManagement.findDistributionSetTag(tagName);
if (sets.size() < dsIds.size()) {
throw new EntityNotFoundException(DistributionSet.class, dsIds,
sets.stream().map(DistributionSet::getId).collect(Collectors.toList()));
}
final DistributionSetTag myTag = tagManagement.findDistributionSetTag(tagName)
.orElseThrow(() -> new EntityNotFoundException(DistributionSetTag.class, tagName));
DistributionSetTagAssignmentResult result;
@@ -207,46 +216,37 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
}
private DistributionSetType findDistributionSetTypeWithExceptionIfNotFound(final String distributionSetTypekey) {
final DistributionSetType module = findDistributionSetTypeByKey(distributionSetTypekey);
if (module == null) {
throw new EntityNotFoundException(
"DistributionSetType with key {" + distributionSetTypekey + "} does not exist");
}
return module;
return findDistributionSetTypeByKey(distributionSetTypekey)
.orElseThrow(() -> new EntityNotFoundException(DistributionSetType.class, distributionSetTypekey));
}
private JpaDistributionSetType findDistributionSetTypeAndThrowExceptionIfNotFound(final Long setId) {
final JpaDistributionSetType set = (JpaDistributionSetType) findDistributionSetTypeById(setId);
return (JpaDistributionSetType) findDistributionSetTypeById(setId)
.orElseThrow(() -> new EntityNotFoundException(DistributionSetType.class, setId));
if (set == null) {
throw new EntityNotFoundException("Distribution set type cannot be updated as it does not exixt" + setId);
}
return set;
}
private JpaDistributionSet findDistributionSetAndThrowExceptionIfNotFound(final Long setId) {
final JpaDistributionSet set = (JpaDistributionSet) findDistributionSetById(setId);
if (set == null) {
throw new EntityNotFoundException("Distribution set cannot be updated as it does not exixt" + setId);
}
return set;
return (JpaDistributionSet) findDistributionSetById(setId)
.orElseThrow(() -> new EntityNotFoundException(DistributionSet.class, setId));
}
private JpaSoftwareModule findSoftwareModuleAndThrowExceptionIfNotFound(final Long moduleId) {
final JpaSoftwareModule module = softwareModuleRepository.findOne(moduleId);
if (module == null) {
throw new EntityNotFoundException("Distribution set cannot be updated as it does not exixt" + moduleId);
}
return module;
return Optional.ofNullable(softwareModuleRepository.findOne(moduleId))
.orElseThrow(() -> new EntityNotFoundException(SoftwareModule.class, moduleId));
}
@Override
@Modifying
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public void deleteDistributionSet(final Collection<Long> distributionSetIDs) {
final List<DistributionSet> setsFound = findDistributionSetAllById(distributionSetIDs);
if (setsFound.size() < distributionSetIDs.size()) {
throw new EntityNotFoundException(DistributionSet.class, distributionSetIDs,
setsFound.stream().map(DistributionSet::getId).collect(Collectors.toList()));
}
final List<Long> assigned = distributionSetRepository
.findAssignedToTargetDistributionSetsById(distributionSetIDs);
assigned.addAll(distributionSetRepository.findAssignedToRolloutDistributionSetsById(distributionSetIDs));
@@ -303,15 +303,17 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
@Modifying
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public DistributionSet assignSoftwareModules(final Long setId, final Collection<Long> moduleIds) {
final JpaDistributionSet set = findDistributionSetAndThrowExceptionIfNotFound(setId);
final Collection<JpaSoftwareModule> modules = softwareModuleRepository.findByIdIn(moduleIds);
if (modules.size() < moduleIds.size()) {
throw new EntityNotFoundException("Not all given software modules where found.");
throw new EntityNotFoundException(SoftwareModule.class, moduleIds,
modules.stream().map(SoftwareModule::getId).collect(Collectors.toList()));
}
checkDistributionSetIsAssignedToTargets(setId);
final JpaDistributionSet set = findDistributionSetAndThrowExceptionIfNotFound(setId);
modules.forEach(set::addModule);
return distributionSetRepository.save(set);
@@ -363,15 +365,15 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public DistributionSetType assignMandatorySoftwareModuleTypes(final Long dsTypeId,
final Collection<Long> softwareModulesTypeIds) {
final JpaDistributionSetType type = findDistributionSetTypeAndThrowExceptionIfNotFound(dsTypeId);
final Collection<JpaSoftwareModuleType> modules = softwareModuleTypeRepository
.findByIdIn(softwareModulesTypeIds);
if (modules.size() < softwareModulesTypeIds.size()) {
throw new EntityNotFoundException("Not all given software module types where found.");
throw new EntityNotFoundException(SoftwareModuleType.class, softwareModulesTypeIds,
modules.stream().map(SoftwareModuleType::getId).collect(Collectors.toList()));
}
final JpaDistributionSetType type = findDistributionSetTypeAndThrowExceptionIfNotFound(dsTypeId);
checkDistributionSetTypeSoftwareModuleTypesIsAllowedToModify(dsTypeId);
modules.forEach(type::addMandatoryModuleType);
@@ -384,17 +386,17 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public DistributionSetType assignOptionalSoftwareModuleTypes(final Long dsTypeId,
final Collection<Long> softwareModulesTypeIds) {
final JpaDistributionSetType type = findDistributionSetTypeAndThrowExceptionIfNotFound(dsTypeId);
final Collection<JpaSoftwareModuleType> modules = softwareModuleTypeRepository
.findByIdIn(softwareModulesTypeIds);
if (modules.size() < softwareModulesTypeIds.size()) {
throw new EntityNotFoundException("Not all given software module types where found.");
throw new EntityNotFoundException(SoftwareModuleType.class, softwareModulesTypeIds,
modules.stream().map(SoftwareModuleType::getId).collect(Collectors.toList()));
}
final JpaDistributionSetType type = findDistributionSetTypeAndThrowExceptionIfNotFound(dsTypeId);
checkDistributionSetTypeSoftwareModuleTypesIsAllowedToModify(dsTypeId);
modules.forEach(type::addOptionalModuleType);
return distributionSetTypeRepository.save(type);
@@ -545,10 +547,11 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
}
@Override
public DistributionSet findDistributionSetByNameAndVersion(final String distributionName, final String version) {
public Optional<DistributionSet> findDistributionSetByNameAndVersion(final String distributionName,
final String version) {
final Specification<JpaDistributionSet> spec = DistributionSetSpecification
.equalsNameAndVersionIgnoreCase(distributionName, version);
return distributionSetRepository.findOne(spec);
return Optional.ofNullable(distributionSetRepository.findOne(spec));
}
@@ -571,18 +574,20 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
}
@Override
public DistributionSetType findDistributionSetTypeByName(final String name) {
return distributionSetTypeRepository.findOne(DistributionSetTypeSpecification.byName(name));
public Optional<DistributionSetType> findDistributionSetTypeByName(final String name) {
return Optional
.ofNullable(distributionSetTypeRepository.findOne(DistributionSetTypeSpecification.byName(name)));
}
@Override
public DistributionSetType findDistributionSetTypeById(final Long id) {
return distributionSetTypeRepository.findOne(DistributionSetTypeSpecification.byId(id));
public Optional<DistributionSetType> findDistributionSetTypeById(final Long typeId) {
return Optional
.ofNullable(distributionSetTypeRepository.findOne(DistributionSetTypeSpecification.byId(typeId)));
}
@Override
public DistributionSetType findDistributionSetTypeByKey(final String key) {
return distributionSetTypeRepository.findOne(DistributionSetTypeSpecification.byKey(key));
public Optional<DistributionSetType> findDistributionSetTypeByKey(final String key) {
return Optional.ofNullable(distributionSetTypeRepository.findOne(DistributionSetTypeSpecification.byKey(key)));
}
@Override
@@ -599,9 +604,8 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public void deleteDistributionSetType(final Long typeId) {
final JpaDistributionSetType toDelete = Optional.ofNullable(distributionSetTypeRepository.findOne(typeId))
.orElseThrow(() -> new EntityNotFoundException(
"DistributionSet Type with given ID " + typeId + " does not exist"));
final JpaDistributionSetType toDelete = distributionSetTypeRepository.findById(typeId)
.orElseThrow(() -> new EntityNotFoundException(DistributionSetType.class, typeId));
if (distributionSetRepository.countByTypeId(typeId) > 0) {
toDelete.setDeleted(true);
@@ -634,7 +638,8 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
// check if exists otherwise throw entity not found exception
final JpaDistributionSetMetadata toUpdate = (JpaDistributionSetMetadata) findDistributionSetMetadata(dsId,
md.getKey());
md.getKey()).orElseThrow(
() -> new EntityNotFoundException(DistributionSetMetadata.class, dsId, md.getKey()));
toUpdate.setValue(md.getValue());
// touch it to update the lock revision because we are modifying the
// DS indirectly
@@ -645,9 +650,32 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
@Override
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
@Modifying
public void deleteDistributionSetMetadata(final Long distributionSet, final String key) {
touch(distributionSet);
distributionSetMetadataRepository.delete(new DsMetadataCompositeKey(distributionSet, key));
public void deleteDistributionSetMetadata(final Long distributionSetId, final String key) {
final JpaDistributionSetMetadata metadata = (JpaDistributionSetMetadata) findDistributionSetMetadata(
distributionSetId, key).orElseThrow(
() -> new EntityNotFoundException(DistributionSetMetadata.class, distributionSetId, key));
touch(metadata.getDistributionSet());
distributionSetMetadataRepository.delete(metadata.getId());
}
/**
* Method to get the latest distribution set based on DS ID after the
* metadata changes for that distribution set.
*
* @param ds
* is the DS to touch
*/
private JpaDistributionSet touch(final DistributionSet ds) {
// merge base distribution set so optLockRevision gets updated and audit
// log written because
// modifying metadata is modifying the base distribution set itself for
// auditing purposes.
final JpaDistributionSet result = entityManager.merge((JpaDistributionSet) ds);
result.setLastModifiedAt(0L);
return result;
}
/**
@@ -655,24 +683,20 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
* metadata changes for that distribution set.
*
* @param distId
* Distribution set
* of the DS to touch
*/
private JpaDistributionSet touch(final Long distId) {
final DistributionSet latestDistributionSet = findDistributionSetAndThrowExceptionIfNotFound(distId);
// merge base distribution set so optLockRevision gets updated and audit
// log written because
// modifying metadata is modifying the base distribution set itself for
// auditing purposes.
final JpaDistributionSet result = entityManager.merge((JpaDistributionSet) latestDistributionSet);
result.setLastModifiedAt(0L);
return result;
return touch(findDistributionSetById(distId)
.orElseThrow(() -> new EntityNotFoundException(DistributionSet.class, distId)));
}
@Override
public Page<DistributionSetMetadata> findDistributionSetMetadataByDistributionSetId(final Long distributionSetId,
final Pageable pageable) {
if (!distributionSetRepository.exists(distributionSetId)) {
throw new EntityNotFoundException(DistributionSet.class, distributionSetId);
}
return convertMdPage(distributionSetMetadataRepository
.findAll((Specification<JpaDistributionSetMetadata>) (root, query, cb) -> cb.equal(
root.get(JpaDistributionSetMetadata_.distributionSet).get(JpaDistributionSet_.id),
@@ -680,18 +704,14 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
pageable);
}
@Override
public List<DistributionSetMetadata> findDistributionSetMetadataByDistributionSetId(final Long distributionSetId) {
return Collections.unmodifiableList(distributionSetMetadataRepository
.findAll((Specification<JpaDistributionSetMetadata>) (root, query, cb) -> cb.equal(
root.get(JpaDistributionSetMetadata_.distributionSet).get(JpaDistributionSet_.id),
distributionSetId)));
}
@Override
public Page<DistributionSetMetadata> findDistributionSetMetadataByDistributionSetId(final Long distributionSetId,
final String rsqlParam, final Pageable pageable) {
if (!distributionSetRepository.exists(distributionSetId)) {
throw new EntityNotFoundException(DistributionSet.class, distributionSetId);
}
final Specification<JpaDistributionSetMetadata> spec = RSQLUtility.parse(rsqlParam,
DistributionSetMetadataFields.class, virtualPropertyReplacer);
@@ -710,18 +730,18 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
}
@Override
public DistributionSetMetadata findDistributionSetMetadata(final Long distributionSet, final String key) {
final DistributionSetMetadata findOne = distributionSetMetadataRepository
.findOne(new DsMetadataCompositeKey(distributionSet, key));
if (findOne == null) {
throw new EntityNotFoundException("Metadata with key '" + key + "' does not exist");
}
return findOne;
public Optional<DistributionSetMetadata> findDistributionSetMetadata(final Long distributionSet, final String key) {
return Optional.ofNullable(
distributionSetMetadataRepository.findOne(new DsMetadataCompositeKey(distributionSet, key)));
}
@Override
public DistributionSet findDistributionSetByAction(final Long actionId) {
return distributionSetRepository.findByActionId(actionId);
public Optional<DistributionSet> findDistributionSetByAction(final Long actionId) {
if (!actionRepository.exists(actionId)) {
throw new EntityNotFoundException(Action.class, actionId);
}
return Optional.ofNullable(distributionSetRepository.findByActionId(actionId));
}
@Override
@@ -819,10 +839,13 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
public List<DistributionSet> assignTag(final Collection<Long> dsIds, final Long dsTagId) {
final List<JpaDistributionSet> allDs = findDistributionSetListWithDetails(dsIds);
final DistributionSetTag distributionSetTag = Optional
.ofNullable(tagManagement.findDistributionSetTagById(dsTagId))
.orElseThrow(() -> new EntityNotFoundException(
"DistributionSet Tag with given ID " + dsTagId + " not found."));
if (allDs.size() < dsIds.size()) {
throw new EntityNotFoundException(DistributionSet.class, dsIds,
allDs.stream().map(DistributionSet::getId).collect(Collectors.toList()));
}
final DistributionSetTag distributionSetTag = tagManagement.findDistributionSetTagById(dsTagId)
.orElseThrow(() -> new EntityNotFoundException(DistributionSetTag.class, dsTagId));
allDs.forEach(ds -> ds.addTag(distributionSetTag));
@@ -835,10 +858,8 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public List<DistributionSet> unAssignAllDistributionSetsByTag(final Long dsTagId) {
final DistributionSetTag distributionSetTag = Optional
.ofNullable(tagManagement.findDistributionSetTagById(dsTagId))
.orElseThrow(() -> new EntityNotFoundException(
"DistributionSet Tag with given ID " + dsTagId + " not found."));
final DistributionSetTag distributionSetTag = tagManagement.findDistributionSetTagById(dsTagId)
.orElseThrow(() -> new EntityNotFoundException(DistributionSetTag.class, dsTagId));
@SuppressWarnings({ "unchecked", "rawtypes" })
final Collection<JpaDistributionSet> distributionSets = (Collection) distributionSetTag
@@ -854,13 +875,11 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
final List<JpaDistributionSet> allDs = findDistributionSetListWithDetails(Arrays.asList(dsId));
if (allDs.isEmpty()) {
throw new EntityNotFoundException("DistributionSet with given ID " + dsId + " not found.");
throw new EntityNotFoundException(DistributionSet.class, dsId);
}
final DistributionSetTag distributionSetTag = Optional
.ofNullable(tagManagement.findDistributionSetTagById(dsTagId))
.orElseThrow(() -> new EntityNotFoundException(
"DistributionSet Tag with given ID " + dsTagId + " not found."));
final DistributionSetTag distributionSetTag = tagManagement.findDistributionSetTagById(dsTagId)
.orElseThrow(() -> new EntityNotFoundException(DistributionSetTag.class, dsTagId));
final List<JpaDistributionSet> unAssignTag = unAssignTag(allDs, distributionSetTag);
return unAssignTag.isEmpty() ? null : unAssignTag.get(0);
}
@@ -885,7 +904,7 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public void deleteDistributionSet(final Long setId) {
if (!distributionSetRepository.exists(setId)) {
throw new EntityNotFoundException("DistributionSet with given ID " + setId + " does not exist.");
throw new EntityNotFoundException(DistributionSet.class, setId);
}
deleteDistributionSet(Lists.newArrayList(setId));
@@ -893,6 +912,10 @@ public class JpaDistributionSetManagement implements DistributionSetManagement {
@Override
public Long countDistributionSetsByType(final Long typeId) {
if (!distributionSetTypeRepository.exists(typeId)) {
throw new EntityNotFoundException(DistributionSetType.class, typeId);
}
return distributionSetRepository.countByTypeId(typeId);
}

View File

@@ -78,8 +78,8 @@ public class JpaRolloutGroupManagement implements RolloutGroupManagement {
private VirtualPropertyReplacer virtualPropertyReplacer;
@Override
public RolloutGroup findRolloutGroupById(final Long rolloutGroupId) {
return rolloutGroupRepository.findOne(rolloutGroupId);
public Optional<RolloutGroup> findRolloutGroupById(final Long rolloutGroupId) {
return Optional.ofNullable(rolloutGroupRepository.findOne(rolloutGroupId));
}
@Override
@@ -131,14 +131,21 @@ public class JpaRolloutGroupManagement implements RolloutGroupManagement {
}
@Override
public RolloutGroup findRolloutGroupWithDetailedStatus(final Long rolloutGroupId) {
final JpaRolloutGroup rolloutGroup = (JpaRolloutGroup) findRolloutGroupById(rolloutGroupId);
public Optional<RolloutGroup> findRolloutGroupWithDetailedStatus(final Long rolloutGroupId) {
final Optional<RolloutGroup> rolloutGroup = findRolloutGroupById(rolloutGroupId);
if (!rolloutGroup.isPresent()) {
return rolloutGroup;
}
final JpaRolloutGroup jpaRolloutGroup = (JpaRolloutGroup) rolloutGroup.get();
final List<TotalTargetCountActionStatus> rolloutStatusCountItems = actionRepository
.getStatusCountByRolloutGroupId(rolloutGroupId);
final TotalTargetCountStatus totalTargetCountStatus = new TotalTargetCountStatus(rolloutStatusCountItems,
Long.valueOf(rolloutGroup.getTotalTargets()));
rolloutGroup.setTotalTargetCountStatus(totalTargetCountStatus);
Long.valueOf(jpaRolloutGroup.getTotalTargets()));
jpaRolloutGroup.setTotalTargetCountStatus(totalTargetCountStatus);
return rolloutGroup;
}
@@ -168,9 +175,8 @@ public class JpaRolloutGroupManagement implements RolloutGroupManagement {
@Override
public Page<Target> findRolloutGroupTargets(final Long rolloutGroupId, final Pageable page) {
final JpaRolloutGroup rolloutGroup = Optional.ofNullable(rolloutGroupRepository.findOne(rolloutGroupId))
.orElseThrow(() -> new EntityNotFoundException(
"Rollout Group with given ID " + rolloutGroupId + " not found."));
final JpaRolloutGroup rolloutGroup = rolloutGroupRepository.findById(rolloutGroupId)
.orElseThrow(() -> new EntityNotFoundException(RolloutGroup.class, rolloutGroupId));
if (isRolloutStatusReady(rolloutGroup)) {
// in case of status ready the action has not been created yet and

View File

@@ -17,7 +17,6 @@ import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.persistence.EntityNotFoundException;
import javax.validation.ConstraintDeclarationException;
import org.apache.commons.lang3.StringUtils;
@@ -34,6 +33,7 @@ import org.eclipse.hawkbit.repository.builder.RolloutUpdate;
import org.eclipse.hawkbit.repository.event.remote.entity.RolloutGroupCreatedEvent;
import org.eclipse.hawkbit.repository.exception.ConstraintViolationException;
import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException;
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
import org.eclipse.hawkbit.repository.exception.RolloutIllegalStateException;
import org.eclipse.hawkbit.repository.jpa.executor.AfterTransactionCommitExecutor;
import org.eclipse.hawkbit.repository.jpa.model.JpaAction;
@@ -147,7 +147,6 @@ public class JpaRolloutManagement implements RolloutManagement {
@Override
public Page<Rollout> findAllByPredicate(final String rsqlParam, final Pageable pageable) {
final Specification<JpaRollout> specification = RSQLUtility.parse(rsqlParam, RolloutFields.class,
virtualPropertyReplacer);
@@ -156,8 +155,8 @@ public class JpaRolloutManagement implements RolloutManagement {
}
@Override
public Rollout findRolloutById(final Long rolloutId) {
return rolloutRepository.findOne(rolloutId);
public Optional<Rollout> findRolloutById(final Long rolloutId) {
return Optional.ofNullable(rolloutRepository.findOne(rolloutId));
}
@Override
@@ -181,9 +180,9 @@ public class JpaRolloutManagement implements RolloutManagement {
}
private JpaRollout createRollout(final JpaRollout rollout) {
final JpaRollout existingRollout = rolloutRepository.findByName(rollout.getName());
if (existingRollout != null) {
throw new EntityAlreadyExistsException(existingRollout.getName());
final Optional<Rollout> existingRollout = rolloutRepository.findByName(rollout.getName());
if (existingRollout.isPresent()) {
throw new EntityAlreadyExistsException(existingRollout.get().getName());
}
final Long totalTargets = targetManagement.countTargetByTargetFilterQuery(rollout.getTargetFilterQuery());
@@ -294,8 +293,7 @@ public class JpaRolloutManagement implements RolloutManagement {
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
@Modifying
public void fillRolloutGroupsWithTargets(final Long rolloutId) {
final JpaRollout rollout = Optional.ofNullable(rolloutRepository.findOne(rolloutId))
.orElseThrow(() -> new EntityNotFoundException("Rollout with id " + rolloutId + " not found."));
final JpaRollout rollout = getRolloutAndThrowExceptionIfNotFound(rolloutId);
RolloutHelper.verifyRolloutInStatus(rollout, RolloutStatus.CREATING);
@@ -409,7 +407,7 @@ public class JpaRolloutManagement implements RolloutManagement {
throw new ConstraintDeclarationException("Rollout target filter does not match any targets");
}
RolloutGroupsValidation validation = validateTargetsInGroups(groups, baseFilter, totalTargets);
final RolloutGroupsValidation validation = validateTargetsInGroups(groups, baseFilter, totalTargets);
return totalTargets - validation.getTargetsInGroups();
}
@@ -433,14 +431,14 @@ public class JpaRolloutManagement implements RolloutManagement {
final long totalTargets) {
final List<Long> groupTargetCounts = new ArrayList<>(groups.size());
final Map<String, Long> targetFilterCounts = groups.stream()
.map(group -> RolloutHelper.getGroupTargetFilter(baseFilter, group)).distinct().collect(Collectors
.toMap(Function.identity(), filter -> targetManagement.countTargetByTargetFilterQuery(filter)));
.map(group -> RolloutHelper.getGroupTargetFilter(baseFilter, group)).distinct()
.collect(Collectors.toMap(Function.identity(), targetManagement::countTargetByTargetFilterQuery));
long unusedTargetsCount = 0;
for (int i = 0; i < groups.size(); i++) {
final RolloutGroup group = groups.get(i);
String groupTargetFilter = RolloutHelper.getGroupTargetFilter(baseFilter, group);
final String groupTargetFilter = RolloutHelper.getGroupTargetFilter(baseFilter, group);
RolloutHelper.verifyRolloutGroupTargetPercentage(group.getTargetPercentage());
final long targetsInGroupFilter = targetFilterCounts.get(groupTargetFilter);
@@ -474,8 +472,8 @@ public class JpaRolloutManagement implements RolloutManagement {
return 0;
}
final List<RolloutGroup> previousGroups = groups.subList(0, groupIndex);
String overlappingTargetsFilter = RolloutHelper.getOverlappingWithGroupsTargetFilter(baseFilter, previousGroups,
group);
final String overlappingTargetsFilter = RolloutHelper.getOverlappingWithGroupsTargetFilter(baseFilter,
previousGroups, group);
if (targetFilterCounts.containsKey(overlappingTargetsFilter)) {
return targetFilterCounts.get(overlappingTargetsFilter);
@@ -490,8 +488,7 @@ public class JpaRolloutManagement implements RolloutManagement {
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
@Modifying
public Rollout startRollout(final Long rolloutId) {
final JpaRollout rollout = Optional.ofNullable(rolloutRepository.findOne(rolloutId))
.orElseThrow(() -> new EntityNotFoundException("Rollout with id " + rolloutId + " not found."));
final JpaRollout rollout = getRolloutAndThrowExceptionIfNotFound(rolloutId);
RolloutHelper.checkIfRolloutCanStarted(rollout, rollout);
rollout.setStatus(RolloutStatus.STARTING);
rollout.setLastCheck(0);
@@ -516,7 +513,6 @@ public class JpaRolloutManagement implements RolloutManagement {
jpaRollout.setStatus(RolloutStatus.RUNNING);
jpaRollout.setLastCheck(0);
rolloutRepository.save(jpaRollout);
}
private boolean ensureAllGroupsAreScheduled(final Rollout rollout) {
@@ -534,12 +530,6 @@ public class JpaRolloutManagement implements RolloutManagement {
/**
* Schedules a group of the rollout. Scheduled Actions are created to
* achieve this. The creation of those Actions is allowed to fail.
*
* @param rollout
* the Rollout
* @param group
* the RolloutGroup
* @return whether the complete group was scheduled
*/
private boolean scheduleRolloutGroup(final JpaRollout rollout, final JpaRolloutGroup group) {
final long targetsInGroup = rolloutTargetGroupRepository.countByRolloutGroup(group);
@@ -599,19 +589,6 @@ public class JpaRolloutManagement implements RolloutManagement {
* Creates an action entry into the action repository. In case of existing
* scheduled actions the scheduled actions gets canceled. A scheduled action
* is created in-active.
*
* @param targets
* the targets to create scheduled actions for
* @param distributionSet
* the distribution set for the actions
* @param actionType
* the action type for the action
* @param forcedTime
* the forcedTime of the action
* @param rollout
* the roll out for this action
* @param rolloutGroup
* the roll out group for this action
*/
private void createScheduledAction(final Collection<Target> targets, final DistributionSet distributionSet,
final ActionType actionType, final Long forcedTime, final Rollout rollout,
@@ -620,7 +597,7 @@ public class JpaRolloutManagement implements RolloutManagement {
// is already scheduled and a next action is created then cancel the
// current scheduled action to cancel. E.g. a new scheduled action is
// created.
final List<Long> targetIds = targets.stream().map(t -> t.getId()).collect(Collectors.toList());
final List<Long> targetIds = targets.stream().map(Target::getId).collect(Collectors.toList());
actionRepository.switchStatus(Action.Status.CANCELED, targetIds, false, Action.Status.SCHEDULED);
targets.forEach(target -> {
final JpaAction action = new JpaAction();
@@ -640,8 +617,7 @@ public class JpaRolloutManagement implements RolloutManagement {
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
@Modifying
public void pauseRollout(final Long rolloutId) {
final JpaRollout rollout = Optional.ofNullable(rolloutRepository.findOne(rolloutId))
.orElseThrow(() -> new EntityNotFoundException("Rollout with id " + rolloutId + " not found."));
final JpaRollout rollout = getRolloutAndThrowExceptionIfNotFound(rolloutId);
if (rollout.getStatus() != RolloutStatus.RUNNING) {
throw new RolloutIllegalStateException("Rollout can only be paused in state running but current state is "
+ rollout.getStatus().name().toLowerCase());
@@ -659,8 +635,7 @@ public class JpaRolloutManagement implements RolloutManagement {
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
@Modifying
public void resumeRollout(final Long rolloutId) {
final JpaRollout rollout = Optional.ofNullable(rolloutRepository.findOne(rolloutId))
.orElseThrow(() -> new EntityNotFoundException("Rollout with id " + rolloutId + " not found."));
final JpaRollout rollout = getRolloutAndThrowExceptionIfNotFound(rolloutId);
if (!(RolloutStatus.PAUSED.equals(rollout.getStatus()))) {
throw new RolloutIllegalStateException("Rollout can only be resumed in state paused but current state is "
+ rollout.getStatus().name().toLowerCase());
@@ -855,7 +830,6 @@ public class JpaRolloutManagement implements RolloutManagement {
startFirstRolloutGroup(rollout);
}
});
}
@Override
@@ -876,7 +850,6 @@ public class JpaRolloutManagement implements RolloutManagement {
startRollout(rollout.getId());
}
});
}
private List<JpaRollout> getRolloutsToCheckForStatus(final long delayBetweenChecks, final RolloutStatus status) {
@@ -891,7 +864,6 @@ public class JpaRolloutManagement implements RolloutManagement {
}
return rolloutRepository.findByLastCheckAndStatus(lastCheck, status);
}
@Override
@@ -913,7 +885,7 @@ public class JpaRolloutManagement implements RolloutManagement {
}
@Override
public Rollout findRolloutByName(final String rolloutName) {
public Optional<Rollout> findRolloutByName(final String rolloutName) {
return rolloutRepository.findByName(rolloutName);
}
@@ -922,8 +894,7 @@ public class JpaRolloutManagement implements RolloutManagement {
@Modifying
public Rollout updateRollout(final RolloutUpdate u) {
final GenericRolloutUpdate update = (GenericRolloutUpdate) u;
final JpaRollout rollout = Optional.ofNullable(rolloutRepository.findOne(update.getId()))
.orElseThrow(() -> new EntityNotFoundException("Rollout with id " + update.getId() + " not found."));
final JpaRollout rollout = getRolloutAndThrowExceptionIfNotFound(update.getId());
update.getName().ifPresent(rollout::setName);
update.getDescription().ifPresent(rollout::setDescription);
@@ -931,32 +902,40 @@ public class JpaRolloutManagement implements RolloutManagement {
update.getForcedTime().ifPresent(rollout::setForcedTime);
update.getStartAt().ifPresent(rollout::setStartAt);
update.getSet().ifPresent(setId -> {
final DistributionSet set = distributionSetManagement.findDistributionSetById(setId);
if (set == null) {
throw new EntityNotFoundException("Distribution set cannot be set as it does not exists" + setId);
}
final DistributionSet set = distributionSetManagement.findDistributionSetById(setId)
.orElseThrow(() -> new EntityNotFoundException(DistributionSet.class, setId));
rollout.setDistributionSet(set);
});
return rolloutRepository.save(rollout);
}
private JpaRollout getRolloutAndThrowExceptionIfNotFound(final Long rolloutId) {
return rolloutRepository.findById(rolloutId)
.orElseThrow(() -> new EntityNotFoundException(Rollout.class, rolloutId));
}
@Override
public Page<Rollout> findAllRolloutsWithDetailedStatus(final Pageable pageable) {
final Page<JpaRollout> rollouts = rolloutRepository.findAll(pageable);
setRolloutStatusDetails(rollouts);
return RolloutHelper.convertPage(rollouts, pageable);
}
@Override
public Rollout findRolloutWithDetailedStatus(final Long rolloutId) {
final Rollout rollout = findRolloutById(rolloutId);
public Optional<Rollout> findRolloutWithDetailedStatus(final Long rolloutId) {
final Optional<Rollout> rollout = findRolloutById(rolloutId);
if (!rollout.isPresent()) {
return rollout;
}
final List<TotalTargetCountActionStatus> rolloutStatusCountItems = actionRepository
.getStatusCountByRolloutId(rolloutId);
final TotalTargetCountStatus totalTargetCountStatus = new TotalTargetCountStatus(rolloutStatusCountItems,
rollout.getTotalTargets());
((JpaRollout) rollout).setTotalTargetCountStatus(totalTargetCountStatus);
rollout.get().getTotalTargets());
((JpaRollout) rollout.get()).setTotalTargetCountStatus(totalTargetCountStatus);
return rollout;
}
@@ -966,8 +945,7 @@ public class JpaRolloutManagement implements RolloutManagement {
}
private void setRolloutStatusDetails(final Slice<JpaRollout> rollouts) {
final List<Long> rolloutIds = rollouts.getContent().stream().map(rollout -> rollout.getId())
.collect(Collectors.toList());
final List<Long> rolloutIds = rollouts.getContent().stream().map(Rollout::getId).collect(Collectors.toList());
final Map<Long, List<TotalTargetCountActionStatus>> allStatesForRollout = getStatusCountItemForRollout(
rolloutIds);
@@ -980,20 +958,24 @@ public class JpaRolloutManagement implements RolloutManagement {
@Override
public float getFinishedPercentForRunningGroup(final Long rolloutId, final Long rolloutGroupId) {
final RolloutGroup rolloutGroup = Optional.ofNullable(rolloutGroupRepository.findOne(rolloutGroupId))
.orElseThrow(() -> new EntityNotFoundException(
"Rollout group with given ID " + rolloutGroupId + " not found."));
final RolloutGroup rolloutGroup = rolloutGroupRepository.findById(rolloutGroupId)
.orElseThrow(() -> new EntityNotFoundException(RolloutGroup.class, rolloutGroupId));
final long totalGroup = rolloutGroup.getTotalTargets();
final Long finished = actionRepository.countByRolloutIdAndRolloutGroupIdAndStatus(rolloutId,
rolloutGroup.getId(), Action.Status.FINISHED);
if (totalGroup == 0) {
// in case e.g. targets has been deleted we don't have any actions
// left for this group, so the group is finished
return 100;
}
final Long finished = actionRepository.countByRolloutIdAndRolloutGroupIdAndStatus(rolloutId,
rolloutGroup.getId(), Action.Status.FINISHED);
// calculate threshold
return ((float) finished / (float) totalGroup) * 100;
}
@Override
public boolean exists(final Long rolloutId) {
return rolloutRepository.exists(rolloutId);
}
}

View File

@@ -55,6 +55,7 @@ import org.eclipse.hawkbit.repository.jpa.specifications.SoftwareModuleSpecifica
import org.eclipse.hawkbit.repository.jpa.specifications.SpecificationsBuilder;
import org.eclipse.hawkbit.repository.model.Artifact;
import org.eclipse.hawkbit.repository.model.AssignedSoftwareModule;
import org.eclipse.hawkbit.repository.model.DistributionSet;
import org.eclipse.hawkbit.repository.model.MetaData;
import org.eclipse.hawkbit.repository.model.SoftwareModule;
import org.eclipse.hawkbit.repository.model.SoftwareModuleMetadata;
@@ -122,9 +123,8 @@ public class JpaSoftwareManagement implements SoftwareManagement {
public SoftwareModule updateSoftwareModule(final SoftwareModuleUpdate u) {
final GenericSoftwareModuleUpdate update = (GenericSoftwareModuleUpdate) u;
final JpaSoftwareModule module = Optional.ofNullable(softwareModuleRepository.findOne(update.getId()))
.orElseThrow(() -> new EntityNotFoundException(
"Software module cannot be updated as it does not exixt" + update.getId()));
final JpaSoftwareModule module = softwareModuleRepository.findById(update.getId())
.orElseThrow(() -> new EntityNotFoundException(SoftwareModule.class, update.getId()));
update.getDescription().ifPresent(module::setDescription);
update.getVendor().ifPresent(module::setVendor);
@@ -138,7 +138,8 @@ public class JpaSoftwareManagement implements SoftwareManagement {
public SoftwareModuleType updateSoftwareModuleType(final SoftwareModuleTypeUpdate u) {
final GenericSoftwareModuleTypeUpdate update = (GenericSoftwareModuleTypeUpdate) u;
final JpaSoftwareModuleType type = findSoftwareModuleTypeAndThrowExceptionIfNotFound(update.getId());
final JpaSoftwareModuleType type = (JpaSoftwareModuleType) findSoftwareModuleTypeById(update.getId())
.orElseThrow(() -> new EntityNotFoundException(SoftwareModuleType.class, update.getId()));
update.getDescription().ifPresent(type::setDescription);
update.getColour().ifPresent(type::setColour);
@@ -146,15 +147,6 @@ public class JpaSoftwareManagement implements SoftwareManagement {
return softwareModuleTypeRepository.save(type);
}
private JpaSoftwareModuleType findSoftwareModuleTypeAndThrowExceptionIfNotFound(final Long smTypeid) {
final JpaSoftwareModuleType set = softwareModuleTypeRepository.findOne(smTypeid);
if (set == null) {
throw new EntityNotFoundException("Software module type cannot be updated as it does not exixt" + smTypeid);
}
return set;
}
@Override
@Modifying
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
@@ -173,6 +165,7 @@ public class JpaSoftwareManagement implements SoftwareManagement {
@Override
public Slice<SoftwareModule> findSoftwareModulesByType(final Pageable pageable, final Long typeId) {
throwExceptionIfSoftwareModuleTypeDoesNotExist(typeId);
final List<Specification<JpaSoftwareModule>> specList = Lists.newArrayListWithExpectedSize(2);
@@ -182,6 +175,12 @@ public class JpaSoftwareManagement implements SoftwareManagement {
return convertSmPage(findSwModuleByCriteriaAPI(pageable, specList), pageable);
}
private void throwExceptionIfSoftwareModuleTypeDoesNotExist(final Long typeId) {
if (!softwareModuleTypeRepository.exists(typeId)) {
throw new EntityNotFoundException(SoftwareModuleType.class, typeId);
}
}
private static Slice<SoftwareModule> convertSmPage(final Slice<JpaSoftwareModule> findAll,
final Pageable pageable) {
return new PageImpl<>(Collections.unmodifiableList(findAll.getContent()), pageable, 0);
@@ -197,14 +196,16 @@ public class JpaSoftwareManagement implements SoftwareManagement {
}
@Override
public SoftwareModule findSoftwareModuleById(final Long id) {
return softwareModuleRepository.findOne(id);
public Optional<SoftwareModule> findSoftwareModuleById(final Long id) {
return Optional.ofNullable(softwareModuleRepository.findOne(id));
}
@Override
public SoftwareModule findSoftwareModuleByNameAndVersion(final String name, final String version,
public Optional<SoftwareModule> findSoftwareModuleByNameAndVersion(final String name, final String version,
final Long typeId) {
throwExceptionIfSoftwareModuleTypeDoesNotExist(typeId);
return softwareModuleRepository.findOneByNameAndVersionAndTypeId(name, version, typeId);
}
@@ -233,6 +234,12 @@ public class JpaSoftwareManagement implements SoftwareManagement {
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public void deleteSoftwareModules(final Collection<Long> ids) {
final List<JpaSoftwareModule> swModulesToDelete = softwareModuleRepository.findByIdIn(ids);
if (swModulesToDelete.size() < ids.size()) {
throw new EntityNotFoundException(SoftwareModule.class, ids,
swModulesToDelete.stream().map(SoftwareModule::getId).collect(Collectors.toList()));
}
final Set<Long> assignedModuleIds = new HashSet<>();
swModulesToDelete.forEach(swModule -> {
@@ -329,6 +336,8 @@ public class JpaSoftwareManagement implements SoftwareManagement {
}
if (null != typeId) {
throwExceptionIfSoftwareModuleTypeDoesNotExist(typeId);
spec = SoftwareModuleSpecification.equalType(typeId);
specList.add(spec);
}
@@ -393,8 +402,8 @@ public class JpaSoftwareManagement implements SoftwareManagement {
Predicate[] unassignedSpec;
if (!assignedSoftwareModules.isEmpty()) {
unassignedSpec = specificationsToPredicate(buildSpecificationList(searchText, typeId), unassignedRoot,
unassignedQuery, cb, cb.not(unassignedRoot.get(JpaSoftwareModule_.id)
.in(assignedSoftwareModules.stream().map(sw -> sw.getId()).collect(Collectors.toList()))));
unassignedQuery, cb, cb.not(unassignedRoot.get(JpaSoftwareModule_.id).in(
assignedSoftwareModules.stream().map(SoftwareModule::getId).collect(Collectors.toList()))));
} else {
unassignedSpec = specificationsToPredicate(buildSpecificationList(searchText, typeId), unassignedRoot,
unassignedQuery, cb);
@@ -412,13 +421,14 @@ public class JpaSoftwareManagement implements SoftwareManagement {
return new SliceImpl<>(resultList);
}
private static List<Specification<JpaSoftwareModule>> buildSpecificationList(final String searchText,
final Long typeId) {
private List<Specification<JpaSoftwareModule>> buildSpecificationList(final String searchText, final Long typeId) {
final List<Specification<JpaSoftwareModule>> specList = Lists.newArrayListWithExpectedSize(3);
if (!Strings.isNullOrEmpty(searchText)) {
specList.add(SoftwareModuleSpecification.likeNameOrVersion(searchText));
}
if (typeId != null) {
throwExceptionIfSoftwareModuleTypeDoesNotExist(typeId);
specList.add(SoftwareModuleSpecification.equalType(typeId));
}
specList.add(SoftwareModuleSpecification.isDeletedFalse());
@@ -447,6 +457,8 @@ public class JpaSoftwareManagement implements SoftwareManagement {
}
if (null != typeId) {
throwExceptionIfSoftwareModuleTypeDoesNotExist(typeId);
spec = SoftwareModuleSpecification.equalType(typeId);
specList.add(spec);
}
@@ -465,17 +477,17 @@ public class JpaSoftwareManagement implements SoftwareManagement {
}
@Override
public SoftwareModuleType findSoftwareModuleTypeByKey(final String key) {
public Optional<SoftwareModuleType> findSoftwareModuleTypeByKey(final String key) {
return softwareModuleTypeRepository.findByKey(key);
}
@Override
public SoftwareModuleType findSoftwareModuleTypeById(final Long id) {
return softwareModuleTypeRepository.findOne(id);
public Optional<SoftwareModuleType> findSoftwareModuleTypeById(final Long smTypeId) {
return Optional.ofNullable(softwareModuleTypeRepository.findOne(smTypeId));
}
@Override
public SoftwareModuleType findSoftwareModuleTypeByName(final String name) {
public Optional<SoftwareModuleType> findSoftwareModuleTypeByName(final String name) {
return softwareModuleTypeRepository.findByName(name);
}
@@ -492,9 +504,8 @@ public class JpaSoftwareManagement implements SoftwareManagement {
@Modifying
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public void deleteSoftwareModuleType(final Long typeId) {
final JpaSoftwareModuleType toDelete = Optional.ofNullable(softwareModuleTypeRepository.findOne(typeId))
.orElseThrow(() -> new EntityNotFoundException(
"Software Module Type with giben ID " + typeId + " does not exist."));
final JpaSoftwareModuleType toDelete = softwareModuleTypeRepository.findById(typeId)
.orElseThrow(() -> new EntityNotFoundException(SoftwareModuleType.class, typeId));
if (softwareModuleRepository.countByType(toDelete) > 0
|| distributionSetTypeRepository.countByElementsSmType(toDelete) > 0) {
@@ -507,6 +518,10 @@ public class JpaSoftwareManagement implements SoftwareManagement {
@Override
public Page<SoftwareModule> findSoftwareModuleByAssignedTo(final Pageable pageable, final Long setId) {
if (!distributionSetRepository.exists(setId)) {
throw new EntityNotFoundException(DistributionSet.class, setId);
}
return softwareModuleRepository.findByAssignedToId(pageable, setId);
}
@@ -548,11 +563,12 @@ public class JpaSoftwareManagement implements SoftwareManagement {
public SoftwareModuleMetadata updateSoftwareModuleMetadata(final Long moduleId, final MetaData md) {
// check if exists otherwise throw entity not found exception
final JpaSoftwareModuleMetadata metadata = findSoftwareModuleMetadata(
new SwMetadataCompositeKey(moduleId, md.getKey()));
final JpaSoftwareModuleMetadata metadata = (JpaSoftwareModuleMetadata) findSoftwareModuleMetadata(moduleId,
md.getKey()).orElseThrow(
() -> new EntityNotFoundException(SoftwareModuleMetadata.class, moduleId, md.getKey()));
metadata.setValue(md.getValue());
touch(moduleId);
touch(metadata.getSoftwareModule());
return softwareModuleMetadataRepository.save(metadata);
}
@@ -560,40 +576,63 @@ public class JpaSoftwareManagement implements SoftwareManagement {
* Method to get the latest module based on ID after the metadata changes
* for that module.
*
* @param distributionSet
* Distribution set
* @param latestModule
* module to touch
*/
private JpaSoftwareModule touch(final Long moduleId) {
final JpaSoftwareModule latestModule = softwareModuleRepository.findOne(moduleId);
private JpaSoftwareModule touch(final SoftwareModule latestModule) {
// merge base distribution set so optLockRevision gets updated and audit
// log written because
// modifying metadata is modifying the base distribution set itself for
// auditing purposes.
final JpaSoftwareModule result = entityManager.merge(latestModule);
final JpaSoftwareModule result = entityManager.merge((JpaSoftwareModule) latestModule);
result.setLastModifiedAt(0L);
return result;
}
/**
* Method to get the latest module based on ID after the metadata changes
* for that module.
*
* @param moduleId
* of the module to touch
*/
private JpaSoftwareModule touch(final Long moduleId) {
return touch(findSoftwareModuleById(moduleId)
.orElseThrow(() -> new EntityNotFoundException(SoftwareModule.class, moduleId)));
}
@Override
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
@Modifying
public void deleteSoftwareModuleMetadata(final Long moduleId, final String key) {
touch(moduleId);
softwareModuleMetadataRepository.delete(new SwMetadataCompositeKey(moduleId, key));
final JpaSoftwareModuleMetadata metadata = (JpaSoftwareModuleMetadata) findSoftwareModuleMetadata(moduleId, key)
.orElseThrow(() -> new EntityNotFoundException(SoftwareModuleMetadata.class, moduleId, key));
touch(metadata.getSoftwareModule());
softwareModuleMetadataRepository.delete(metadata.getId());
}
@Override
public Page<SoftwareModuleMetadata> findSoftwareModuleMetadataBySoftwareModuleId(final Long swId,
final Pageable pageable) {
throwExceptionIfSoftwareModuleDoesNotExist(swId);
return softwareModuleMetadataRepository.findBySoftwareModuleId(swId, pageable);
}
private void throwExceptionIfSoftwareModuleDoesNotExist(final Long swId) {
if (!softwareModuleRepository.exists(swId)) {
throw new EntityNotFoundException(SoftwareModule.class, swId);
}
}
@Override
public Page<SoftwareModuleMetadata> findSoftwareModuleMetadataBySoftwareModuleId(final Long softwareModuleId,
final String rsqlParam, final Pageable pageable) {
throwExceptionIfSoftwareModuleDoesNotExist(softwareModuleId);
final Specification<JpaSoftwareModuleMetadata> spec = RSQLUtility.parse(rsqlParam,
SoftwareModuleMetadataFields.class, virtualPropertyReplacer);
return convertSmMdPage(
@@ -609,6 +648,8 @@ public class JpaSoftwareManagement implements SoftwareManagement {
@Override
public List<SoftwareModuleMetadata> findSoftwareModuleMetadataBySoftwareModuleId(final Long softwareModuleId) {
throwExceptionIfSoftwareModuleDoesNotExist(softwareModuleId);
return Collections.unmodifiableList(softwareModuleMetadataRepository
.findAll((Specification<JpaSoftwareModuleMetadata>) (root, query, cb) -> cb
.and(cb.equal(root.get(JpaSoftwareModuleMetadata_.softwareModule).get(JpaSoftwareModule_.id),
@@ -616,16 +657,8 @@ public class JpaSoftwareManagement implements SoftwareManagement {
}
@Override
public SoftwareModuleMetadata findSoftwareModuleMetadata(final Long moduleId, final String key) {
return findSoftwareModuleMetadata(new SwMetadataCompositeKey(moduleId, key));
}
private JpaSoftwareModuleMetadata findSoftwareModuleMetadata(final SwMetadataCompositeKey id) {
final JpaSoftwareModuleMetadata findOne = softwareModuleMetadataRepository.findOne(id);
if (findOne == null) {
throw new EntityNotFoundException("Metadata with key '" + id.getKey() + "' does not exist");
}
return findOne;
public Optional<SoftwareModuleMetadata> findSoftwareModuleMetadata(final Long moduleId, final String key) {
return Optional.ofNullable(softwareModuleMetadataRepository.findOne(new SwMetadataCompositeKey(moduleId, key)));
}
private void checkAndThrowAlreadyExistsIfSoftwareModuleMetadataExists(final SwMetadataCompositeKey metadataId) {

View File

@@ -62,7 +62,7 @@ public class JpaTagManagement implements TagManagement {
private VirtualPropertyReplacer virtualPropertyReplacer;
@Override
public TargetTag findTargetTag(final String name) {
public Optional<TargetTag> findTargetTag(final String name) {
return targetTagRepository.findByNameEquals(name);
}
@@ -73,7 +73,7 @@ public class JpaTagManagement implements TagManagement {
final JpaTargetTag targetTag = create.buildTargetTag();
if (findTargetTag(targetTag.getName()) != null) {
if (findTargetTag(targetTag.getName()).isPresent()) {
throw new EntityAlreadyExistsException();
}
@@ -95,7 +95,8 @@ public class JpaTagManagement implements TagManagement {
@Modifying
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public void deleteTargetTag(final String targetTagName) {
final JpaTargetTag tag = targetTagRepository.findByNameEquals(targetTagName);
final TargetTag tag = targetTagRepository.findByNameEquals(targetTagName)
.orElseThrow(() -> new EntityNotFoundException(TargetTag.class, targetTagName));
targetRepository.findByTag(tag.getId()).forEach(set -> {
set.removeTag(tag);
@@ -107,11 +108,6 @@ public class JpaTagManagement implements TagManagement {
}
@Override
public List<TargetTag> findAllTargetTags() {
return Collections.unmodifiableList(targetTagRepository.findAll());
}
@Override
public Page<TargetTag> findAllTargetTags(final String rsqlParam, final Pageable pageable) {
@@ -139,8 +135,8 @@ public class JpaTagManagement implements TagManagement {
public TargetTag updateTargetTag(final TagUpdate u) {
final GenericTagUpdate update = (GenericTagUpdate) u;
final JpaTargetTag tag = Optional.ofNullable(targetTagRepository.findOne(update.getId()))
.orElseThrow(() -> new EntityNotFoundException("Target tag with ID " + update.getId() + " not found"));
final JpaTargetTag tag = targetTagRepository.findById(update.getId())
.orElseThrow(() -> new EntityNotFoundException(TargetTag.class, update.getId()));
update.getName().ifPresent(tag::setName);
update.getDescription().ifPresent(tag::setDescription);
@@ -155,9 +151,8 @@ public class JpaTagManagement implements TagManagement {
public DistributionSetTag updateDistributionSetTag(final TagUpdate u) {
final GenericTagUpdate update = (GenericTagUpdate) u;
final JpaDistributionSetTag tag = Optional.ofNullable(distributionSetTagRepository.findOne(update.getId()))
.orElseThrow(() -> new EntityNotFoundException(
"Distribution set tag with ID " + update.getId() + " not found"));
final JpaDistributionSetTag tag = distributionSetTagRepository.findById(update.getId())
.orElseThrow(() -> new EntityNotFoundException(DistributionSetTag.class, update.getId()));
update.getName().ifPresent(tag::setName);
update.getDescription().ifPresent(tag::setDescription);
@@ -167,7 +162,7 @@ public class JpaTagManagement implements TagManagement {
}
@Override
public DistributionSetTag findDistributionSetTag(final String name) {
public Optional<DistributionSetTag> findDistributionSetTag(final String name) {
return distributionSetTagRepository.findByNameEquals(name);
}
@@ -179,7 +174,7 @@ public class JpaTagManagement implements TagManagement {
final JpaDistributionSetTag distributionSetTag = create.buildDistributionSetTag();
if (distributionSetTagRepository.findByNameEquals(distributionSetTag.getName()) != null) {
if (distributionSetTagRepository.findByNameEquals(distributionSetTag.getName()).isPresent()) {
throw new EntityAlreadyExistsException();
}
@@ -203,7 +198,8 @@ public class JpaTagManagement implements TagManagement {
@Modifying
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public void deleteDistributionSetTag(final String tagName) {
final JpaDistributionSetTag tag = distributionSetTagRepository.findByNameEquals(tagName);
final DistributionSetTag tag = distributionSetTagRepository.findByNameEquals(tagName)
.orElseThrow(() -> new EntityNotFoundException(DistributionSetTag.class, tagName));
distributionSetRepository.findByTag(tag).forEach(set -> {
set.removeTag(tag);
@@ -219,13 +215,13 @@ public class JpaTagManagement implements TagManagement {
}
@Override
public TargetTag findTargetTagById(final Long id) {
return targetTagRepository.findOne(id);
public Optional<TargetTag> findTargetTagById(final Long id) {
return Optional.ofNullable(targetTagRepository.findOne(id));
}
@Override
public DistributionSetTag findDistributionSetTagById(final Long id) {
return distributionSetTagRepository.findOne(id);
public Optional<DistributionSetTag> findDistributionSetTagById(final Long id) {
return Optional.ofNullable(distributionSetTagRepository.findOne(id));
}
@Override

View File

@@ -76,7 +76,7 @@ public class JpaTargetFilterQueryManagement implements TargetFilterQueryManageme
final JpaTargetFilterQuery query = create.build();
if (targetFilterQueryRepository.findByName(query.getName()) != null) {
if (targetFilterQueryRepository.findByName(query.getName()).isPresent()) {
throw new EntityAlreadyExistsException(query.getName());
}
return targetFilterQueryRepository.save(query);
@@ -86,6 +86,9 @@ public class JpaTargetFilterQueryManagement implements TargetFilterQueryManageme
@Modifying
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public void deleteTargetFilterQuery(final Long targetFilterQueryId) {
findTargetFilterQueryById(targetFilterQueryId)
.orElseThrow(() -> new EntityNotFoundException(TargetFilterQuery.class, targetFilterQueryId));
targetFilterQueryRepository.delete(targetFilterQueryId);
}
@@ -165,13 +168,13 @@ public class JpaTargetFilterQueryManagement implements TargetFilterQueryManageme
}
@Override
public TargetFilterQuery findTargetFilterQueryByName(final String targetFilterQueryName) {
public Optional<TargetFilterQuery> findTargetFilterQueryByName(final String targetFilterQueryName) {
return targetFilterQueryRepository.findByName(targetFilterQueryName);
}
@Override
public TargetFilterQuery findTargetFilterQueryById(final Long targetFilterQueryId) {
return targetFilterQueryRepository.findOne(targetFilterQueryId);
public Optional<TargetFilterQuery> findTargetFilterQueryById(final Long targetFilterQueryId) {
return Optional.ofNullable(targetFilterQueryRepository.findOne(targetFilterQueryId));
}
@Override
@@ -201,18 +204,13 @@ public class JpaTargetFilterQueryManagement implements TargetFilterQueryManageme
}
private JpaDistributionSet findDistributionSetAndThrowExceptionIfNotFound(final Long setId) {
final JpaDistributionSet set = (JpaDistributionSet) distributionSetManagement
.findDistributionSetByIdWithDetails(setId);
if (set == null) {
throw new EntityNotFoundException("Distribution set cannot be updated as it does not exixt" + setId);
}
return set;
return (JpaDistributionSet) distributionSetManagement.findDistributionSetByIdWithDetails(setId)
.orElseThrow(() -> new EntityNotFoundException(DistributionSet.class, setId));
}
private JpaTargetFilterQuery findTargetFilterQueryOrThrowExceptionIfNotFound(final Long queryId) {
return Optional.ofNullable(targetFilterQueryRepository.findOne(queryId)).orElseThrow(
() -> new EntityNotFoundException("TargetFilterQuery with given ID " + queryId + " not found!"));
return targetFilterQueryRepository.findById(queryId)
.orElseThrow(() -> new EntityNotFoundException(TargetFilterQuery.class, queryId));
}
@Override

View File

@@ -48,6 +48,8 @@ import org.eclipse.hawkbit.repository.jpa.model.JpaTarget_;
import org.eclipse.hawkbit.repository.jpa.rsql.RSQLUtility;
import org.eclipse.hawkbit.repository.jpa.specifications.SpecificationsBuilder;
import org.eclipse.hawkbit.repository.jpa.specifications.TargetSpecifications;
import org.eclipse.hawkbit.repository.model.DistributionSet;
import org.eclipse.hawkbit.repository.model.RolloutGroup;
import org.eclipse.hawkbit.repository.model.Target;
import org.eclipse.hawkbit.repository.model.TargetFilterQuery;
import org.eclipse.hawkbit.repository.model.TargetTag;
@@ -85,6 +87,12 @@ public class JpaTargetManagement implements TargetManagement {
@Autowired
private TargetRepository targetRepository;
@Autowired
private RolloutGroupRepository rolloutGroupRepository;
@Autowired
private DistributionSetRepository distributionSetRepository;
@Autowired
private TargetFilterQueryRepository targetFilterQueryRepository;
@@ -110,25 +118,29 @@ public class JpaTargetManagement implements TargetManagement {
private VirtualPropertyReplacer virtualPropertyReplacer;
@Override
public Target findTargetByControllerID(final String controllerId) {
public Optional<Target> findTargetByControllerID(final String controllerId) {
return targetRepository.findByControllerId(controllerId);
}
@Override
public Target findTargetByControllerIDWithDetails(final String controllerId) {
final Target result = targetRepository.findByControllerId(controllerId);
public Optional<Target> findTargetByControllerIDWithDetails(final String controllerId) {
final Optional<Target> result = targetRepository.findByControllerId(controllerId);
// load lazy relations
if (result != null) {
result.getTargetInfo().getControllerAttributes().size();
if (result.getTargetInfo() != null && result.getTargetInfo().getInstalledDistributionSet() != null) {
result.getTargetInfo().getInstalledDistributionSet().getName();
result.getTargetInfo().getInstalledDistributionSet().getModules().size();
}
if (result.getAssignedDistributionSet() != null) {
result.getAssignedDistributionSet().getName();
result.getAssignedDistributionSet().getModules().size();
}
if (!result.isPresent()) {
return result;
}
result.get().getTargetInfo().getControllerAttributes().size();
if (result.get().getTargetInfo() != null
&& result.get().getTargetInfo().getInstalledDistributionSet() != null) {
result.get().getTargetInfo().getInstalledDistributionSet().getName();
result.get().getTargetInfo().getInstalledDistributionSet().getModules().size();
}
if (result.get().getAssignedDistributionSet() != null) {
result.get().getAssignedDistributionSet().getName();
result.get().getAssignedDistributionSet().getModules().size();
}
return result;
}
@@ -159,10 +171,8 @@ public class JpaTargetManagement implements TargetManagement {
@Override
public Slice<Target> findTargetsByTargetFilterQuery(final Long targetFilterQueryId, final Pageable pageable) {
final TargetFilterQuery targetFilterQuery = Optional
.ofNullable(targetFilterQueryRepository.findOne(targetFilterQueryId))
.orElseThrow(() -> new EntityNotFoundException(
"TargetFilterQuery with given ID" + targetFilterQueryId + " not found"));
final TargetFilterQuery targetFilterQuery = targetFilterQueryRepository.findById(targetFilterQueryId)
.orElseThrow(() -> new EntityNotFoundException(TargetFilterQuery.class, targetFilterQueryId));
return findTargetsBySpec(
RSQLUtility.parse(targetFilterQuery.getQuery(), TargetFields.class, virtualPropertyReplacer), pageable);
@@ -192,9 +202,8 @@ public class JpaTargetManagement implements TargetManagement {
public Target updateTarget(final TargetUpdate u) {
final JpaTargetUpdate update = (JpaTargetUpdate) u;
final JpaTarget target = Optional.ofNullable(targetRepository.findByControllerId(update.getControllerId()))
.orElseThrow(() -> new EntityNotFoundException(
"Target with ID " + update.getControllerId() + " not found."));
final JpaTarget target = (JpaTarget) targetRepository.findByControllerId(update.getControllerId())
.orElseThrow(() -> new EntityNotFoundException(Target.class, update.getControllerId()));
target.setNew(false);
@@ -210,6 +219,13 @@ public class JpaTargetManagement implements TargetManagement {
@Modifying
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public void deleteTargets(final Collection<Long> targetIDs) {
final List<JpaTarget> targets = targetRepository.findAll(targetIDs);
if (targets.size() < targetIDs.size()) {
throw new EntityNotFoundException(Target.class, targetIDs,
targets.stream().map(Target::getId).collect(Collectors.toList()));
}
targetRepository.deleteByIdIn(targetIDs);
targetIDs.forEach(targetId -> eventPublisher.publishEvent(new TargetDeletedEvent(tenantAware.getCurrentTenant(),
@@ -220,22 +236,23 @@ public class JpaTargetManagement implements TargetManagement {
@Modifying
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public void deleteTarget(final String controllerID) {
final Long targetId = Optional.ofNullable(targetRepository.findByControllerId(controllerID))
.orElseThrow(
() -> new EntityNotFoundException("Target with given ID " + controllerID + " does not exist."))
.getId();
final Target target = targetRepository.findByControllerId(controllerID)
.orElseThrow(() -> new EntityNotFoundException(Target.class, controllerID));
targetRepository.delete(targetId);
targetRepository.delete(target.getId());
}
@Override
public Page<Target> findTargetByAssignedDistributionSet(final Long distributionSetID, final Pageable pageReq) {
throwEntityNotFoundIfDsDoesNotExist(distributionSetID);
return targetRepository.findByAssignedDistributionSetId(pageReq, distributionSetID);
}
@Override
public Page<Target> findTargetByAssignedDistributionSet(final Long distributionSetID, final String rsqlParam,
final Pageable pageReq) {
throwEntityNotFoundIfDsDoesNotExist(distributionSetID);
final Specification<JpaTarget> spec = RSQLUtility.parse(rsqlParam, TargetFields.class, virtualPropertyReplacer);
@@ -248,6 +265,12 @@ public class JpaTargetManagement implements TargetManagement {
pageReq);
}
private void throwEntityNotFoundIfDsDoesNotExist(final Long distributionSetID) {
if (!distributionSetRepository.exists(distributionSetID)) {
throw new EntityNotFoundException(DistributionSet.class, distributionSetID);
}
}
private static Page<Target> convertPage(final Page<JpaTarget> findAll, final Pageable pageable) {
return new PageImpl<>(Collections.unmodifiableList(findAll.getContent()), pageable, findAll.getTotalElements());
}
@@ -258,12 +281,14 @@ public class JpaTargetManagement implements TargetManagement {
@Override
public Page<Target> findTargetByInstalledDistributionSet(final Long distributionSetID, final Pageable pageReq) {
throwEntityNotFoundIfDsDoesNotExist(distributionSetID);
return targetRepository.findByTargetInfoInstalledDistributionSetId(pageReq, distributionSetID);
}
@Override
public Page<Target> findTargetByInstalledDistributionSet(final Long distributionSetId, final String rsqlParam,
final Pageable pageable) {
throwEntityNotFoundIfDsDoesNotExist(distributionSetId);
final Specification<JpaTarget> spec = RSQLUtility.parse(rsqlParam, TargetFields.class, virtualPropertyReplacer);
@@ -303,7 +328,7 @@ public class JpaTargetManagement implements TargetManagement {
return countByCriteriaAPI(specList);
}
private static List<Specification<JpaTarget>> buildSpecificationList(final FilterParams filterParams,
private List<Specification<JpaTarget>> buildSpecificationList(final FilterParams filterParams,
final boolean fetch) {
final List<Specification<JpaTarget>> specList = new ArrayList<>();
if (filterParams.getFilterByStatus() != null && !filterParams.getFilterByStatus().isEmpty()) {
@@ -313,6 +338,8 @@ public class JpaTargetManagement implements TargetManagement {
specList.add(TargetSpecifications.isOverdue(TimestampCalculator.calculateOverdueTimestamp()));
}
if (filterParams.getFilterByDistributionId() != null) {
throwEntityNotFoundIfDsDoesNotExist(filterParams.getFilterByDistributionId());
specList.add(TargetSpecifications
.hasInstalledOrAssignedDistributionSet(filterParams.getFilterByDistributionId()));
}
@@ -352,7 +379,8 @@ public class JpaTargetManagement implements TargetManagement {
@Modifying
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public TargetTagAssignmentResult toggleTagAssignment(final Collection<String> targetIds, final String tagName) {
final TargetTag tag = targetTagRepository.findByNameEquals(tagName);
final TargetTag tag = targetTagRepository.findByNameEquals(tagName)
.orElseThrow(() -> new EntityNotFoundException(TargetTag.class, tagName));
final List<JpaTarget> alreadyAssignedTargets = targetRepository.findByTagNameAndControllerIdIn(tagName,
targetIds);
final List<JpaTarget> allTargets = targetRepository
@@ -388,8 +416,8 @@ public class JpaTargetManagement implements TargetManagement {
final List<JpaTarget> allTargets = targetRepository
.findAll(TargetSpecifications.byControllerIdWithStatusAndTagsInJoin(controllerIds));
final JpaTargetTag tag = Optional.ofNullable(targetTagRepository.findOne(tagId))
.orElseThrow(() -> new EntityNotFoundException("Tag with given ID " + tagId + "does not exist"));
final JpaTargetTag tag = targetTagRepository.findById(tagId)
.orElseThrow(() -> new EntityNotFoundException(TargetTag.class, tagId));
allTargets.forEach(target -> target.addTag(tag));
return Collections
@@ -411,8 +439,8 @@ public class JpaTargetManagement implements TargetManagement {
@Transactional(isolation = Isolation.READ_UNCOMMITTED)
public List<Target> unAssignAllTargetsByTag(final Long targetTagId) {
final TargetTag tag = Optional.ofNullable(targetTagRepository.findOne(targetTagId)).orElseThrow(
() -> new EntityNotFoundException("TargetTag with given ID " + targetTagId + " does not exist."));
final TargetTag tag = targetTagRepository.findById(targetTagId)
.orElseThrow(() -> new EntityNotFoundException(TargetTag.class, targetTagId));
if (tag.getAssignedToTargets().isEmpty()) {
return Collections.emptyList();
@@ -428,8 +456,8 @@ public class JpaTargetManagement implements TargetManagement {
final List<Target> allTargets = Collections.unmodifiableList(targetRepository
.findAll(TargetSpecifications.byControllerIdWithStatusAndTagsInJoin(Arrays.asList(controllerID))));
final TargetTag tag = Optional.ofNullable(targetTagRepository.findOne(targetTagId)).orElseThrow(
() -> new EntityNotFoundException("TargetTag with given ID " + targetTagId + " does not exist."));
final TargetTag tag = targetTagRepository.findById(targetTagId)
.orElseThrow(() -> new EntityNotFoundException(TargetTag.class, targetTagId));
final List<Target> unAssignTag = unAssignTag(allTargets, tag);
return unAssignTag.isEmpty() ? null : unAssignTag.get(0);
@@ -438,7 +466,6 @@ public class JpaTargetManagement implements TargetManagement {
@Override
public Slice<Target> findTargetsAllOrderByLinkedDistributionSet(final Pageable pageable,
final Long orderByDistributionId, final FilterParams filterParams) {
final CriteriaBuilder cb = entityManager.getCriteriaBuilder();
final CriteriaQuery<JpaTarget> query = cb.createQuery(JpaTarget.class);
final Root<JpaTarget> targetRoot = query.from(JpaTarget.class);
@@ -496,17 +523,22 @@ public class JpaTargetManagement implements TargetManagement {
@Override
public Long countTargetByAssignedDistributionSet(final Long distId) {
throwEntityNotFoundIfDsDoesNotExist(distId);
return targetRepository.countByAssignedDistributionSetId(distId);
}
@Override
public Long countTargetByInstalledDistributionSet(final Long distId) {
throwEntityNotFoundIfDsDoesNotExist(distId);
return targetRepository.countByTargetInfoInstalledDistributionSetId(distId);
}
@Override
public Page<Target> findAllTargetsByTargetFilterQueryAndNonDS(@NotNull final Pageable pageRequest,
final Long distributionSetId, @NotNull final String targetFilterQuery) {
public Page<Target> findAllTargetsByTargetFilterQueryAndNonDS(final Pageable pageRequest,
final Long distributionSetId, final String targetFilterQuery) {
throwEntityNotFoundIfDsDoesNotExist(distributionSetId);
final Specification<JpaTarget> spec = RSQLUtility.parse(targetFilterQuery, TargetFields.class,
virtualPropertyReplacer);
@@ -534,6 +566,10 @@ public class JpaTargetManagement implements TargetManagement {
@Override
public Page<Target> findAllTargetsInRolloutGroupWithoutAction(@NotNull final Pageable pageRequest,
@NotNull final Long group) {
if (!rolloutGroupRepository.exists(group)) {
throw new EntityNotFoundException(RolloutGroup.class, group);
}
return findTargetsBySpec(
(root, cq, cb) -> TargetSpecifications.hasNoActionInRolloutGroup(group).toPredicate(root, cq, cb),
pageRequest);
@@ -552,6 +588,8 @@ public class JpaTargetManagement implements TargetManagement {
@Override
public Long countTargetsByTargetFilterQueryAndNonDS(final Long distributionSetId, final String targetFilterQuery) {
throwEntityNotFoundIfDsDoesNotExist(distributionSetId);
final Specification<JpaTarget> spec = RSQLUtility.parse(targetFilterQuery, TargetFields.class,
virtualPropertyReplacer);
final List<Specification<JpaTarget>> specList = new ArrayList<>(2);
@@ -569,7 +607,7 @@ public class JpaTargetManagement implements TargetManagement {
final JpaTarget target = create.build();
if (targetRepository.findByControllerId(target.getControllerId()) != null) {
if (targetRepository.findByControllerId(target.getControllerId()).isPresent()) {
throw new EntityAlreadyExistsException();
}
@@ -592,16 +630,18 @@ public class JpaTargetManagement implements TargetManagement {
@Override
public List<Target> findTargetsByTag(final String tagName) {
final JpaTargetTag tag = targetTagRepository.findByNameEquals(tagName);
return Collections.unmodifiableList(targetRepository.findByTag(tag.getId()));
final Optional<TargetTag> tag = targetTagRepository.findByNameEquals(tagName);
if (!tag.isPresent()) {
return Collections.emptyList();
}
return Collections.unmodifiableList(targetRepository.findByTag(tag.get().getId()));
}
@Override
public Long countTargetByTargetFilterQuery(final Long targetFilterQueryId) {
final TargetFilterQuery targetFilterQuery = Optional
.ofNullable(targetFilterQueryRepository.findOne(targetFilterQueryId))
.orElseThrow(() -> new EntityNotFoundException(
"TargetFilterQuery with given ID" + targetFilterQueryId + " not found"));
final TargetFilterQuery targetFilterQuery = targetFilterQueryRepository.findById(targetFilterQueryId)
.orElseThrow(() -> new EntityNotFoundException(TargetFilterQuery.class, targetFilterQueryId));
final Specification<JpaTarget> specs = RSQLUtility.parse(targetFilterQuery.getQuery(), TargetFields.class,
virtualPropertyReplacer);
@@ -619,8 +659,8 @@ public class JpaTargetManagement implements TargetManagement {
}
@Override
public Target findTargetById(final Long id) {
return targetRepository.findOne(id);
public Optional<Target> findTargetById(final Long id) {
return Optional.ofNullable(targetRepository.findOne(id));
}
@Override

View File

@@ -61,7 +61,7 @@ public interface LocalArtifactRepository extends BaseEntityRepository<JpaArtifac
* to search
* @return {@link Artifact} the first in the result list
*/
JpaArtifact findFirstBySha1Hash(String sha1Hash);
Optional<Artifact> findFirstBySha1Hash(String sha1Hash);
/**
* Searches for a {@link Artifact} based user provided filename at upload.
@@ -70,7 +70,7 @@ public interface LocalArtifactRepository extends BaseEntityRepository<JpaArtifac
* to search
* @return list of {@link Artifact}.
*/
List<Artifact> findByFilename(String filename);
Optional<Artifact> findFirstByFilename(String filename);
/**
* Searches for local artifact for a base software module.
@@ -94,6 +94,6 @@ public interface LocalArtifactRepository extends BaseEntityRepository<JpaArtifac
* selected software module id
* @return list of {@link Artifact}.
*/
List<Artifact> findByFilenameAndSoftwareModuleId(final String filename, final Long softwareModuleId);
Optional<Artifact> findFirstByFilenameAndSoftwareModuleId(final String filename, final Long softwareModuleId);
}

View File

@@ -9,6 +9,7 @@
package org.eclipse.hawkbit.repository.jpa;
import java.util.List;
import java.util.Optional;
import org.eclipse.hawkbit.repository.jpa.model.JpaRollout;
import org.eclipse.hawkbit.repository.model.Rollout;
@@ -67,5 +68,5 @@ public interface RolloutRepository
* the rollout name
* @return {@link Rollout} for specific name
*/
JpaRollout findByName(String name);
Optional<Rollout> findByName(String name);
}

View File

@@ -9,6 +9,7 @@
package org.eclipse.hawkbit.repository.jpa;
import java.util.List;
import java.util.Optional;
import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSet;
import org.eclipse.hawkbit.repository.jpa.model.JpaSoftwareModule;
@@ -55,7 +56,7 @@ public interface SoftwareModuleRepository
* @return the found {@link SoftwareModule} with the given name AND version
* AND type
*/
JpaSoftwareModule findOneByNameAndVersionAndTypeId(String name, String version, Long typeId);
Optional<SoftwareModule> findOneByNameAndVersionAndTypeId(String name, String version, Long typeId);
/**
* deletes the {@link SoftwareModule}s with the given IDs.

View File

@@ -9,6 +9,7 @@
package org.eclipse.hawkbit.repository.jpa;
import java.util.List;
import java.util.Optional;
import org.eclipse.hawkbit.repository.jpa.model.JpaSoftwareModuleType;
import org.eclipse.hawkbit.repository.model.SoftwareModule;
@@ -52,7 +53,7 @@ public interface SoftwareModuleTypeRepository
* @return all {@link SoftwareModuleType}s in the repository with given
* {@link SoftwareModuleType#getKey()}
*/
JpaSoftwareModuleType findByKey(String key);
Optional<SoftwareModuleType> findByKey(String key);
/**
*
@@ -61,7 +62,7 @@ public interface SoftwareModuleTypeRepository
* @return all {@link SoftwareModuleType}s in the repository with given
* {@link SoftwareModuleType#getName()}
*/
JpaSoftwareModuleType findByName(String name);
Optional<SoftwareModuleType> findByName(String name);
/**
* retrieves all software module types with a given

View File

@@ -8,6 +8,8 @@
*/
package org.eclipse.hawkbit.repository.jpa;
import java.util.Optional;
import org.eclipse.hawkbit.repository.jpa.model.JpaTargetFilterQuery;
import org.eclipse.hawkbit.repository.model.TargetFilterQuery;
import org.springframework.data.domain.Page;
@@ -32,7 +34,7 @@ public interface TargetFilterQueryRepository
* @param name
* @return custom target filter
*/
TargetFilterQuery findByName(final String name);
Optional<TargetFilterQuery> findByName(final String name);
/**
* Find list of all custom target filters.

View File

@@ -10,6 +10,7 @@ package org.eclipse.hawkbit.repository.jpa;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import org.eclipse.hawkbit.repository.jpa.model.JpaDistributionSet;
import org.eclipse.hawkbit.repository.jpa.model.JpaTarget;
@@ -44,7 +45,7 @@ public interface TargetRepository extends BaseEntityRepository<JpaTarget, Long>,
* @return found {@link Target} or <code>null</code> if not found.
*/
@EntityGraph(value = "Target.detail", type = EntityGraphType.LOAD)
JpaTarget findByControllerId(String controllerID);
Optional<Target> findByControllerId(String controllerID);
/**
* Deletes the {@link Target}s with the given target IDs.

View File

@@ -9,6 +9,7 @@
package org.eclipse.hawkbit.repository.jpa;
import java.util.List;
import java.util.Optional;
import org.eclipse.hawkbit.repository.jpa.model.JpaTargetTag;
import org.eclipse.hawkbit.repository.model.TargetTag;
@@ -43,7 +44,7 @@ public interface TargetTagRepository
* to filter on
* @return the {@link TargetTag} if found, otherwise null
*/
JpaTargetTag findByNameEquals(String tagName);
Optional<TargetTag> findByNameEquals(String tagName);
/**
* Returns all instances of the type.

View File

@@ -47,14 +47,8 @@ public class JpaDistributionSetCreate extends AbstractDistributionSetUpdateCreat
}
private DistributionSetType findDistributionSetTypeWithExceptionIfNotFound(final String distributionSetTypekey) {
final DistributionSetType module = distributionSetManagement
.findDistributionSetTypeByKey(distributionSetTypekey);
if (module == null) {
throw new EntityNotFoundException(
"DistributionSetType with key {" + distributionSetTypekey + "} does not exist");
}
return module;
return distributionSetManagement.findDistributionSetTypeByKey(distributionSetTypekey)
.orElseThrow(() -> new EntityNotFoundException(DistributionSetType.class, distributionSetTypekey));
}
private Collection<SoftwareModule> findSoftwareModuleWithExceptionIfNotFound(
@@ -65,8 +59,7 @@ public class JpaDistributionSetCreate extends AbstractDistributionSetUpdateCreat
final Collection<SoftwareModule> module = softwareManagement.findSoftwareModulesById(softwareModuleId);
if (module.size() < softwareModuleId.size()) {
throw new EntityNotFoundException(
"Some SoftwareModules out of the range {" + softwareModuleId + "} due not exist");
throw new EntityNotFoundException(SoftwareModule.class, softwareModuleId);
}
return module;

View File

@@ -51,8 +51,7 @@ public class JpaDistributionSetTypeCreate extends AbstractDistributionSetTypeUpd
final Collection<SoftwareModuleType> module = softwareManagement
.findSoftwareModuleTypesById(softwareModuleTypeId);
if (module.size() < softwareModuleTypeId.size()) {
throw new EntityNotFoundException(
"SoftwareModules types out of the range {" + softwareModuleTypeId + "} due not exist");
throw new EntityNotFoundException(SoftwareModuleType.class, softwareModuleTypeId);
}
return module;

View File

@@ -44,11 +44,7 @@ public class JpaRolloutCreate extends AbstractRolloutUpdateCreate<RolloutCreate>
}
private DistributionSet findDistributionSetAndThrowExceptionIfNotFound(final Long setId) {
final DistributionSet set = distributionSetManagement.findDistributionSetById(setId);
if (set == null) {
throw new EntityNotFoundException("Distribution set cannot be set as it does not exixt" + setId);
}
return set;
return distributionSetManagement.findDistributionSetById(setId)
.orElseThrow(() -> new EntityNotFoundException(DistributionSet.class, setId));
}
}

View File

@@ -39,12 +39,7 @@ public class JpaSoftwareModuleCreate extends AbstractSoftwareModuleUpdateCreate<
throw new ConstraintViolationException("type cannot be null");
}
final SoftwareModuleType smType = softwareManagement.findSoftwareModuleTypeByKey(type.trim());
if (smType == null) {
throw new EntityNotFoundException(type.trim());
}
return smType;
return softwareManagement.findSoftwareModuleTypeByKey(type.trim())
.orElseThrow(() -> new EntityNotFoundException(SoftwareModuleType.class, type.trim()));
}
}

View File

@@ -36,12 +36,8 @@ public class JpaTargetFilterQueryCreate extends AbstractTargetFilterQueryUpdateC
}
private DistributionSet findDistributionSetAndThrowExceptionIfNotFound(final Long setId) {
final DistributionSet set = distributionSetManagement.findDistributionSetById(setId);
if (set == null) {
throw new EntityNotFoundException("Distribution set cannot be set as it does not exixt" + setId);
}
return set;
return distributionSetManagement.findDistributionSetById(setId)
.orElseThrow(() -> new EntityNotFoundException(DistributionSet.class, setId));
}
}

View File

@@ -60,6 +60,7 @@ public class JpaAction extends AbstractJpaTenantAwareBaseEntity implements Actio
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "distribution_set", foreignKey = @ForeignKey(value = ConstraintMode.CONSTRAINT, name = "fk_action_ds"))
@NotNull
private JpaDistributionSet distributionSet;
@ManyToOne(fetch = FetchType.LAZY)

View File

@@ -268,7 +268,7 @@ public class JpaDistributionSet extends AbstractJpaNamedVersionedEntity implemen
checkTypeCompatability(softwareModule);
final Optional<SoftwareModule> found = modules.stream()
.filter(module -> module.getId().equals(softwareModule.getId())).findFirst();
.filter(module -> module.getId().equals(softwareModule.getId())).findAny();
if (found.isPresent()) {
return false;
@@ -279,7 +279,7 @@ public class JpaDistributionSet extends AbstractJpaNamedVersionedEntity implemen
if (allready >= softwareModule.getType().getMaxAssignments()) {
modules.stream().filter(module -> module.getType().getKey().equals(softwareModule.getType().getKey()))
.findFirst().map(modules::remove);
.findAny().map(modules::remove);
}
if (modules.add(softwareModule)) {
@@ -308,7 +308,7 @@ public class JpaDistributionSet extends AbstractJpaNamedVersionedEntity implemen
}
final Optional<SoftwareModule> found = modules.stream()
.filter(module -> module.getId().equals(softwareModule.getId())).findFirst();
.filter(module -> module.getId().equals(softwareModule.getId())).findAny();
if (found.isPresent()) {
modules.remove(found.get());

View File

@@ -170,7 +170,7 @@ public class JpaDistributionSetType extends AbstractJpaNamedEntity implements Di
// check if this was in the list before before
final Optional<DistributionSetTypeElement> existing = elements.stream()
.filter(element -> element.getSmType().getKey().equals(smType.getKey())).findFirst();
.filter(element -> element.getSmType().getKey().equals(smType.getKey())).findAny();
if (existing.isPresent()) {
existing.get().setMandatory(mandatory);
@@ -187,7 +187,7 @@ public class JpaDistributionSetType extends AbstractJpaNamedEntity implements Di
}
// we search by id (standard equals compares also revison)
elements.stream().filter(element -> element.getSmType().getId().equals(smTypeId)).findFirst()
elements.stream().filter(element -> element.getSmType().getId().equals(smTypeId)).findAny()
.ifPresent(elements::remove);
return this;

View File

@@ -235,7 +235,7 @@ public final class RSQLUtility {
private Optional<Join<Object, Object>> findCurrentJoinOfType(final Class<?> type) {
return getCurrentJoins().stream()
.filter(j -> type.equals(j.getJavaType())).findFirst();
.filter(j -> type.equals(j.getJavaType())).findAny();
}
private void addCurrentJoin(Join<Object, Object> join) {