View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2005 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.tests.remoting.jaxws.service.impl;
18  
19  import javax.jws.WebMethod;
20  import javax.jws.WebService;
21  
22  import com.sun.xml.ws.developer.UsesJAXBContext;
23  
24  import ch.elca.el4j.services.persistence.generic.dao.DaoRegistry;
25  import ch.elca.el4j.services.persistence.hibernate.dao.ConvenienceGenericHibernateDao;
26  import ch.elca.el4j.services.persistence.hibernate.dao.extent.DataExtent;
27  import ch.elca.el4j.services.remoting.jaxb.hibernate.JAXBContextFactoryImpl;
28  import ch.elca.el4j.tests.person.dao.PersonDao;
29  import ch.elca.el4j.tests.person.dao.impl.hibernate.GenericHibernatePersonDaoInterface;
30  import ch.elca.el4j.tests.person.dom.Brain;
31  import ch.elca.el4j.tests.person.dom.Person;
32  import ch.elca.el4j.tests.remoting.jaxws.service.LazyPerson;
33  
34  /**
35   *
36   * A service that returns {@link Person} objects that contain lazily loaded properties.
37   *
38   * @svnLink $Revision: 4122 $;$Date: 2010-08-19 13:27:36 +0200 (Do, 19. Aug 2010) $;$Author: swrelca $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/tests/remoting_jaxws/jar-hibernate/src/main/java/ch/elca/el4j/tests/remoting/jaxws/service/impl/LazyPersonImplJaxws.java $
39   *
40   * @author Stefan Wismer (SWI)
41   *
42   *
43   * Recommended naming convention:
44   * name = name of implemented core interface
45   * serviceName = name of implemented core interface + "Service"
46   */
47  @WebService(name = "LazyPerson",
48  	serviceName = "LazyPersonService",
49  	targetNamespace = "http://webservice.jaxws.remoting.tests.el4j.elca.ch/")
50  @UsesJAXBContext(JAXBContextFactoryImpl.class)
51  public class LazyPersonImplJaxws implements LazyPerson {
52  	
53  	/**
54  	 * The Person Dao.
55  	 */
56  	private GenericHibernatePersonDaoInterface personDao;
57  	
58  	/**
59  	 * The id (key) of the person.
60  	 */
61  	private int personId = -1;
62  
63  	@Override
64  	@SuppressWarnings("unchecked")
65  	public Person getPerson(boolean loadBrain) {
66  		ConvenienceGenericHibernateDao<Person, Integer> dao
67  			= (ConvenienceGenericHibernateDao<Person, Integer>) personDao;
68  		if (personId == -1) {
69  			Person person = new Person("Peter Muster");
70  			Brain b = new Brain();
71  			b.setIq(99);
72  			person.setBrain(b);
73  			dao.saveOrUpdate(person);
74  			
75  			personId = person.getKey();
76  		}
77  		
78  		if (loadBrain) {
79  			try {
80  				DataExtent ex = new DataExtent(Person.class).with("brain");
81  				Person person = dao.findById(personId, ex);
82  				// avoid infinite cycle during marshaling
83  				person.getBrain().setOwner(null);
84  				return person;
85  			} catch (Exception e) {
86  				return null;
87  			}
88  		} else {
89  			return dao.findById(personId);
90  		}
91  	}
92  	
93  	@Override
94  	@SuppressWarnings("unchecked")
95  	public void setPerson(Person person) {
96  		ConvenienceGenericHibernateDao<Person, Integer> dao
97  			= (ConvenienceGenericHibernateDao<Person, Integer>) personDao;
98  		dao.saveOrUpdate(person);
99  		personId = person.getKey();
100 	}
101 
102 	/**
103 	 * @return    the Person Dao
104 	 */
105 	@WebMethod(exclude = true)
106 	public GenericHibernatePersonDaoInterface getPersonDao() {
107 		return personDao;
108 	}
109 
110 	/**
111 	 * @param personDao    the Person Dao
112 	 */
113 	@WebMethod(exclude = true)
114 	public void setPersonDao(GenericHibernatePersonDaoInterface personDao) {
115 		this.personDao = personDao;
116 	}	
117 	
118 }