gpt4 book ai didi

java - 在 ofbiz 中查找今天生日的人 : how to extract month and day from Ofbiz entity

转载 作者:行者123 更新时间:2023-12-01 17:51:50 24 4
gpt4 key购买 nike

我有兴趣使用 ofbiz 查找今天生日的人。

这是用户的实体:

<entity entity-name="User">
<field name="identifier" type="id-long"></field>
<field name="dateOfBirth" type="date"></field>
</entity>

这是搜索今天生日的用户的损坏代码:

SimpleDateFormat monthDayFormat = new SimpleDateFormat("MM-dd");
Calendar cal = Calendar.getInstance();
String today = monthDayFormat.format(cal.getTime());

List<GenericValue> people = dctx.getDelegator().findList("User",
EntityCondition.makeCondition("dateOfBirth", EntityOperator.LIKE, "%".today),
null, null, null, false);

这显然不起作用,因为我们正在尝试比较字符串和日期对象。

使其工作的另一种尝试是创建一个 View 实体,并将日期转换为两个整数:日和月,或者使用上面的代码将日期转换为字符串。显然,我找不到任何让它发挥作用的方法。

最佳答案

最有效的方法是构建 View 实体。以下定义应该适用于多个数据库(例如 PostgreSQL ),即支持 EXTRACT 的数据库功能:

<view-entity entity-name="UserView">
<member-entity entity-alias="USER" entity-name="User"/>
<alias-all entity-alias="USER"/>
<alias name="dayOfBirth" function="extract-day" entity-alias="USER" field="dateOfBirth"/>
<alias name="monthOfBirth" function="extract-month" entity-alias="USER" field="dateOfBirth"/>
</view-entity>

通过上面的 View ,您可以通过约束两个新字段轻松地执行查询:

List<GenericValue> users = EntityQuery.use(dctx.getDelegator()).from("UserView").where("dayOfBirth", day, "monthOfBirth", month).queryList();

或者,如果您正在编写 Groovy 脚本,则其在 OFBiz DSL 中的等效项:

List<GenericValue> users = from("UserView").where("dayOfBirth", day, "monthOfBirth", month).queryList();

或者,按照完全不同的方法,您可以通过添加“dayOfBirth”和“monthOfBirth”字段(类型为“numeric”)来扩展“User”实体:

<entity entity-name="User">
<field name="identifier" type="id-long"></field>
<field name="dateOfBirth" type="date"></field>
<field name="dayOfBirth" type="numeric"></field>
<field name="monthOfBirth" type="numeric"></field>
</entity>

然后,您可以定义一个 eca 规则来触发服务的执行,以便在每次使用非空 dateOfBirth 字段创建或更新用户记录时填充两个新字段:

<eca entity="User" operation="create-store" event="return">
<condition field-name="dateOfBirth" operator="is-not-empty"/>
<action service="populateDayAndMonthOfBirth" mode="sync"/>
</eca>

populateDayAndMonthOfBirth 服务的服务定义如下所示:

<service name="populateDayAndMonthOfBirth">
<attribute name="identifier" type="String" mode="IN" optional="false"/>
</service>

(请填写缺少的属性,如“引擎”、“位置”和“调用”)。该服务将简单地选择记录,从其 dateOfBirth 字段中提取代表日期和月份的整数,并将它们存储在 dayOfBirth 和 MonthOfBirth 字段中。

关于java - 在 ofbiz 中查找今天生日的人 : how to extract month and day from Ofbiz entity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49285834/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com