package org.sagebionetworks.repo.model.jdo;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.sagebionetworks.repo.model.InvalidModelException;

/**
 * Validation for entity names.
 * 
 * @author jmhill
 *
 */
public class EntityNameValidation {
	
	
	// match one or more whitespace characters
	private static final Pattern ALLOWABLE_CHARS = Pattern.compile("^[a-z,A-Z,0-9,_,., ,\\-,\\+,(,)]+");

	/**
	 * Validate the name
	 * @param key
	 * @throws InvalidModelException
	 */
	public static String valdiateName(String key)  {
		if(key == null) throw new IllegalArgumentException("Entity names cannot be null");
		key = key.trim();
		if("".equals(key)) throw new IllegalArgumentException("Entity names cannot be empty strings");
		Matcher matcher = ALLOWABLE_CHARS.matcher(key);
		if (!matcher.matches()) {
			throw new IllegalArgumentException("Invalid Entity name: '"+key+"'. Entity names may only contain; letters, numbers, spaces, '_' and '.'");
		}
		return key;
	}
}

@@ -236,16 +236,17 @@
 hill%0D%0A *
+ 
 %0D%0A */%0D%0Ap
@@ -281,20 +281,16 @@
 tion %7B%0D%0A
-%09%0D%0A%09
 %0D%0A%09// ma
@@ -383,16 +383,21 @@
  Pattern
+%0D%0A%09%09%09
 .compile
@@ -467,16 +467,22 @@
 e name%0D%0A
+%09 * %0D%0A
 %09 * @par
@@ -580,17 +580,16 @@
 key)
- 
  %7B%0D%0A%09%09if
 (key
@@ -584,16 +584,17 @@
  %7B%0D%0A%09%09if
+ 
 (key == 
@@ -598,17 +598,21 @@
 == null)
- 
+%0D%0A%09%09%09
 throw ne
@@ -696,16 +696,17 @@
 );%0D%0A%09%09if
+ 
 (%22%22.equa
@@ -713,17 +713,21 @@
 ls(key))
- 
+%0D%0A%09%09%09
 throw ne
@@ -745,32 +745,39 @@
 gumentException(
+%0D%0A%09%09%09%09%09
 %22Entity names ca
@@ -920,16 +920,23 @@
 ception(
+%0D%0A%09%09%09%09%09
 %22Invalid
@@ -955,13 +955,33 @@
 : '%22
-+key+
+%0D%0A%09%09%09%09%09%09%09+ key%0D%0A%09%09%09%09%09%09%09+ 
 %22'. 
@@ -1009,17 +1009,17 @@
  contain
-;
+:
  letters
@@ -1041,19 +1041,65 @@
 es, 
-'_' and '.'
+underscores, hypens, periods, plus signs, and parentheses
 %22);%0D

fix for PLFM-769