package org.springframework.roo.classpath;

import org.springframework.roo.metadata.MetadataIdentificationUtils;
import org.springframework.roo.model.JavaType;
import org.springframework.roo.project.LogicalPath;
import org.springframework.roo.project.Path;
import org.springframework.roo.support.util.Assert;

/**
 * Provides string manipulation functions for {@link PhysicalTypeMetadata}.
 *
 * @author Ben Alex
 * @since 1.0
 */
public final class PhysicalTypeIdentifier {

	// Constants
	private static final String PHYSICAL_METADATA_TYPE = PhysicalTypeIdentifier.class.getName();
	
	/**
	 * The class-level ID for physical type metadata.
	 * 
	 * @since 1.2.0
	 */
	public static final String PHYSICAL_METADATA_TYPE_ID = MetadataIdentificationUtils.create(PHYSICAL_METADATA_TYPE);

	/**
	 * Returns the class-level ID for physical type metadata. Equivalent to
	 * accessing {@link #PHYSICAL_METADATA_TYPE_ID} directly.
	 * 
	 * @return {@value #PHYSICAL_METADATA_TYPE_ID}
	 */
	public static String getMetadataIdentiferType() {
		return PHYSICAL_METADATA_TYPE_ID;
	}

	/**
	 * Creates an identifier from the given arguments
	 *
	 * @param javaType, assumed to be in {@link Path#SRC_MAIN_JAVA} (required)
	 * @return a non-blank ID
	 * @since 1.2.0
	 */
	public static String createIdentifier(final JavaType javaType) {
		return createIdentifier(javaType, LogicalPath.getInstance(Path.SRC_MAIN_JAVA));
	}

	/**
	 * Creates a physical type metadata ID for the given user project type
	 *
	 * @param javaType the type (required)
	 * @param path the path in which it's located (required)
	 * @return a non-blank ID
	 */
	public static String createIdentifier(final JavaType javaType, final LogicalPath path) {
		return PhysicalTypeIdentifierNamingUtils.createIdentifier(PHYSICAL_METADATA_TYPE, javaType, path);
	}

	/**
	 * Parses the given metadata ID for the user project type to which it relates.
	 * 
	 * @param metadataId the metadata ID to parse (must identify an instance of {@link PhysicalTypeIdentifier#PHYSICAL_METADATA_TYPE})
	 * @return a non-<code>null</code> type
	 */
	public static JavaType getJavaType(final String metadataId) {
		return PhysicalTypeIdentifierNamingUtils.getJavaType(PHYSICAL_METADATA_TYPE, metadataId);
	}

	/**
	 * Parses the given metadata ID for the path of the user project type to which it relates.
	 * 
	 * @param metadataId the metadata ID to parse (must identify an instance of {@link PhysicalTypeIdentifier#PHYSICAL_METADATA_TYPE})
	 * @return a non-<code>null</code> path
	 */
	public static LogicalPath getPath(final String metadataId) {
		return PhysicalTypeIdentifierNamingUtils.getPath(PHYSICAL_METADATA_TYPE, metadataId);
	}

	/**
	 * Indicates whether the given metadata ID identifies a physical Java type,
	 * in other words an interface, class, annotation, or enum.
	 *
	 * @param metadataIdentificationString the metadata ID to check
	 * @return see above
	 */
	public static boolean isValid(final String metadataIdentificationString) {
		return PhysicalTypeIdentifierNamingUtils.isValid(PHYSICAL_METADATA_TYPE, metadataIdentificationString);
	}

	public static String getFriendlyName(final String metadataId) {
		Assert.isTrue(isValid(metadataId), "Invalid metadata id '" + metadataId + "'");
		return getPath(metadataId) + "/" + getJavaType(metadataId);
	}

	/**
	 * Constructor is private to prevent instantiation
	 * 
	 * @since 1.2.0
	 */
	private PhysicalTypeIdentifier() {}
}

@@ -381,16 +381,20 @@
 etadata%7D
+ IDs
 .%0A *%0A * 
@@ -1105,11 +1105,10 @@
 er f
+o
 r
-om
  the
@@ -1118,21 +1118,46 @@
 ven 
-arguments
+%7B@link JavaType%7D's physical type.
 %0A%09 *
+ 
 %0A%09 *
@@ -1176,53 +1176,46 @@
 Type
-, assumed to be in %7B@link Path#SRC_MAIN_JAVA%7D
+ the type for which to generate the ID
  (re
@@ -1261,33 +1261,254 @@
 @since 1.2.0%0A%09 *
-/
+ @deprecated use %7B@link #createIdentifier(JavaType, LogicalPath)%7D if you know%0A%09 * the path, or %7B@link TypeLocationService#getPhysicalTypeIdentifier(JavaType)%7D%0A%09 * if you don't and you know the type exists%0A%09 */%0A%09@Deprecated
 %0A%09public static 
@@ -1636,16 +1636,20 @@
 AIN_JAVA
+, %22%22
 ));%0A%09%7D%0A%0A
@@ -1757,16 +1757,38 @@
 the type
+, which need not exist
  (requir

Support multi module Maven projects - deprecated PhysicalTypeIdentifier#createIdentifier(JavaType)