Refactor Management interfaces: find/get pattern (#2609)

Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2025-08-15 16:18:32 +03:00
committed by GitHub
parent fa4dea75a3
commit b4edde8cc3
100 changed files with 713 additions and 986 deletions

View File

@@ -9,8 +9,6 @@
*/
package org.eclipse.hawkbit.repository;
import java.util.Optional;
import jakarta.validation.ConstraintViolationException;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
@@ -19,8 +17,6 @@ import jakarta.validation.constraints.NotNull;
import org.eclipse.hawkbit.im.authentication.SpPermission;
import org.eclipse.hawkbit.repository.artifact.model.DbArtifact;
import org.eclipse.hawkbit.im.authentication.SpringEvalExpressions;
import org.eclipse.hawkbit.repository.exception.ArtifactDeleteFailedException;
import org.eclipse.hawkbit.repository.exception.ArtifactUploadFailedException;
import org.eclipse.hawkbit.repository.exception.EntityAlreadyExistsException;
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
import org.eclipse.hawkbit.repository.exception.InvalidMD5HashException;
@@ -28,8 +24,6 @@ import org.eclipse.hawkbit.repository.exception.InvalidSHA1HashException;
import org.eclipse.hawkbit.repository.model.Artifact;
import org.eclipse.hawkbit.repository.model.ArtifactUpload;
import org.eclipse.hawkbit.repository.model.SoftwareModule;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.security.access.prepost.PreAuthorize;
/**
@@ -42,12 +36,6 @@ public interface ArtifactManagement extends PermissionSupport {
return SpPermission.SOFTWARE_MODULE;
}
/**
* @return the total amount of local artifacts stored in the artifact management
*/
@PreAuthorize(SpringEvalExpressions.HAS_READ_REPOSITORY)
long count();
/**
* Persists artifact binary as provided by given InputStream. assign the
* artifact in addition to given {@link SoftwareModule}.
@@ -56,7 +44,7 @@ public interface ArtifactManagement extends PermissionSupport {
* @return uploaded {@link Artifact}
* @throws EntityNotFoundException if given software module does not exist
* @throws EntityAlreadyExistsException if File with that name already exists in the Software Module
* @throws ArtifactUploadFailedException if upload fails with internal server errors
* @throws org.eclipse.hawkbit.repository.artifact.exception.ArtifactUploadFailedException if upload fails with internal server errors
* @throws InvalidMD5HashException if check against provided MD5 checksum failed
* @throws InvalidSHA1HashException if check against provided SHA1 checksum failed
* @throws ConstraintViolationException if {@link ArtifactUpload} contains invalid values
@@ -64,75 +52,6 @@ public interface ArtifactManagement extends PermissionSupport {
@PreAuthorize(SpringEvalExpressions.HAS_CREATE_REPOSITORY)
Artifact create(@NotNull @Valid ArtifactUpload artifactUpload);
/**
* Deletes {@link Artifact} based on given id.
*
* @param id of the {@link Artifact} that has to be deleted.
* @throws ArtifactDeleteFailedException if deletion failed (MongoDB is not available)
* @throws EntityNotFoundException if artifact with given ID does not exist
*/
@PreAuthorize(SpringEvalExpressions.HAS_DELETE_REPOSITORY)
void delete(long id);
/**
* Searches for {@link Artifact} with given {@link Identifiable}.
*
* @param id to search for
* @return found {@link Artifact}
*/
@PreAuthorize(SpringEvalExpressions.HAS_READ_REPOSITORY + " or " + SpringEvalExpressions.IS_CONTROLLER)
Optional<Artifact> get(long id);
/**
* Find by artifact by software module id and filename.
*
* @param filename file name
* @param softwareModuleId software module id.
* @return found {@link Artifact}
* @throws EntityNotFoundException if software module with given ID does not exist
*/
@PreAuthorize(SpringEvalExpressions.HAS_READ_REPOSITORY + " or " + SpringEvalExpressions.IS_CONTROLLER)
Optional<Artifact> getByFilenameAndSoftwareModule(@NotNull String filename, long softwareModuleId);
/**
* Find all local artifact by sha1 and return the first artifact.
*
* @param sha1 the sha1
* @return the first local artifact
*/
@PreAuthorize(SpringEvalExpressions.HAS_READ_REPOSITORY + " or " + SpringEvalExpressions.IS_CONTROLLER)
Optional<Artifact> findFirstBySHA1(@NotNull String sha1);
/**
* Searches for {@link Artifact} with given file name.
*
* @param filename to search for
* @return found List of {@link Artifact}s.
*/
@PreAuthorize(SpringEvalExpressions.HAS_READ_REPOSITORY + " or " + SpringEvalExpressions.IS_CONTROLLER)
Optional<Artifact> getByFilename(@NotNull String filename);
/**
* Get local artifact for a base software module.
*
* @param softwareModuleId software module id
* @param pageable Pageable parameter
* @return Page<Artifact>
* @throws EntityNotFoundException if software module with given ID does not exist
*/
@PreAuthorize(SpringEvalExpressions.HAS_READ_REPOSITORY)
Page<Artifact> findBySoftwareModule(long softwareModuleId, @NotNull Pageable pageable);
/**
* Count local artifacts for a base software module.
*
* @param softwareModuleId software module id
* @return count by software module
* @throws EntityNotFoundException if software module with given ID does not exist
*/
@PreAuthorize(SpringEvalExpressions.HAS_READ_REPOSITORY)
long countBySoftwareModule(long softwareModuleId);
/**
* Loads {@link DbArtifact} from store for given {@link Artifact}.
*
@@ -142,6 +61,14 @@ public interface ArtifactManagement extends PermissionSupport {
* @return loaded {@link DbArtifact}
*/
@PreAuthorize("hasAuthority('" + SpPermission.DOWNLOAD_REPOSITORY_ARTIFACT + "')" + " or " + SpringEvalExpressions.IS_CONTROLLER)
Optional<DbArtifact> loadArtifactBinary(@NotEmpty String sha1Hash, long softwareModuleId,
final boolean isEncrypted);
DbArtifact loadArtifactBinary(@NotEmpty String sha1Hash, long softwareModuleId, final boolean isEncrypted);
/**
* Deletes {@link Artifact} based on given id.
*
* @param id of the {@link Artifact} that has to be deleted.
* @throws EntityNotFoundException if artifact with given ID does not exist
*/
@PreAuthorize(SpringEvalExpressions.HAS_DELETE_REPOSITORY)
void delete(long id);
}

