-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add solution data model and endpoints (#224)
* fix deleting file * fix mime type when creating a file * add solution data model and endpoints * add solutionType to solution --------- Co-authored-by: mbeisel <beiselmn@gmail.com>
- Loading branch information
Showing
8 changed files
with
570 additions
and
3 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
org.planqk.atlas.core/src/main/java/org/planqk/atlas/core/model/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 the qc-atlas contributors. | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*******************************************************************************/ | ||
|
||
package org.planqk.atlas.core.model; | ||
|
||
import java.util.UUID; | ||
import javax.persistence.Entity; | ||
import javax.persistence.FetchType; | ||
import javax.persistence.JoinColumn; | ||
import javax.persistence.JoinTable; | ||
import javax.persistence.OneToOne; | ||
|
||
import lombok.Data; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.NoArgsConstructor; | ||
|
||
/** | ||
* Entity representing a solution. | ||
*/ | ||
@NoArgsConstructor | ||
@EqualsAndHashCode(callSuper = true) | ||
@Data | ||
@Entity | ||
public class Solution extends KnowledgeArtifact { | ||
|
||
private UUID patternId; | ||
|
||
@OneToOne(fetch = FetchType.LAZY, orphanRemoval = true) | ||
@EqualsAndHashCode.Exclude | ||
@JoinTable( | ||
name = "SolutionFile", | ||
joinColumns = @JoinColumn(name = "solution_id"), | ||
inverseJoinColumns = @JoinColumn(name = "file_id") | ||
) | ||
private File file; | ||
|
||
private String solutionType; | ||
} |
36 changes: 36 additions & 0 deletions
36
org.planqk.atlas.core/src/main/java/org/planqk/atlas/core/repository/SolutionRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2024 the qc-atlas contributors. | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*******************************************************************************/ | ||
|
||
package org.planqk.atlas.core.repository; | ||
|
||
import java.util.UUID; | ||
|
||
import org.planqk.atlas.core.model.Solution; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.data.rest.core.annotation.RepositoryRestResource; | ||
import org.springframework.stereotype.Repository; | ||
|
||
/** | ||
* Repository to access {@link Solution}s available in the database. | ||
*/ | ||
@Repository | ||
@RepositoryRestResource(exported = false) | ||
public interface SolutionRepository extends JpaRepository<Solution, UUID> { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
org.planqk.atlas.core/src/main/java/org/planqk/atlas/core/services/SolutionService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020 the qc-atlas contributors. | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*******************************************************************************/ | ||
|
||
package org.planqk.atlas.core.services; | ||
|
||
import java.util.UUID; | ||
|
||
import org.planqk.atlas.core.model.File; | ||
import org.planqk.atlas.core.model.Implementation; | ||
import org.planqk.atlas.core.model.Solution; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
/** | ||
* Service class for operations related to interacting and modifying {@link Solution}s in the database. | ||
*/ | ||
public interface SolutionService { | ||
|
||
/** | ||
* Creates a new database entry for a given {@link Solution} and save it to the database. | ||
* <p> | ||
* The ID of the {@link Solution} parameter should be null, since the ID will be generated by the database when | ||
* creating the entry. The validation for this is done by the Controller layer, which will reject {@link Solution}s | ||
* with a given ID in its create path. | ||
* | ||
* @param solution The {@link Solution} object describing the properties of a solution that should be saved to | ||
* the database | ||
* @return The {@link Solution} object that represents the saved status from the database | ||
*/ | ||
@Transactional | ||
Solution create(Solution solution); | ||
|
||
/** | ||
* Retrieve multiple {@link Solution} entries from the database. | ||
* <p> | ||
* The amount of entries is based on the given {@link Pageable} parameter. If the {@link Pageable} is unpaged a | ||
* {@link Page} with all entries is queried. | ||
* <p> | ||
* If no search should be executed the search parameter can be left null or empty. | ||
* | ||
* @param pageable The page information, namely page size and page number, of the page we want to retrieve | ||
* @return The page of queried {@link Solution} entries | ||
*/ | ||
Page<Solution> findAll(Pageable pageable); | ||
|
||
/** | ||
* Find a database entry of a {@link Solution} that is already saved in the database. This search is based on the | ||
* ID the database has given the {@link Solution} object when it was created and first saved to the database. | ||
* <p> | ||
* If there is no entry found in the database this method will throw a {@link java.util.NoSuchElementException}. | ||
* | ||
* @param solutionId The ID of the {@link Solution} we want to find | ||
* @return The {@link Solution} with the given ID | ||
*/ | ||
Solution findById(UUID solutionId); | ||
|
||
/** | ||
* Update an existing {@link Solution} database entry by saving the updated {@link Solution} object to the | ||
* database. | ||
* <p> | ||
* The ID of the {@link Solution} parameter has to be set to the ID of the database entry we want to update. The | ||
* validation for this ID to be set is done by the Controller layer, which will reject {@link Solution}s without a | ||
* given ID in its update path. This ID will be used to query the existing {@link Solution} entry we want to | ||
* update. If no {@link Solution} entry with the given ID is found this method will throw a {@link | ||
* java.util.NoSuchElementException}. | ||
* | ||
* @param solution The {@link Solution} we want to update with its updated properties | ||
* @return the updated {@link Solution} object that represents the updated status of the database | ||
*/ | ||
@Transactional | ||
Solution update(Solution solution); | ||
|
||
/** | ||
* Delete an existing {@link Solution} entry from the database. This deletion is based on the ID the database has | ||
* given the {@link Solution} when it was created and first saved to the database. | ||
* <p> | ||
* When deleting an {@link Solution} the file referenced by the {@link Solution} will be deleted together | ||
* with it. | ||
* If no entry with the given ID is found this method will throw a {@link java.util.NoSuchElementException}. | ||
* | ||
* @param solutionId The ID of the {@link Solution} we want to delete | ||
*/ | ||
@Transactional | ||
void delete(UUID solutionId); | ||
|
||
/** | ||
* Creates a {@link File} entry in the database from a multipartfile and links it to a given {@link | ||
* Solution}. | ||
* | ||
* @param solutionId The ID of the {@link Solution} we want the {@link File} to be linked. | ||
* @param multipartFile The multipart from which we want to create a File entity and link it to the {@link | ||
* Solution} | ||
* @return The created and linked {@link File} | ||
*/ | ||
File addFileToSolution(UUID solutionId, MultipartFile multipartFile); | ||
} |
94 changes: 94 additions & 0 deletions
94
org.planqk.atlas.core/src/main/java/org/planqk/atlas/core/services/SolutionServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2020-2021 the qc-atlas contributors. | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*******************************************************************************/ | ||
|
||
package org.planqk.atlas.core.services; | ||
|
||
import java.util.UUID; | ||
|
||
import org.planqk.atlas.core.model.File; | ||
import org.planqk.atlas.core.model.Solution; | ||
import org.planqk.atlas.core.repository.SolutionRepository; | ||
import org.planqk.atlas.core.util.ServiceUtils; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.NonNull; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Service | ||
@AllArgsConstructor | ||
public class SolutionServiceImpl implements SolutionService { | ||
|
||
private final SolutionRepository solutionRepository; | ||
|
||
private final FileService fileService; | ||
|
||
@Override | ||
@Transactional | ||
public Solution create(Solution solution) { | ||
return solutionRepository.save(solution); | ||
} | ||
|
||
@Override | ||
public Page<Solution> findAll(@NonNull Pageable pageable) { | ||
return solutionRepository.findAll(pageable); | ||
} | ||
|
||
@Override | ||
public Solution findById(@NonNull UUID solutionId) { | ||
return ServiceUtils.findById(solutionId, Solution.class, solutionRepository); | ||
} | ||
|
||
@Override | ||
@Transactional | ||
public Solution update(@NonNull Solution solution) { | ||
final Solution persistedAlgorithm = findById(solution.getId()); | ||
persistedAlgorithm.setPatternId(solution.getPatternId()); | ||
|
||
return solutionRepository.save(persistedAlgorithm); | ||
} | ||
|
||
@Override | ||
@Transactional | ||
public void delete(@NonNull UUID solutionId) { | ||
final Solution algorithm = findById(solutionId); | ||
|
||
if (algorithm.getFile() != null) { | ||
fileService.delete(algorithm.getFile().getId()); | ||
} | ||
|
||
solutionRepository.deleteById(solutionId); | ||
} | ||
|
||
@Override | ||
public File addFileToSolution(UUID solutionId, MultipartFile multipartFile) { | ||
final Solution solution = | ||
ServiceUtils.findById(solutionId, Solution.class, solutionRepository); | ||
final File file = fileService.create(multipartFile); | ||
solution.setFile(file); | ||
solutionRepository.save(solution); | ||
|
||
return file; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.