5

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

现在我有:

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

在那里的某个地方,我需要检查单个通知的到期日期,如果今天已经过了那个日期,就不要退回它。

现在,通知模型如下所示:

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 上发帖,所以放轻松!并提前感谢您的帮助!

4

2 回答 2

5

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

在这种情况下,您可以只使用简单的数据库比较(迭代整个结果集绝对是错误的想法!

  • gt代表Greater Than
  • 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);
}
于 2012-11-14T19:44:14.957 回答
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));
  }

如果列表是不可变的,您可以返回它的过滤副本。

于 2012-11-14T14:13:58.363 回答