Skip to content

Commit

Permalink
Merge pull request #6 from sbxcloud/annotationlib
Browse files Browse the repository at this point in the history
Annotationlib
  • Loading branch information
lgguzman authored Mar 18, 2017
2 parents f03f117 + 8866f8c commit 4b5d3e0
Show file tree
Hide file tree
Showing 10 changed files with 745 additions and 155 deletions.
39 changes: 37 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Agregamos la librería como dependencia

dependencies {
//...otras dependencias de tu proyeco aquí.....
compile 'com.github.sbxcloud:androidlib:v2.0.0'
compile 'com.github.sbxcloud:androidlib:v2.0.1'
}
Esta librería se basa en annotaciones. Para crear tu propia Clase usuario puedes hacerla así:
Expand Down Expand Up @@ -113,7 +113,7 @@ Luego, puedes conectarte con tus datos utilizando el cliente hhtp que consideres

```java
//Iniciar la librería con el dominio y el App-key, puede ser en tu custom Application.class
SbxAuth.initialize(80,"KASDFasd-asdfadsf-asdfadsf-asdf");
SbxAuth.initializeIfIsNecessary(Context,110,"d4cd3cac-043a-48ab-9d06-18aa4fd23cbd");

//Datos
//Registrar un usuario
Expand Down Expand Up @@ -333,6 +333,41 @@ User user= new User("luis gabriel","lgguzman","lgguzman@sbxcloud.com","123456");
user.logOut();
```

Incluso puedes hacer fecth a los objetos

```java

Product p = new Product();
p.key="1d3cd5e6-21a8-4de8-97c9-f4177aa6bad4";
p.fetchInBackground(new SbxSimpleResponse<Product>() {
@Override
public void onError(Exception e) {
e.printStackTrace();
}

@Override
public void onSuccess(Product product) {
Log.e("producto",product.name);
}
});
String []properties={"category"};
p.fetchInBackground(new SbxSimpleResponse<Product>() {
@Override
public void onError(Exception e) {
e.printStackTrace();
}

@Override
public void onSuccess(Product product) {
Log.e("producto",product.name);
Log.e("categoria",product.category.name);
}
},properties);

```






Expand Down
5 changes: 5 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ android {
}
}

testOptions {
unitTests.returnDefaultValues = true
}

}

