Im using the following technologies: Google App Engine, GAE Datastore, Struts2. Common lang java library were also used.
Because of the limitation on aggregation for datastore, im using jagg for aggregation on List of Profile for some sort of report.
Profile object have basic properties like email, name, gender, country, dateCreated, etc.
For Chart Report I have below:
public class ChartReport {
private Date date = null;
private String country = null;
private Map<String, Integer> count = null; //by gender
public ChartReport() {
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("m", 0);
map.put("f", 0);
map.put(null, 0);
count = map;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public Map<String, Integer> getCount() {
return count;
}
public void setCount(Map<String, Integer> count) {
this.count = count;
}
}
I used map for count so that report will have different count for male, female and null.
For my Struts2 action, I have below:
if ( dateFrom != null && dateTo != null )
list = access.getNewProfiles(dateFrom, dateTo);
//remove timestamp
if ( type.equals("Daily")){
for(Profile p: list)
p.setDateCreated( DateUtils.truncate(p.getDateCreated(), Calendar.DATE) );
}else{
for(Profile p: list)
p.setDateCreated( DateUtils.truncate(p.getDateCreated(), Calendar.MONTH) );
}
List<String> properties = new ArrayList<String>();
properties.add("dateCreated");
properties.add("gender");
List<Aggregator> aggregators = new ArrayList<Aggregator>();
aggregators.add(new CountAggregator("*"));
List<AggregateValue<Profile>> aggValues = Aggregations.groupBy(
list, properties, aggregators);
Profile profile = null;
ChartReport report = null;
reportList = new ArrayList<ChartReport>();
Date date = null;
for (AggregateValue<Profile> aggValue : aggValues ){
profile = aggValue.getObject();
Aggregator aggregator = aggregators.get(0); //only one aggregator
if ( date == null || ! date.equals( profile.getDateCreated() ) ){
report = new ChartReport();
report.setDate( profile.getDateCreated() );
report.getCount().put(profile.getGender(), Integer.parseInt(String.valueOf(aggValue.getAggregateValue(aggregator))));
reportList.add(report);
}else{
report.getCount().put(profile.getGender(), Integer.parseInt(String.valueOf(aggValue.getAggregateValue(aggregator))));
date = profile.getDateCreated();
continue;
}
date = profile.getDateCreated();
}
The condition ( date == null || ! date.equals( profile.getDateCreated() ) ) was used for grouping purposes. The same dateCreated value will be in one group.
Everything works perfectly. But I need suggestions, comments, and feedback for my code. Can I improve it's performance in terms of speed? Include in your comment the way I write code.
Thanks a lot.