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

时间:2012-11-14 13:52:29

标签: java playframework playframework-2.0 ebean

我正在试图找出如何找到列表成员 - 但只有那些没有超过其过期日期的成员 - 这是该模型的属性之一。

现在我有:

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帖子,所以对我来说很容易!并提前感谢您的帮助!

2 个答案:

答案 0 :(得分:5)

嗯,您正在查找expires日期大于当前OR null(未设置)的所有行,对吗?

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

  • gt代表Greater Than
  • {li> lt Lower Than

在你的模型中添加查找器:

// 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);
}

答案 1 :(得分:1)

您可以使用迭代器并删除所有过期的通知。

类似的东西:

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

      Iterator<Notification> iterator = notifications.iterator();

      while (iterator.hasNext()) {
        Notification next = iterator.next();
        if (new Date().after(next.expires)) {
            iterator.remove();
        }
      }


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

      return ok(toJson(notifications));
  }

如果List是不可变的,你可以返回它的过滤副本。