package com.tmk.uploadmanager.model;

import java.io.Serializable;
import java.util.TreeMap;

/**
 * Storage class for uploads
 *
 * @author tmk
 */
public class UploadCollection implements Serializable {

	private static final long serialVersionUID = 123L;

	private TreeMap<String, Upload> collection;
	private String name;
	private String template;
	private String tags;

	public UploadCollection(String name, String template, String tags) {
		this.name = name;
		this.template = template;
		this.tags = tags;

		this.collection = new TreeMap<>();
	}

	public void addUpload(String key, Upload up) {
		if (up != null && key != null) {
			collection.put(key, up);
		}
	}

	public void removeUpload(String key) {
		if (key != null) {
			collection.remove(key);
		}
	}

	public String format(Upload u) {
		if (u == null) {
			return null;
		}

		return template.replaceAll("_COLLECTIONNAME_", name).replaceAll("_UPLOADNAME_", u.getTitle()).replaceAll("_UPLOADDESC_", u.getDescription()).replaceAll("_UPLOADIMAGE_", u.getImage());
	}

	public int size() {
		return collection.size();
	}

	public boolean contains(String key) {
		return (name != null) ? collection.containsKey(key) : null;
	}

	public Upload getUpload(String key) {
		return collection.get(key);
	}

	public TreeMap<String, Upload> getCollection() {
		return collection;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTemplate() {
		return template;
	}

	public void setTemplate(String template) {
		this.template = template;
	}

	public String getTags() {
		return tags;
	}

	public void setTags(String tags) {
		this.tags = tags;
	}
}

@@ -85,16 +85,48 @@
 TreeMap;
+%0Aimport java.util.regex.Matcher;
 %0A%0A/**%0A *
@@ -886,16 +886,23 @@
 template
+%0A%09%09%09%09%09%09
 .replace
@@ -925,21 +925,54 @@
 NAME_%22, 
-name)
+Matcher.quoteReplacement(name))%0A%09%09%09%09%09%09
 .replace
@@ -987,24 +987,49 @@
 LOADNAME_%22, 
+Matcher.quoteReplacement(
 u.getTitle()
@@ -1029,16 +1029,24 @@
 Title())
+)
+%0A%09%09%09%09%09%09
 .replace
@@ -1061,24 +1061,49 @@
 LOADDESC_%22, 
+Matcher.quoteReplacement(
 u.getDescrip
@@ -1109,16 +1109,24 @@
 ption())
+)
+%0A%09%09%09%09%09%09
 .replace
@@ -1146,16 +1146,41 @@
 MAGE_%22, 
+Matcher.quoteReplacement(
 u.getIma
@@ -1184,16 +1184,17 @@
 Image())
+)
 ;%0A%09%7D%0A%0A%09p

Fix format error with dollar signs