Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play 2.0 Framework - find all that haven't expired

I'm trying to figure out how I can go about finding members of a list - but only ones that haven't past their expired date - which is one of the properties of the model.

Right now I have:

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

Somewhere in there I need to check the expiration date of an individual notification and not return it if today is past that date.

Right now that model for a notificatin looks like this:

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

This is my first ever Stackoverflow post so go easy on me! And thank you, in advance, for the help!

like image 816
pixelworlds Avatar asked Nov 14 '12 13:11

pixelworlds


People also ask

What is Play Framework?

The Play Framework combines productivity and performance making it easy to build scalable web applications with Java and Scala. Play is developer friendly with a "just hit refresh" workflow and built-in testing support. With Play, applications scale predictably due to a stateless and non-blocking architecture.

What version of play do I need to use?

We recommend that you use the latest version of Play. However, if you need to work with an older version, you will find links on this page. Versions of Play older than 2.2 are packaged in zip files. The traditional Play zip provides a play command to create new applications, run tests, and run the application.

Can I work with an older version of Google Play?

However, if you need to work with an older version, you will find links on this page. Versions of Play older than 2.2 are packaged in zip files. The traditional Play zip provides a play command to create new applications, run tests, and run the application. After 2.3.0, the play command was replaced with sbt.


1 Answers

Hm, you're looking for all rows which expires date is bigger than current one OR it's null (not set), right?

In such case you can just use simple DB comparission (iterating whole resultset is definitely WRONG idea!)

  • gt stands for Greater Than
  • lt for Lower Than

in your model add 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();
}

So you'll get the list not expired (or not expired) notifications in your controller:

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);
}
like image 70
biesior Avatar answered Sep 21 '22 12:09

biesior