一、根据一个字段进行修改
/**
* 根据一个字段进行修改(这个条件字段可以是主键, 也可以不是主键)
* @param dctx
* @param context
* @return
*/
public static Map<String,Object> updateUomByOneField(
DispatchContext dctx, Map<String,Object> context ){
//取得实体引擎实例
GenericDelegator delegator = dctx.getDelegator();
//添加需要修改的字段
Map<String,Object> updateFields = FastMap.newInstance();
updateFields.put("description", "修改后的现金管理系统"); //这里删除前面创建的基线产品信息
/*.... 如果有其它字段需要修改, 还可以继续把要修改的值放到updateFields中.*/
//设置修改条件
EntityCondition updateCon = EntityCondition.makeCondition("abbreviation", EntityComparisonOperator.EQUALS,"Cashm");
try {
//执行按条件修改操作
delegator.storeByCondition("Uom", updateFields, updateCon);
} catch (GenericEntityException e) {
Debug.logError(e, module);
//把指定的错误码对应的描述信息返回给服务调用者
return ReturnMapUtil.getErrorMap(DemoErrorMapping.BASE0005,updateCon.toString(),e.getMessage());
}
//把表示成功的信息返回给服务调用者
return ReturnMapUtil.getSuccessMap();
}
二、根据多条件进行修改
/**
* 根据多个字段进行修改(这个条件字段可以是主键, 也可以不是主键)
* @param dctx
* @param context
* @return
*/
public static Map<String,Object> updateUomByMoreField( DispatchContext dctx, Map<String,Object> context ){
//取得实体引擎实例
GenericDelegator delegator = dctx.getDelegator();
//添加需要修改的字段
Map<String,Object> updateFields = FastMap.newInstance();
updateFields.put("uomTypeId", "OTHER_MEASURE"); //这里删除前面创建的基线产品信息
/*.... 如果有其它字段需要修改, 还可以继续把要修改的值放到updateFields中.*/
//设置修改条件
List<EntityCondition> updateConList = FastList.newInstance();
updateConList.add( EntityCondition.makeCondition("abbreviation", EntityComparisonOperator.EQUALS,"BLP") );
updateConList.add( EntityCondition.makeCondition("description", EntityComparisonOperator.EQUALS,"基线产品") );
/*.... 如果有其它的修改条件, 还可以继续把修改条件放到updateConList中*/
EntityCondition updateCondition = EntityCondition.makeCondition(updateConList, EntityOperator.AND);
try {
//执行按条件修改操作
delegator.storeByCondition("Uom", updateFields, updateCondition);
} catch (GenericEntityException e) {
Debug.logError(e, module);
//把指定的错误码对应的描述信息返回给服务调用者
return ReturnMapUtil.getErrorMap(DemoErrorMapping.BASE0005,updateCondition.toString(),e.getMessage());
}
//把表示成功的信息返回给服务调用者
return ReturnMapUtil.getSuccessMap();
}
三、对一个已经查询出的记录进行修改
/**
* 对已经查询出的记录进行修改
* @param dctx
* @param context
* @return
*/
public static Map<String,Object> updateUomAfterQuery( DispatchContext dctx, Map<String,Object> context ){
//取得实体引擎实例
GenericDelegator delegator = dctx.getDelegator();
//获取服务引擎
LocalDispatcher dispatcher = dctx.getDispatcher();
//先调用查询服务从Uom表中查询出基线产品信息
Map<String,Object> queryInput = FastMap.newInstance();
Map<String,Object> queryOutput = null;
try {
queryOutput = dispatcher.runSync("queryUomByPrimaryKey", queryInput);
} catch (GenericServiceException e) {
Debug.logError(e, module);
//把指定的错误码对应的描述信息返回给服务调用者
return ReturnMapUtil.getErrorMap(DemoErrorMapping.BASE0007,e.getMessage());
}
//根据返回Map检查服务是否执行成功
if( !ServiceUtil.isSuccess(queryOutput)){
return queryOutput;
}
//从返回结果中取出查询到的基线产品信息
GenericValue oneUom = (GenericValue) queryOutput.get("oneUom");
//修改基线产品信息中的描述
oneUom.setString("description", "修改后的基线产品信息");
//保存
try {
oneUom.store();
} catch (GenericEntityException e) {
Debug.logError(e, module);
//把指定的错误码对应的描述信息返回给服务调用者
return ReturnMapUtil.getErrorMap(DemoErrorMapping.BASE0008,e.getMessage());
}
//返回
return ReturnMapUtil.getSuccessMap();
}