Add message prioritization module
[ccsdk/cds.git] / ms / blueprintsprocessor / functions / message-prioritizaion / src / main / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / functions / message / prioritization / db / MessagePrioritizationRepositories.kt
1 /*
2  * Copyright © 2018-2019 AT&T Intellectual Property.
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
17 package org.onap.ccsdk.cds.blueprintsprocessor.functions.message.prioritization.db
18
19 import org.springframework.data.domain.Pageable
20 import org.springframework.data.jpa.repository.JpaRepository
21 import org.springframework.data.jpa.repository.Modifying
22 import org.springframework.data.jpa.repository.Query
23 import org.springframework.stereotype.Repository
24 import org.springframework.transaction.annotation.Transactional
25 import java.util.*
26
27 @Repository
28 @Transactional(readOnly = true)
29 interface PrioritizationMessageRepository : JpaRepository<MessagePrioritization, String> {
30
31     @Query("FROM MessagePrioritization pm WHERE pm.group = :group ORDER BY pm.createdDate asc")
32     fun findByGroup(group: String, count: Pageable): List<MessagePrioritization>?
33
34     @Query("FROM MessagePrioritization pm WHERE pm.group = :group AND pm.state in :states " +
35             "ORDER BY pm.createdDate asc")
36     fun findByGroupAndStateIn(group: String, states: List<String>, count: Pageable): List<MessagePrioritization>?
37
38     @Query("FROM MessagePrioritization pm WHERE pm.group = :group AND pm.state in :states " +
39             "ORDER BY pm.updatedDate asc")
40     fun findByGroupAndStateInOrderByUpdatedDate(group: String, states: List<String>, count: Pageable)
41             : List<MessagePrioritization>?
42
43     @Query("FROM MessagePrioritization pm WHERE pm.group = :group AND pm.state in :states " +
44             "AND pm.expiryDate > :expiryCheckDate ORDER BY pm.createdDate asc")
45     fun findByGroupAndStateInAndNotExpiredDate(group: String, states: List<String>, expiryCheckDate: Date,
46                                                count: Pageable): List<MessagePrioritization>?
47
48     @Query("FROM MessagePrioritization pm WHERE pm.state in :states " +
49             "AND pm.expiryDate < :expiryCheckDate ORDER BY pm.createdDate asc")
50     fun findByStateInAndExpiredDate(states: List<String>, expiryCheckDate: Date,
51                                     count: Pageable): List<MessagePrioritization>?
52
53     @Query("FROM MessagePrioritization pm WHERE pm.group = :group AND pm.state in :states " +
54             "AND pm.expiryDate < :expiryCheckDate ORDER BY pm.createdDate asc")
55     fun findByGroupAndStateInAndExpiredDate(group: String, states: List<String>, expiryCheckDate: Date,
56                                             count: Pageable): List<MessagePrioritization>?
57
58     @Query("FROM MessagePrioritization pm WHERE pm.group = :group " +
59             "AND pm.expiryDate < :expiryCheckDate ORDER BY pm.createdDate asc")
60     fun findByByGroupAndExpiredDate(group: String, expiryCheckDate: Date, count: Pageable): List<MessagePrioritization>?
61
62     @Query("FROM MessagePrioritization pm WHERE pm.group = :group AND pm.state in :states " +
63             "AND pm.correlationId = :correlationId ORDER BY pm.createdDate asc")
64     fun findByGroupAndCorrelationId(group: String, states: List<String>, correlationId: String)
65             : List<MessagePrioritization>?
66
67     @Query("FROM MessagePrioritization pm WHERE pm.group = :group AND pm.state in :states " +
68             "AND pm.type in :types AND pm.correlationId = :correlationId ORDER BY pm.createdDate asc")
69     fun findByGroupAndTypesAndCorrelationId(group: String, states: List<String>, types: List<String>,
70                                             correlationId: String): List<MessagePrioritization>?
71
72     @Modifying
73     @Transactional
74     @Query("UPDATE MessagePrioritization pm SET pm.state = :state WHERE pm.id = :id")
75     fun setStatusForMessageId(id: String, state: String): Int
76
77     @Modifying
78     @Transactional
79     @Query("UPDATE MessagePrioritization pm SET pm.state = :state WHERE pm.id IN :ids")
80     fun setStatusForMessageIds(ids: List<String>, state: String): Int
81
82     @Modifying
83     @Transactional
84     @Query("UPDATE MessagePrioritization pm SET pm.aggregatedMessageIds = :aggregatedMessageIds " +
85             "WHERE pm.id = :id")
86     fun setAggregatedMessageIds(id: String, aggregatedMessageIds: String): Int
87
88     @Modifying
89     @Transactional
90     @Query("DELETE FROM MessagePrioritization pm WHERE pm.group = :group")
91     fun deleteGroup(group: String)
92
93     @Modifying
94     @Transactional
95     @Query("DELETE FROM MessagePrioritization pm WHERE pm.group = :group AND pm.state IN :states")
96     fun deleteGroupAndStateIn(group: String, states: List<String>)
97 }
98