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

com.teamscale.index.metadata.abap.AbapFileMetadataReader Maven / Gradle / Ivy

The newest version!
package com.teamscale.index.metadata.abap;

import java.util.EnumSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

import org.conqat.lib.commons.collections.CollectionUtils;
import org.conqat.lib.commons.collections.Pair;
import org.conqat.lib.commons.string.StringUtils;

import com.teamscale.commons.utils.ParameterFileUtils;
import com.teamscale.index.metadata.IFileMetadata;

import eu.cqse.check.framework.scanner.ELanguage;

/**
 * Reader for metadata files for ABAP code files/objects.
 */
public class AbapFileMetadataReader {

	/** File extension for metadata files in Abap projects. */
	public static final String ABAP_METADATA_FILE_EXTENSION = "metadata";

	/**
	 * The languages that can have Abap Metadata files. More precise: files of these languages can have
	 * Abap Metadata files next to them. The metadata file will have the same name as the code file,
	 * suffixed with ".metadata".
	 */
	public static final EnumSet LANGUAGES_WITH_ABAP_METADATA_FILES = EnumSet.of(ELanguage.ABAP,
			ELanguage.ABAP_DDIC, ELanguage.ABAP_CDS);

	private static final Set METADATA_FIELDS = CollectionUtils.asHashSet(
			AbapFileMetadata.CREATION_DATE_PROPERTY, AbapFileMetadata.LAST_UPDATE_DATE_TIME_PROPERTY,
			AbapFileMetadata.ORIGINATING_SYSTEM_PROPERTY, AbapFileMetadata.PROGRAM_TYPE_PROPERTY,
			AbapFileMetadata.AUTHORIZATION_GROUP_PROPERTY, AbapFileMetadata.LANGUAGE_VERSION_PROPERTY,
			AbapFileMetadata.IS_RFC_ENABLED_PROPERTY, AbapFileMetadata.IS_GENERATED_CODE_PROPERTY,
			// Release contract fields
			ReleaseContract.SUB_OBJECT_NAME_PROPERTY, ReleaseContract.SUB_OBJECT_TYPE_PROPERTY,
			ReleaseContract.COMPATIBILITY_CONTRACT_PROPERTY, ReleaseContract.RELEASE_STATE_PROPERTY,
			ReleaseContract.USE_IN_KEY_USER_APPS_PROPERTY, ReleaseContract.USE_IN_SAP_CLOUD_PLATFORM_PROPERTY);

	private static boolean hasReleaseContractInfos(String fileContents) {
		return fileContents.contains(ReleaseContract.SUB_OBJECT_NAME_PROPERTY)
				|| fileContents.contains(ReleaseContract.SUB_OBJECT_TYPE_PROPERTY)
				|| fileContents.contains(ReleaseContract.COMPATIBILITY_CONTRACT_PROPERTY)
				|| fileContents.contains(ReleaseContract.RELEASE_STATE_PROPERTY)
				|| fileContents.contains(ReleaseContract.USE_IN_KEY_USER_APPS_PROPERTY)
				|| fileContents.contains(ReleaseContract.USE_IN_SAP_CLOUD_PLATFORM_PROPERTY);
	}

	/**
	 * Returns whether a given file is a metadata file.
	 */
	public static boolean isMetadataFile(String uniformPath) {
		return uniformPath.endsWith("." + ABAP_METADATA_FILE_EXTENSION);
	}

	/**
	 * Returns the file name from some metadata file.
	 */
	public static String getFileNameFromMetadataFile(String uniformPath) {
		return StringUtils.stripSuffix(uniformPath, "." + ABAP_METADATA_FILE_EXTENSION);
	}

