Search in sources :

Example 11 with Todo

use of org.eclipse.sw360.datahandler.thrift.licenses.Todo 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)

Example 12 with Todo

use of org.eclipse.sw360.datahandler.thrift.licenses.Todo in project sw360portal by sw360.

the class ConvertRecordTest method prepare.

@Before
public void prepare() {
    emptyRecord = readToRecord(" ");
    String tooSmall = "header\n 1, prop";
    tooSmallRecord = readToRecord(tooSmall);
    String emptyProp = "header\n 1,,val";
    emptyPropRecord = readToRecord(emptyProp);
    String whiteProp = "header\n 1,  ,val";
    whitePropRecord = readToRecord(whiteProp);
    String emptyValue = "header\n 1,prop,";
    emptyValRecord = readToRecord(emptyValue);
    String whiteValue = "header\n 1,prop, ";
    whiteValRecord = readToRecord(whiteValue);
    String smallestPossible = "headers\n 1,prop1,val1";
    smallestPossibleRecord = readToRecord(smallestPossible);
    String full = "headers\n 1,A,A1 \n 2,A,A2 \n 3,A,A3 \n 4,B,B1 \n 5,B,B2";
    fullRecord = readToRecord(full);
    String fullTodo = "header \n '1','text1','true','true' \n '2','text2','false','false', '{\"key\": \"value\"}'";
    fullTodoRecord = readToRecord(fullTodo);
    Map<String, String> todoCustomProperties1 = new HashMap<>();
    todoCustomProperties1.put("A", "A1");
    Todo todo1 = new Todo().setTodoId(1).setCustomPropertyToValue(todoCustomProperties1);
    Map<String, String> todoCustomProperties2 = new HashMap<>();
    todoCustomProperties2.put("A", "A2");
    todoCustomProperties2.put("C", "C2");
    Todo todo2 = new Todo().setTodoId(2).setCustomPropertyToValue(todoCustomProperties2);
    todos = Arrays.asList(todo1, todo2);
}
Also used : Todo(org.eclipse.sw360.datahandler.thrift.licenses.Todo) Before(org.junit.Before)

Example 13 with Todo

use of org.eclipse.sw360.datahandler.thrift.licenses.Todo in project sw360portal by sw360.

the class AttachmentContentWrapperTest method testUpdateNonMetadataTouchesAllFields.

public void testUpdateNonMetadataTouchesAllFields() throws Exception {
    AttachmentContent source;
    source = new AttachmentContent();
    source.setFilename("a");
    source.setType("b");
    source.setContentType("v");
    source.setPartsCount("1");
    // TODO this is not required !
    source.setRemoteUrl("uskt");
    AttachmentContentWrapper attachmentContentWrapper = new AttachmentContentWrapper();
    attachmentContentWrapper.updateNonMetadata(source);
    assertTFields(source, attachmentContentWrapper, AttachmentContentWrapper.class, AttachmentContent._Fields.class);
}
Also used : AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)

Example 14 with Todo

use of org.eclipse.sw360.datahandler.thrift.licenses.Todo in project sw360portal by sw360.

the class TypeMappings method updateTodoMapWithCustomPropertiesAndWriteToDatabase.

@NotNull
public static Map<Integer, Todo> updateTodoMapWithCustomPropertiesAndWriteToDatabase(LicenseService.Iface licenseClient, Map<Integer, Todo> todoMap, Map<Integer, PropertyWithValue> customPropertiesMap, Map<Integer, Set<Integer>> todoPropertiesMap, User user) throws TException {
    for (Integer todoId : todoPropertiesMap.keySet()) {
        Todo todo = todoMap.get(todoId);
        if (!todo.isSetCustomPropertyToValue()) {
            todo.setCustomPropertyToValue(new HashMap<>());
        }
        for (Integer propertyWithValueId : todoPropertiesMap.get(todoId)) {
            PropertyWithValue propertyWithValue = customPropertiesMap.get(propertyWithValueId);
            todo.getCustomPropertyToValue().put(propertyWithValue.getProperty(), propertyWithValue.getValue());
        }
    }
    if (todoMap.values().size() > 0) {
        final List<Todo> addedTodos = licenseClient.addTodos(todoMap.values().stream().collect(Collectors.toList()), user);
        if (addedTodos != null) {
            final ImmutableMap<Integer, Todo> addedTodoMap = Maps.uniqueIndex(addedTodos, getTodoIdentifier());
            todoMap.putAll(addedTodoMap);
        }
    }
    return todoMap;
}
Also used : PropertyWithValue(org.eclipse.sw360.commonIO.ConvertRecord.PropertyWithValue) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with Todo

