aboutsummaryrefslogtreecommitdiffstats
path: root/openecomp-be/tools/zusammen-tools/src/main/java/org/openecomp/core/tools/Commands/importdata/ItemImport.java
blob: c7042743c2101d89c4516ce7dcb32283bd7c3a39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package org.openecomp.core.tools.Commands.importdata;

import com.amdocs.zusammen.datatypes.SessionContext;
import com.amdocs.zusammen.datatypes.item.Item;
import com.amdocs.zusammen.plugin.statestore.cassandra.dao.ItemDao;
import com.amdocs.zusammen.plugin.statestore.cassandra.dao.ItemDaoFactory;
import org.openecomp.core.utilities.json.JsonUtil;
import org.openecomp.sdc.logging.api.Logger;
import org.openecomp.sdc.logging.api.LoggerFactory;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static org.openecomp.core.tools.Commands.exportdata.ImportProperties.JSON_POSTFIX;

public class ItemImport {
    private static final Logger logger = LoggerFactory.getLogger(ItemImport.class);

    public void loadPath(SessionContext sessionContext, Path itemDir, String itemName) {
        try {
            Path itemPath = Paths.get(itemDir.toString() + File.separator + itemName + JSON_POSTFIX);
            if (!Files.exists(itemPath)) {
                return;
            }
            String itemJson = new String(Files.readAllBytes(itemPath));
            if (itemJson == null || itemJson.trim().isEmpty()) {
                return;
            }
            Item item = JsonUtil.json2Object(itemJson, Item.class);
            ItemDao itemDao = getItemDao(sessionContext);
            itemDao.create(sessionContext, item.getId(), item.getInfo(), item.getCreationTime());
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        }
    }

    private ItemDao getItemDao(SessionContext context) {
        return ItemDaoFactory.getInstance().createInterface(context);
    }
}