Hello,

I'm creating a class that help me keep track on which dates certain
types of activities occur (user registration, logins, etc).

I'm having a problem getting my incrementCounter() method to correct
query for whether an event of that type has already occurred on that
date. I believe it's because I'm not doing the date comparison of the
query correctly. Any help would be appreciated as there isn't a lot of
sample code out there on date queries.

Thanks,
cw

-------------------

import java.util.*;
import java.util.logging.Logger;
import javax.jdo.Query;
import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import java.util.Properties;
import com.something.PMF;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class DayCount {
        private static final Logger log = Logger.getLogger
(DayCount.class.getName());

        @PrimaryKey
        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
        private Long id;

        @Persistent
        private String countType;

        @Persistent
        private Date whichDay;

        @Persistent
        private int count;

        public DayCount(String countType, Date whichDay) {
                this.countType = countType;
                this.whichDay = removeTime(whichDay);
                this.count = 0;
        }

        public static List<DayCount> getCounts(String countType) {
                PersistenceManager pm = PMF.get().getPersistenceManager();
                Query query = pm.newQuery(com.mathblitz.models.DayCount.class);
                query.setFilter("countType == countTypeParam");
                query.setOrdering("whichDay desc");
                query.declareParameters("String countTypeParam");

                List<DayCount> results = null;

                try {
                        results = (List<DayCount>) query.execute(countType);
                } finally {
                    query.closeAll();
                }

                return results;
        }

        public static boolean incrementCount(String countType, Date whichDay)
{
                boolean returnVal = false;

                PersistenceManager pm = PMF.get().getPersistenceManager();
                Query query = pm.newQuery(DayCount.class);
                query.setFilter("countType == countTypeParam && whichDay ==
whichDayParam");
                query.declareImports("import java.util.Date");
                query.declareParameters("String countTypeParam, Date
whichDayParam");

                List<DayCount> results = null;
                DayCount dc = null;

                try {
                        results = (List<DayCount>) query.execute(countType, 
removeTime
(whichDay));
                        log.warning("Results: " + results.size() + " for date: 
" +
removeTime(whichDay));

                        if (results.size() == 0) {
                                dc = new DayCount(countType, whichDay);
                                dc.count = 1;
                                log.warning("New Object Count: " + dc.count);
                                pm.makePersistent(dc);
                                returnVal = true;
                        } else if (results.size() == 1) {
                                dc = results.get(0);
                                dc.count++;
                                log.warning("Existing Object Count: " + 
dc.count);
                                pm.makePersistent(dc);
                                returnVal = true;
                        } else {
                                log.warning("Too many results for countType: " 
+ countType + "
day: " + whichDay.toString());
                        }
                } catch (Exception e) {
                        log.warning(e + "");
                } finally {
                    query.closeAll();
                    pm.close();
                }

                return returnVal;
        }

        // Get rid of time stamp
        public static Date removeTime(Date whichDay) {
                whichDay.setHours(0);
                whichDay.setMinutes(0);
                whichDay.setSeconds(0);
                return whichDay;
        }
}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to