您的位置:首页 > 其它

WCS学习笔记(第九天) - MyNewControllerCmd整合Bonus entity bean

2013-10-14 18:54 429 查看
1. 修改BonusDataBean.java实现:

a. 参数userId、totalBonusPoints的get、set方法

/// Section 1 ///////////////////////////////////////////////

// create fields and accessors (setter/getter methods)

private java.lang.String userId;
private java.lang.Integer totalBonusPoints;

public java.lang.String getUserId() {
return userId;
}

public void setUserId(java.lang.String newUserId) {
userId = newUserId;

///////////////////////////////////////
/// Section A : instantiate BonusAccessbean

if (userId != null)
this.setInitKey_memberId(new Long(newUserId));

///////////////////////////////////////
}

public java.lang.Integer getTotalBonusPoints() {
return totalBonusPoints;
}
public void setTotalBonusPoints(java.lang.Integer newTotalBonusPoints) {
totalBonusPoints= newTotalBonusPoints;
}

//// End of section 1 ////////////////////////////////////////////


b. 创建BonusDataBean(BonusAccessBean bb)方法:

/// Section 2///////////////////////////////////////////////

// create a new constructor for passing access bean into databean
// so that JSP can work with the access bean

public BonusDataBean(BonusAccessBean bb)
throws com.ibm.commerce.exception.ECException {
try {
super.setEJBRef(bb.getEJBRef());
}  catch (javax.ejb.FinderException e) {
throw new ECSystemException(ECMessage._ERR_CREATE_EXCEPTION,
"BonusDataBean", "BonusDataBean(bb)");
} catch (javax.naming.NamingException e) {
throw new ECSystemException(ECMessage._ERR_NAMING_EXCEPTION,
"BonusDataBean", "BonusDataBean(bb)");
} catch (java.rmi.RemoteException e) {
throw new ECSystemException(ECMessage._ERR_REMOTE_EXCEPTION,
"BonusDataBean", "BonusDataBean(bb)");
} catch (javax.ejb.CreateException e) {
throw new ECSystemException(ECMessage._ERR_CREATE_EXCEPTION,
"BonusDataBean", "BonusDataBean(bb)");
}
}

//// End of section 2 ////////////////////////////////////////////


c. 具体实现populate()方法体

//// Section 3 /////////////////////////////////////////////////

// set additional data field that is used for instantiating BonusAccessbean

try
{

setUserId(getRequestProperties().getString("taskOutputUserId"));

try {
super.refreshCopyHelper();
} catch (javax.ejb.FinderException e) {
throw new ECSystemException(ECMessage._ERR_CREATE_EXCEPTION,
"BonusDataBean", "populate");
} catch (javax.naming.NamingException e) {
throw new ECSystemException(ECMessage._ERR_NAMING_EXCEPTION,
"BonusDataBean", "populate");
} catch (java.rmi.RemoteException e) {
throw new ECSystemException(ECMessage._ERR_REMOTE_EXCEPTION,
"BonusDataBean", "populate");
} catch (javax.ejb.CreateException e) {
throw new ECSystemException(ECMessage._ERR_CREATE_EXCEPTION,
"BonusDataBean", "populate");
}

}
catch (ParameterNotFoundException e){}

///// End of Section 3 /////////////////////////////////////////
}


d. 具体实现setRequestProperties(com.ibm.commerce.datatype.TypedProperty aParam)
方法体

/// Section 4 ///////////////////////////////////////////

// copy input TypedProperties to local

requestProperties = aParam;

/// End of section 4 ////////////////////////////////////


2. 修改MyNewTaskCmd.java接口代码

a. 打开 WebSphereCommerceServerExtensionsLogic >
src >
com.ibm.commerce.sample.commands.MyNewTaskCmd.java


b. 导入BonusAccessBean

/// Import section 2 /////////////////////////////////////
import com.ibm.commerce.extension.objects.BonusAccessBean;
/// End of import section 2 //////////////////////////////


c. 增加接口方法

/// Section 4 ////////////////////////////////////////////////

public java.lang.Integer getOldBonusPoints();
public Integer getTotalBonusPoints();

public void setBonusAccessBean(BonusAccessBean bb);
public BonusAccessBean getBonusAccessBean();

/// End of section 4//////////////////////////////////////////


3. 修改MyNewTaskCmdImpl.java接口代码实现

a. 打开 WebSphereCommerceServerExtensionsLogic >
src >
com.ibm.commerce.sample.commands.MyNewTaskCmdImpl.java


