eb66562f57bff8a85aa1385b167faf8550147822
[smo/teiv.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2024 Ericsson
4  *  Modifications Copyright (C) 2024 OpenInfra Foundation Europe
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21 package org.oran.smo.teiv.exposure.consumerdata.operation;
22
23 import java.util.Map;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.ArrayList;
27
28 import org.jooq.Configuration;
29 import org.jooq.Condition;
30 import org.jooq.DSLContext;
31 import org.jooq.Field;
32 import org.jooq.JSONB;
33 import org.jooq.Record;
34 import org.jooq.Result;
35 import org.jooq.UpdateResultStep;
36 import org.oran.smo.teiv.exposure.consumerdata.model.ConsumerData;
37 import org.oran.smo.teiv.exposure.consumerdata.model.PersistableIdMap;
38 import org.oran.smo.teiv.schema.EntityType;
39 import org.oran.smo.teiv.schema.RelationType;
40 import org.oran.smo.teiv.service.models.OperationResult;
41 import org.springframework.context.annotation.Profile;
42 import org.springframework.stereotype.Component;
43
44 import static org.jooq.impl.DSL.exists;
45 import static org.jooq.impl.DSL.field;
46 import static org.jooq.impl.DSL.select;
47 import static org.jooq.impl.DSL.table;
48 import static org.jooq.impl.DSL.val;
49 import static org.oran.smo.teiv.utils.TiesConstants.QUOTED_STRING;
50
51 @Component
52 @Profile("exposure")
53 public class DeleteClassifiersOperation extends ClassifiersOperation {
54
55     public DeleteClassifiersOperation(DSLContext readDataDslContext, DSLContext writeDataDslContext) {
56         super(readDataDslContext, writeDataDslContext);
57     }
58
59     @Override
60     protected List<OperationResult> performOperation(final PersistableIdMap map, final List<String> consumerData,
61             final DSLContext writeDataDslContext) {
62
63         final Map<String, OperationResult> allResults = new HashMap<>();
64
65         map.persistableWithIds().forEach((persistable, ids) -> {
66
67             for (String data : consumerData) {
68                 final String classifiersColumnName = String.format(QUOTED_STRING, persistable.getClassifiersColumnName());
69                 final Field<JSONB> targetField = field(classifiersColumnName, JSONB.class);
70                 final Field<JSONB> concatField = field(String.format("%s - '%s'", classifiersColumnName, data),
71                         JSONB.class);
72
73                 final UpdateResultStep<Record> update = writeDataDslContext.update(table(persistable.getTableName())).set(
74                         targetField, concatField).where(existsCondition(classifiersColumnName, data)).and(field(persistable
75                                 .getIdColumnNameWithTableName()).in(ids)).returning(field(persistable
76                                         .getIdColumnNameWithTableName()), field(String.format(QUOTED_STRING, persistable
77                                                 .getClassifiersColumnName()), JSONB.class));
78
79                 final Result<Record> newResults = update.fetch();
80                 final Map<String, OperationResult> newOpResults = createOperationResults(persistable, newResults);
81                 allResults.putAll(newOpResults);
82             }
83         });
84
85         return new ArrayList<>(allResults.values());
86     }
87
88     private Condition existsCondition(final String classifiersColumnName, final String data) {
89         return exists(select(field("1")).from(table("jsonb_array_elements_text(" + field(classifiersColumnName) + ")").as(
90                 "element")).where(field("element").eq(val(data))));
91     }
92
93     /**
94      * Executes the operation for the given consumer data for specific entity type.
95      *
96      * @param consumerData
97      *     which holds the information for classifiers/decorators, entity ids and relationship ids
98      * @param entityType
99      *     entity type
100      */
101     public List<OperationResult> delete(final ConsumerData<List<String>> consumerData, EntityType entityType) {
102         PersistableIdMap persistableIdMap = PersistableIdMap.builder().persistableWithIds(Map.of(entityType, consumerData
103                 .entityIds())).build();
104
105         final List<OperationResult> results = new ArrayList<>();
106         writeDataDslContext.transaction((Configuration config) -> results.addAll(performOperation(persistableIdMap,
107                 consumerData.data(), config.dsl())));
108         return results;
109     }
110
111     /**
112      * Executes the operation for the given consumer data for specific relation type.
113      *
114      * @param consumerData
115      *     which holds the information for classifiers/decorators, entity ids and relationship ids
116      * @param relationType
117      *     relation type
118      */
119     public List<OperationResult> delete(final ConsumerData<List<String>> consumerData, RelationType relationType) {
120         PersistableIdMap persistableIdMap = PersistableIdMap.builder().persistableWithIds(Map.of(relationType, consumerData
121                 .relationshipIds())).build();
122
123         final List<OperationResult> results = new ArrayList<>();
124         writeDataDslContext.transaction((Configuration config) -> results.addAll(performOperation(persistableIdMap,
125                 consumerData.data(), config.dsl())));
126         return results;
127     }
128 }