1)I made a dependency out of model module, inside it there's a UserModel
class. But I can't import it to client side because my endpoints api getUser
method returns com.example.serjsmor.backend.userModelApi.model.UserModel
, and not com.serjsmor.digitalpheromone.module.UserModel
. I can import this generated class to my client but then what's the point of my dependency ?
2) Any changes I do UserModel
doesn't affect the backend generated class (like adding another Ctor
). How can I make the backend to recompile it again after a change ? (tried to add/remove, rebuild, clean etc.)
I'm using Android Studio 1.02 on Yosemite
client code :
package com.serjsmor.digitalpheromone;
import com.example.serjsmor.backend.userModelApi.UserModelApi;
import com.example.serjsmor.backend.userModelApi.model.UserModel;
import com.google.android.gms.maps.model.LatLng;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.extensions.android.json.AndroidJsonFactory;
import com.google.api.client.googleapis.services.AbstractGoogleClientRequest;
import com.google.api.client.googleapis.services.GoogleClientRequestInitializer;
import com.serjsmor.digitalpheromone.module.UserAlreadyExistsException;
import com.serjsmor.digitalpheromone.module.UserNotFoundException;
import com.serjsmor.digitalpheromone.pheromones.Pheromone;
import java.io.IOException;
import java.util.ArrayList;
public class EndpointsServer implements Server {
private static final String TAG = "EndpointsServer";
final UserModelApi userEndpointsApi;
public EndpointsServer() {
UserModelApi.Builder builder = new UserModelApi.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(), null)
.setRootUrl("http://10.0.2.2:8080/_ah/api/")
.setGoogleClientRequestInitializer(new GoogleClientRequestInitializer() {
@Override
public void initialize(AbstractGoogleClientRequest<?> abstractGoogleClientRequest) throws IOException {
abstractGoogleClientRequest.setDisableGZipContent(true);
}
});
userEndpointsApi = builder.build();
}
@Override
public User getUser(String email) {
User user = null;
try {
Log.d(TAG, "in getUser with email " +email);
// get user from db
UserModel userModel = userEndpointsApi.getUserModel(email).execute();
if (userModel != null) {
Log.d(TAG, "user != null with email " + email);
user = new User(userModel);
}
else { // add user to db
Log.d(TAG, "user == null with email " + email);
user = new User(email);
UserModel afterInsert = userEndpointsApi.storeUserModel(user.getUserModel()).execute();
Log.d(TAG, "after insert user email" + afterInsert.getEmail());
}
} catch (IOException e) {
e.printStackTrace();
}
return user;
}
}
Endpoint :
package com.example.serjsmor.myapplication.backend;
import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiNamespace;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Transaction;
import com.serjsmor.digitalpheromone.module.UserAlreadyExistsException;
import com.serjsmor.digitalpheromone.module.UserModel;
import com.serjsmor.digitalpheromone.module.UserNotFoundException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.inject.Named;
/**
* An endpoint class we are exposing
*/
@Api(
name = "userModelApi",
version = "v1",
resource = "userModel",
namespace = @ApiNamespace(
ownerDomain = "backend.myapplication.serjsmor.example.com",
ownerName = "backend.myapplication.serjsmor.example.com",
packagePath = ""
)
)
public class UserModelEndpoint {
private static final Logger logger = Logger.getLogger(UserModelEndpoint.class.getName());
/**
* This method gets the <code>UserModel</code> object associated with the specified <code>email</code>.
*
* @param email The id of the object to be returned.
* @return The <code>UserModel</code> associated with <code>id</code>.
*/
@ApiMethod(name = "getUserModel")
public UserModel getUserModel(@Named("email") String email) throws UserNotFoundException {
logger.log(Level.INFO, "serj.pheromone.BeanEndpoint inside getUserModel");
logger.log(Level.INFO, "serj.pheromone.BeanEndpoint email" + email);
DatastoreService datastoreService = DatastoreServiceFactory.getDatastoreService();
UserModel user = null;
// throw new UserNotFoundException();
try {
Key k = KeyFactory.createKey(UserModel.class.getSimpleName(), email);
logger.log(Level.INFO, "serj.pheromone.BeanEndpoint key ");
Entity entity = datastoreService.get(k);
user = new UserModel();
user.setEmail(email);
user.setNickname((String) entity.getProperty("nickname"));
} catch (EntityNotFoundException e) {
return null;
}
return user;
}
}
Model dependency :
package com.serjsmor.digitalpheromone.module;
import com.serjsmor.digitalpheromone.module.PheromoneModel;
import java.util.List;
public class UserModel {
protected String email;
protected String nickname;
protected List<PheromoneModel> pheromoneList;
public UserModel() {
}
public UserModel(String email, String nickname) {
this.email = email;
this.nickname = nickname;
}
public UserModel(String email, String firstName, String lastName) {
this.email = email;
this.nickname = firstName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public List<PheromoneModel> getPheromoneList() {
return pheromoneList;
}
public void setPheromoneList(List<PheromoneModel> pheromoneList) {
this.pheromoneList = pheromoneList;
}
}
Forgot to answer, I had a mysterios compelation error that wasn't surfaced until I started manually all gradle tasks and specially the getClientLibs task. More on debugging gradle.