b. 导入BonusAccessBean

/// Import section 2 //////////////////////////////////////
import com.ibm.commerce.extension.objects.BonusAccessBean;
///  End of Import section 2 ///////////////////////////////

c. 声明并实现set、get方法

//// Section 3A //////////////////////////////////////////////

private java.lang.Integer oldBonusPoints;
private java.lang.Integer totalBonusPoints;

private BonusAccessBean bb = null;

////End of Section 3A /////////////////////////////////////////

//// Section 3B //////////////////////////////////////////////

public void setBonusAccessBean(BonusAccessBean newBB) {
bb = newBB;
}

public BonusAccessBean getBonusAccessBean(){
return bb;
}

public java.lang.Integer getOldBonusPoints() {
return oldBonusPoints;
}

public Integer getTotalBonusPoints(){
return totalBonusPoints;
}

/// End of section 3B ///////////////////////////////////////////

d. 增加validateParameters方法代码实现

// section 2 /////////////////////////////////////////////////////

try {
oldBonusPoints = bb.getBonusPoint();
} catch (javax.ejb.FinderException e) {
try {
// If bb is null, create a new instance
short optCounter=(short)0;
bb = new BonusAccessBean(new Long(foundUserId), new Integer(0), optCounter);
oldBonusPoints = new Integer(0);
} catch (javax.ejb.CreateException ec) {
throw new ECSystemException(ECMessage._ERR_CREATE_EXCEPTION,
this.getClass().getName(), "validateParameters");
} catch (javax.naming.NamingException ec) {
throw new ECSystemException(ECMessage._ERR_NAMING_EXCEPTION,
this.getClass().getName(), "validateParameters");
} catch (java.rmi.RemoteException ec) {
throw new ECSystemException(ECMessage._ERR_REMOTE_EXCEPTION,
this.getClass().getName(), "validateParameters");
}
} catch (javax.naming.NamingException e) {
throw new ECSystemException(ECMessage._ERR_NAMING_EXCEPTION,
this.getClass().getName(), "validateParameters");
} catch (java.rmi.RemoteException e) {
throw new ECSystemException(ECMessage._ERR_REMOTE_EXCEPTION,
this.getClass().getName(), "validateParameters");
} catch (javax.ejb.CreateException e) {
throw new ECSystemException(ECMessage._ERR_CREATE_EXCEPTION,
this.getClass().getName(), "validateParameters");
}

// end of section 2 ///////////////////////////////////////////////

e. 增加performExecute方法代码实现

/// Section 2 ////////////////////////////////////////////////////// use BonusAccessBean to update new bonus point

int newBP =  oldBonusPoints.intValue() + getInputPoints().intValue();
totalBonusPoints = new Integer (newBP);
bb.setBonusPoint(totalBonusPoints)   ;

try {
bb.commitCopyHelper();
} catch (javax.ejb.FinderException e) {
throw new ECSystemException(ECMessage._ERR_FINDER_EXCEPTION,
this.getClass().getName(), "performExecute");
} catch (javax.naming.NamingException e) {
throw new ECSystemException(ECMessage._ERR_NAMING_EXCEPTION,
this.getClass().getName(), "performExecute");
} catch (java.rmi.RemoteException e) {
throw new ECSystemException(ECMessage._ERR_REMOTE_EXCEPTION,
this.getClass().getName(), "performExecute");
} catch (javax.ejb.CreateException e) {
throw new ECSystemException(ECMessage._ERR_CREATE_EXCEPTION,
this.getClass().getName(), "performExecute");
}

/// End of section 2 ////////////////////////////////////////////


4. 修改MyNewControllerCmdImpl.java控制器代码实现

a. 打开 WebSphereCommerceServerExtensionsLogic
> src >
com.ibm.commerce.sample.commands.MyNewControllerCmdImpl.java


b. 增加参数

/// Section 3 ////////////////////////////////////////////////
/// Create an instance variable of type AccessVector to hold
/// the resources and a BonusAccessBean instance variable for
/// access control purposes.

private AccessVector resources = null;
private BonusAccessBean bb = null;

/// End of Section 3 /////////////////////////////////////////

c. 增加getResources()方法实现

/// AccessControl Section ////////////////////////////////////

public AccessVector getResources() throws ECException{

if (resources == null ) {

/// use UserRegistryAccessBean to check user reference number

String refNum = null;
String methodName = "getResources";

rrb = new UserRegistryAccessBean();

try {
rrb = rrb.findByUserLogonId(getUserName());
refNum = rrb.getUserId();
} catch (javax.ejb.FinderException e) {
throw new ECSystemException(ECMessage._ERR_FINDER_EXCEPTION,
this.getClass().getName(),methodName,e);
} catch (javax.naming.NamingException e) {
throw new ECSystemException(ECMessage._ERR_NAMING_EXCEPTION,
this.getClass().getName(), methodName,e);
} catch (java.rmi.RemoteException e) {
throw new ECSystemException(ECMessage._ERR_REMOTE_EXCEPTION,
this.getClass().getName(), methodName,e);
} catch (javax.ejb.CreateException e) {
throw new ECSystemException(ECMessage._ERR_CREATE_EXCEPTION,
this.getClass().getName(), methodName,e);
}

/// find the bonus bean for this registered user

bb = new com.ibm.commerce.extension.objects.BonusAccessBean();
try {
if (refNum != null) {
bb.setInitKey_memberId(new Long(refNum));
bb.refreshCopyHelper();
resources = new AccessVector(bb);
}
} catch (javax.ejb.FinderException e) {

//We don't have a bonus object so return the container that will hold the
//bonus object when it's created
UserAccessBean uab = new UserAccessBean();
uab.setInitKey_MemberId(refNum);
resources = new AccessVector(uab);
return resources;

} catch (javax.naming.NamingException e) {
throw new ECSystemException(ECMessage._ERR_NAMING_EXCEPTION,
this.getClass().getName(), methodName);
} catch (java.rmi.RemoteException e) {
throw new ECSystemException(ECMessage._ERR_REMOTE_EXCEPTION,
this.getClass().getName(), methodName);
} catch (javax.ejb.CreateException e) {
throw new ECSystemException(ECMessage._ERR_CREATE_EXCEPTION,
this.getClass().getName(), methodName);
}

}
return resources;
}

/// End of AccessControl Section //////////////////////////////////

d. 修改performExecute方法

// Section 4E ////////////////////////////////////
/// pass bb instance variable to the task command
cmd.setBonusAccessBean(bb);
// End of section 4E /////////////////////////////

// Section 4G ///////////////////////////////////////
if (cmd.getOldBonusPoints() != null) {
rspProp.put("oldBonusPoints", cmd.getOldBonusPoints());
}
// End of section 4G /////////////////////////////

// Section 4H ///////////////////////////////////////
///Instantiate the bonus data bean , then put it to response properties
BonusDataBean bdb  =
new com.ibm.commerce.sample.databeans.BonusDataBean(
cmd.getBonusAccessBean());
rspProp.put("bdbInstance", bdb );
// End of section 4H ////////////////////////////////


5. 确认商店(如:Madisons)的对应会员ID:

a. 启动wcs服务器;

b. IE打开:URL: http://localhost/webapp/wcs/admin/servlet/db.jsp;

c. 查询sql:

select member_id from storeent where storeent_id=10051;


6. 注册页面:

a. 停止server

b. 编写SampleACPolicy.xml 和 SampleACPolicy_en_US.xml 放在
WCDE_installdir\xml\policies\xml (ConsumerDirectMemberId替换为sql查询的-2003)

SampleACPolicy.xml

<?xml version="1.0" encoding="ISO-8859-1" standalone="no" ?>
<!DOCTYPE Policies SYSTEM "../dtd/accesscontrolpolicies.dtd">

<Policies>
<Action Name="MyNewControllerCmd"
CommandName="com.ibm.commerce.sample.commands.MyNewControllerCmd">
</Action>

<ResourceCategory Name="com.ibm.commerce.extension.objects.BonusResourceCategory"
ResourceBeanClass="com.ibm.commerce.extension.objects.Bonus" >
<ResourceAction Name="MyNewControllerCmd"/>
</ResourceCategory>

<ActionGroup Name="MyNewControllerCmdActionGroup"
OwnerID="RootOrganization">
<ActionGroupAction Name="MyNewControllerCmd"/>
</ActionGroup>

<ResourceGroup Name="BonusResourceGroup"  OwnerID="RootOrganization" >
<ResourceGroupResource Name="com.ibm.commerce.extension.objects.BonusResourceCategory" />
</ResourceGroup>

<Policy Name="AllUsersUpdateBonusResourceGroup"
OwnerID="-2003"
UserGroup="AllUsers"
UserGroupOwner="RootOrganization"
ActionGroupName="MyNewControllerCmdActionGroup"
ResourceGroupName="BonusResourceGroup"
RelationName="creator"
PolicyType="groupableStandard">
</Policy>

