添加排序顺序Objectify

时间:2016-08-27 16:27:13

标签: android sorting objectify

我有一个课程。

package com.data.model;

import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Index;

import java.io.Serializable;

/**
 * Created by Lenovo on 16/07/2016.
 * base class. all locations implements this
 */
@Entity
public class Place implements Serializable {
    // place id
     @Id
    protected long id;
    // place latitude
    protected  double myLat;
    // place longtitude
    protected  double myLongtitude;
    // place name
    @Index
    protected  String placeName;
    // altitude
    protected double altitude;

    public Place() {
    }

    public Place(long id, double myLat, double myLongtitude, String placeName, double altitude) {
        this.id = id;
        this.myLat = myLat;
        this.myLongtitude = myLongtitude;
        this.placeName = placeName;
        this.altitude = altitude;
    }

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public double getMyLat() {
        return myLat;
    }

    public void setMyLat(double myLat) {
        this.myLat = myLat;
    }

    public double getMyLongtitude() {
        return myLongtitude;
    }

    public void setMyLongtitude(double myLongtitude) {
        this.myLongtitude = myLongtitude;
    }

    public String getPlaceName() {
        return placeName;
    }

    public void setPlaceName(String placeName) {
        this.placeName = placeName;
    }

    public double getAltitude() {
        return altitude;
    }

    public void setAltitude(double altitude) {
        this.altitude = altitude;
    }
}

我有一个子类事件。

package com.data.model;

import com.googlecode.objectify.annotation.Index;
import com.googlecode.objectify.annotation.Subclass;

/**
 * Created by Lenovo on 16/07/2016.
 */
@Subclass(index = true)
public class Incidents extends  Place {
    // incident date
    @Index
    private  long incidentdate;
    // incident description
    @Index
    private  String incidentDescription;
    // who reported it
    private String reportedBy;
    //Image urs
    private  String[]imageurl;
    // default constructor
    public Incidents(){}

    public Incidents(long id, double myLat, double myLongtitude, String placeName, double altitude, long incidentdate, String incidentDescription, String reportedBy,String[]imageurl) {
        super(id, myLat, myLongtitude, placeName, altitude);
        this.incidentdate = incidentdate;
        this.incidentDescription = incidentDescription;
        this.reportedBy = reportedBy;
        this.imageurl=imageurl;
    }

    public String[] getImageurl() {
        return imageurl;
    }

    public void setImageurl(String[] imageurl) {
        this.imageurl = imageurl;
    }

    public long getIncidentdate() {
        return incidentdate;
    }

    public void setIncidentdate(long incidentdate) {
        this.incidentdate = incidentdate;
    }

    public String getIncidentDescription() {
        return incidentDescription;
    }

    public void setIncidentDescription(String incidentDescription) {
        this.incidentDescription = incidentDescription;
    }

    public String getReportedBy() {
        return reportedBy;
    }

    public void setReportedBy(String reportedBy) {
        this.reportedBy = reportedBy;
    }

    @Override
    public String toString() {
        return  "An Incident has occured at " + this.placeName +
                 " Incident details" + this.incidentDescription +
                " Time :" + this.incidentdate +
                 "Reported by"+ this.reportedBy;

    }
}

我从以上两个正在运行的类中创建了一个端点。 我想使用incidentDate字段添加降序排序顺序。 所以这是方法" listIncident"。

 @ApiMethod(
            name = "listIncidents",
            path = "incidents",
            httpMethod = ApiMethod.HttpMethod.GET)
    public CollectionResponse<Incidents> listIncidents(@Nullable @Named("cursor") String cursor, @Nullable @Named("limit") Integer limit) {
        limit = limit == null ? DEFAULT_LIST_LIMIT : limit;
        Query<Incidents> query = ofy().load().type(Incidents.class).order("-incidentdate").limit(limit);
        if (cursor != null) {
            query = query.startAt(Cursor.fromWebSafeString(cursor));
        }
        QueryResultIterator<Incidents> queryIterator = query.iterator();
        List<Incidents> incidentsList = new ArrayList<Incidents>(limit);
        while (queryIterator.hasNext()) {
            incidentsList.add(queryIterator.next());
        }
        return CollectionResponse.<Incidents>builder().setItems(incidentsList).setNextPageToken(queryIterator.getCursor().toWebSafeString()).build();
    }

    private void checkExists(long id) throws NotFoundException {
        try {
            ofy().load().type(Incidents.class).id(id).safe();
        } catch (com.googlecode.objectify.NotFoundException e) {
            throw new NotFoundException("Could not find Incidents with ID: " + id);
        }
    }
}

当我部署到App Engine并添加一些事件时,执行&#34; ListIncidents&#34; ,我得到以下答复。

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "backendError",
    "message": "com.google.appengine.api.datastore.DatastoreNeedIndexException: no matching index found. recommended index is:\n- kind: Place\n  properties:\n  - name: ^i\n  - name: incidentdate\n    direction: desc\n\nThe suggested index for this query is:\n    <datastore-index kind=\"Place\" ancestor=\"false\" source=\"manual\">\n        <property name=\"^i\" direction=\"asc\"/>\n        <property name=\"incidentdate\" direction=\"desc\"/>\n    </datastore-index>\n\n"
   } 

我应该在代码中添加什么内容,使其按日期按降序对事件进行排序?

罗纳德

0 个答案:

没有答案