Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Deleting complex object in Java list (for implementation on WSDL)

I am following those three tutorials and I have completed it with success.

( http://oracleadfhowto.blogspot.in/2013/03/create-simple-web-service-using-oracle.html )

( http://oracleadfhowto.blogspot.in/2013/03/consuming-web-service-using-web-service.html )

( http://oracleadfmobile.blogspot.in/2013/03/consuming-soap-web-service-in-adf.html )

But then, as author haven't implemented removeCountries method I tried to create it.

What I did initially was to just add to class Countries this method:

  public boolean removeCountry(Country country) {

  return countries.remove(country);

  }

But although compiler wasn't complaining it didn't work. Actually it worked last night (before reboot) but not today.

Must be some SOAP iterator/bindig thing or whatever. Or I thought  that it worked but in fact it didn't.

Here are original classes:

//-------------------------------

public class Country {

    String CountryId;

    String CountryName;

   

    public Country() {

        super();

    }

   

    public Country( String id, String name ) {

        super();

        this.CountryId = id;

        this.CountryName = name;

    }

    public void setCountryId(String CountryId) {

        this.CountryId = CountryId;

    }

    public String getCountryId() {

        return CountryId;

    }

    public void setCountryName(String CountryName) {

        this.CountryName = CountryName;

    }

    public String getCountryName() {

        return CountryName;

    }

}

//----------------------------------

public class Countries {   

    List<Country> countries = new ArrayList<Country>();

   

    public Countries() {

        super();

    }

    public void setCountries(List<Country> countries) {

        this.countries = countries;

    }

    public List<Country> getCountries() {

        if ( countries.size() == 0 ) {

            countries.add( new Country("IT","ITALY"));

            countries.add( new Country("IN","INDIA"));

            countries.add( new Country("US","UNITED STATES"));

        }

        return countries;

    }

   

    public boolean addCountry( Country country ) {

        return countries.add( country );

    }

  // I added this

  public boolean removeCountry( Country country ) {

        return countries.remove( country );

    }

}

//----------------------------------

Then I decided to write (for a start) just plain Java classes and now it looks like below shown code.

It works in IDE, not yet implemented on weblogic server. I hope it would work.

//----------------------------------

package client;

public class Country {

  String CountryId;

  String CountryName;

  public Country() {

  super();

  }

  public Country(String id, String name) {

  super();

  this.CountryId = id;

  this.CountryName = name;

  }

  public void setCountryId(String CountryId) {

  this.CountryId = CountryId;

  }

  public String getCountryId() {

  return CountryId;

  }

  public void setCountryName(String CountryName) {

  this.CountryName = CountryName;

  }

  public String getCountryName() {

  return CountryName;

  }

}

//------------------------

package client;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

public class Countries {

  List<Country> countries = new ArrayList<Country>();

  public Countries() {

  super();

  }

  public void setCountries(List<Country> countries) {

  this.countries = countries;

  }

  public List<Country> getCountries() {

  if (countries.size() == 0) {

  countries.add(new Country("IT", "ITALY"));

  countries.add(new Country("IN", "INDIA"));

  countries.add(new Country("US", "UNITED STATES"));

  }

  return countries;

  }

  public boolean addCountry(Country country) {

  return countries.add(country);

  }

  // This left unused

  public boolean removeCountry(Country country) {

  return countries.remove(country);

  }

  // I added this - what would be more elegant or smarter way to do this?

  public void removeCountry(String CountryId, String countryName) {

  Iterator<Country> iterator = countries.iterator();

  while (iterator.hasNext()) {

  Country value = iterator.next();

  if (CountryId.equals(value.CountryId) || countryName.equals(value.CountryName)) {

  iterator.remove();

  break;

  }

  }

  }

}

//------------------

This class is just for test it won't go on server (JDeveloper integrated web logic server)

//-------------------------------

package client;

public class UserInterface {

  public static void main(String[] args) {

  String CountryId = "";

  String CountryName = "";

  CountryName = "ENGLAND";

  Countries co = new Countries();

  co.getCountries();

  for (int i = 0; i < co.countries.size(); i++) {

  System.out.print(co.getCountries().get(i).getCountryId());

  System.out.print(" - ");

  System.out.println(co.getCountries().get(i).getCountryName());

  }

  System.out.println("-------------------------");

  // Add some countries

  co.countries.add(new Country("DE", "GERMANY"));

  co.countries.add(new Country("EN", "ENGLAND"));

  for (int i = 0; i < co.countries.size(); i++) {

  System.out.print(co.getCountries().get(i).getCountryId());

  System.out.print(" - ");

  System.out.println(co.getCountries().get(i).getCountryName());

  }

  System.out.println("-------------------------");

  // Remove some countries, this works but can't use this (index)

  // co.countries.remove(0); <--- there should be some index instead of 0

  // I need to set properties

  CountryId = "DE";

  CountryName = "";

  // Then remove country

  co.removeCountry(CountryId, CountryName);

  CountryId = "";

  CountryName = "ENGLAND";

  // Then remove country

  co.removeCountry(CountryId, CountryName);

  // Is there any way to remove object directly? Parameters should be set by web service iterator.

  // co.countries.remove(o);

  // co.removeCountry(country)

  for (int i = 0; i < co.countries.size(); i++) {

  System.out.print(co.getCountries().get(i).getCountryId());

  System.out.print(" - ");

  System.out.println(co.getCountries().get(i).getCountryName());

  }

  }

}

//------------------------


I would like to avoid my own iterator as JDeveloper can generate automatically iterators for webservices, but if I can't get it that way, what would be better way to write above mentioned iterator in removeCountry method?

Is there any way to remove object directly with something like this:

co.countries.remove(o);

co.removeCountry(country)

using method

  // This left unused

  public boolean removeCountry(Country country) {

  return countries.remove(country);

  }

from class Countries?

Parameters should be set by web service iterator.

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 1 2014
Added on Sep 3 2014
0 comments
695 views