1

okay guys, here is the thing, I have one application consuming ODATA service, in SMP server, I'm getting this Data like this:

public class callService extends AsyncTask<Void, Void, ArrayList<String>> 
    {
        public ArrayList<String> doInBackground(Void... params) 
        {
            ODataConsumer c = ODataJerseyConsumer.create("http://MyUrlService:8080");
            List<OEntity> listEntities = c.getEntities("MYENTITYTOCONSUME").execute().toList();
            System.out.println("Size" + listEntities.size());
            if (listEntities.size() > 0) 
            {               
                for (OEntity entity : listEntities) 
                {
                zmob_kunnr.add((String) entity.getProperty("Name1").getValue()
                    + " - "
                + entity.getProperty("Kunnr").getValue().toString());
                }
            }
            return zmob_kunnr;
        }   
        protected void onPostExecute(ArrayList<String> result) 
        {
        super.onPostExecute(result);
        adapter = new ArrayAdapter<String>(ConsumoKnuur.this, android.R.layout.simple_list_item_1, result);
        list.setAdapter(adapter);
        }
    }

Okay I got this solution from web and could implement as list, and I need to store this entity which one is a List of customers and get the two attributes from this entity and save in my database so:

Entity Customer:Custormer_ID, Customer_Name

Here is my code to call my sqlite:

 public void sqlite() 
    {
        sql_obj.open();
        sql_obj.deleteAll();    
            for(int i=0; i < zmob_kunnr.size(); i++)
            {
                sql_obj.insert(zmob_kunnr.get(i).toString(), zmob_kunnr.get(i).toString() );   
            }
        sql_obj.close();
    }

And my SQLite:

private static final String TABLE_CLIENTE = "CREATE TABLE " 
            + TB_CLIENTE
            + "(ID_CLIENTE INTEGER PRIMARY KEY AUTOINCREMENT, " //Id for controller my logics
            + " Kunnr TEXT , " //customer ID
            + " Name1 TEXT );"; //customer_name
public long insert(String name1, String Kunnr) 
    {   
        ContentValues initialValues = new ContentValues();
        initialValues.put("Name1", Name1); //Customer_Name
        initialValues.put("Kunnr", Kunnr); //Customer_ID
        return database.insert(TB_CLIENTE, null, initialValues);
    }

And off course my other methods, that is not important, so whats happening when I run my "for" in the sql call method, I get the size() of the list and the rows of the list and store the entire row in the one column of the database each time, so I got two different tables with the same values,

how can I change solve this problem instead of consume in list I need to consume in array ? or I need to create a method that get the list values and after a ,(coma) , create two differents objects to store these data ??

I took a long look in the internet and didn't find nothing, probably it's because i don't know yet, how so, I don't know for what I'm looking for it, I'm using the odata4j API and here is the link of the documentation, http://odata4j.org/v/0.7/javadoc/

I'm new on programming, so I'm really in trouble with this, any suggestions any helps will be truly, appreciate,

Thanks a lot and have a nice day !!!