dependencies {
Expand All @@ -33,6 +37,7 @@ dependencies {
//androidTestCompile 'com.android.support:support-annotations:24.0.0'
compile 'com.android.support:appcompat-v7:25.1.1'
testCompile 'junit:junit:4.12'
// testCompile 'org.mockito:mockito-core:1.10.19'
// testCompile 'org.json:json:20140107'
}

Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.sbxcloud.android.sbxcloudsdk.auth;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import com.sbxcloud.android.sbxcloudsdk.auth.config.SbxAppKeyField;
import com.sbxcloud.android.sbxcloudsdk.auth.config.SbxDomainField;
Expand Down Expand Up @@ -30,6 +32,12 @@ public class SbxAuth {
private String appKey;
private String token;
private boolean HttpLog;
private Context context;
private SharedPreferences sharedPreferences;
private static final String FILE_NAME=SbxAuth.class.getName();
private static final String FILE_NAME_TOKEN=FILE_NAME+"_Token";
private static final String FILE_NAME_DOMAIN=FILE_NAME+"_Domain";
private static final String FILE_NAME_APPKEY=FILE_NAME+"_Appkey";

public boolean isHttpLog() {
return HttpLog;
Expand All @@ -44,10 +52,16 @@ public void setHttpLog(boolean httpLog) {
* @param domain the id of the domain on sbxcloud.com
* @param appKey the app key on sbxcloud.com
*/
public static void initialize(int domain, String appKey) {
defaultSbxAuth = new SbxAuth();
defaultSbxAuth.domain=domain;
defaultSbxAuth.appKey=appKey;
public static void initializeIfIsNecessary(Context context, int domain, String appKey) {
if(defaultSbxAuth ==null) {
defaultSbxAuth = new SbxAuth();
defaultSbxAuth.context = context;
defaultSbxAuth.domain = domain;
defaultSbxAuth.appKey = appKey;
defaultSbxAuth.sharedPreferences = context.getSharedPreferences(FILE_NAME,Context.MODE_PRIVATE);
defaultSbxAuth.sharedPreferences.edit().putString(FILE_NAME_APPKEY, defaultSbxAuth.appKey)
.putInt(FILE_NAME_DOMAIN, defaultSbxAuth.domain).apply();
}
}

/**
Expand All @@ -57,8 +71,13 @@ public static void initialize(int domain, String appKey) {
*/
public static void initialize(Application app)throws SbxConfigException{
defaultSbxAuth = new SbxAuth();
defaultSbxAuth.context = app.getApplicationContext();
defaultSbxAuth.context=app.getApplicationContext();
defaultSbxAuth.domain=getDomainAnnotation(app);
defaultSbxAuth.appKey=getAppKeyAnnotation(app);
defaultSbxAuth.sharedPreferences = defaultSbxAuth.context.getSharedPreferences(FILE_NAME,Context.MODE_PRIVATE);
defaultSbxAuth.sharedPreferences.edit().putString(FILE_NAME_APPKEY, defaultSbxAuth.appKey)
.putInt(FILE_NAME_DOMAIN, defaultSbxAuth.domain).apply();
}

/**
Expand All @@ -79,7 +98,10 @@ public static SbxAuth getDefaultSbxAuth()throws SbxConfigException {
*/
public int getDomain()throws SbxConfigException {
if(domain==0){
throw new SbxConfigException("SbxAuth not initialized");
if(sharedPreferences==null)
throw new SbxConfigException("SbxAuth not initialized");
else
return domain = sharedPreferences.getInt(FILE_NAME_DOMAIN,0);
}
return domain;
}
Expand All @@ -91,7 +113,9 @@ public int getDomain()throws SbxConfigException {
*/
public String getToken()throws SbxConfigException {
if(token==null){
throw new SbxConfigException("User is not login yet.");
if (sharedPreferences.getString(FILE_NAME_TOKEN,"").isEmpty())
throw new SbxConfigException("User is not login yet.");
return token = sharedPreferences.getString(FILE_NAME_TOKEN,"");
}
return token;
}
Expand All @@ -103,14 +127,19 @@ public String getToken()throws SbxConfigException {
*/
public String getAppKey()throws SbxConfigException{
if(appKey==null){
throw new SbxConfigException("SbxAuth not initialized");
if(sharedPreferences==null)
throw new SbxConfigException("SbxAuth not initialized");
else
return appKey = sharedPreferences.getString(FILE_NAME_APPKEY,"");
}
return appKey;
}


public void resetToken(){
token=null;
if(sharedPreferences!=null)
sharedPreferences.edit().putString(FILE_NAME_TOKEN,"").apply();
}

/**
Expand All @@ -132,6 +161,7 @@ public String refreshToken(final Object obj)throws SbxConfigException, SbxAuthE
variable.setAccessible(true);
token= (String)variable.get(obj);
variable.setAccessible(isAccessible);
sharedPreferences.edit().putString(FILE_NAME_TOKEN,token).apply();
return token;
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new SbxConfigException(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.sbxcloud.android.sbxcloudsdk.net.auth;

import android.content.Context;
import android.content.SharedPreferences;

import com.sbxcloud.android.sbxcloudsdk.auth.SbxAuth;
import com.sbxcloud.android.sbxcloudsdk.auth.user.SbxAuthToken;
import com.sbxcloud.android.sbxcloudsdk.auth.user.SbxUsernameField;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.sbxcloud.android.sbxcloudsdk.net.model;

import android.util.Log;

import com.sbxcloud.android.sbxcloudsdk.auth.SbxAuth;
import com.sbxcloud.android.sbxcloudsdk.net.ApiManager;
import com.sbxcloud.android.sbxcloudsdk.net.auth.SbxUser;
import com.sbxcloud.android.sbxcloudsdk.net.callback.SbxArrayResponse;
import com.sbxcloud.android.sbxcloudsdk.net.callback.SbxSimpleResponse;
import com.sbxcloud.android.sbxcloudsdk.query.SbxModelHelper;
import com.sbxcloud.android.sbxcloudsdk.query.SbxQueryBuilder;
import com.sbxcloud.android.sbxcloudsdk.util.SbxDataValidator;
import com.sbxcloud.android.sbxcloudsdk.util.SbxMagicComposer;
import com.sbxcloud.android.sbxcloudsdk.util.SbxUrlComposer;

import org.json.JSONArray;
Expand Down Expand Up @@ -52,6 +56,89 @@ public void onResponse(Call call, Response response) throws IOException {
});
}

public void fetchInBackground(final SbxSimpleResponse simpleResponse)throws Exception{

String []keys = {SbxModelHelper.getKeyFromAnnotation(this)};
for (String key:keys){
Log.e("key",key);
}
SbxQueryBuilder sbxQueryBuilder = (new SbxQuery(this.getClass())).sbxQueryBuilder;
SbxUrlComposer sbxUrlComposer = SbxModelHelper.getUrlQueryKeys(sbxQueryBuilder, keys);
Request request = ApiManager.getInstance().sbxUrlComposer2Request(sbxUrlComposer);
ApiManager.getInstance().getOkHttpClient().newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
simpleResponse.onError(e);
}

@Override
public void onResponse(Call call, Response response) throws IOException {
try {
JSONObject jsonObject = new JSONObject(response.body().string());
if(jsonObject.getBoolean("success")) {

JSONArray jsonArray=jsonObject.getJSONArray("results");
if(jsonArray.length()>0) {
SbxMagicComposer.getSbxModel(jsonArray.getJSONObject(0), SbxModel.this.getClass(),SbxModel.this, 0);
simpleResponse.onSuccess(SbxModel.this);
}else{
simpleResponse.onError(new Exception("key not found"));
}


}else{
simpleResponse.onError(new Exception(jsonObject.getString("error")));
}
}catch (Exception e ){
simpleResponse.onError(e);
}
}
});
}

public void fetchInBackground(final SbxSimpleResponse simpleResponse, String []properties)throws Exception{

String []keys = {SbxModelHelper.getKeyFromAnnotation(this)};
SbxQueryBuilder sbxQueryBuilder = (new SbxQuery(this.getClass())).fetch(properties).sbxQueryBuilder;
SbxUrlComposer sbxUrlComposer = SbxModelHelper.getUrlQueryKeys(sbxQueryBuilder, keys);
Request request = ApiManager.getInstance().sbxUrlComposer2Request(sbxUrlComposer);
ApiManager.getInstance().getOkHttpClient().newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
simpleResponse.onError(e);
}

@Override
public void onResponse(Call call, Response response) throws IOException {
try {
JSONObject jsonObject = new JSONObject(response.body().string());
if(jsonObject.getBoolean("success")) {

JSONArray jsonArray=jsonObject.getJSONArray("results");
if(jsonArray.length()>0) {
if(jsonObject.has("fetched_results")) {
SbxMagicComposer.getSbxModel(jsonArray.getJSONObject(0), SbxModel.this.getClass(), SbxModel.this, 0,jsonObject.getJSONObject("fetched_results"));
}else{
SbxMagicComposer.getSbxModel(jsonArray.getJSONObject(0), SbxModel.this.getClass(), SbxModel.this, 0);
}
simpleResponse.onSuccess(SbxModel.this);
}else{
simpleResponse.onError(new Exception("key not found"));
}


}else{
simpleResponse.onError(new Exception(jsonObject.getString("error")));
}
}catch (Exception e ){
simpleResponse.onError(e);
}
}
});
}



public void deleteInBackground(final SbxSimpleResponse simpleResponse)throws Exception{
SbxQueryBuilder sbxQueryBuilder = SbxModelHelper.prepareQueryToDelete(this.getClass());
sbxQueryBuilder.addDeleteKey(SbxModelHelper.getKeyFromAnnotation(this));
Expand Down Expand Up @@ -83,7 +170,7 @@ public void onResponse(Call call, Response response) throws IOException {
private void updateKey(JSONObject jsonObject)throws Exception{
if(jsonObject.has("keys")) {
String key = jsonObject.getJSONArray("keys").getString(0);
SbxModelHelper.setKeyFromAnnotation(SbxModel.this, key);
SbxDataValidator.setKeyFromAnnotation(SbxModel.this, key);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.IOException;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

Expand All @@ -36,13 +37,13 @@ public class SbxQuery{
public SbxQuery(Class<?> clazz) throws Exception{
this.mClazz=clazz;
sbxQueryBuilder= SbxModelHelper.prepareQuery(clazz);
sbxQueryBuilder.insertNewEmptyRow();
// sbxQueryBuilder.insertNewEmptyRow();
}

public SbxQuery(Class<?> clazz, int page, int limit) throws Exception{
this.mClazz=clazz;
sbxQueryBuilder= SbxModelHelper.prepareQuery(clazz, page, limit);
sbxQueryBuilder.insertNewEmptyRow();
// sbxQueryBuilder.insertNewEmptyRow();
}

public SbxQuery addAND(){
Expand Down Expand Up @@ -80,6 +81,11 @@ public SbxQuery whereLike(String field, Object value) throws JSONException{
return this;
}

public SbxQuery fetch(String propieties[]) throws JSONException{
sbxQueryBuilder.fetch(propieties);
return this;
}

public <T> void findInBackground(final SbxArrayResponse <T> sbxArrayResponse) throws Exception{
SbxUrlComposer sbxUrlComposer=SbxModelHelper.getUrlQuery(sbxQueryBuilder);
Request request = ApiManager.getInstance().sbxUrlComposer2Request(sbxUrlComposer);
Expand All @@ -99,8 +105,15 @@ public void onResponse(Call call, Response response) throws IOException {
List <T> list= new ArrayList<T>();
JSONArray jsonArray=jsonObject.getJSONArray("results");
for (int i=0;i<jsonArray.length();i++){
list.add((T)SbxMagicComposer.getSbxModel(jsonArray.getJSONObject(i),mClazz,0));
if(jsonObject.has("fetched_results")) {
list.add((T) SbxMagicComposer.getSbxModel(jsonArray.getJSONObject(i), mClazz, 0,jsonObject.getJSONObject("fetched_results")));
}else{
list.add((T) SbxMagicComposer.getSbxModel(jsonArray.getJSONObject(i), mClazz, 0));
}
}



sbxArrayResponse.onSuccess(list);


Expand All @@ -118,4 +131,5 @@ public void onResponse(Call call, Response response) throws IOException {




}
Loading

0 comments on commit 4b5d3e0

Please sign in to comment.