View File

@@ -268,7 +268,7 @@ public interface ControllerManagement {
* @see Target#getControllerId()
*/
@PreAuthorize(SpringEvalExpressions.IS_CONTROLLER + " or " + SpringEvalExpressions.IS_SYSTEM_CODE)
Optional<Target> getByControllerId(@NotEmpty String controllerId);
Optional<Target> findByControllerId(@NotEmpty String controllerId);
/**
* Finds {@link Target} based on given ID returns found Target without details, i.e.
@@ -280,7 +280,7 @@ public interface ControllerManagement {
* @see Target#getId()
*/
@PreAuthorize(SpringEvalExpressions.IS_CONTROLLER + " or " + SpringEvalExpressions.IS_SYSTEM_CODE)
Optional<Target> get(long targetId);
Optional<Target> find(long targetId);
/**
* Retrieves the specified number of messages from action history of the given {@link Action} based on messageCount. Regardless of the

View File

@@ -37,7 +37,6 @@ import org.eclipse.hawkbit.repository.exception.RSQLParameterSyntaxException;
import org.eclipse.hawkbit.repository.exception.RSQLParameterUnsupportedFieldException;
import org.eclipse.hawkbit.repository.exception.UnsupportedSoftwareModuleForThisDistributionSetException;
import org.eclipse.hawkbit.repository.model.DistributionSet;
import org.eclipse.hawkbit.repository.model.DistributionSetFilter;
import org.eclipse.hawkbit.repository.model.DistributionSetTag;
import org.eclipse.hawkbit.repository.model.DistributionSetType;
import org.eclipse.hawkbit.repository.model.NamedEntity;
@@ -46,7 +45,6 @@ import org.eclipse.hawkbit.repository.model.SoftwareModule;
import org.eclipse.hawkbit.repository.model.Statistic;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.security.access.prepost.PreAuthorize;
/**
@@ -62,7 +60,7 @@ public interface DistributionSetManagement<T extends DistributionSet>
/**
* Find {@link DistributionSet} based on given ID including (lazy loaded) details, e.g. {@link DistributionSet#getModules()}. <br/>
* For performance reasons it is recommended to use {@link #get(long)} if the details are not required.
* For performance reasons it is recommended to use {@link #find(long)} if the details are not required.
*
* @param id to look for.
* @return {@link DistributionSet}
@@ -70,16 +68,6 @@ public interface DistributionSetManagement<T extends DistributionSet>
@PreAuthorize(HAS_READ_REPOSITORY)
Optional<T> getWithDetails(long id);
/**
* Find distribution set by id and throw an exception if it is (soft) deleted.
*
* @param id id of {@link DistributionSet}
* @return the found valid {@link DistributionSet}
* @throws EntityNotFoundException if distribution set with given ID does not exist
*/
@PreAuthorize(HAS_READ_REPOSITORY)
T getOrElseThrowException(long id);
@PreAuthorize(HAS_READ_REPOSITORY)
boolean shouldLockImplicitly(final DistributionSet distributionSet);

View File

@@ -11,6 +11,7 @@ package org.eclipse.hawkbit.repository;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import jakarta.validation.ConstraintViolationException;
@@ -26,7 +27,6 @@ import org.eclipse.hawkbit.repository.exception.RSQLParameterUnsupportedFieldExc
import org.eclipse.hawkbit.repository.model.BaseEntity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.security.access.prepost.PreAuthorize;
/**
@@ -58,6 +58,16 @@ public interface RepositoryManagement<T extends BaseEntity, C, U extends Identif
@PreAuthorize(SpringEvalExpressions.HAS_CREATE_REPOSITORY)
List<T> create(@NotNull @Valid Collection<C> create);
/**
* Retrieve {@link BaseEntity} and throws exception if not found.
*
* @param id to search for
* @return {@link BaseEntity} in the repository with given {@link BaseEntity#getId()}
* @throws EntityNotFoundException if no entity with given ID exists
*/
@PreAuthorize(SpringEvalExpressions.HAS_READ_REPOSITORY)
T get(long id);
/**
* Retrieve {@link BaseEntity}
*
@@ -65,16 +75,26 @@ public interface RepositoryManagement<T extends BaseEntity, C, U extends Identif
* @return {@link BaseEntity} in the repository with given {@link BaseEntity#getId()}
*/
@PreAuthorize(SpringEvalExpressions.HAS_READ_REPOSITORY)
Optional<T> get(long id);
Optional<T> find(long id);
/**
* Retrieves all {@link BaseEntity}s without details.
* Retrieves {@link BaseEntity}s by id and throws exception if any of the requested entities are not found.
*
* @param ids the ids to for
* @return the found {@link BaseEntity}s
* @throws EntityNotFoundException if at least one of the given ids does not exist
*/
@PreAuthorize(SpringEvalExpressions.HAS_READ_REPOSITORY)
List<T> get(@NotEmpty Collection<Long> ids);
/**
* Retrieves {@link BaseEntity}s by id and skips not found.
*
* @param ids the ids to for
* @return the found {@link BaseEntity}s
*/
@PreAuthorize(SpringEvalExpressions.HAS_READ_REPOSITORY)
List<T> get(@NotEmpty Collection<Long> ids);
List<T> find(@NotEmpty Collection<Long> ids);
/**
* Retrieves {@link Page} of all {@link BaseEntity} of given type.
@@ -134,6 +154,18 @@ public interface RepositoryManagement<T extends BaseEntity, C, U extends Identif
@PreAuthorize(SpringEvalExpressions.HAS_UPDATE_REPOSITORY)
T update(@NotNull @Valid U update);
/**
* Updates existing {@link BaseEntity}s.
*
* @param update bean with properties of the object to update
* @return updated entity map -> key is the ID of the entity, value is the updated entity
* @throws EntityReadOnlyException if the {@link BaseEntity} cannot be updated (e.g. is already in use)
* @throws EntityNotFoundException in case the {@link BaseEntity} does not exist and cannot be updated
* @throws ConstraintViolationException if fields are not filled as specified. Check {@link BaseEntity} for field constraints.
*/
@PreAuthorize(SpringEvalExpressions.HAS_UPDATE_REPOSITORY)
Map<Long, T> update(@NotNull @Valid Collection<U> update);
/**
* Deletes or marks as delete in case the {@link BaseEntity} is in use.
*

View File

@@ -235,18 +235,16 @@ public interface RolloutManagement extends PermissionSupport {
* Retrieves a specific rollout by its ID.
*
* @param rolloutId the ID of the rollout to retrieve
* @return the founded rollout or {@code null} if rollout with given ID does
* not exists
* @return the found rollout or empty if rollout with given ID does not exist
*/
@PreAuthorize(SpringEvalExpressions.HAS_READ_REPOSITORY)
Optional<Rollout> get(long rolloutId);
Optional<Rollout> find(long rolloutId);
/**
* Retrieves a specific rollout by its name.
*
* @param rolloutName the name of the rollout to retrieve
* @return the founded rollout or {@code null} if rollout with given name
* does not exists
* @return the found rollout or empty if rollout with given ID does not exist
*/
@PreAuthorize(SpringEvalExpressions.HAS_READ_REPOSITORY)
Optional<Rollout> getByName(@NotEmpty String rolloutName);

View File

@@ -1,58 +0,0 @@
/**
* Copyright (c) 2021 Bosch.IO GmbH and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.hawkbit.repository.exception;
import java.io.Serial;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.eclipse.hawkbit.exception.AbstractServerRtException;
import org.eclipse.hawkbit.exception.SpServerError;
import org.eclipse.hawkbit.repository.model.SoftwareModule;
/**
* Exception indicating that an artifact's binary does not exist anymore. This
* might be caused due to the soft deletion of a {@link SoftwareModule}.
*/
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class ArtifactBinaryNoLongerExistsException extends AbstractServerRtException {
@Serial
private static final long serialVersionUID = 1L;
private static final SpServerError THIS_ERROR = SpServerError.SP_ARTIFACT_BINARY_DELETED;
/**
* Creates a new ArtifactBinaryGoneException error.
*/
public ArtifactBinaryNoLongerExistsException() {
super(THIS_ERROR);
}
/**
* Creates a new ArtifactBinaryGoneException error with cause.
*
* @param cause for the exception
*/
public ArtifactBinaryNoLongerExistsException(final Throwable cause) {
super(THIS_ERROR, cause);
}
/**
* Creates a new ArtifactBinaryGoneException error with message.
*
* @param message of the error
*/
public ArtifactBinaryNoLongerExistsException(final String message) {
super(message, THIS_ERROR);
}
}

View File

@@ -1,47 +0,0 @@
/**
* Copyright (c) 2015 Bosch Software Innovations GmbH and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.hawkbit.repository.exception;
import java.io.Serial;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.eclipse.hawkbit.exception.AbstractServerRtException;
import org.eclipse.hawkbit.exception.SpServerError;
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public final class ArtifactBinaryNotFoundException extends AbstractServerRtException {
@Serial
private static final long serialVersionUID = 1L;
/**
* Creates a new FileUploadFailedException with
* {@link SpServerError#SP_ARTIFACT_LOAD_FAILED} error.
*/
public ArtifactBinaryNotFoundException() {
super(SpServerError.SP_ARTIFACT_LOAD_FAILED);
}
/**
* @param cause for the exception
*/
public ArtifactBinaryNotFoundException(final Throwable cause) {
super(SpServerError.SP_ARTIFACT_LOAD_FAILED, cause);
}
/**
* @param message of the error
*/
public ArtifactBinaryNotFoundException(final String message) {
super(message, SpServerError.SP_ARTIFACT_LOAD_FAILED);
}
}