Kaushik
  • 6,150
  • 5
  • 39
  • 54
  • How is `zmob_kunnr` defined? If you change the type of zmob_kunnr to `ArrayList` you can, in your `insert` statement use `sql_obj.insert(zmob_kunnr.get(i).getProperty("Name1").getValue(), zmob_kunnr.get(i).getProperty("Kunnr").getValue().toString());` – Ian2thedv Dec 04 '14 at 13:28
  • Hello Nayeem thanks a lot for your helpfull answer, i understand your logics, and make sense now, I changed my mob_kunnr to ArrayListand i also changed all the other calls from zmob_kunnr, but the method zmob_kunnr.add gives to me the "add" a problem (The method add(OEntity) in the type ArrayList is not applicable for the arguments (String)) to change to addAll and if i change still with the problem and tells me to chande to add – Vinicius Biscolla Dec 04 '14 at 16:19
  • Haha well I am not Nayeem, but you need to change the body of the `for` loop to `zmob_kunnr.add(entity)` – Ian2thedv Dec 05 '14 at 06:21
  • Oh Sorry lan2thedv, i just realize right now my mystakes sorry !!! – Vinicius Biscolla Dec 05 '14 at 10:39
  • Hey thanks a lot man, i could solve my problem, really thanks i appreciated that, by the way, in this solution i'm able to get 4 different entities ?w or i need to create a loop for each one ?? – Vinicius Biscolla Dec 05 '14 at 10:51
  • Is your problem solved? Can I add as an answer? And I don't really understand your last question, can you give an example? – Ian2thedv Dec 05 '14 at 11:05
  • Yeahp thanks man, like in this example i consumed one Entity rigth ?? but i have 3 more, so how can i perform 4 consume and generate 4 independent lists like: ODataConsumer c = ODataJerseyConsumer.create(Url1,Url2,Url3,Url4(example)); List listEntities = c.getEntities("ZMOB_BAULTSet").execute().toList(); List listEntities1 = c.getEntities("ZMOB_KUNNRSet").execute().toList();... to 4 for (OEntity entity : listEntities) { zmob_kunnr.add(entity); for (OEntity entity : listEntities) { zmob_bault.add(entity); – Vinicius Biscolla Dec 05 '14 at 11:46
  • My question is: I need to do 4 of all itens each, or there is any other way to do the same ?? thanks a lot man u saved my life hahaha – Vinicius Biscolla Dec 05 '14 at 11:50

2 Answers2

1

You can add each entity to the `ArrayList' array by doing the following:

for (OEntity entity : listEntities) {
    zmob_kunnr.add(entity);
}

This will allow you to access the data contained in the entity via getProperty() when inserted into the database.

The following statement is also not needed, as the for each loop runs through every element in the list, thus for (OEntity entity : listEntities) will not execute if the list is empty.

if (listEntities.size() > 0) {               
    ...
}

If you have multiple ODataConsumers, you have two choices, depending on your requirements (if I understand you question correctly):

  1. You can sequentially get each ODataConsumer, get the listEntities, and add it to the zmob_kunnr list, and after the list items are added to the database, clear the zmob_kunnr list, and call doInBackground with a new URL. This is what your current solution allows.

It appears to need to know which property is associated with a URL when reading the values into the DB. You can use a POJO as a holder for the entity and its list of properties. You can now add and remove properties. Note that properties will be removed in the same order they where inserted.

public class OEntityHolder {
    private final OEntity entity;
    private Queue<String> properties;

    public OEntityHolder(OEntity entity) {
        this.entity = entity;
        this.properties = new LinkedBlockingQueue<>();
    } 

    public OEntity getEntity() {
        return this.entity;
    }

    public void addProperty(String property) {
        this.properties.add(property);
    }

    public void removeProperty() {
        this.properties.poll();
    }
}

This will require a change to the list holding the entities:

ArrayList<OEntityHolder> zmob_entity_holders = new ArrayList<>();

If you would like to add all the entities from the different URLs at the same time, you will need to have access to all the URLs when doInBackground is called. Something like this:

public ArrayList<OEntityHolder> doInBackground(Void... params) {
    String [][] urls = {{"http:MyUrl/ZMOB_FECODSet", "Name1", "Fecod"},
                        {"http:MyUrl/ZMOB_OTEILSet", "Name2", "Oteil"},
                        {"http:MyUrl/ZMOB_KUNNRSet", "Name3", "Kunnr"},
                        {"http:MyUrl/ZMOB_BAULTSet", "Name4", "Bault"}};
    for (String [] urlProp:urls) {
        //Here you get the list of entities from the url
        List<OEntity> listEntities = ODataJerseyConsumer.create(urlProp[0]).getEntities("MYENTITYTOCONSUME").execute().toList();
        for (OEntity entity:listEntities) {
            OEntityHolder holder = new OEntityHolder(entity);
            for (int i = 1; i < urlProp.length; i++)
                holder.addProperty(urlProp[i]);
            zmob_entity_holders.add(holder);
        }
    }
    //At this point, all of the entities associated with the list of URLS will be added to the list
    return zmob_entity_holders;
}   

You now have ALL of the entities associated with the list of URLs in zmob_kunnr. Before you can and can insert then into the DB like so:

for (OEntityHolder holder : zmob_entity_holders) {
    sql_obj.insert(holder.getEntity().getProperty(holder.removeProperty()).toString(), holder.getEntity().getProperty(holder.removeProperty()).toString());
}

If each entity has a associated name, you can store the names in a map, where the key is the URL and the value the name.

HashMap<String, String> urlEntityNames = new HashMap<>();
urlEntityNames.put("http://MyUrlService:8080", "MYENTITYTOCONSUME");
...//Add more URLs and entity names

You can then, when running through the list of entities, do a look-up in the map to find the correct name:

List<OEntity> listEntities = ODataJerseyConsumer.create(url).getEntities(urlEntityNames.get(url)).execute().toList();

I hope this helps, if I misunderstood you just correct me in the comments.

EDIT: Added list of URLs, holder and DB insert.

Community
  • 1
  • 1
Ian2thedv
  • 2,691
  • 2
  • 26
  • 47
  • Oh, thanks a lot also @lan2thedv, it was exaclty also what i was nedding thank to you two... !!! – Vinicius Biscolla Dec 06 '14 at 03:12
  • Yes, the answer adds all of the entities to `zmob_kunnr`. I now see the association between `ZMOB_KUNNRSet` and `zmob_kunnr` list, but I am not sure if I understand correctly. Would you like to add the entities from http:MyUrl/ZMOB_KUNNRSet to `zmob_kunnr`, http:MyUrl/ZMOB_OTEILSet to `zmob_oteil`, etc. And does their properties change? example: http:MyUrl/ZMOB_OTEILSet - `entity.getProperty("Oteil")`? – Ian2thedv Dec 08 '14 at 06:26
  • Okay, I see, I updated the answer. Unfortunately I can't help you any further due to my lack of knowledge regarding you full problem or explanation. If there is something else that you are struggling with that I can't help you with, post a new question, it will get more attention. Good luck! – Ian2thedv Dec 08 '14 at 07:03
  • Hey @lan2thedv, could you please re-upload, your last code, it was making more sense to me, i almost got a solution to what i was trying to say to you, and posted as an answer but i'm getting an exception on the final of my 2nd AsyncTask on the postExecute method when i'm calling the 2rd that's why i want your last code, to see how can i manage that, thanks a lot... – Vinicius Biscolla Dec 09 '14 at 01:51
0

I guess i found a solution, but my log cat, is giving an exception to me any updtades about my 2nd doInBackgroundBault (Material),

public class callServiceCliente extends AsyncTask<Void, Void, ArrayList<OEntity>> {
    protected void onPreExecute() {
        progressC = ProgressDialog.show(Atualizar_Dados.this, "Aguarde...", "Atualizando Clientes", true, true);
    }

    public ArrayList<OEntity> doInBackground(Void... params) {
        ODataConsumer ccli = ODataJerseyConsumer.create(URL);
        List<OEntity> listEntitiesKunnr = ccli.getEntities("ZMOB_KUNNRSet").execute().toList();

        System.out.println("Size" + listEntitiesKunnr.size());

        for (OEntity entityKunnr : listEntitiesKunnr) {
            zmob_kunnr.add(entityKunnr);
        }
        return zmob_kunnr;
    }

    protected void onPostExecute(ArrayList<OEntity> kunnr) {
        super.onPostExecute(kunnr);
        try {
            sql_obj.open();
            sql_obj.deleteAll();

            for (int k = 0; k < zmob_kunnr.size(); k++) {
                sql_obj.insertCliente(zmob_kunnr.get(k).getProperty("Kunnr").getValue().toString().toUpperCase(), zmob_kunnr.get(k).getProperty("Name1").getValue().toString().toUpperCase());
            }
            sql_obj.close();
        } catch (Exception e) {
        }
        try {
            clienteAdapter = new ArrayAdapter<OEntity>(Atualizar_Dados.this, android.R.layout.simple_list_item_1, kunnr);
            listCliente.setAdapter(clienteAdapter);

        } catch (Exception eq) {
        }
        progressC.dismiss();
        new callServiceMaterial().execute();
    }
}

public class callServiceMaterial extends AsyncTask<Void, Void, ArrayList<OEntity>> {
    protected void onPreExecute() {
        progressM = ProgressDialog.show(Atualizar_Dados.this, "Aguarde...", "Atualizando Materiais", true, true);
    }

    public ArrayList<OEntity> doInBackground(Void... params) {
        ODataConsumer cmat = ODataJerseyConsumer.create(URL);
        List<OEntity> listEntitiesBault = cmat.getEntities("ZMOB_BAULTSet").filter("IErsda eq '20141101'").execute().toList();
        System.out.println("Size" + listEntitiesBault.size());
        for (OEntity entityBault : listEntitiesBault) {
            zmob_bault.add(entityBault);
        }
        return zmob_bault;
    }

    protected void onPostExecute(ArrayList<OEntity> bault) {
        super.onPostExecute(bault);
        try {
            sql_obj.open();
            sql_obj.deleteAll();
            for (int b = 0; b < zmob_bault.size(); b++) {
                sql_obj.insertMaterial(zmob_bault.get(b).getProperty("Matnr").getValue().toString().toUpperCase(), zmob_bault.get(b).getProperty("Maktxt").getValue().toString().toUpperCase());
            }
            sql_obj.close();
        } catch (Exception e) {
        }
        try {
            materialAdapter = new ArrayAdapter<OEntity>(Atualizar_Dados.this, android.R.layout.simple_list_item_1, bault);
            listMaterial.setAdapter(clienteAdapter);

        } catch (Exception eq) {
        }
        progressM.dismiss();
        new callServiceProblema().execute();
    }
}

public class callServiceProblema extends AsyncTask<Void, Void, ArrayList<OEntity>> {
    protected void onPreExecute() {
        progressProb = ProgressDialog.show(Atualizar_Dados.this, "Aguarde...", "Atualizando Problemas", true, true);
    }

    public ArrayList<OEntity> doInBackground(Void... params) {
        ODataConsumer cprob = ODataJerseyConsumer.create(URL);
        List<OEntity> listEntitiesFecod = cprob.getEntities("ZMOB_FECODSet").execute().toList();

        System.out.println("Size" + listEntitiesFecod.size());
        for (OEntity entityFecod : listEntitiesFecod) {
            zmob_fecod.add(entityFecod);
        }
        return zmob_fecod;
    }

    protected void onPostExecute(ArrayList<OEntity> fecod) {
        super.onPostExecute(fecod);
        try {
            sql_obj.open();
            sql_obj.deleteAll();
            for (int f = 0; f < zmob_fecod.size(); f++) {
                sql_obj.insertProblema(zmob_fecod.get(f).getProperty("Fecod").getValue().toString().toUpperCase(), zmob_fecod.get(f).getProperty("Kurztext").getValue().toString().toUpperCase());
            }
            sql_obj.close();
        } catch (Exception e) {
        }
        try {
            problemaAdapter = new ArrayAdapter<OEntity>(Atualizar_Dados.this, android.R.layout.simple_list_item_1, fecod);
            listProblema.setAdapter(problemaAdapter);

        } catch (Exception eq) {
        }
        progressProb.dismiss();
        new callServiceProcedencia().execute();
    }
}

public class callServiceProcedencia extends AsyncTask<Void, Void, ArrayList<OEntity>> {
    protected void onPreExecute() {
        progressProc = ProgressDialog.show(Atualizar_Dados.this, "Aguarde...", "Atualizando base de dados", true, true);
    }

    public ArrayList<OEntity> doInBackground(Void... params) {
        ODataConsumer c = ODataJerseyConsumer.create(URL);
        List<OEntity> listEntitiesProcedencia = c.getEntities("ZMOB_OTEILSet").execute().toList();
        System.out.println("Size" + listEntitiesProcedencia.size());
        for (OEntity entityProcedencia : listEntitiesProcedencia) {
            zmob_oteil.add(entityProcedencia);
        }
        return zmob_oteil;
    }

    protected void onPostExecute(ArrayList<OEntity> oteil) {
        super.onPostExecute(oteil);
        try {
            sql_obj.open();
            sql_obj.deleteAll();

            for (int o = 0; o < zmob_oteil.size(); o++) {
                sql_obj.insertCliente(zmob_oteil.get(o).getProperty("Fecod").getValue().toString().toUpperCase(), zmob_oteil.get(o).getProperty("Kurztext").getValue().toString().toUpperCase());
            }
            sql_obj.close();
        } catch (Exception e) {
        }
        try {
            procedenciaAdapter = new ArrayAdapter<OEntity>(Atualizar_Dados.this, android.R.layout.simple_list_item_1, oteil);
            // listCliente.setAdapter(clienteAdapter);

        } catch (Exception eq) {
        }
        progressProc.show(Atualizar_Dados.this, "Finalizado", "Base de dados atualizada", true, true).dismiss();
        Toast.makeText(Atualizar_Dados.this, "Base de dados atualizada com sucesso", Toast.LENGTH_LONG).show();
    }
}

Okay, so here is the solution that i find, and i couldn't insert your solution because, when i put inser.add(entity), they didn't show me the properties but if you have a better way to do what i did, i will really appreciate,

and by the way i need to query this consume by range date in the filter(). like i did here...

List listEntitiesBault = cmat.getEntities("ZMOB_BAULTSet").filter("IErsda eq '20141101'").execute().toList(); but isn't working, so i don't have any ideas why, i saw couple close solution on the internet and saw fields like .top(1) and .first(); that i didn't understand...

thanks a lot !!!