/*
 * ContactSoapService.java - address book SOAP webservice
 * 
 * Copyright (C) 2015 Paolo Rovelli 
 * 
 * Author: Paolo Rovelli <paolorovelli@yahoo.it> 
 */

package org.epalrov.addressbook;

import javax.ejb.Stateless;

import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebResult;
import javax.jws.WebParam;

import javax.persistence.Query;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import java.util.List;

/**
 * ContactSoapService is an EJB exposed as SOAP webservice
 */
@Stateless
@WebService(
    serviceName="address-book/soap-api",
    portName = "ContactSoapServicePort",
    targetNamespace = "http://addressbook.epalrov.org")/*,
    endpointInterface = "org.epalrov.addressbook.ContactSoapService")*/
public class ContactSoapService {

    @PersistenceContext
    private EntityManager em;

    @WebMethod(operationName="GetContacts")
    @WebResult(name="contact")
    public List<Contact> getContacts(
/*            @DefaultValue("0") @QueryParam("start") int start,
            @DefaultValue("100") @QueryParam("max") int max*/) {
        Query query = em.createNamedQuery("getContacts")
/*            .setFirstResult(start)
            .setMaxResults(max)*/;
        return (List<Contact>)query.getResultList();
    }

    @WebMethod(operationName="GetContact")
    @WebResult(name="contact")
    public Contact getContact(
            @WebParam(name="id") Integer id) {
        Query query = em.createNamedQuery("getContact")
           .setParameter("id", id);
        return (Contact)query.getSingleResult();
    }

    @WebMethod(operationName="CreateContact")
    public void createContact(
             @WebParam(name="contact") Contact contact) {
        em.persist(contact);
    }

    @WebMethod(operationName="UpdateContact")
    public void updateContact(
             @WebParam(name="id") Integer id,
             @WebParam(name="contact") Contact contact) {
        Query query = em.createNamedQuery("getContact")
           .setParameter("id", id);
        Contact managedContact = (Contact)query.getSingleResult();
        managedContact.setFirstName(contact.getFirstName());
        managedContact.setLastName(contact.getLastName());
        managedContact.setEmail(contact.getEmail());
    }

    @WebMethod(operationName="DeleteContact")
    public void deleteContact(
            @WebParam(name="id") Integer id) {
        Query query = em.createNamedQuery("getContact")
           .setParameter("id", id);
        Contact contact = (Contact)query.getSingleResult();
        em.remove(contact);
    }

}


@@ -704,15 +704,15 @@
 rg%22)
-/*,
 %0A   
+ //
  end
@@ -772,19 +772,16 @@
 Service%22
-)*/
 %0Apublic 
@@ -979,135 +979,8 @@
 cts(
-%0A/*            @DefaultValue(%220%22) @QueryParam(%22start%22) int start,%0A            @DefaultValue(%22100%22) @QueryParam(%22max%22) int max*/
 ) %7B%0A
@@ -1039,22 +1039,18 @@
 ts%22)
+;
 %0A
-/*   
         
  .se
@@ -1049,81 +1049,39 @@
     
- .setFirstResult(start)%0A            .setMaxResults(max)*/;%0A        return
+List%3CContact%3E managedContacts =
  (Li
@@ -1111,24 +1111,56 @@
 sultList();%0A
+        return managedContacts;%0A
     %7D%0A%0A    @
@@ -1406,22 +1406,40 @@
         
-return
+Contact managedContact =
  (Contac
@@ -1461,24 +1461,55 @@
 leResult();%0A
+        return managedContact;%0A
     %7D%0A%0A    @
@@ -1553,27 +1553,56 @@
 t%22)%0A    
-public void
+@WebResult(name=%22id%22)%0A    public Integer
  createC
@@ -1692,24 +1692,76 @@
 t(contact);%0A
+        em.flush();%0A        return contact.getId();%0A
     %7D%0A%0A    @
@@ -1805,27 +1805,61 @@
 t%22)%0A    
-public void
+@WebResult(name=%22contact%22)%0A    public Contact
  updateC
@@ -2298,24 +2298,55 @@
 etEmail());%0A
+        return managedContact;%0A
     %7D%0A%0A    @
@@ -2568,25 +2568,32 @@
     Contact 
-c
+managedC
 ontact = (Co
@@ -2641,17 +2641,24 @@
 .remove(
-c
+managedC
 ontact);

fix SOAP responses for CRUD operations