Skip to content

Commit

Permalink
BugsFix
Browse files Browse the repository at this point in the history
  • Loading branch information
FabioPinheiro committed May 6, 2014
1 parent 17950c6 commit 73e9d90
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<MetaProduct>());
pantry.setName(userPantryDTO.getPantryName());
pantry = new Pantry(userPantryDTO.getPantryName());
//pantry.setProducts(new ArrayList<MetaProduct>());
//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();
Expand All @@ -266,7 +266,6 @@ public UserPantry insertUserPantry(UserPantryDTO userPantryDTO/* UserPantry user
}
mgr.persist(userpantry);
} finally {

mgr.close();
}
return userpantry;
Expand Down
5 changes: 5 additions & 0 deletions EasyStockAndroid-AppEngine/src/epic/easystock/Pantry.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
@Entity
public class Pantry {

public Pantry(String name){
this.name = name;
this.products = new ArrayList<MetaProduct>();
}

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long key;
Expand Down
14 changes: 10 additions & 4 deletions EasyStockAndroid/src/epic/easystock/activitys/PantyActivity.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand All @@ -44,14 +43,21 @@ 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() {
@Override
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);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* the License.
*/

package epic.easystock.io;
package epic.easystock.data;

import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -178,8 +176,7 @@ public void putAllProducts(Collection<LocalMetaProduct> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public AddProductToPantryTask(MetaProductAdapter adapter, String pantrySelected,
this.mail = EndPointCall.getEmailAccount();
this.pantrySelected = pantrySelected;
this.productId = productId;

}

@Override
Expand Down
17 changes: 14 additions & 3 deletions EasyStockAndroid/src/epic/easystock/io/EndPointCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -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();
Expand Down
74 changes: 41 additions & 33 deletions EasyStockAndroid/src/epic/easystock/io/ListPantryProductTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Context, Integer, List<LocalMetaProduct>> {
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;
Expand All @@ -30,54 +31,61 @@ public ListPantryProductTask(MetaProductAdapter adapter, String pantryID) {
@Override
protected void onPostExecute(List<LocalMetaProduct> 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<LocalMetaProduct> 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<LocalMetaProduct> doInBackground(Context... contexts) {

List<MetaProduct> 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<MetaProduct>();
}
}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<LocalMetaProduct> result = new ArrayList<LocalMetaProduct>();
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<MetaProduct> products = pantry.getProducts();
if (products == null) { // FIXME
Log.e(LOG_TAG, "products == null");
products = new ArrayList<MetaProduct>();
}
// 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;
}
}

0 comments on commit 73e9d90

Please sign in to comment.