编程语言
首页 > 编程语言> > java – Play 2.0 Framework – 找到所有尚未过期的内容

java – Play 2.0 Framework – 找到所有尚未过期的内容

作者:互联网

我试图弄清楚如何找到列表的成员 – 但只有那些没有超过其过期日期 – 这是模型的属性之一.

现在我有:

public static Result getAllNotifications() {
 List<Notification> notifications = Notification.getAllNotifications();

 for (Notification i: notifications) {
     List<Attachments> attachments = Attachments.findAllById(i.id);
     i.attached = attachments;
 }

    return ok(toJson(notifications));
}

在那里的某个地方,我需要检查单个通知的到期日期,如果今天超过该日期则不返回.

现在,notificatin的模型看起来像这样:

public class Notification extends Model {

/**
 * 
 */
private static final long serialVersionUID = 1L;

@Id
@NonEmpty
public Long id;

@Constraints.Required
public String title;

@Formats.DateTime(pattern = "dd/MM/yyyy")
public Date created = new Date();

@Constraints.Required
@Column(columnDefinition="TEXT")
public String text;

@Formats.DateTime(pattern = "dd/MM/yyyy")
public Date updated = new Date();

public Boolean status;

public Date expires;

public String author;

public List<Attachments> attached;

public Notification() {
}

public Notification(Long id, String title, String text) {
    this.created = new Date();
    this.title = title;
    this.text = text;
    this.id = id;
}

public static Model.Finder<String, Notification> find = new Model.Finder<String,     Notification>(String.class, Notification.class);

这是我的第一个Stackoverflow帖子,所以对我来说很容易!并提前感谢您的帮助!

解决方法:

嗯,你正在寻找到期日期大于当前行的所有行或它是空的(未设置),对吧?

在这种情况下你可以使用简单的DB comparission(迭代整个结果集肯定是错误的想法!)

> gt代表大于
> LT为低于

在你的模型中添加finders:

// If expires date is grater than current one, the Notification IS expired
public static List<Notification> findAllExpired() {
    return find.where().gt("expires", new Date()).findList();
}

// If expires date is lower than current one OR isn't set at all,
// the Notification is valid.
public static List<Notification> findAllNotExpired() {
    return find.where().or(
              Expr.lt("expires", new Date()),
              Expr.isNull("expires")
           ).findList();
}

因此,您将获得控制器中的列表未过期(或未过期)通知:

List<Notification> notExpiredList = Notification.findAllNotExpired();

// check in terminal
for (Notification notification : notExpiredList) {
    Logger.info("This Notification IS NOT expired: " + notification.title);
}

List<Notification> expiredList = Notification.findAllExpired();

// check in terminal
for (Notification notification : expiredList) {
    Logger.warn("This Notification IS expired: " + notification.title);
}

标签:java,playframework,playframework-2-0,ebean
来源: https://codeday.me/bug/20190517/1123855.html