<Policy Name="AllUsersExecuteMyNewControllerCmdOnUserResource"
OwnerID="-2003"
UserGroup="AllUsers"
UserGroupOwner="RootOrganization"
ActionGroupName="MyNewControllerCmdActionGroup"
ResourceGroupName="UserDataResourceGroup"
RelationName="owner"
PolicyType="groupableStandard">
</Policy>

<PolicyGroup Name="ManagementAndAdministrationPolicyGroup" OwnerID="RootOrganization">
<!-- Define policies in this policy group -->
<PolicyGroupPolicy 	Name="AllUsersUpdateBonusResourceGroup" PolicyOwnerID="-2003" />
<PolicyGroupPolicy 	Name="AllUsersExecuteMyNewControllerCmdOnUserResource" PolicyOwnerID="-2003" />

</PolicyGroup>

</Policies>


SampleACPolicy_en_US.xml

<?xml version="1.0" encoding="ISO-8859-1" standalone="no" ?>
<!DOCTYPE PoliciesNLS SYSTEM "../dtd/accesscontrolpoliciesnls.dtd">

<PoliciesNLS LanguageID="en_US">

<Action_nls ActionName="com.ibm.commerce.sample.commands.MyNewControllerCmd"
DisplayName_nls="MyNewControllerCmd"
Description_nls="Run MyNewControllerCmd" />

<ActionGroup_nls ActionGroupName="MyNewControllerCmdActionGroup"
DisplayName_nls="MyNewControllerCmd Action Group"
Description_nls="MyNewControllerCmd Action Group"  />

<ResourceCategory_nls ResourceCategoryName="com.ibm.commerce.extension.objects.Bonus"
DisplayName_nls="Bonus Resource"
Description_nls="Bonus Resource"  />

<ResourceGroup_nls ResourceGroupName="BonusResourceGroup"
DisplayName_nls="Bonus Resources Group"
Description_nls="Bonus Resources"  />

<Policy_nls PolicyName="AllUsersUpdateBonusResourceGroup" OwnerID="-2003"
DisplayName_nls="All Users Update Bonus Resource Group"
Description_nls="All Users Update Bonus Resource Group"  />

<Policy_nls PolicyName="AllUsersExecuteMyNewControllerCmdOnUserResource" OwnerID="-2003"
DisplayName_nls="All Users Execute MyNewControllerCmd On User Resource"
Description_nls="All Users Execute MyNewControllerCmd On User Resource"  />

</PoliciesNLS>


c. 命令定位到WCDE_installdir\bin
(如:cd C:\IBM\WCDE_PRO70\bin),执行下面命令





7. 修改JSP页面:

a. 在MyNewJSPTemplate.jsp的SECTION 9中增加下面代码,用于显示bean传递和更新的信息:

<!-- SECTION 9 -->

<h2><fmt:message key="BonusAdmin" bundle="${tutorial}" /> </h2>

<c:if test="${!empty taskOutputUserId}">
<ul>
<li>
<b>
<fmt:message key="PointBeforeUpdate" bundle="${tutorial}" />
<c:out value="${oldBonusPoints}"/>
</b>
</li>
<li>
<b>
<fmt:message key="PointAfterUpdate" bundle="${tutorial}" />
<c:out value="${bdbInstance.bonusPoint}" />
</b>
</li>
</ul>
</c:if>

<br />
<b><fmt:message key="EnterPoint" bundle="${tutorial}" /></b><p />

<form name="Bonus" action="MyNewControllerCmd">
<table>
<tr>
<td>
<b>Logon ID </b>
</td>
<td>
<input type="text" name="input1" value="<c:out
value="${userName}"/>" />
</td>
</tr>
<tr>
<td>
<b>Bonus Point</b>
</td>
<td>
<input type="text" name="input2" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" />
</td>
</tr>
</table>
</form>

<!-- END OF SECTION 9 -->


8. 测试:

a. 启动server

b. 登陆前台用户

c. 访问: http://localhost/webapp/wcs/stores/servlet/MyNewControllerCmd?input1=logon_id&input2=2000 (注:由于缓存可在url后加如:
?StoreId=10051 )




参考: http://pic.dhe.ibm.com/infocenter/wchelp/v7r0m0/topic/com.ibm.commerce.developer.tutorial.doc/tutorial/ttd18a.htm
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