38d61e78d721309e0a4e33b23cc40c05849f0a0e
[ccsdk/cds.git] /
1 /*
2  * Copyright (C) 2019 Bell Canada.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.ccsdk.cds.blueprintsprocessor.functions.resource.resolution.db
17
18 import org.springframework.data.jpa.repository.JpaRepository
19 import org.springframework.data.jpa.repository.Query
20 import org.springframework.data.repository.query.Param
21 import org.springframework.stereotype.Repository
22 import javax.transaction.Transactional
23
24 @Repository
25 interface TemplateResolutionRepository : JpaRepository<TemplateResolution, String> {
26
27     fun findByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
28         resourceId: String,
29         resourceType: String,
30         blueprintName: String?,
31         blueprintVersion: String?,
32         artifactName: String,
33         occurrence: Int
34     ): TemplateResolution?
35
36     fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
37         key: String,
38         blueprintName: String?,
39         blueprintVersion: String?,
40         artifactName: String,
41         occurrence: Int
42     ): TemplateResolution?
43
44     @Query(
45         value = """
46          SELECT * FROM TEMPLATE_RESOLUTION WHERE resolution_key = :key 
47             AND blueprint_name = :blueprintName AND blueprint_version = :blueprintVersion 
48             AND artifact_name = :artifactName 
49             AND occurrence <=  :firstN
50         """,
51         nativeQuery = true
52     )
53     fun findFirstNOccurrences(
54         @Param("key")key: String,
55         @Param("blueprintName")blueprintName: String,
56         @Param("blueprintVersion")blueprintVersion: String,
57         @Param("artifactName")artifactName: String,
58         @Param("firstN")begin: Int
59     ): List<TemplateResolution>
60
61     @Query(
62         value = """
63         SELECT * FROM TEMPLATE_RESOLUTION WHERE resolution_key = :key 
64             AND blueprint_name = :blueprintName AND blueprint_version = :blueprintVersion 
65             AND artifact_name = :artifactName 
66             AND occurrence > ( 
67                 select max(occurrence) - :lastN from RESOURCE_RESOLUTION 
68                 WHERE resolution_key = :key 
69                     AND blueprint_name = :blueprintName AND blueprint_version = :blueprintVersion 
70                     AND artifact_name = :artifactName) 
71                     ORDER BY occurrence DESC, creation_date DESC
72       """,
73         nativeQuery = true
74     )
75     fun findLastNOccurrences(
76         @Param("key")key: String,
77         @Param("blueprintName")blueprintName: String,
78         @Param("blueprintVersion")blueprintVersion: String,
79         @Param("artifactName")artifactName: String,
80         @Param("lastN")begin: Int
81     ): List<TemplateResolution>
82
83     @Query(
84         value = """
85         SELECT * FROM TEMPLATE_RESOLUTION WHERE resolution_key = :key 
86             AND blueprint_name = :blueprintName AND blueprint_version = :blueprintVersion 
87             AND artifact_name = :artifactName 
88             AND occurrence BETWEEN :begin AND :end 
89             ORDER BY occurrence DESC, creation_date DESC
90        """,
91         nativeQuery = true
92     )
93     fun findOccurrencesWithinRange(
94         @Param("key")key: String,
95         @Param("blueprintName")blueprintName: String,
96         @Param("blueprintVersion")blueprintVersion: String,
97         @Param("artifactName")artifactName: String,
98         @Param("begin")begin: Int,
99         @Param("end")end: Int
100     ): List<TemplateResolution>
101
102     fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
103         resolutionKey: String,
104         blueprintName: String,
105         blueprintVersion: String,
106         artifactPrefix: String
107     ): List<TemplateResolution>
108
109     @Query(
110         "select tr.resolutionKey from TemplateResolution tr where tr.blueprintName = :blueprintName and tr.blueprintVersion = :blueprintVersion and tr.artifactName = :artifactName and tr.occurrence = :occurrence"
111     )
112     fun findResolutionKeysByBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
113         @Param("blueprintName") blueprintName: String?,
114         @Param("blueprintVersion") blueprintVersion: String?,
115         @Param("artifactName") artifactName: String,
116         @Param("occurrence") occurrence: Int
117     ): List<String>?
118
119     @Query(
120         "select tr.artifactName as artifactName, tr.resolutionKey as resolutionKey from TemplateResolution tr where tr.blueprintName = :blueprintName and tr.blueprintVersion = :blueprintVersion and tr.occurrence = :occurrence"
121     )
122     fun findArtifactNamesAndResolutionKeysByBlueprintNameAndBlueprintVersionAndOccurrence(
123         @Param("blueprintName") blueprintName: String?,
124         @Param("blueprintVersion") blueprintVersion: String?,
125         @Param("occurrence") occurrence: Int
126     ): List<TemplateResolutionSelector>?
127
128     @Transactional
129     fun deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
130         resourceId: String,
131         resourceType: String,
132         blueprintName: String?,
133         blueprintVersion: String?,
134         artifactName: String,
135         occurrence: Int
136     )
137
138     @Transactional
139     fun deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
140         key: String,
141         blueprintName: String?,
142         blueprintVersion: String?,
143         artifactName: String,
144         occurrence: Int
145     )
146 }