Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I hope I understand the question correctly - you want to implement a news feed into your application and allow users to follow each other. The new followers need to be able to see the users actions. I am sure there are multiple other ways of solving this problem, but I will attempt to help you out by providing a solution that makes use of JAVA JDO to access the datastore. </p> <p>I would first design the <a href="https://developers.google.com/appengine/docs/java/datastore/jdo/relationships" rel="nofollow">entity relationships in JDO</a> as follows: </p> <pre><code>1 User to many actions. 1 User to many followers (User). 1 User to many following (User). </code></pre> <p>Here are simple JDO classes:</p> <p>User Class:</p> <pre><code>@PersistenceCapable(identityType=IdentityType.APPLICATION) public class User { @PrimaryKey @Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY) private Key key; @Persistent private String userId; // Google unique user ID, could also store user email. @Persistent private Set&lt;Key&gt; actions; @Persistent private Set&lt;Key&gt; followers; @Persistent private List&lt;Key&gt; following; public User(Key key, String userId) { this.key = key; this.userId = userId; this.actions = new HashSet&lt;Key&gt;(); this.followers = new HashSet&lt;Key&gt;(); this.following = new HashSet&lt;Key&gt;(); } public Key getKey() { return this.key; } public void addAction(Key actionKey) { this.actions.add(actionKey); } public void addActions(Set&lt;Key&gt; actionKeys) { this.actions.addAll(actionKeys); } public Set&lt;Key&gt; getActions() { return this.actions; } public void addFollower(Key followerKey) { this.followers.add(followerKey); } public void addFollowers(Set&lt;Key&gt; followerKeys) { this.followers.addAll(followerKeys); } public Set&lt;Key&gt; getFollowers() { return this.followers; } public void addFollowing(Key followingKey) { this.following.add(followingKey); } public void addAllFollowing(Set&lt;Key&gt; followingKeys) { this.following.addAll(followingKeys); } public Set&lt;Key&gt; getFollowing() { return this.following; } } </code></pre> <p>Action Class:</p> <pre><code>@PersistenceCapable(identityType=IdentityType.APPLICATION) public class Action { @PrimaryKey @Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY) private Key key; @Persistent Date date; @Persistent private String title; public Action(Key key, String title) { this.key = key; this.title = title; this.date = new Date(); // date of creation (now). } public Key getKey() { return this.key; } public void setTitle(String title) { this.title = title; } public String getTitle() { return this.title; } } </code></pre> <p>The Action class makes use of a Date property, you can refer to the <a href="https://developers.google.com/appengine/docs/java/datastore/entities#Java_Properties_and_value_types" rel="nofollow">documentation</a> for applicable data types in the datastore. When an action is created, <a href="http://docs.oracle.com/javase/6/docs/api/java/util/Date.html" rel="nofollow">a Date object is allocated and initialized so that it represents the time at which it was allocated, measured to the nearest millisecond</a>.</p> <p>In my example above I linked the entities by their Keys, you could instead link them by their classes as follows:</p> <pre><code>List&lt;Action&gt; actions; </code></pre> <p>The relationship in my example is one of an unowned one-to-many relationship, perhaps it should be owned one-to-many. More information <a href="https://developers.google.com/appengine/docs/java/datastore/jdo/relationships" rel="nofollow">here</a> for your to take a look and perhaps decide which would be best for your solution.</p> <p>Once the relationships have been defined, you can <a href="https://developers.google.com/eclipse/docs/endpoints-addentities" rel="nofollow">create your endpoint classes around the JDO model classes</a>. This will create basic api methods. You might want to change the endpoint class methods to suit your needs, for example change the way an action is created. A basic example would be to create the key from the actions title as follows (ActionEnpoint.java):</p> <pre><code>... @ApiMethod(name = "insertAction") public Action insertAction( @Named("title") String title ) { PersistenceManager pm = getPersistenceManager(); Key key = KeyFactory.createKey(Action.class.getSimpleName(), title); Action action = null; try { action = new Action(key, title); pm.makePersistent(action); } finally { pm.close(); } return action; } ... </code></pre> <p>If you want to, you can add a method to your UserEndpoint class to query the datastore and return all actions belonging to that user and per date using the <a href="https://developers.google.com/appengine/docs/java/datastore/jdo/queries" rel="nofollow">datastore query objects</a>. </p> <p>You need to add a method to your UserEndpoint class that allows you to add an action to that user, here is a simple example:</p> <pre><code>... @ApiMethod(name = "addActionToUser") public Achiever addActionToUser( @Named("userId") String userId, @Named("actionTitle") String actionTitle) { PersistenceManager pm = getPersistenceManager(); Key userKey = KeyFactory.createKey(User.class.getSimpleName(), userId); Key actionKey = KeyFactory.createKey(Action.class.getSimpleName(), actionTitle); User user = null; try { user = (User) pm.getObjectById(User.class, userKey); user.addAction(actionKey); pm.makePersistent(user); } catch (Exception e) { } return user; } ... </code></pre> <p>Once all of the above is complete you can easily get the list of actions per user by calling the getUser method in your UserEndpoint class, which returns a User object. You can then call [ReturnedUserObject].getActions(). A new follower can now view all of the "followees" actions by just calling the api method to get that "followees" object and get his/her actions. You can then just sort the actions by date or however you envision it.</p> <p>I hope I understood your question correctly, I was unsure about the first component you mentioned, but it seemed as though you got your relationships mixed up. I hope this solution points you in the right direction at least :).</p> <p>If you need any additional help or clarification, or my answer was completely off point to what you were looking for then please let me know. </p> <p>Kind regards, Miki</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload