<cfcomponent extends="components.common.formObject">
	<cfset variables.idEmployee=0>
	<cfset variables.employee="">
	<cfset variables.username="">
	<cfset variables.password="">
	<cfset variables.roles="">

	<cffunction name="init" access="public" output="false">
		<cfset variables.idEmployee=0>
		<cfset variables.employee="">
		<cfset variables.username="">
		<cfset variables.password="">
		<cfset variables.roles="">
	</cffunction>

	<cffunction name="getFormObjectId" access="public" output="false" returntype="numeric">
		<cfreturn getIdEmployee()>
	</cffunction>
	
	<cffunction name="setFormObjectId" access="public" output="false">
		<cfargument name="objectId" type="numeric" required="yes">
		<cfset setIdEmployee(arguments.objectId)>
	</cffunction>

	<cffunction name="setIdEmployee" access="remote" output="false">
		<cfargument name="idEmployee" type="numeric" required="yes" default="0">
		<cfset variables.idEmployee=arguments.idEmployee>
	</cffunction>

	<cffunction name="getIdEmployee" access="remote" returntype="numeric" output="false">
		<cfreturn variables.idEmployee>
	</cffunction>

	<cffunction name="getEmployee" access="remote" returntype="components.corporate.employee" output="false">
		<cfset variables.employee=CreateObject("component", "components.corporate.employee")>
		<cfset variables.employee.setIdEmployee(variables.idEmployee)>
		<cfset variables.employee.get()>
		<cfreturn variables.employee>
	</cffunction>

	<cffunction name="setName" access="remote" output="false">
		<cfargument name="name" type="string" required="yes" default="">
		<cfset variables.name=arguments.name>
	</cffunction>

	<cffunction name="getName" access="remote" returntype="string" output="false">
		<cfreturn variables.name>
	</cffunction>

	<cffunction name="setUsername" access="remote" output="false">
		<cfargument name="username" type="string" required="yes" default="">
		<cfset variables.username=arguments.username>
	</cffunction>

	<cffunction name="getUsername" access="remote" returntype="string" output="false">
		<cfreturn variables.username>
	</cffunction>

	<cffunction name="setPassword" access="remote" output="false">
		<cfargument name="password" type="string" required="yes" default="">
		<cfset variables.password=arguments.password>
	</cffunction>

	<cffunction name="getPassword" access="remote" returntype="string" output="false">
		<cfreturn variables.password>
	</cffunction>

	<cffunction name="setRoles" access="remote" output="false">
		<cfargument name="roles" type="string" required="yes" default="">
		<cfset variables.roles=arguments.roles>
	</cffunction>

	<cffunction name="getRoles" access="remote" returntype="string" output="false">
		<cfreturn variables.roles>
	</cffunction>
	
	<cffunction name="setVariablesFromQuery" access="private" output="false">
		<cfargument name="getQuery" type="query" required="yes">
		<cfif getQuery.recordcount gt 0>
			<cfset variables.idEmployee=getQuery.idEmployee>
			<cfset variables.username=getQuery.username>
			<cfset variables.password=getQuery.password>
			<cfset variables.roles=getQuery.roles>
		</cfif>
	</cffunction>
	
	<cffunction name="get" access="remote" output="false">
		<cfquery name="getQuery" datasource="#application.datasource#">
			SELECT 
				#getQueryString()#
			WHERE
				employeeUser.idEmployee=<cfqueryparam value="#variables.idEmployee#" cfsqltype="cf_sql_integer">
		</cfquery>
		<cfif getQuery.recordcount gt 0>
			<cfset setVariablesFromQuery(getQuery)>
		</cfif>
		<!--- ADDED: set that object was retrieved from DB --->
		<cfset module.retrievedFromDB=true>
	</cffunction>

	<cffunction name="checkPassword" access="remote" output="false">
		<cfquery name="getQuery" datasource="#application.datasource#">
			SELECT 
				#getQueryString()#
			WHERE
				employeeUser.username=<cfqueryparam value="#variables.username#" cfsqltype="cf_sql_varchar" maxlength="255">
				AND employeeUser.password=<cfqueryparam value="#variables.password#" cfsqltype="cf_sql_varchar" maxlength="255">
		</cfquery>
		<cfif getQuery.recordcount gt 0>
			<cfset setVariablesFromQuery(getQuery)>
		</cfif>
	</cffunction>

	<cffunction name="getQueryString" access="private" output="false" returntype="string">
		<cfset var result="
				employeeUser.idEmployee, 
				employeeUser.username,
				employeeUser.password,
				employeeUser.roles
			FROM 
				employeeUser">
		<cfreturn result>
	</cffunction>

	<cffunction name="create" access="remote" returntype="struct" output="false">
		<cfset var result=StructNew()>
		<cfset result.id=0>
		<cfset result.message="">
		<!--- check parameters --->
		<cftry>
			<cfquery name="createQuery" datasource="#application.datasource#">
				INSERT INTO employeeUser
					(
					idEmployee, 
					username, password, 
					roles
					) 
				VALUES 
					(
					<cfqueryparam value="#variables.idEmployee#" cfsqltype="cf_sql_integer">, 
					<cfqueryparam value="#variables.username#" cfsqltype="cf_sql_varchar" maxlength="255">,
					<cfqueryparam value="#variables.password#" cfsqltype="cf_sql_varchar" maxlength="255">,
					<cfqueryparam value="#variables.roles#" cfsqltype="cf_sql_varchar" maxlength="255">
					)
				ON DUPLICATE KEY UPDATE 
					username=<cfqueryparam value="#variables.username#" cfsqltype="cf_sql_varchar" maxlength="255">,
					password=<cfqueryparam value="#variables.password#" cfsqltype="cf_sql_varchar" maxlength="255">,
					roles=<cfqueryparam value="#variables.roles#" cfsqltype="cf_sql_varchar" maxlength="255">
			</cfquery>
			<cfset result.id=variables.idEmployee>
			<!--- catch any error --->
			<cfcatch type="any">
				<cfset result.id=-1>
				<cfset result.message="Error [employeeUser.create()]: " & cfcatch.Message & "<br>Details: " & cfcatch.Detail>
			</cfcatch>
		</cftry>
		<cfreturn result>
	</cffunction>

	<cffunction name="update" access="remote" returntype="struct" output="false">
		<cfset var result=create()>
		<cfreturn result>
	</cffunction>

	<cffunction name="delete" access="remote" returntype="struct" output="false">
		<cfset var result=StructNew()>
		<cfset result.id=0>
		<cfset result.message="">
		<!--- check parameters --->
		<cftry>
			<cfquery name="deleteQuery" datasource="#application.datasource#">
				DELETE FROM employeeUser
				WHERE 
					idEmployee=<cfqueryparam value="#variables.idEmployee#" cfsqltype="cf_sql_integer">
			</cfquery>
			<cfset result.id=0>
			<cfset init()>
			<!--- catch any error --->
			<cfcatch type="any">
				<cfset result.id=-1>
				<cfset result.message="Error [employeeUser.delete()]: " & cfcatch.Message & "<br>Details: " & cfcatch.Detail>
			</cfcatch>
		</cftry>
		<cfreturn result>
	</cffunction>
	
</cfcomponent>