use of org.eclipse.sw360.datahandler.thrift.licenses.Todo in project sw360portal by sw360.

the class AttachmentStreamConnector method getAttachmentBundleStream.

/**
 * It is highly recommended to close this stream after using to avoid connection leak
 */
public <T> InputStream getAttachmentBundleStream(Set<AttachmentContent> attachments, User user, T context) throws IOException, SW360Exception {
    assertNotNull(context);
    PipedInputStream in = new PipedInputStream();
    PipedOutputStream out = new PipedOutputStream(in);
    new Thread(() -> {
        byte[] buffer = new byte[1024];
        int length;
        try (ZipOutputStream zip = new ZipOutputStream(out)) {
            for (AttachmentContent attachment : attachments) {
                // TODO: handle attachments with equal name
                ZipEntry zipEntry = new ZipEntry(attachment.getFilename());
                zip.putNextEntry(zipEntry);
                try (InputStream attachmentStream = getAttachmentStream(attachment, user, context)) {
                    while ((length = attachmentStream.read(buffer)) >= 0) {
                        zip.write(buffer, 0, length);
                    }
                } catch (TException e) {
                    log.error("failed to get AttachmentStream, maybe due to permission problems", e);
                }
                zip.closeEntry();
            }
        } catch (IOException e) {
            log.error("failed to write zip stream", e);
        }
    }).start();
    return in;
}
Also used : TException(org.apache.thrift.TException) ZipOutputStream(java.util.zip.ZipOutputStream) ConcatClosingInputStream(org.eclipse.sw360.datahandler.common.ConcatClosingInputStream) PipedInputStream(java.io.PipedInputStream) AttachmentInputStream(org.ektorp.AttachmentInputStream) InputStream(java.io.InputStream) ZipEntry(java.util.zip.ZipEntry) AttachmentContent(org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) IOException(java.io.IOException)

Aggregations

User (org.eclipse.sw360.datahandler.thrift.users.User)7 TException (org.apache.thrift.TException)6 CommonUtils.isTemporaryTodo (org.eclipse.sw360.datahandler.common.CommonUtils.isTemporaryTodo)6 AttachmentContent (org.eclipse.sw360.datahandler.thrift.attachments.AttachmentContent)4 Strings (com.google.common.base.Strings)3 java.util (java.util)3 CSVRecord (org.apache.commons.csv.CSVRecord)3 Logger (org.apache.log4j.Logger)3 CommonUtils (org.eclipse.sw360.datahandler.common.CommonUtils)3 ThriftEnumUtils (org.eclipse.sw360.datahandler.common.ThriftEnumUtils)3 Ternary (org.eclipse.sw360.datahandler.thrift.Ternary)3 Attachment (org.eclipse.sw360.datahandler.thrift.attachments.Attachment)3 org.eclipse.sw360.datahandler.thrift.licenses (org.eclipse.sw360.datahandler.thrift.licenses)3 Todo (org.eclipse.sw360.datahandler.thrift.licenses.Todo)3 UsedAsLiferayAction (org.eclipse.sw360.portal.common.UsedAsLiferayAction)3 NotNull (org.jetbrains.annotations.NotNull)3 Function (com.google.common.base.Function)2 com.google.common.collect (com.google.common.collect)2 Gson (com.google.gson.Gson)2 GsonBuilder (com.google.gson.GsonBuilder)2