<commit_before>/*
 * Our Xtext Java class generator
 */
package com.temenos.interaction.rimdsl.generator

import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.IGenerator
import org.eclipse.xtext.generator.IFileSystemAccess
import com.temenos.interaction.rimdsl.rim.ResourceCommand
import com.temenos.interaction.rimdsl.rim.State
import com.temenos.interaction.rimdsl.rim.Transition
import com.temenos.interaction.rimdsl.rim.TransitionForEach
import com.temenos.interaction.rimdsl.rim.TransitionAuto
import com.temenos.interaction.rimdsl.rim.ResourceInteractionModel
import org.eclipse.emf.common.util.EList
import com.temenos.interaction.rimdsl.rim.UriLink
import com.temenos.interaction.rimdsl.rim.OKFunction;
import com.temenos.interaction.rimdsl.rim.NotFoundFunction
import com.temenos.interaction.rimdsl.rim.Function
import com.temenos.interaction.rimdsl.rim.Expression
import javax.inject.Inject
import org.eclipse.xtext.naming.IQualifiedNameProvider
import com.temenos.interaction.rimdsl.rim.ImplRef
import com.temenos.interaction.rimdsl.rim.RelationConstant
import com.temenos.interaction.rimdsl.rim.Relation
import com.temenos.interaction.rimdsl.rim.TransitionEmbedded
import com.temenos.interaction.rimdsl.rim.TransitionRedirect
import com.temenos.interaction.rimdsl.rim.TransitionRef
import com.temenos.interaction.rimdsl.rim.MethodRef

class RIMDslGeneratorSpringPRD implements IGenerator {
	
	@Inject extension IQualifiedNameProvider
	
	override void doGenerate(Resource resource, IFileSystemAccess fsa) {
	    if (resource == null) {
            throw new RuntimeException("Generator called with null resource");	        
	    }
        for (rim : resource.allContents.toIterable.filter(typeof(ResourceInteractionModel))) {
            generate(resource, rim, fsa);
        }
	}
		
	def void generate(Resource resource, ResourceInteractionModel rim, IFileSystemAccess fsa) {
        val rimName = rim.fullyQualifiedName.toString("_")
        		
        // generate resource state files
        
        fsa.generateFile("IRIS-" + rimName + "-PRD.xml", toSpringXML(rim))

        fsa.generateFile("IRIS-" + rimName + ".properties", toBeanMap(rim))
	}

	
	def className(Resource res) {
		var name = res.URI.lastSegment
		return name.substring(0, name.indexOf('.'))
	}
	
	def toBeanMap(ResourceInteractionModel rim) '''
		# «System::currentTimeMillis»
		«FOR state : rim.states»
		«stateVariableName(state)»=«produceMethods(rim, state)» «producePath(rim, state)»
		«ENDFOR»
	'''
	
	def toSpringBean(ResourceInteractionModel rim, State state)'''
		<!-- Define Spring bean for resource : «state.name» -->
		<!-- begin spring bean for state : «stateVariableName(state)» -->
		<bean id="«stateVariableName(state)»" class="«produceResourceStateType(state)»">
			<constructor-arg name="entityName" value="«state.entity.name»" />
			<constructor-arg name="name" value="« state.name »" />
			<constructor-arg>
				<list>
				«produceActionList(state, state.impl)»
				</list>
			</constructor-arg>
			<constructor-arg name="path" value="«producePath(rim, state)»" />
			<constructor-arg name="rels">
				«produceRelations(state)»
			</constructor-arg>
			<constructor-arg name="uriSpec">«IF state.path != null »<bean class="com.temenos.interaction.core.hypermedia.UriSpecification"><constructor-arg name="name" value="«state.name»" /><constructor-arg name="template" value="«producePath(rim, state)»" /></bean>«ELSE»<null />«ENDIF»</constructor-arg>
			<constructor-arg name="errorState"><null /></constructor-arg>

			«IF state.isInitial»
			<property name="initial" value="true" />
			«ENDIF»
			«IF state.isException»
			<property name="exception" value="true" />
			«ENDIF»
			«IF state.cache > 0»
			<property name="maxAge" value="«state.cache»" />
			«ENDIF»
			<!-- Start property transitions list -->
			<property name="transitions">
				<list>
				<!-- create transitions  -->
			«FOR t : state.transitions»					
				«IF (t.state != null && t.state.name != null) || (t.name != null ) || t.locator != null »
					<!-- begin transition : «IF (t.locator != null)»«t.locator.name»«ELSE»«transitionTargetStateVariableName(t)»«ENDIF» -->
					«IF t instanceof Transition»                
			    	«produceTransitions(rim, state, t as Transition)»
			    	«ENDIF»
					«IF t instanceof TransitionForEach»                
					«produceTransitionsForEach(rim, state, t as TransitionForEach)»
					«ENDIF»
					«IF t instanceof TransitionAuto»                
					«produceTransitionsAuto(rim, state, t as TransitionAuto)»
					«ENDIF»
					«IF t instanceof TransitionRedirect»                
					«produceTransitionsRedirect(rim, state, t as TransitionRedirect)»
					«ENDIF»
					«IF t instanceof TransitionEmbedded»                
					«produceTransitionsEmbedded(rim, state, t as TransitionEmbedded)»
					«ENDIF»
					<!-- end transition : «IF (t.locator != null)»«t.locator.name»«ELSE»«transitionTargetStateVariableName(t)»«ENDIF» -->
				«ENDIF» 				
			«ENDFOR»
				</list>
			</property>
		</bean>	
		<!-- end spring bean for state : «stateVariableName(state)» -->		
	'''
    	