	/**
	 * Parses the contents of some metadata file for a language and returns the appropriate
	 * {@link IFileMetadata}.
	 */
	public static AbapFileMetadata parseFileContents(String filename, String fileContents,
			Consumer errorReporter) {
		AbapFileMetadata fileMetadata = new AbapFileMetadata(filename, 0, 0, null, null, null, null, null, false,
				false);
		List lines = StringUtils.splitLinesAsList(fileContents);
		ReleaseContract releaseContract = ReleaseContract.getEmptyContract();
		for (String line : lines) {
			Pair parameterNameAndValue = ParameterFileUtils.findParameterNameAndValue(line,
					METADATA_FIELDS);
			if (parameterNameAndValue != null) {
				AbapFileMetadataReader.saveFieldAndValueIntoMetadata(fileMetadata, releaseContract,
						parameterNameAndValue);
			} else {
				errorReporter.accept("Unknown parameter in file " + filename + " on line\\n" + line);
			}
		}

		if (hasReleaseContractInfos(fileContents)) {
			fileMetadata.setReleaseContract(releaseContract);
		}

		return fileMetadata;
	}

	private static void saveFieldAndValueIntoMetadata(AbapFileMetadata metadata, ReleaseContract releaseContract,
			Pair fieldNameAndValue) {
		String fieldName = fieldNameAndValue.getFirst();
		switch (fieldName) {
		case AbapFileMetadata.CREATION_DATE_PROPERTY:
			metadata.setCreationDate(Long.parseLong(fieldNameAndValue.getSecond()));
			break;
		case AbapFileMetadata.LAST_UPDATE_DATE_TIME_PROPERTY:
			metadata.setLastUpdateDateTime(Long.parseLong(fieldNameAndValue.getSecond()));
			break;
		case AbapFileMetadata.ORIGINATING_SYSTEM_PROPERTY:
			metadata.setOriginatingSystem(fieldNameAndValue.getSecond());
			break;
		case AbapFileMetadata.AUTHORIZATION_GROUP_PROPERTY:
			metadata.setAuthorizationGroup(fieldNameAndValue.getSecond());
			break;
		case AbapFileMetadata.PROGRAM_TYPE_PROPERTY:
			EAbapProgramType programType = EAbapProgramType.valueOf(fieldNameAndValue.getSecond());
			metadata.setProgramType(programType);
			break;
		case AbapFileMetadata.LANGUAGE_VERSION_PROPERTY:
			EAbapLanguageVersion languageVersion = EAbapLanguageVersion.valueOf(fieldNameAndValue.getSecond());
			metadata.setLanguageVersion(languageVersion);
			break;
		case AbapFileMetadata.IS_RFC_ENABLED_PROPERTY:
			metadata.setRfcEnabled(Boolean.parseBoolean(fieldNameAndValue.getSecond()));
			break;
		case AbapFileMetadata.IS_GENERATED_CODE_PROPERTY:
			metadata.setCodeIsGenerated(Boolean.parseBoolean(fieldNameAndValue.getSecond()));
			break;
		default:
			saveReleaseContractIntoMetadata(releaseContract, fieldName, fieldNameAndValue);
			break;
		}
	}

	private static void saveReleaseContractIntoMetadata(ReleaseContract releaseContract, String fieldName,
			Pair fieldNameAndValue) {
		switch (fieldName) {
		case ReleaseContract.SUB_OBJECT_NAME_PROPERTY:
			releaseContract.setSubObjectName(fieldNameAndValue.getSecond());
			break;
		case ReleaseContract.SUB_OBJECT_TYPE_PROPERTY:
			releaseContract.setSubObjectType(fieldNameAndValue.getSecond());
			break;
		case ReleaseContract.COMPATIBILITY_CONTRACT_PROPERTY:
			releaseContract.setCompatibilityContract(
					ECompatibilityContract.getCompatibilityContract(fieldNameAndValue.getSecond()));
			break;
		case ReleaseContract.RELEASE_STATE_PROPERTY:
			releaseContract.setReleaseState(EReleaseState.getReleaseStateFromName(fieldNameAndValue.getSecond()));
			break;
		case ReleaseContract.USE_IN_KEY_USER_APPS_PROPERTY:
			releaseContract.setUseInKeyUserApps(Boolean.parseBoolean(fieldNameAndValue.getSecond()));
			break;
		case ReleaseContract.USE_IN_SAP_CLOUD_PLATFORM_PROPERTY:
			releaseContract.setUseInSapCloudPlatform(Boolean.parseBoolean(fieldNameAndValue.getSecond()));
			break;
		default:
			// fall-through intended
			break;
		}
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy