Reformat openecomp-be
[sdc.git] / openecomp-be / tools / zusammen-tools / src / main / java / org / openecomp / core / tools / store / HealingHandler.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.openecomp.core.tools.store;
21
22 import com.datastax.driver.core.ResultSet;
23 import com.datastax.driver.mapping.annotations.Accessor;
24 import com.datastax.driver.mapping.annotations.Query;
25 import java.util.List;
26 import org.openecomp.core.nosqldb.api.NoSqlDb;
27 import org.openecomp.core.nosqldb.factory.NoSqlDbFactory;
28 import org.openecomp.core.tools.store.zusammen.datatypes.HealingEntity;
29
30 /**
31  * Created by ayalaben on 10/15/2017
32  */
33 public class HealingHandler {
34
35     private static NoSqlDb nnoSqlDb = NoSqlDbFactory.getInstance().createInterface();
36     private static HealingAccessor accessor = nnoSqlDb.getMappingManager().createAccessor(HealingAccessor.class);
37
38     public void populateHealingTable(List<HealingEntity> healingEntities) {
39         healingEntities.forEach(healingEntity -> {
40             if (isHealingRecordExist(healingEntity)) {
41                 accessor.updateFlag(healingEntity.isHealingFlag(), healingEntity.getSpace(), healingEntity.getItemId(), healingEntity.getVersionId());
42             } else {
43                 accessor.create(healingEntity.getSpace(), healingEntity.getItemId(), healingEntity.getVersionId(), healingEntity.isHealingFlag(),
44                     healingEntity.getOldVersion());
45             }
46         });
47     }
48
49     private boolean isHealingRecordExist(HealingEntity healingEntity) {
50         return accessor.getFlag(healingEntity.getSpace(), healingEntity.getItemId(), healingEntity.getVersionId()).getAvailableWithoutFetching() == 1;
51     }
52
53     @Accessor
54     interface HealingAccessor {
55
56         @Query("SELECT healing_needed FROM healing WHERE space=? AND item_id=? AND version_id=?")
57         ResultSet getFlag(String space, String itemId, String versionId);
58
59         @Query("Insert into healing (space, item_id, version_id, healing_needed, old_version) " + "values (?,?,?,?,?)")
60         void create(String space, String itemId, String versionId, boolean flag, String oldVersion);
61
62         @Query("UPDATE healing SET healing_needed=? WHERE space=? AND item_id=? AND version_id=?")
63         void updateFlag(boolean flag, String space, String itemId, String versionId);
64     }
65 }