Search in sources :

Example 21 with License

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

the class OutputGenerator method renderTemplateWithDefaultValues.

/**
 * Creates a velocity context and fills it with the default, commonly used
 * values:
 * <ul>
 * <li>allLicenseNamesWithTexts: list of {@link LicenseNameWithText} objects,
 * sorted by license name</li>
 * <li>licenseNameWithTextToReferenceId: map from the license name to a unique
 * id inside the file. May be used to reference a license.</li>
 * <li>licenseInfoResults: map of {@link LicenseInfoParsingResult} objects,
 * where the key is the name of the release. The licenses within the objects are
 * sorted by name. Contains only the results with status {@link LicenseInfoRequestStatus#SUCCESS}</li>
 * <li>licenseInfoErrorResults: map {@link List}of {@link LicenseInfoParsingResult} objects,
 * where the key is the name of the release. Contains only the results with status other than
 * {@link LicenseInfoRequestStatus#SUCCESS}. These results are not merged, that's why the map values are lists.</li>
 * <li>acknowledgments: map of acknowledgments for a release where the key is
 * the release and the value is a set of strings (acknowledgments)</li>
 * </ul>
 * The given file will be used as velocity template and will be rendered with
 * the described data.
 *
 * @param projectLicenseInfoResults
 *            parsing results to be rendered
 * @param file
 *            name of template file
 * @return rendered template
 */
protected String renderTemplateWithDefaultValues(Collection<LicenseInfoParsingResult> projectLicenseInfoResults, String file, String projectTitle, String licenseInfoHeaderText) {
    VelocityContext vc = getConfiguredVelocityContext();
    // set header
    vc.put(LICENSE_INFO_PROJECT_TITLE, projectTitle);
    vc.put(LICENSE_INFO_HEADER_TEXT, licenseInfoHeaderText);
    // sorted lists of all license to be displayed at the end of the file at once
    List<LicenseNameWithText> licenseNamesWithTexts = getSortedLicenseNameWithTexts(projectLicenseInfoResults);
    vc.put(ALL_LICENSE_NAMES_WITH_TEXTS, licenseNamesWithTexts);
    // assign a reference id to each license in order to only display references for
    // each release. The references will point to
    // the list with all details at the and of the file (see above)
    int referenceId = 1;
    Map<LicenseNameWithText, Integer> licenseToReferenceId = Maps.newHashMap();
    for (LicenseNameWithText licenseNamesWithText : licenseNamesWithTexts) {
        licenseToReferenceId.put(licenseNamesWithText, referenceId++);
    }
    vc.put(LICENSE_REFERENCE_ID_MAP_CONTEXT_PROPERTY, licenseToReferenceId);
    Map<Boolean, List<LicenseInfoParsingResult>> partitionedResults = projectLicenseInfoResults.stream().collect(Collectors.partitioningBy(r -> r.getStatus() == LicenseInfoRequestStatus.SUCCESS));
    List<LicenseInfoParsingResult> goodResults = partitionedResults.get(true);
    Map<String, List<LicenseInfoParsingResult>> badResultsPerRelease = partitionedResults.get(false).stream().collect(Collectors.groupingBy(this::getComponentLongName));
    vc.put(LICENSE_INFO_ERROR_RESULTS_CONTEXT_PROPERTY, badResultsPerRelease);
    // be sure that the licenses inside a release are sorted by id. This looks nicer
    SortedMap<String, LicenseInfoParsingResult> sortedLicenseInfos = getSortedLicenseInfos(goodResults);
    // this will effectively change the objects in the collection and therefore the
    // objects in the sorted map above
    sortLicenseNamesWithinEachLicenseInfoById(sortedLicenseInfos.values(), licenseToReferenceId);
    vc.put(LICENSE_INFO_RESULTS_CONTEXT_PROPERTY, sortedLicenseInfos);
    // also display acknowledgments
    SortedMap<String, Set<String>> acknowledgements = getSortedAcknowledgements(sortedLicenseInfos);
    vc.put(ACKNOWLEDGEMENTS_CONTEXT_PROPERTY, acknowledgements);
    StringWriter sw = new StringWriter();
    Velocity.mergeTemplate(file, "utf-8", vc, sw);
    IOUtils.closeQuietly(sw);
    return sw.toString();
}
Also used : SW360Utils(org.eclipse.sw360.datahandler.common.SW360Utils) java.util(java.util) StringWriter(java.io.StringWriter) RuntimeConstants(org.apache.velocity.runtime.RuntimeConstants) ClasspathResourceLoader(org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader) LicenseNameWithTextUtils(org.eclipse.sw360.licenseinfo.util.LicenseNameWithTextUtils) SW360Exception(org.eclipse.sw360.datahandler.thrift.SW360Exception) Maps(com.google.common.collect.Maps) VelocityContext(org.apache.velocity.VelocityContext) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) ToolManager(org.apache.velocity.tools.ToolManager) IOUtils(org.apache.commons.io.IOUtils) CommonUtils(org.eclipse.sw360.datahandler.common.CommonUtils) org.eclipse.sw360.datahandler.thrift.licenseinfo(org.eclipse.sw360.datahandler.thrift.licenseinfo) Velocity(org.apache.velocity.app.Velocity) NotNull(org.jetbrains.annotations.NotNull) FileResourceLoader(org.apache.velocity.runtime.resource.loader.FileResourceLoader) VelocityContext(org.apache.velocity.VelocityContext) StringWriter(java.io.StringWriter)

