Search in sources :

Example 26 with CSVRecord

use of org.apache.commons.csv.CSVRecord in project sw360portal by sw360.

the class ConvertRecord method convertRelationalTable.

public static Map<String, Set<Integer>> convertRelationalTable(List<CSVRecord> records) {
    Map<String, Set<Integer>> map = new HashMap<>(records.size());
    for (CSVRecord record : records) {
        if (record.size() < 2)
            break;
        String mainId = record.get(0);
        int otherId = Integer.parseInt(record.get(1));
        if (map.containsKey(mainId)) {
            map.get(mainId).add(otherId);
        } else {
            Set<Integer> ids = new HashSet<>();
            ids.add(otherId);
            map.put(mainId, ids);
        }
    }
    return map;
}
Also used : CSVRecord(org.apache.commons.csv.CSVRecord)

Example 27 with CSVRecord

use of org.apache.commons.csv.CSVRecord in project sw360portal by sw360.

the class ConvertRecord method convertObligation.

public static List<Obligation> convertObligation(List<CSVRecord> records) {
    List<Obligation> list = new ArrayList<>(records.size());
    for (CSVRecord record : records) {
        if (record.size() < 2)
            break;
        String id = record.get(0);
        String name = record.get(1);
        Obligation obligation = new Obligation().setObligationId(Integer.parseInt(id)).setName(name);
        list.add(obligation);
    }
    return list;
}
Also used : CSVRecord(org.apache.commons.csv.CSVRecord)

Example 28 with CSVRecord

use of org.apache.commons.csv.CSVRecord in project sw360portal by sw360.

the class ConvertRecord method convertCustomProperties.

public static Map<String, Set<String>> convertCustomProperties(List<CSVRecord> records) {
    Map<String, Set<String>> resultProperties = new HashMap<>();
    for (CSVRecord record : records) {
        if (!isValidPropertyRecord(record)) {
            break;
        }
        String property = record.get(1).trim();
        String value = record.get(2).trim();
        addPropertyAndValueToMap(property, value, resultProperties);
    }
    return resultProperties;
}
Also used : CSVRecord(org.apache.commons.csv.CSVRecord)

Example 29 with CSVRecord

use of org.apache.commons.csv.CSVRecord in project sw360portal by sw360.

the class ConvertRecord method convertCustomPropertiesById.

public static Map<Integer, PropertyWithValue> convertCustomPropertiesById(List<CSVRecord> records) {
    Map<Integer, PropertyWithValue> resultPropertiesById = new HashMap<>();
    for (CSVRecord record : records) {
        if (!isValidPropertyRecord(record)) {
            break;
        }
        Integer id = Integer.parseInt(record.get(0));
        String property = record.get(1).trim();
        String value = record.get(2).trim();
        resultPropertiesById.put(id, new PropertyWithValue(property, value));
    }
    return resultPropertiesById;
}
Also used : CSVRecord(org.apache.commons.csv.CSVRecord)

Example 30 with CSVRecord

use of org.apache.commons.csv.CSVRecord in project sw360portal by sw360.

the class ConvertRecord method fillLicenses.

public static List<License> fillLicenses(List<CSVRecord> records, Map<Integer, LicenseType> licenseTypeMap, Map<Integer, Todo> todoMap, Map<Integer, Risk> riskMap, Map<String, Set<Integer>> licenseTodo, Map<String, Set<Integer>> licenseRisk) {
    List<License> licenses = new ArrayList<>(records.size());
    for (CSVRecord record : records) {
        if (record.size() < 7)
            break;
        String identifier = record.get(0);
        String fullname = record.get(1);
        License license = new License().setId(identifier).setFullname(fullname);
        String typeString = record.get(2);
        if (!Strings.isNullOrEmpty(typeString) && !"NULL".equals(typeString)) {
            Integer typeId = Integer.parseInt(typeString);
            LicenseType licenseType = licenseTypeMap.get(typeId);
            license.setLicenseType(licenseType);
        }
        String gplv2CompatString = record.get(3);
        if (!Strings.isNullOrEmpty(gplv2CompatString) && !"NULL".equals(gplv2CompatString)) {
            Ternary gplv2Compat = ThriftEnumUtils.stringToEnum(gplv2CompatString, Ternary.class);
            license.setGPLv2Compat(gplv2Compat);
        }
        String gplv3CompatString = record.get(4);
        if (!Strings.isNullOrEmpty(gplv3CompatString) && !"NULL".equals(gplv3CompatString)) {
            Ternary gplv3Compat = ThriftEnumUtils.stringToEnum(gplv3CompatString, Ternary.class);
            license.setGPLv3Compat(gplv3Compat);
        }
        String reviewdate = record.get(5);
        license.setReviewdate(ConvertUtil.parseDate(reviewdate));
        String text = record.get(6);
        license.setText(text);
        if (record.size() > 7) {
            String externalLink = record.get(7);
            license.setExternalLicenseLink(externalLink);
        }
        if (record.size() > 8) {
            Optional.ofNullable(record.get(8)).map(json -> gson.fromJson(json, new TypeToken<Map<String, String>>() {
            }.getType())).map(o -> (Map<String, String>) o).ifPresent(license::setExternalIds);
        }
        // Add all risks
        Set<Integer> riskIds = licenseRisk.get(identifier);
        if (riskIds != null) {
            for (int riskId : riskIds) {
                Risk risk = riskMap.get(riskId);
                if (risk != null) {
                    license.addToRiskDatabaseIds(risk.getId());
                }
            }
        }
        // Add all todos
        Set<Integer> todoIds = licenseTodo.get(identifier);
        if (todoIds != null) {
            for (int todoId : todoIds) {
                Todo todo = todoMap.get(todoId);
                if (todo != null) {
                    license.addToTodoDatabaseIds(todo.getId());
                }
            }
        }
        licenses.add(license);
    }
    return licenses;
}
Also used : java.util(java.util) Function(com.google.common.base.Function) TypeToken(com.google.gson.reflect.TypeToken) User(org.eclipse.sw360.datahandler.thrift.users.User) CSVRecord(org.apache.commons.csv.CSVRecord) TException(org.apache.thrift.TException) GsonBuilder(com.google.gson.GsonBuilder) Strings(com.google.common.base.Strings) CommonUtils(org.eclipse.sw360.datahandler.common.CommonUtils) Logger(org.apache.log4j.Logger) Gson(com.google.gson.Gson) ThriftEnumUtils(org.eclipse.sw360.datahandler.common.ThriftEnumUtils) Ternary(org.eclipse.sw360.datahandler.thrift.Ternary) org.eclipse.sw360.datahandler.thrift.licenses(org.eclipse.sw360.datahandler.thrift.licenses) NotNull(org.jetbrains.annotations.NotNull) com.google.common.collect(com.google.common.collect) CommonUtils.isNullEmptyOrWhitespace(org.eclipse.sw360.datahandler.common.CommonUtils.isNullEmptyOrWhitespace) Ternary(org.eclipse.sw360.datahandler.thrift.Ternary) CSVRecord(org.apache.commons.csv.CSVRecord)

Aggregations

CSVRecord (org.apache.commons.csv.CSVRecord)96 CSVParser (org.apache.commons.csv.CSVParser)47 IOException (java.io.IOException)26 ArrayList (java.util.ArrayList)23 CSVFormat (org.apache.commons.csv.CSVFormat)23 StringReader (java.io.StringReader)19 FileReader (java.io.FileReader)15 Test (org.junit.Test)13 InputStreamReader (java.io.InputStreamReader)12 PreparedStatement (java.sql.PreparedStatement)10 InputStream (java.io.InputStream)9 Reader (java.io.Reader)9 ResultSet (java.sql.ResultSet)9 HashMap (java.util.HashMap)9 PhoenixConnection (org.apache.phoenix.jdbc.PhoenixConnection)9 CSVCommonsLoader (org.apache.phoenix.util.CSVCommonsLoader)9 File (java.io.File)6 Map (java.util.Map)6 User (org.eclipse.sw360.datahandler.thrift.users.User)6 FileNotFoundException (java.io.FileNotFoundException)5