841bcf539a8ef82d5e7a643fd6175f9d1b893611
[ccsdk/cds.git] / ms / error-catalog / services / src / main / kotlin / org / onap / ccsdk / error / catalog / services / ErrorCatalogDBService.kt
1 /*
2  *  Copyright © 2020 IBM, Bell Canada.
3  *  Modifications Copyright © 2019-2020 AT&T Intellectual Property.
4  *
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17
18 package org.onap.ccsdk.error.catalog.services
19
20 import org.onap.ccsdk.error.catalog.core.ErrorMessageLibConstants
21 import org.onap.ccsdk.error.catalog.services.domain.Domain
22 import org.onap.ccsdk.error.catalog.services.domain.ErrorMessageModel
23 import org.onap.ccsdk.error.catalog.services.repository.DomainRepository
24 import org.onap.ccsdk.error.catalog.services.repository.ErrorMessageModelRepository
25 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
26 import org.springframework.data.domain.Page
27 import org.springframework.data.domain.Pageable
28 import org.springframework.stereotype.Service
29
30 @Service
31 @ConditionalOnProperty(
32     name = [ErrorMessageLibConstants.ERROR_CATALOG_TYPE],
33     havingValue = ErrorMessageLibConstants.ERROR_CATALOG_TYPE_DB
34 )
35 open class ErrorCatalogDBService(
36     private val domainRepository: DomainRepository,
37     private val errorMessageModelRepository: ErrorMessageModelRepository
38 ) {
39
40     /**
41      * This is a getAllDomains method to retrieve all the Domain in Error Catalog Database by pages
42      *
43      * @return Page<Domain> list of the domains by page
44     </Domain> */
45     open fun getAllDomains(pageRequest: Pageable): Page<Domain> {
46         return domainRepository.findAll(pageRequest)
47     }
48
49     /**
50      * This is a getAllDomains method to retrieve all the Domain in Error Catalog Database
51      *
52      * @return List<Domain> list of the domains
53     </Domain> */
54     open fun getAllDomains(): List<Domain> {
55         return domainRepository.findAll()
56     }
57
58     /**
59      * This is a getAllDomainsByApplication method to retrieve all the Domain that belong to an application in Error Catalog Database
60      *
61      * @return List<Domain> list of the domains
62     </Domain> */
63     open fun getAllDomainsByApplication(applicationId: String): List<Domain> {
64         return domainRepository.findAllByApplicationId(applicationId)
65     }
66
67     /**
68      * This is a getAllErrorMessagesByApplication method to retrieve all the Messages that belong to an application in Error Catalog Database
69      *
70      * @return MutableMap<String, ErrorCode> list of the abstractErrorModel
71     </Domain> */
72     open fun getAllErrorMessagesByApplication(applicationId: String): MutableMap<String, ErrorMessageModel> {
73         val domains = domainRepository.findAllByApplicationId(applicationId)
74         val errorMessages = mutableMapOf<String, ErrorMessageModel>()
75         for (domain in domains) {
76             val errorMessagesFound = errorMessageModelRepository.findByDomainsId(domain.id)
77             for (errorMessageFound in errorMessagesFound) {
78                 errorMessages["${domain.name}$MESSAGE_KEY_SEPARATOR${errorMessageFound.messageID}"] = errorMessageFound
79             }
80         }
81         return errorMessages
82     }
83
84     open fun saveDomain(
85         domain: String,
86         applicationId: String,
87         description: String = "",
88         errorMessageList: List<ErrorMessageModel>
89     ) {
90         val domainModel = Domain(domain, applicationId, description)
91         domainModel.errorMessages.addAll(errorMessageList)
92         domainRepository.saveAndFlush(domainModel)
93     }
94
95     companion object {
96         private const val MESSAGE_KEY_SEPARATOR = "."
97     }
98 }