All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lionbridge.content.sdk.models.templates.GenerateQuoteTemplate Maven / Gradle / Ivy

There is a newer version: 2.2.0
Show newest version
package com.lionbridge.content.sdk.models.templates;

import com.lionbridge.content.sdk.models.LBFile;
import com.lionbridge.content.sdk.models.Project;
import com.lionbridge.content.sdk.models.TranslationOptions;

import java.util.ArrayList;
import java.util.List;

import static com.lionbridge.content.sdk.utilities.XmlUtils.appendXmlTag;

public class GenerateQuoteTemplate {
	private TranslationOptions translationOptions = new TranslationOptions();
	private List files = new ArrayList<>();
	private List referenceFiles = new ArrayList<>();
	private List projects = new ArrayList<>();
    protected List notifications = new ArrayList<>();

    public GenerateQuoteTemplate(final List fileAssetIds, final List referenceAssetIds, TranslationOptions options) throws IllegalArgumentException {
		if (options == null) {
			throw new IllegalArgumentException("Options must be defined.");
		}
		this.addToFiles(LBFile.getFilesFromAssetIds(fileAssetIds));
		this.addToReferenceFiles(LBFile.getFilesFromAssetIds(referenceAssetIds));
		this.setTranslationOptions(options);
	}
	
	public GenerateQuoteTemplate(final TranslationOptions translationOptions, final List projects) {
        this(translationOptions, projects, null);
	}

    public GenerateQuoteTemplate(TranslationOptions translationOptions, List projects, List notifications) {
        this.translationOptions = translationOptions;
        this.addToProjects(projects);

        if (null != notifications) {
            this.addToNotifications(notifications);
        }
    }

    protected void addToNotifications(List notifications) {
        this.notifications.addAll(notifications);
    }

    public TranslationOptions getTranslationOptions() {
		return translationOptions;
	}

	public void setTranslationOptions(final TranslationOptions translationOptions) {
		this.translationOptions = translationOptions;
	}

	public List getFiles() {
		return files;
	}

	public void setFiles(final List files) {
		this.files = files;
	}

	public void addToFiles(final List files) {
		this.files.addAll(files);
	}

    public List getReferenceFiles() {
        return referenceFiles;
    }

    public void setReferenceFiles(List referenceFiles) {
        this.referenceFiles = referenceFiles;
    }

    public void addToReferenceFiles(final List files) {
		this.referenceFiles.addAll(files);
	}


	public List getProjects() {
		return projects;
	}

	public void setProjects(final List projects) {
		this.projects = projects;
	}

	public void addToProjects(final List projects) {
		this.projects.addAll(projects);
	}

	public String toXmlString() {
		StringBuilder builder = new StringBuilder();

		builder.append("");

		if (hasFiles()) {
            appendXmlTag(builder, "Files", filesAsXml(getFiles(), false));
		}

		if (hasProjects()) {
			builder.append("");
            for(Project project : getProjects()) {
                builder.append(project.idToXmlString());
            }
			builder.append("");
		}

		builder = appendNofiticationsXml(builder);

		builder.append("");

		return builder.toString();
	}

    protected StringBuilder appendNofiticationsXml(StringBuilder builder) {
		if (notifications.size() > 0) {
			builder.append("");
			for (String notification : notifications) {
				builder.append("");
				builder.append("quote-ready");
				if (notification.startsWith("http")) {
					builder = appendXmlTag(builder, "Endpoint", notification);
				} else {
					builder = appendXmlTag(builder, "Endpoint", "mailto:" + notification);
				}
				builder.append("");
			}
			builder.append("");
		}
		return builder;
	}

    private boolean hasProjects() {
        return projects != null && projects.size() > 0;
    }

    protected boolean hasFiles() {
		return files != null && files.size() > 0;
	}

    protected boolean hasReferenceFiles() {
		return referenceFiles != null && referenceFiles.size() > 0;
	}

    protected String filesAsXml(List files, boolean areReferenceFiles) {
        StringBuilder builder = new StringBuilder();

        for (LBFile file: files) {
            builder.append(file.toXmlString(areReferenceFiles));
        }

        return builder.toString();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy