0

I am unable to achieve to pass an entire arraylist to a method in my java program.
I searched in the forum and tried some solution where other programmers were able to resolve, but in my program i am unable to implement the same.

I have attached the code here and I highlighted the line where i am facing the problem.

import java.io.File;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import org.apache.axiom.om.OMElement;
import org.apache.axis2.context.ConfigurationContext;
import org.apache.axis2.context.ConfigurationContextFactory;

import com.mercury.itg.ws.dm.client.AddRequestNotesDocument;
import com.mercury.itg.ws.dm.client.AddRequestNotesResponseDocument;
import com.mercury.itg.ws.dm.client.Column;
import com.mercury.itg.ws.dm.client.CreateRequestDocument;
import com.mercury.itg.ws.dm.client.CreateRequestResponseDocument;
import com.mercury.itg.ws.dm.client.DeleteRequestsDocument;
import com.mercury.itg.ws.dm.client.DeleteRequestsResponseDocument;
import com.mercury.itg.ws.dm.client.DemandServiceStub;
import com.mercury.itg.ws.dm.client.ExecuteWFTransitionsDocument;
import com.mercury.itg.ws.dm.client.ExecuteWFTransitionsResponseDocument;
import com.mercury.itg.ws.dm.client.GetRequestsDocument;
import com.mercury.itg.ws.dm.client.GetRequestsResponseDocument;
import com.mercury.itg.ws.dm.client.Identifier;
import com.mercury.itg.ws.dm.client.Note;
import com.mercury.itg.ws.dm.client.RemoteReference;
import com.mercury.itg.ws.dm.client.Request;
import com.mercury.itg.ws.dm.client.SetRequestFieldsDocument;
import com.mercury.itg.ws.dm.client.SetRequestFieldsResponseDocument;
import com.mercury.itg.ws.dm.client.SetRequestRemoteReferenceStatusDocument;
import com.mercury.itg.ws.dm.client.SetRequestRemoteReferenceStatusResponseDocument;
import com.mercury.itg.ws.dm.client.SimpleField;
import com.mercury.itg.ws.dm.client.Table;
import com.mercury.itg.ws.dm.client.URLReference;

import oracle.jdbc.OracleTypes;
import oracle.jdbc.OracleDriver;

public class PMCDeleteRequest {

    protected ConfigurationContext ctx = null;

    public PMCDeleteRequest() {
        final String repositoryPath = System.getProperty("client.repository.dir");
        final String axis2 = repositoryPath + "/conf/client-axis2.xml";
        final File file = new File(axis2);
        if (file.exists()) {
            try {
                this.ctx = ConfigurationContextFactory.createConfigurationContextFromFileSystem(repositoryPath, axis2);
            } catch (final Exception e) {
                e.printStackTrace();
                System.exit(1);
            }
        }
    }
      String name;
     public void setName(String name) {
            this.name = name;
        }
    /**
     * The main program Parameter: args[0] - service URL. e.g.
     * http://server:port/itg/ppmservices/DemandService
     */

    public static void main(final String[] args) throws Exception {

        String URL = args[0];
        String username = args[1];
        String password = args[2];
        String requestids = args[3];
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection con = DriverManager.getConnection(URL, username, password);
        String request_ids = requestids;
        String commaSeparateRequestIds = request_ids.replaceAll("#@#", ",");
        String request_delete_sql = "SELECT REQUEST_ID FROM (SELECT KFMP.REQUEST_ID FROM KCRT_FG_MASTER_PROJ_REF KFMP, PM_PROJECTS PP WHERE KFMP.REF_MASTER_PROJECT_ID = PP.PROJECT_ID AND PP.PROJECT_ID IN ("
                + commaSeparateRequestIds + "))";
        PreparedStatement sgStmt = con.prepareStatement(request_delete_sql);
        ResultSet rs = sgStmt.executeQuery();
        List<String> delete = new ArrayList<String>();
        try {
        while (rs.next()) {

            final String serviceURL = "https://.../DemandService";
            String requestId = Integer.toString(rs.getInt(1));

            delete.add(requestId);


        }
        System.out.println("arraylist: "+delete);


                pmc.deleteRequests(serviceURL,delete);  // Here i am unable to pass the entire arraylist

            } 

        catch (final Exception e) {
                if (e instanceof org.apache.axis2.AxisFault) {
                    final org.apache.axis2.AxisFault xe = (org.apache.axis2.AxisFault) e;
                    System.out.println(xe.getMessage());
                    System.out.println("---detail---");
                    final Iterator iter = xe.getDetail().getChildElements();
                    while (iter.hasNext()) {
                        final OMElement item = (OMElement) iter.next();
                        System.out.println(item.getText());
                    }
                    System.out.println("------------");
                } else {
                    throw e;

                }
                System.exit(1);
            }

        rs.close();
        con.close();

    }

    private void deleteRequests(final String serviceURL, final String[] requestIds) throws Exception {

        // construct the Identifier array
        final Identifier[] ids = new Identifier[requestIds.length];

        for (int i = 0; i < requestIds.length; i++) {

            ids[i] = Identifier.Factory.newInstance();
            ids[i].setId(requestIds[i]);

        }

        // get Webservice handle

        final DemandServiceStub stub = new DemandServiceStub(this.ctx, serviceURL);

        // Construct message to send
        final DeleteRequestsDocument inDoc = DeleteRequestsDocument.Factory.newInstance();

        final DeleteRequestsDocument.DeleteRequests deletes = inDoc.addNewDeleteRequests();

        deletes.setRequestIdsArray(ids);

        //Set the path for cacerts for searching certificate before invoking web service
System.setProperty("javax.net.ssl.trustStore","C:\\Java\\jdk1.7.0_80\\jre\\lib\\security\\cacerts");
// Invoke web service
        final DeleteRequestsResponseDocument outDoc = stub.deleteRequests(inDoc);

        // Process the return request
        final int returnCode = outDoc.getDeleteRequestsResponse().getReturn();
        System.out.println("Delete Requests Succeeded. Return Code: " + returnCode);
    }

}

The line where i am facing the problem in passing the entire arraylist to a method:

pmc.deleteRequests(serviceURL,delete);

Here the deleteRequests parametes can have (String,String[])

Any guidance/suggestion will be really helpfull.

Thanks & Regards, Sameet

Peter Wippermann
  • 3,458
  • 3
  • 30
  • 46
Sameet
  • 11
  • 2

1 Answers1

1

Use toArray(T[] a) method to convert a List to an array.

String[] deleteArray = delete.toArray(new String[delete.size()]);
pmc.deleteRequests(serviceURL, deleteArray);
Roshana Pitigala
  • 7,058
  • 8
  • 38
  • 66
  • 2
    The toArray method only needs an array of the expected type. Its size doesn't matter. So better to write: String[] deleteArray = delete.toArray(new String[0]); – Conffusion Dec 17 '18 at 11:28
  • 3
    @Conffusion Here, size does matter: If you pass an array that is too small, another one will be created. That's wasted performance. Since the collection's size is known, you should instantiate the array with the correct length. – Peter Wippermann Dec 17 '18 at 12:22
  • 1
    Wasn't aware of that. Tested and confirmed. Learned something new today. Thanks – Conffusion Dec 17 '18 at 12:26