Example 22 with License

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

the class TestComponentClient method main.

public static void main(String[] args) throws TException, IOException {
    THttpClient thriftClient = new THttpClient("http://127.0.0.1:8080/components/thrift");
    TProtocol protocol = new TCompactProtocol(thriftClient);
    ComponentService.Iface client = new ComponentService.Client(protocol);
// List<Component> components = client.getComponentSummary(user);
// List<Component> recentComponents = client.getRecentComponents();
// List<Release> releases = client.getReleaseSummary(user);
// 
// System.out.println("Fetched " + components.size() + " components from license service");
// System.out.println("Fetched " + releases.size() + " releases from license service");
// System.out.println("Fetched " + recentComponents.size() + " recent components from license service");
// 
// String referenceId =null;
// for (Component component : recentComponents) {
// if(referenceId==null) referenceId=component.getId();
// System.out.println(component.getId() + ": " + component.getName());
// }
// 
// if(referenceId!=null) {
// System.out.println(client.getComponentById(referenceId, user).toString());
// Component component = new ComponentHandler("http://localhost:5984", "sw360db", "sw360attachments").getComponentById(referenceId, user);
// System.out.println(component.toString());
// System.out.println(client.getComponentById(referenceId, user).toString());
// }
// 
// for(Release release : releases) {
// System.out.println(release.toString());
// }
// // This fails with a thrift error... debug!
// if(releases.size()>0) {
// String releaseNameStart = releases.get(0).getName().substring(0,1);
// System.out.println("The following releases start with " + releaseNameStart );
// 
// List<Release> releases1 = client.searchReleaseByName(releaseNameStart);
// for(Release release : releases1) {
// System.out.println(release.toString());
// }
// 
// }
// final Component cpe = client.getComponentForReportFromCPEId("cpe");
// System.out.println(cpe.toString());
}
Also used : TProtocol(org.apache.thrift.protocol.TProtocol) THttpClient(org.apache.thrift.transport.THttpClient) ComponentService(org.eclipse.sw360.datahandler.thrift.components.ComponentService) TCompactProtocol(org.apache.thrift.protocol.TCompactProtocol) THttpClient(org.apache.thrift.transport.THttpClient)

Example 23 with License

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

the class LicenseHandlerTest method createTestEntries.

public void createTestEntries() throws MalformedURLException {
    // List of test objects
    licenses = new HashMap<>();
    todos = new HashMap<>();
    obligations = new HashMap<>();
    License license1 = new License();
    license1.setId("Apache 1.1");
    license1.setFullname("The Apache Software License, Version 1.1");
    license1.setLicenseType(new LicenseType().setLicenseTypeId(3).setType("Red - copyleft effect"));
    license1.addToRisks(new Risk().setRiskId(123123).setText("If Siemens uses this contractor pattern a long text follows here for reading and display... this might be long.").setCategory(new RiskCategory().setRiskCategoryId(32).setText("Beige")));
    license1.addToRisks(new Risk().setRiskId(1223).setText("Apache 1.1 is noy so risky").setCategory(new RiskCategory().setRiskCategoryId(3123).setText("Green")));
    license1.setReviewdate("10.10.2010");
    license1.addToTodoDatabaseIds("T1");
    license1.addToTodoDatabaseIds("T2");
    license1.addToTodoDatabaseIds("T5");
    licenses.put(license1.id, license1);
    License license2 = new License();
    license2.setId("Apache 2.0");
    license2.setFullname("The Apache Software License, Version 2.0");
    license2.setReviewdate("12.12.2012");
    license2.addToTodoDatabaseIds("T3");
    license2.addToTodoDatabaseIds("T4");
    licenses.put(license2.id, license2);
    Todo todo1 = new Todo().setId("T1").setText("You must include the acknowledgement as part of the documentation for the end user. An example looks as following:  This product includes software developed by the Apache Software Foundation (http://www.apache.org/).");
    todo1.addToObligationDatabaseIds("O1");
    todo1.addToObligationDatabaseIds("O2");
    Todo todo2 = new Todo().setId("T2").setText("You must not names listed in in the license at paragraph 4 (for example Apache and Apache Software Foundation) neither in the documentation nor for ads or marketing.");
    todo2.addToObligationDatabaseIds("O3");
    Todo todo3 = new Todo().setId("T3").setText("Then you must add the following sentence in the header of any modified/added file: 'Code modifications by Siemens AG are under Siemens license conditions'");
    Todo todo4 = new Todo().setId("T4").setText("You must include a prominent notice in the header of all modified files in the following form: © Siemens AG, [year]");
    todo4.addToObligationDatabaseIds("O1");
    todo4.addToObligationDatabaseIds("O4");
    Todo todo5 = new Todo().setId("T5").setText("With the Apache License 2.0,no copyleft effect for proprietary code exists. For proprietary Siemens modifications you can choose the license (meaning applying the Apache 2.0 license or any other license)");
    todo5.addToObligationDatabaseIds("O4");
    todos.put("T1", todo1);
    todos.put("T2", todo2);
    todos.put("T3", todo3);
    todos.put("T4", todo4);
    todos.put("T5", todo5);
    obligations.put("O1", new Obligation().setId("O1").setName("Provide acknowledgements in documentation"));
    obligations.put("O2", new Obligation().setId("O2").setName("Advertising materials are restricted subject to limitations"));
    obligations.put("O3", new Obligation().setId("O3").setName("Documentation that represent additional requirements in case of modifications (for example notice file with author's name)"));
    obligations.put("O4", new Obligation().setId("O4").setName("Apache Copyleft effect"));
    DatabaseConnector db = new DatabaseConnector(DatabaseSettings.getConfiguredHttpClient(), dbName);
    // Add obligations to database
    for (Obligation obligation : obligations.values()) {
        db.add(obligation);
    }
    // Add todos to database
    for (Todo todo : todos.values()) {
        db.add(todo);
    }
    // Finally, add the licenses to the database
    for (License license : licenses.values()) {
        db.add(license);
    }
}
Also used : DatabaseConnector(org.eclipse.sw360.datahandler.couchdb.DatabaseConnector)

Example 24 with License

use of org.eclipse.sw360.datahandler.thrift.licenses.License 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 25 with License

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

the class LicenseSummary method summary.

@Override
protected License summary(SummaryType type, License document) {
    // Copy required details
    License copy = new License();
    switch(type) {
        case EXPORT_SUMMARY:
            copyField(document, copy, _Fields.GPLV2_COMPAT);
            copyField(document, copy, _Fields.REVIEWDATE);
            copyField(document, copy, _Fields.RISKS);
        case SUMMARY:
            copyField(document, copy, _Fields.LICENSE_TYPE);
        default:
            copyField(document, copy, _Fields.ID);
            copy.setShortname(document.getId());
            copyField(document, copy, _Fields.FULLNAME);
            copyField(document, copy, _Fields.LICENSE_TYPE_DATABASE_ID);
    }
    return copy;
}
Also used : License(org.eclipse.sw360.datahandler.thrift.licenses.License)

Aggregations

User (org.eclipse.sw360.datahandler.thrift.users.User)20 TException (org.apache.thrift.TException)16 License (org.eclipse.sw360.datahandler.thrift.licenses.License)16 UsedAsLiferayAction (org.eclipse.sw360.portal.common.UsedAsLiferayAction)8 RequestStatus (org.eclipse.sw360.datahandler.thrift.RequestStatus)7 java.util (java.util)5 Collectors (java.util.stream.Collectors)5 Logger (org.apache.log4j.Logger)5 CommonUtils (org.eclipse.sw360.datahandler.common.CommonUtils)5 Project (org.eclipse.sw360.datahandler.thrift.projects.Project)5 CommonUtils.isTemporaryTodo (org.eclipse.sw360.datahandler.common.CommonUtils.isTemporaryTodo)4 SW360Exception (org.eclipse.sw360.datahandler.thrift.SW360Exception)4 Release (org.eclipse.sw360.datahandler.thrift.components.Release)4 Maps (com.google.common.collect.Maps)3 Sets (com.google.common.collect.Sets)3 XWPFRun (org.apache.poi.xwpf.usermodel.XWPFRun)3 CommonUtils.nullToEmptyString (org.eclipse.sw360.datahandler.common.CommonUtils.nullToEmptyString)3 Ternary (org.eclipse.sw360.datahandler.thrift.Ternary)3 LicenseService (org.eclipse.sw360.datahandler.thrift.licenses.LicenseService)3 ModerationRequest (org.eclipse.sw360.datahandler.thrift.moderation.ModerationRequest)3