f0310723cb18098a21a14318a1460cd1020572cb
[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.stereotype.Component;
42
43 import static org.jooq.impl.DSL.exists;
44 import static org.jooq.impl.DSL.field;
45 import static org.jooq.impl.DSL.select;
46 import static org.jooq.impl.DSL.table;
47 import static org.oran.smo.teiv.utils.TiesConstants.QUOTED_STRING;
48
49 @Component
50 public class DeleteClassifiersOperation extends ClassifiersOperation {
51
52     public DeleteClassifiersOperation(DSLContext readDataDslContext, DSLContext writeDataDslContext) {
53         super(readDataDslContext, writeDataDslContext);
54     }
55
56     @Override
57     protected List<OperationResult> performOperation(final PersistableIdMap map, final List<String> consumerData,
58             final DSLContext writeDataDslContext) {
59
60         final Map<String, OperationResult> allResults = new HashMap<>();
61
62         map.persistableWithIds().forEach((persistable, ids) -> {
63
64             for (String data : consumerData) {
65                 final String classifiersColumnName = String.format(QUOTED_STRING, persistable.getClassifiersColumnName());
66                 final Field<JSONB> targetField = field(classifiersColumnName, JSONB.class);
67                 final Field<JSONB> concatField = field(String.format("%s - '%s'", classifiersColumnName, data),
68                         JSONB.class);
69
70                 final UpdateResultStep<Record> update = writeDataDslContext.update(table(persistable.getTableName())).set(
71                         targetField, concatField).where(existsCondition(classifiersColumnName, data)).and(field(persistable
72                                 .getIdColumnNameWithTableName()).in(ids)).returning(field(persistable
73                                         .getIdColumnNameWithTableName()), field(String.format(QUOTED_STRING, persistable
74                                                 .getClassifiersColumnName()), JSONB.class));
75
76                 final Result<Record> newResults = update.fetch();
77                 final Map<String, OperationResult> newOpResults = createOperationResults(persistable, newResults);
78                 allResults.putAll(newOpResults);
79             }
80         });
81
82         return new ArrayList<>(allResults.values());
83     }
84
85     private Condition existsCondition(final String classifiersColumnName, final String data) {
86         return exists(select(field("1")).from(table("jsonb_array_elements_text(" + classifiersColumnName + ")").as(
87                 "element")).where(field("element").eq(data)));
88     }
89
90     /**
91      * Executes the operation for the given consumer data for specific entity type.
92      *
93      * @param consumerData
94      *     which holds the information for classifiers/decorators, entity ids and relationship ids
95      * @param entityType
96      *     entity type
97      */
98     public List<OperationResult> delete(final ConsumerData<List<String>> consumerData, EntityType entityType) {
99         PersistableIdMap persistableIdMap = PersistableIdMap.builder().persistableWithIds(Map.of(entityType, consumerData
100                 .entityIds())).build();
101
102         final List<OperationResult> results = new ArrayList<>();
103         writeDataDslContext.transaction((Configuration config) -> results.addAll(performOperation(persistableIdMap,
104                 consumerData.data(), config.dsl())));
105         return results;
106     }
107
108     /**
109      * Executes the operation for the given consumer data for specific relation type.
110      *
111      * @param consumerData
112      *     which holds the information for classifiers/decorators, entity ids and relationship ids
113      * @param relationType
114      *     relation type
115      */
116     public List<OperationResult> delete(final ConsumerData<List<String>> consumerData, RelationType relationType) {
117         PersistableIdMap persistableIdMap = PersistableIdMap.builder().persistableWithIds(Map.of(relationType, consumerData
118                 .relationshipIds())).build();
119
120         final List<OperationResult> results = new ArrayList<>();
121         writeDataDslContext.transaction((Configuration config) -> results.addAll(performOperation(persistableIdMap,
122                 consumerData.data(), config.dsl())));
123         return results;
124     }
125 }