class NameTemplate extends LayoutTemplate {

	public String format(PersonName personName) {
		
		List<String> personNameLines = new ArrayList<>();
		List<List<Map<String, String>>> lines = getLines();
		String layoutToken = getLayoutToken();
		
		try {
			for (List<Map<String, String>> line : lines) {
				StringBuilder nameLine = new StringBuilder();
				Boolean hasToken = false;
				for (Map<String, String> lineToken : line) {
					if (lineToken.get("isToken").equals(layoutToken)) {
						String tokenValue = BeanUtils.getProperty(personName, lineToken.get("codeName"));
						if (StringUtils.isNotBlank(tokenValue)) {
							hasToken = true;
							nameLine.append(tokenValue);
						}
					} else {
						nameLine.append(lineToken.get("displayText"));
					}
				}
				// only display a line if there's at least one token within it we've been able to resolve
				String nameLineString = nameLine.toString();
				if (StringUtils.isNotBlank(nameLineString) && hasToken) {
					personNameLines.add(nameLineString);
				}
			}
			// bit of hack, but we ignore the "line-by-line" format and just delimit a "line" with blank space
			return StringUtils.join(personNameLines, " ");
		}
		catch (Exception e) {
			throw new APIException("Unable to format personName " + personName.getId() + " using name template", e);
		}
	}

}