0d8946ece0272fc12a40a18b3e6827547e5e88c2
[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     @Query(
28         value = """
29         SELECT * FROM TEMPLATE_RESOLUTION
30              WHERE resource_type = :resourceType AND resource_id = :resourceId
31              AND blueprint_name = :blueprintName AND blueprint_version = :blueprintVersion
32              AND artifact_name = :artifactName AND occurrence = :occurrence
33              ORDER BY creation_date DESC LIMIT 1
34         """,
35         nativeQuery = true
36     )
37     fun findByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
38         resourceId: String,
39         resourceType: String,
40         blueprintName: String?,
41         blueprintVersion: String?,
42         artifactName: String,
43         occurrence: Int
44     ): TemplateResolution?
45
46     @Query(
47         value = """
48         SELECT * FROM TEMPLATE_RESOLUTION WHERE resolution_key = :key 
49              AND blueprint_name = :blueprintName AND blueprint_version = :blueprintVersion 
50              AND artifact_name = :artifactName AND occurrence = :occurrence
51              ORDER BY creation_date DESC LIMIT 1
52         """,
53         nativeQuery = true
54     )
55     fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
56         key: String,
57         blueprintName: String?,
58         blueprintVersion: String?,
59         artifactName: String,
60         occurrence: Int
61     ): TemplateResolution?
62
63     @Query(
64         value = """
65          SELECT * FROM TEMPLATE_RESOLUTION WHERE resolution_key = :key 
66             AND blueprint_name = :blueprintName AND blueprint_version = :blueprintVersion 
67             AND artifact_name = :artifactName 
68             AND occurrence <=  :firstN
69         """,
70         nativeQuery = true
71     )
72     fun findFirstNOccurrences(
73         @Param("key")key: String,
74         @Param("blueprintName")blueprintName: String,
75         @Param("blueprintVersion")blueprintVersion: String,
76         @Param("artifactName")artifactName: String,
77         @Param("firstN")begin: Int
78     ): List<TemplateResolution>
79
80     @Query(
81         value = """
82         SELECT * FROM TEMPLATE_RESOLUTION WHERE resolution_key = :key 
83             AND blueprint_name = :blueprintName AND blueprint_version = :blueprintVersion 
84             AND artifact_name = :artifactName 
85             AND occurrence > ( 
86                 select max(occurrence) - :lastN from RESOURCE_RESOLUTION 
87                 WHERE resolution_key = :key 
88                     AND blueprint_name = :blueprintName AND blueprint_version = :blueprintVersion 
89                     AND artifact_name = :artifactName) 
90                     ORDER BY occurrence DESC, creation_date DESC
91       """,
92         nativeQuery = true
93     )
94     fun findLastNOccurrences(
95         @Param("key")key: String,
96         @Param("blueprintName")blueprintName: String,
97         @Param("blueprintVersion")blueprintVersion: String,
98         @Param("artifactName")artifactName: String,
99         @Param("lastN")begin: Int
100     ): List<TemplateResolution>
101
102     @Query(
103         value = """
104         SELECT * FROM TEMPLATE_RESOLUTION WHERE resolution_key = :key 
105             AND blueprint_name = :blueprintName AND blueprint_version = :blueprintVersion 
106             AND artifact_name = :artifactName 
107             AND occurrence BETWEEN :begin AND :end 
108             ORDER BY occurrence DESC, creation_date DESC
109        """,
110         nativeQuery = true
111     )
112     fun findOccurrencesWithinRange(
113         @Param("key")key: String,
114         @Param("blueprintName")blueprintName: String,
115         @Param("blueprintVersion")blueprintVersion: String,
116         @Param("artifactName")artifactName: String,
117         @Param("begin")begin: Int,
118         @Param("end")end: Int
119     ): List<TemplateResolution>
120
121     fun findByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactName(
122         resolutionKey: String,
123         blueprintName: String,
124         blueprintVersion: String,
125         artifactPrefix: String
126     ): List<TemplateResolution>
127
128     @Query(
129         "select tr.resolutionKey from TemplateResolution tr where tr.blueprintName = :blueprintName and tr.blueprintVersion = :blueprintVersion and tr.artifactName = :artifactName and tr.occurrence = :occurrence"
130     )
131     fun findResolutionKeysByBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
132         @Param("blueprintName") blueprintName: String?,
133         @Param("blueprintVersion") blueprintVersion: String?,
134         @Param("artifactName") artifactName: String,
135         @Param("occurrence") occurrence: Int
136     ): List<String>?
137
138     @Query(
139         "select tr.artifactName as artifactName, tr.resolutionKey as resolutionKey from TemplateResolution tr where tr.blueprintName = :blueprintName and tr.blueprintVersion = :blueprintVersion and tr.occurrence = :occurrence"
140     )
141     fun findArtifactNamesAndResolutionKeysByBlueprintNameAndBlueprintVersionAndOccurrence(
142         @Param("blueprintName") blueprintName: String?,
143         @Param("blueprintVersion") blueprintVersion: String?,
144         @Param("occurrence") occurrence: Int
145     ): List<TemplateResolutionSelector>?
146
147     @Transactional
148     fun deleteByResourceIdAndResourceTypeAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
149         resourceId: String,
150         resourceType: String,
151         blueprintName: String?,
152         blueprintVersion: String?,
153         artifactName: String,
154         occurrence: Int
155     )
156
157     @Transactional
158     fun deleteByResolutionKeyAndBlueprintNameAndBlueprintVersionAndArtifactNameAndOccurrence(
159         key: String,
160         blueprintName: String?,
161         blueprintVersion: String?,
162         artifactName: String,
163         occurrence: Int
164     )
165 }