	def toSpringXML(ResourceInteractionModel rim) '''
		<?xml version="1.0" encoding="UTF-8"?>
		<!--
		  Copyright (C) 2012 - 2013 Temenos Holdings N.V.
		 -->

		<beans xmlns="http://www.springframework.org/schema/beans"
			xmlns:context="http://www.springframework.org/schema/context"
			xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
			xmlns:util="http://www.springframework.org/schema/util"
			xsi:schemaLocation="
				http://www.springframework.org/schema/util 
				http://www.springframework.org/schema/util/spring-util-3.0.xsd
				http://www.springframework.org/schema/beans
				http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
				http://www.springframework.org/schema/context
				http://www.springframework.org/schema/context/spring-context-3.0.xsd"
				default-lazy-init="true">

		«FOR state : rim.states»
			«toSpringBean(rim, state)»
		«ENDFOR»		
		</beans>
	'''

	def String transitionTargetStateVariableName(TransitionRef t) {
		if (t.state != null) {
			return stateVariableName(t.state);
		} else {
			var targetState = "";
			
			if(t.name != null && t.name.length != 0 && t.name.lastIndexOf(".") > 1) {
				// Construct string of format: domain_resource-state
				targetState = t.name.substring(0, t.name.lastIndexOf(".")) + "-" + t.name.substring(t.name.lastIndexOf(".") + 1);
				targetState = targetState.replaceAll("\\.", "_"); 
			} 
			
       		return targetState;
		}<commit_msg>Fix to handle edge case.<commit_after>/*
 * Our Xtext Java class generator
 */
package com.temenos.interaction.rimdsl.generator

import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.xtext.generator.IGenerator
import org.eclipse.xtext.generator.IFileSystemAccess
import com.temenos.interaction.rimdsl.rim.ResourceCommand
import com.temenos.interaction.rimdsl.rim.State
import com.temenos.interaction.rimdsl.rim.Transition
import com.temenos.interaction.rimdsl.rim.TransitionForEach
import com.temenos.interaction.rimdsl.rim.TransitionAuto
import com.temenos.interaction.rimdsl.rim.ResourceInteractionModel
import org.eclipse.emf.common.util.EList
import com.temenos.interaction.rimdsl.rim.UriLink
import com.temenos.interaction.rimdsl.rim.OKFunction;
import com.temenos.interaction.rimdsl.rim.NotFoundFunction
import com.temenos.interaction.rimdsl.rim.Function
import com.temenos.interaction.rimdsl.rim.Expression
import javax.inject.Inject
import org.eclipse.xtext.naming.IQualifiedNameProvider
import com.temenos.interaction.rimdsl.rim.ImplRef
import com.temenos.interaction.rimdsl.rim.RelationConstant
import com.temenos.interaction.rimdsl.rim.Relation
import com.temenos.interaction.rimdsl.rim.TransitionEmbedded
import com.temenos.interaction.rimdsl.rim.TransitionRedirect
import com.temenos.interaction.rimdsl.rim.TransitionRef
import com.temenos.interaction.rimdsl.rim.MethodRef

class RIMDslGeneratorSpringPRD implements IGenerator {
	
	@Inject extension IQualifiedNameProvider
	
	override void doGenerate(Resource resource, IFileSystemAccess fsa) {
	    if (resource == null) {
            throw new RuntimeException("Generator called with null resource");	        
	    }
        for (rim : resource.allContents.toIterable.filter(typeof(ResourceInteractionModel))) {
            generate(resource, rim, fsa);
        }
	}
		
	def void generate(Resource resource, ResourceInteractionModel rim, IFileSystemAccess fsa) {
        val rimName = rim.fullyQualifiedName.toString("_")
        		
        // generate resource state files
        
        fsa.generateFile("IRIS-" + rimName + "-PRD.xml", toSpringXML(rim))

        fsa.generateFile("IRIS-" + rimName + ".properties", toBeanMap(rim))
	}

	
	def className(Resource res) {
		var name = res.URI.lastSegment
		return name.substring(0, name.indexOf('.'))
	}
	
	def toBeanMap(ResourceInteractionModel rim) '''
		# «System::currentTimeMillis»
		«FOR state : rim.states»
		«stateVariableName(state)»=«produceMethods(rim, state)» «producePath(rim, state)»
		«ENDFOR»
	'''
	
	def toSpringBean(ResourceInteractionModel rim, State state)'''
		<!-- Define Spring bean for resource : «state.name» -->
		<!-- begin spring bean for state : «stateVariableName(state)» -->
		<bean id="«stateVariableName(state)»" class="«produceResourceStateType(state)»">
			<constructor-arg name="entityName" value="«state.entity.name»" />
			<constructor-arg name="name" value="« state.name »" />
			<constructor-arg>
				<list>
				«produceActionList(state, state.impl)»
				</list>
			</constructor-arg>
			<constructor-arg name="path" value="«producePath(rim, state)»" />
			<constructor-arg name="rels">
				«produceRelations(state)»
			</constructor-arg>
			<constructor-arg name="uriSpec">«IF state.path != null »<bean class="com.temenos.interaction.core.hypermedia.UriSpecification"><constructor-arg name="name" value="«state.name»" /><constructor-arg name="template" value="«producePath(rim, state)»" /></bean>«ELSE»<null />«ENDIF»</constructor-arg>
			<constructor-arg name="errorState"><null /></constructor-arg>

			«IF state.isInitial»
			<property name="initial" value="true" />
			«ENDIF»
			«IF state.isException»
			<property name="exception" value="true" />
			«ENDIF»
			«IF state.cache > 0»
			<property name="maxAge" value="«state.cache»" />
			«ENDIF»
			<!-- Start property transitions list -->
			<property name="transitions">
				<list>
				<!-- create transitions  -->
			«FOR t : state.transitions»					
				«IF (t.state != null && t.state.name != null) || (t.name != null ) || t.locator != null »
					<!-- begin transition : «IF (t.locator != null)»«t.locator.name»«ELSE»«transitionTargetStateVariableName(t)»«ENDIF» -->
					«IF t instanceof Transition»                
			    	«produceTransitions(rim, state, t as Transition)»
			    	«ENDIF»
					«IF t instanceof TransitionForEach»                
					«produceTransitionsForEach(rim, state, t as TransitionForEach)»
					«ENDIF»
					«IF t instanceof TransitionAuto»                
					«produceTransitionsAuto(rim, state, t as TransitionAuto)»
					«ENDIF»
					«IF t instanceof TransitionRedirect»                
					«produceTransitionsRedirect(rim, state, t as TransitionRedirect)»
					«ENDIF»
					«IF t instanceof TransitionEmbedded»                
					«produceTransitionsEmbedded(rim, state, t as TransitionEmbedded)»
					«ENDIF»
					<!-- end transition : «IF (t.locator != null)»«t.locator.name»«ELSE»«transitionTargetStateVariableName(t)»«ENDIF» -->
				«ENDIF» 				
			«ENDFOR»
				</list>
			</property>
		</bean>	
		<!-- end spring bean for state : «stateVariableName(state)» -->		
	'''
    	
	def toSpringXML(ResourceInteractionModel rim) '''
		<?xml version="1.0" encoding="UTF-8"?>
		<!--
		  Copyright (C) 2012 - 2013 <NAME>.
		 -->

		<beans xmlns="http://www.springframework.org/schema/beans"
			xmlns:context="http://www.springframework.org/schema/context"
			xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
			xmlns:util="http://www.springframework.org/schema/util"
			xsi:schemaLocation="
				http://www.springframework.org/schema/util 
				http://www.springframework.org/schema/util/spring-util-3.0.xsd
				http://www.springframework.org/schema/beans
				http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
				http://www.springframework.org/schema/context
				http://www.springframework.org/schema/context/spring-context-3.0.xsd"
				default-lazy-init="true">

		«FOR state : rim.states»
			«toSpringBean(rim, state)»
		«ENDFOR»		
		</beans>
	'''

	def String transitionTargetStateVariableName(TransitionRef t) {
		if (t.state != null) {
			return stateVariableName(t.state);
		} else {
			var targetState = "";
			
			if(t.name != null && t.name.length != 0 && t.name.lastIndexOf(".") > 1) {
				// Construct string of format: domain_resource-state
				targetState = t.name.substring(0, t.name.lastIndexOf(".")) + "-" + t.name.substring(t.name.lastIndexOf(".") + 1);
				targetState = targetState.replaceAll("\\.", "_"); 
			} else if(t.name != null) {
				targetState = t.name;
			}
			
       		return targetState;
		}
{'fraction_non_alphanumeric': 0.14241156333206542, 'fraction_numerical': 0.004260174971472043, 'mean_word_length': 3.7253774263120056, 'pattern_counts': {'":': 0, '<': 82, '<?xml version=': 2, '>': 86, 'https://': 0, 'lorem ipsum': 0, 'www.': 20, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4906882', 'n_tokens_mistral': 4777, 'n_tokens_neox': 4134, 'n_words': 1013}