package com.raychenon.licensedroid;

import com.raychenon.licensedroid.license.LicenseInfos;

/**
 * @author Raymond Chenon
 */
public class OpenSourceTransformer {

    public OpenSourceTransformer() {
    }

    public OpenSourceModel transform(final OpenSource openSource) {
        return new OpenSourceModel(openSource.getProjectName(), openSource.getAuthor(), constructLicense(openSource));
    }

    private OpenSourceModel.License constructLicense(final OpenSource openSource) {
        return new OpenSourceModel.License(extractName(openSource), extractLicenseText(openSource), extractFullDescription(openSource));
    }

    private String extractLicenseText(final OpenSource openSource) {
        if (openSource.isLicenseText) {
            return openSource.getLicenseText();
        } else {
            LicenseInfos sourceLicense = openSource.getLicense();
            if (sourceLicense.hasCopyrightFormat()) {
                return sourceLicense.getNiceCopyright(sourceLicense.getYear(), openSource.getAuthor()) + "\n" + sourceLicense.getLicenseText();
            } else {
                return sourceLicense.getLicenseText();
            }
        }
    }

    private String extractName(final OpenSource openSource) {
        if (openSource.getLicense() == null) {
            // custom license
            int endIndex = openSource.getLicenseText().indexOf(" ", 20);
            return openSource.getLicenseText().substring(0, endIndex) + " ...";
        }
        return openSource.getLicense().getName();
    }

    private String extractFullDescription(final OpenSource openSource) {
        if (openSource.getLicense() != null && openSource.getLicense().hasLongerLicenseText()) {
            return openSource.getLicense().getLongerLicenseText();
        }
        return openSource.getLicenseText();
    }

}

@@ -527,24 +527,31 @@
 ense(extract
+License
 Name(openSou
@@ -587,32 +587,39 @@
 Source), extract
+License
 FullDescription(
@@ -1210,16 +1210,23 @@
  extract
+License
 Name(fin
@@ -1576,16 +1576,23 @@
  extract
+License
 FullDesc

rename methods internally