//Bidirectional subscription activation
public boolean activateSubscription(String from, String to) {
Query q_from = new Query(ACCOUNT_KIND);
q_from.setKeysOnly();
q_from.addFilter(ACCOUNT_ID_PROPERTY, FilterOperator.EQUAL, from);
PreparedQuery from_key = datastore.prepare(q_from);
Entity e_from = from_key.asSingleEntity();
if(e_from == null)
return false;
Key key_from = e_from.getKey();
Query q_to = new Query(ACCOUNT_KIND);
q_to.setKeysOnly();
q_to.addFilter(ACCOUNT_ID_PROPERTY, FilterOperator.EQUAL, to);
PreparedQuery to_key = datastore.prepare(q_to);
Entity e_to = to_key.asSingleEntity();
if(e_to == null)
return false;
Key key_to = e_to.getKey();
Query s_to = new Query(SUBSCRIPTION_KIND);
s_to.setAncestor(key_to);
s_to.addFilter(SUBSCRIPTION_EMAIL_PROPERTY, FilterOperator.EQUAL, from);
PreparedQuery pq_to = datastore.prepare(s_to);
Entity sub_to = pq_to.asSingleEntity();
if(sub_to == null || !sub_to.getProperty(SUBSCRIPTION_STATUS_PROPERTY).equals(SUBSCRIPTION_STATUS_WAITING))
return false;
sub_to.setProperty(SUBSCRIPTION_STATUS_PROPERTY, SUBSCRIPTION_STATUS_ACTIVE);
Query s_from = new Query(SUBSCRIPTION_KIND);
s_from.setAncestor(key_from);
s_from.addFilter(SUBSCRIPTION_EMAIL_PROPERTY, FilterOperator.EQUAL, to);
PreparedQuery pq_from = datastore.prepare(s_from);
Entity sub_from = pq_from.asSingleEntity();
if(sub_from == null || !sub_from.getProperty(SUBSCRIPTION_STATUS_PROPERTY).equals(SUBSCRIPTION_STATUS_PENDING))
return false;
sub_from.setProperty(SUBSCRIPTION_STATUS_PROPERTY, SUBSCRIPTION_STATUS_ACTIVE);
Transaction txn = datastore.beginTransaction();
datastore.put(sub_to);