From 73e9d9000a5edf82f75fc91f9ac776f50cafcdbe Mon Sep 17 00:00:00 2001 From: FabioPinheiro Date: Tue, 6 May 2014 20:16:10 +0100 Subject: [PATCH] BugsFix --- .../src/epic/easystock/ApiEndpoint.java | 7 +- .../src/epic/easystock/Pantry.java | 5 ++ .../easystock/activitys/PantyActivity.java | 14 +++- .../{io => data}/PantryDbAdapter.java | 9 +-- .../easystock/{io => data}/UserBdAdapter.java | 2 +- .../easystock/io/AddProductToPantryTask.java | 1 + .../src/epic/easystock/io/EndPointCall.java | 17 ++++- .../easystock/io/ListPantryProductTask.java | 74 ++++++++++--------- 8 files changed, 78 insertions(+), 51 deletions(-) rename EasyStockAndroid/src/epic/easystock/{io => data}/PantryDbAdapter.java (97%) rename EasyStockAndroid/src/epic/easystock/{io => data}/UserBdAdapter.java (99%) diff --git a/EasyStockAndroid-AppEngine/src/epic/easystock/ApiEndpoint.java b/EasyStockAndroid-AppEngine/src/epic/easystock/ApiEndpoint.java index beb792a..f351810 100644 --- a/EasyStockAndroid-AppEngine/src/epic/easystock/ApiEndpoint.java +++ b/EasyStockAndroid-AppEngine/src/epic/easystock/ApiEndpoint.java @@ -253,9 +253,9 @@ public UserPantry insertUserPantry(UserPantryDTO userPantryDTO/* UserPantry user if (containsPantry(pantry)) throw new EntityExistsException("insertUserPantry: Object (pantry) already exists"); if (pantryIsNull) { - pantry = new Pantry(); - pantry.setProducts(new ArrayList()); - pantry.setName(userPantryDTO.getPantryName()); + pantry = new Pantry(userPantryDTO.getPantryName()); + //pantry.setProducts(new ArrayList()); + //pantry.setName(userPantryDTO.getPantryName()); pantry = insertPantry(pantry); //mgr.persist();// FIXME verificar se está aqui bem devido if (containsUserPantry(userpantry)) }else {}//FIXME care pantry.getKey() userpantry = new UserPantry(); @@ -266,7 +266,6 @@ public UserPantry insertUserPantry(UserPantryDTO userPantryDTO/* UserPantry user } mgr.persist(userpantry); } finally { - mgr.close(); } return userpantry; diff --git a/EasyStockAndroid-AppEngine/src/epic/easystock/Pantry.java b/EasyStockAndroid-AppEngine/src/epic/easystock/Pantry.java index 353b4f3..59645f3 100644 --- a/EasyStockAndroid-AppEngine/src/epic/easystock/Pantry.java +++ b/EasyStockAndroid-AppEngine/src/epic/easystock/Pantry.java @@ -14,6 +14,11 @@ @Entity public class Pantry { + public Pantry(String name){ + this.name = name; + this.products = new ArrayList(); + } + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long key; diff --git a/EasyStockAndroid/src/epic/easystock/activitys/PantyActivity.java b/EasyStockAndroid/src/epic/easystock/activitys/PantyActivity.java index 231eb6b..b94824f 100644 --- a/EasyStockAndroid/src/epic/easystock/activitys/PantyActivity.java +++ b/EasyStockAndroid/src/epic/easystock/activitys/PantyActivity.java @@ -1,11 +1,12 @@ package epic.easystock.activitys; import java.util.ArrayList; -import java.util.List; +import android.app.Activity; import android.app.AlertDialog; import android.app.ListActivity; import android.content.DialogInterface; +import android.content.DialogInterface.OnCancelListener; import android.os.Bundle; import android.util.Log; import android.view.View; @@ -16,12 +17,10 @@ import epic.easystock.assist.MetaProductAdapter; import epic.easystock.data.LocalMetaProduct; import epic.easystock.io.EndPointCall; -import epic.easystock.io.UserBdAdapter; public class PantyActivity extends ListActivity { String mail; Button addProduct; - private String name; MetaProductAdapter adapter; @Override @@ -44,6 +43,13 @@ public void onClick(DialogInterface dialog, int which) { EndPointCall.listPantryProductTask(adapter, EndPointCall.getSelectedPantry()); //FIXME } }); + final Activity aux = this; + alert.setOnCancelListener(new OnCancelListener() { + @Override + public void onCancel(DialogInterface dialog) { + aux.finish(); + } + }); alert.show(); addProduct.setOnClickListener(new OnClickListener() { @@ -51,7 +57,7 @@ public void onClick(DialogInterface dialog, int which) { public void onClick(View v) { Log.i("ADDPRODUCT PANTRY", "new AddProductTask: " + mail); Long productId = Long.valueOf(((EditText) findViewById(R.id.NumberId)).getText().toString()); - EndPointCall.addProductToPantryTask(adapter, name, productId); + EndPointCall.addProductToPantryTask(adapter, productId); } }); diff --git a/EasyStockAndroid/src/epic/easystock/io/PantryDbAdapter.java b/EasyStockAndroid/src/epic/easystock/data/PantryDbAdapter.java similarity index 97% rename from EasyStockAndroid/src/epic/easystock/io/PantryDbAdapter.java rename to EasyStockAndroid/src/epic/easystock/data/PantryDbAdapter.java index 5ca7c46..18beaea 100755 --- a/EasyStockAndroid/src/epic/easystock/io/PantryDbAdapter.java +++ b/EasyStockAndroid/src/epic/easystock/data/PantryDbAdapter.java @@ -14,7 +14,7 @@ * the License. */ -package epic.easystock.io; +package epic.easystock.data; import java.util.ArrayList; import java.util.Collection; @@ -23,7 +23,6 @@ import com.google.android.gms.internal.ar; import epic.easystock.apiEndpoint.model.MetaProduct; -import epic.easystock.data.LocalMetaProduct; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; @@ -136,8 +135,7 @@ public void close() { * the body of the note * @return rowId or -1 if failed */ - public long createProduct(String name, String description, Long barcode, - Long id, Double amount) { + public long createProduct(String name, String description, Long barcode, Long id, Double amount) { ContentValues initialValues = new ContentValues(); initialValues.put(PROD_NAME, name); initialValues.put(PROD_DESCRIPTION, description); @@ -178,8 +176,7 @@ public void putAllProducts(Collection products) { for (LocalMetaProduct lmp : products) { try { - createProduct(lmp.getName(), lmp.getDescription(), - lmp.getBarCode(), lmp.getId(), lmp.getAmount()); + createProduct(lmp.getName(), lmp.getDescription(), lmp.getBarCode(), lmp.getId(), lmp.getAmount()); } catch (SQLiteConstraintException e) { updateProduct(lmp.getName(), lmp.getDescription(), lmp.getBarCode(), lmp.getId(), lmp.getAmount()+1); diff --git a/EasyStockAndroid/src/epic/easystock/io/UserBdAdapter.java b/EasyStockAndroid/src/epic/easystock/data/UserBdAdapter.java similarity index 99% rename from EasyStockAndroid/src/epic/easystock/io/UserBdAdapter.java rename to EasyStockAndroid/src/epic/easystock/data/UserBdAdapter.java index eb1ccbd..4aea0a6 100644 --- a/EasyStockAndroid/src/epic/easystock/io/UserBdAdapter.java +++ b/EasyStockAndroid/src/epic/easystock/data/UserBdAdapter.java @@ -13,7 +13,7 @@ * License for the specific language governing permissions and limitations under * the License. */ -package epic.easystock.io; +package epic.easystock.data; import java.util.ArrayList; import java.util.List; diff --git a/EasyStockAndroid/src/epic/easystock/io/AddProductToPantryTask.java b/EasyStockAndroid/src/epic/easystock/io/AddProductToPantryTask.java index 07c70eb..52c7d2d 100644 --- a/EasyStockAndroid/src/epic/easystock/io/AddProductToPantryTask.java +++ b/EasyStockAndroid/src/epic/easystock/io/AddProductToPantryTask.java @@ -25,6 +25,7 @@ public AddProductToPantryTask(MetaProductAdapter adapter, String pantrySelected, this.mail = EndPointCall.getEmailAccount(); this.pantrySelected = pantrySelected; this.productId = productId; + } @Override diff --git a/EasyStockAndroid/src/epic/easystock/io/EndPointCall.java b/EasyStockAndroid/src/epic/easystock/io/EndPointCall.java index 42b26a3..cea24e6 100644 --- a/EasyStockAndroid/src/epic/easystock/io/EndPointCall.java +++ b/EasyStockAndroid/src/epic/easystock/io/EndPointCall.java @@ -21,6 +21,8 @@ import epic.easystock.assist.AppConstants; import epic.easystock.assist.MetaProductAdapter; import epic.easystock.assist.ProductAdapter; +import epic.easystock.data.PantryDbAdapter; +import epic.easystock.data.UserBdAdapter; public class EndPointCall { static private final String EndPointCall_TAG = "EndPointCall"; @@ -34,11 +36,14 @@ public class EndPointCall { static public final String FAIL_TO_LIST_PRODUCTS = "FAIL_TO_LIST_PRODUCTS"; static public final String FAIL_TO_LIST_PANTRY_PRODUCTS = "FAIL_TO_LIST_PANTRY_PRODUCTS"; static public final String FAIL_TO_LOAD_PANTRY = "FAIL_TO_LOAD_PANTRY"; + static public final String FAIL_TO_LOAD_PRODUCT = "FAIL_TO_LOAD_PRODUCT"; static public final String FAIL_TO_CREATE_PANTRY_WITHOUT_A_NAME = "FAIL_TO_CREATE_PANTRY_WITHOUT_A_NAME"; static public final String FAIL_TO_CREATE_PANTRY_WITH_THE_NAME_OF_ANOTHER = "FAIL_TO_CREATE_PANTRY_WITH_THE_NAME_OF_ANOTHER"; static public final String INSERT_NEW_USER_IN_APPENGINE = "INSERT_NEW_USER_IN_APPENGINE"; + static public final String PANTRY_IS_EMPTY = "PANTRY_IS_EMPTY"; static public final String DONE = "AsyncTask Done"; // FIXME remove!!! use static public final String DEBUG = "DEBUG"; + static public final String ERROR = "ERROR"; // in debug static private Context globalContext = null; static private SharedPreferences globalSettings; @@ -65,7 +70,12 @@ public static void setSelectedPantry(String selectedPantry) { globalSettings.edit().putString(PREFS_LAST_USED_PANTRY, selectedPantry).commit(); } public static String getSelectedPantry() { - return globalSettings.getString(PREFS_LAST_USED_PANTRY, ERROR_PANTRY_NAME); + String aux = globalSettings.getString(PREFS_LAST_USED_PANTRY, ERROR_PANTRY_NAME); + if(Strings.isNullOrEmpty(aux)){ + msg(EndPointCall_TAG,"getSelectedPantry is null or empty"); //FIXME TEXT + Log.e(EndPointCall_TAG, "getSelectedPantry is null or empty"); //FIXME TEXT + } + return aux; } // ################################################################################## private static boolean isConnected() { @@ -141,9 +151,10 @@ static public ApiEndpoint getApiEndpoint() { static public void addToProductListTask(String name, Long barCode, String description, String image) { new AddToProductListTask(name, barCode, description, image).execute(); } - static public void addProductToPantryTask(MetaProductAdapter adapter, String selectedPantry, Long productId) { + static public void addProductToPantryTask(MetaProductAdapter adapter, Long productId) { + String selectedPantry = getSelectedPantry(); new AddProductToPantryTask(adapter, selectedPantry, productId).execute(); // FIXME getSelectedPantry() - Log.i(EndPointCall_TAG, "addProductToPantryTask:" + "productId " + productId); + Log.i(EndPointCall_TAG, "addProductToPantryTask:" + " productId " + productId + " to " + selectedPantry); } static public void listProductTask(ProductAdapter adapter) { new ListAllProductTask(adapter).execute(); diff --git a/EasyStockAndroid/src/epic/easystock/io/ListPantryProductTask.java b/EasyStockAndroid/src/epic/easystock/io/ListPantryProductTask.java index 2a08a52..4ddf526 100644 --- a/EasyStockAndroid/src/epic/easystock/io/ListPantryProductTask.java +++ b/EasyStockAndroid/src/epic/easystock/io/ListPantryProductTask.java @@ -13,14 +13,15 @@ import epic.easystock.apiEndpoint.model.Product; import epic.easystock.assist.MetaProductAdapter; import epic.easystock.data.LocalMetaProduct; +import epic.easystock.data.PantryDbAdapter; public class ListPantryProductTask extends AsyncTask> { static private final String LOG_TAG = AddProductToPantryTask.class.getCanonicalName(); - private MetaProductAdapter adapter; private PantryDbAdapter dbAdapter; private String pantryID; private Boolean pantryLoaded; + private Boolean productLoaded; public ListPantryProductTask(MetaProductAdapter adapter, String pantryID) { this.adapter = adapter; @@ -30,54 +31,61 @@ public ListPantryProductTask(MetaProductAdapter adapter, String pantryID) { @Override protected void onPostExecute(List result) { super.onPostExecute(result); - if (pantryLoaded) { - EndPointCall.msg(LOG_TAG , EndPointCall.FAIL_TO_LOAD_PANTRY); - Log.e(LOG_TAG, EndPointCall.FAIL_TO_LOAD_PANTRY); + if (!pantryLoaded) { + EndPointCall.msg(LOG_TAG, EndPointCall.FAIL_TO_LOAD_PANTRY); + } + if (!productLoaded) { + EndPointCall.msg(LOG_TAG ,EndPointCall.FAIL_TO_LIST_PANTRY_PRODUCTS); } if (result != null) { Collection localProducts = result; + if (result.isEmpty()){ + EndPointCall.msg(LOG_TAG ,EndPointCall.PANTRY_IS_EMPTY); + } dbAdapter.putAllProducts(localProducts); adapter.addAll(localProducts); - } else { - Log.e(LOG_TAG, "EndPointCall.FAIL_TO_LIST_PANTRY_PRODUCTS"); - EndPointCall.msg(EndPointCall.FAIL_TO_LIST_PANTRY_PRODUCTS); + }else { + EndPointCall.msg(LOG_TAG ,EndPointCall.ERROR); + Log.e(LOG_TAG, EndPointCall.ERROR); } } @Override protected List doInBackground(Context... contexts) { - - List products = null; + Pantry pantry; try { - Pantry pantry = EndPointCall.getApiEndpoint().getPantryByMailAndName(EndPointCall.getEmailAccount(), pantryID).execute(); - pantryLoaded = (pantry != null); - if (pantryLoaded) { - products = pantry.getProducts(); - if (products == null) { //FIXME - Log.e(LOG_TAG, "products == null"); - products = new ArrayList(); - } - }else { - Log.e(LOG_TAG, EndPointCall.FAIL_TO_LOAD_PANTRY); - } + pantry = EndPointCall.getApiEndpoint().getPantryByMailAndName(EndPointCall.getEmailAccount(), pantryID).execute(); + pantryLoaded = true; } catch (IOException e) { + pantryLoaded = false; + pantry = null; + Log.e(LOG_TAG, EndPointCall.FAIL_TO_LOAD_PANTRY); e.printStackTrace(); } List result = new ArrayList(); - if (pantryLoaded){ - for (MetaProduct mp : products) { - Product aux = null; - try { - aux = EndPointCall.getApiEndpoint().getProduct(mp.getProduct()).execute(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - continue; - } - result.add(new LocalMetaProduct(aux.getBarCode(), aux - .getName(), aux.getDescription(), aux.getKey(), mp - .getAmount())); + // if (pantry == null) { + // Log.e(LOG_TAG, EndPointCall.FAIL_TO_LOAD_PANTRY); + // }else { + List products = pantry.getProducts(); + if (products == null) { // FIXME + Log.e(LOG_TAG, "products == null"); + products = new ArrayList(); + } + // if (pantryLoaded){ + productLoaded = false; + for (MetaProduct mp : products) { + try { + Product aux = EndPointCall.getApiEndpoint().getProduct(mp.getProduct()).execute(); + result.add(new LocalMetaProduct(aux.getBarCode(), aux.getName(), aux.getDescription(), aux.getKey(), mp.getAmount())); + } catch (IOException e) { + // TODO Auto-generated catch block + Log.e(LOG_TAG, EndPointCall.FAIL_TO_LOAD_PRODUCT); + e.printStackTrace(); + continue; } + productLoaded = true; } + // } + // } return result; } } \ No newline at end of file