ZNize Platform 6.0 Developer Guide

Embedded Beans - Entity

A bean of type "entity" is an EntityBackingBean that is used to show a persisted entity, create a new entity or show a query form.

Show Entity

To show an existing entity, set mode to VIEW, and add criteriaElements as query criteria. For example, embed the employee with id 100 from HR module.

	<bean id="viewEmployee" type="entity">
		<entityType>HR.EMP</entityType>
		<mode>VIEW</mode>

		<criteriaElements>
			<function name="EQ" property="nid">100</function>
		</criteriaElements>

		<viewConfig>
			<viewType>ENTITY</viewType>
		</viewConfig>
	</bean>

Query

For query, set viewType to QUERY. Example: Query full-time employees

	<bean id="queryEmployees" type="entity">
		<entityType>HR.EMP</entityType>

		<criteriaElements>
			<function name="EQ" property="type">FULL_TIME</function>
		</criteriaElements>

		<viewConfig>
			<viewType>QUERY</viewType>
			<propertiesToHide>type</propertiesToHide>
		</viewConfig>
	</bean>
Query supports search options such as group by and order by properties. For example, retrieve sum(order.total) group by customer, and order by customer(ascending) and sum(order.total) (descending).

	<bean id="queryOrders" type="entity">
		<entityType>Sales.SO</entityType>

		<criteriaElements>
			<function name="IS_NOT_NULL" property="payment"></function>
		</criteriaElements>

		<searchOptions>
			<groupBy>customer</groupBy>
			<asc property="customer"/>
			<desc property="total"/>
		</searchOptions>

		<viewConfig>
			<viewType>QUERY</viewType>
			<showSearchOptions>false</showSearchOptions>

			<viewConfig name="queryResults">
				<viewType>ENTITY_LIST_WIDE</viewType>
			</viewConfig>
		</viewConfig>
	</bean>
Order.total is a statistics property(SUM), and "order by total" is actually order by sum(order.total) for group-by query. Refer to Entity Backing Bean on Statistics Properties.

Create Entity

For creating entity, set viewType to ENTITY and set mode to CREATE. Example: create an ExpenseClaim entity with ExpenseClaim items initialized.

	<bean id="createExpenseClaim" type="entity">
		<entityType>HR.EC</entityType>
		<mode>CREATE</mode>

		<criteriaElements>
			<function name="EQ" property="expenseClaimItems">
				<expenseClaimItem>
					<code>00</code>
					<expense>200.00</expense>
				</expenseClaimItem>

				<expenseClaimItem>
					<code>01</code>
					<expense>300.00</expense>
				</expenseClaimItem>
			</function>
		</criteriaElements>

		<viewConfig>
			<viewType>ENTITY</viewType>
		</viewConfig>

	</bean>

Entity Not Found

If an embedded bean is retrieving and showing an entity, but the entity is not found by the query criteria, how to deal with it? The viewConfig parameter entity.not.found will be consulted to determine what to return.

	<viewConfig>
		<viewType>ENTITY</viewType>
		<param name="entity.not.found" value="ignore" /> 
	</viewConfig>
Parameter: entity.not.found. (default: error) Parameter: entity.not.found.msg

If present, show the specified message instead of the default message if the entity is not found. The message will be shown even if the value of the parameter entity.not.found is "ignore".

Example: redirect if entity is not found

When loading page /createExpenseClaim.xpg for submitting an ExpenseClaim, if the Employee entity is not found for current user, redirect to page /createEmployee.xpg to create the Employee entity. Redirect back to /createExpenseClaim.xpg after the Employee is created.

Page: /createExpenseClaim.xpg


	<bean id="checkEmployee" type="entity">
		<entityType>HR.EMP</entityType>
		<mode>VIEW</mode>

		<criteriaElements>
			<function name="EQ" property="user">#{containerBean.authenticatedUser}</function>
		</criteriaElements>

		<viewConfig>
			<viewType>ENTITY</viewType>

			<!-- entity not found: redirect to a URL -->
			<param name="entity.not.found"  value="redirect:EntityNotFound" />

			<redirect action="EntityNotFound" toURL="/createEmployee.xpg">
				<add name="redirectAfterCreate" value="#{containerBean.viewInfo.bookmarkableURL}" />
			</redirect>

		</viewConfig>

		<hidden>true</hidden>
	</bean>

	<bean id="createExpenseClaim" type="entity">
		<entityType>HR.EC</entityType>
		<mode>CREATE</mode>

		<viewConfig>
			<viewType>ENTITY</viewType>
		</viewConfig>
	</bean>
Page: /createEmployee.xpg

	<bean xmlns="http://www.znize.com/platform/objects"
			id="createEmployee" type="entity">
		<entityType>HR.EMP</entityType>
		<mode>CREATE</mode>

		<criteriaElements>
			<function name="EQ" property="user" nullable="false">#{containerBean.authenticatedUser}</function>
		</criteriaElements>

		<viewConfig>
			<viewType>ENTITY</viewType>

			<redirect action="cmd.Create"
				enabled="#{!empty param.redirectAfterCreate}"
				toURL="#{param.redirectAfterCreate}">
			</redirect>

		</viewConfig>
	</bean>

Example: User Sign Up


	<bean id="userSignUp" type="entity">
		<entityType>System.USR</entityType>
		<mode>CREATE</mode>

		<viewConfig>
			<viewType>ENTITY</viewType>
			<param name="countries" value="US,CN" />
		</viewConfig>
	</bean>
Parameter: countries
Embedded Beans: Criteria ElementsEmbedded Beans: EntitiesFrames / No Frames