View File

@@ -1,50 +0,0 @@
/**
* Copyright (c) 2015 Bosch Software Innovations GmbH and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.hawkbit.repository.exception;
import java.io.Serial;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.eclipse.hawkbit.exception.AbstractServerRtException;
import org.eclipse.hawkbit.exception.SpServerError;
/**
* Thrown if artifact deletion failed.
*/
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public final class ArtifactDeleteFailedException extends AbstractServerRtException {
@Serial
private static final long serialVersionUID = 1L;
/**
* Creates a new FileUploadFailedException with
* {@link SpServerError#SP_ARTIFACT_DELETE_FAILED} error.
*/
public ArtifactDeleteFailedException() {
super(SpServerError.SP_ARTIFACT_DELETE_FAILED);
}
/**
* @param cause for the exception
*/
public ArtifactDeleteFailedException(final Throwable cause) {
super(SpServerError.SP_ARTIFACT_DELETE_FAILED, cause);
}
/**
* @param message of the error
*/
public ArtifactDeleteFailedException(final String message) {
super(message, SpServerError.SP_ARTIFACT_DELETE_FAILED);
}
}

View File

@@ -1,56 +0,0 @@
/**
* Copyright (c) 2015 Bosch Software Innovations GmbH and others
*
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.hawkbit.repository.exception;
import java.io.Serial;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.eclipse.hawkbit.exception.AbstractServerRtException;
import org.eclipse.hawkbit.exception.SpServerError;
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public final class ArtifactUploadFailedException extends AbstractServerRtException {
@Serial
private static final long serialVersionUID = 1L;
/**
* Creates a new FileUploadFailedException with
* {@link SpServerError#SP_ARTIFACT_UPLOAD_FAILED} error.
*/
public ArtifactUploadFailedException() {
super(SpServerError.SP_ARTIFACT_UPLOAD_FAILED);
}
/**
* @param cause for the exception
*/
public ArtifactUploadFailedException(final Throwable cause) {
super(SpServerError.SP_ARTIFACT_UPLOAD_FAILED, cause);
}
/**
* @param message of the error
*/
public ArtifactUploadFailedException(final String message) {
super(message, SpServerError.SP_ARTIFACT_UPLOAD_FAILED);
}
/**
* @param message for the error
* @param cause of the error
*/
public ArtifactUploadFailedException(final String message, final Throwable cause) {
super(message, SpServerError.SP_ARTIFACT_UPLOAD_FAILED, cause);
}
}