KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencrx > demo > kernel > SimpleClient


1 /*
2  * ====================================================================
3  * Project: opencrx, http://www.opencrx.org/
4  * Name: $Id: SimpleClient.java,v 1.12 2006/03/24 01:07:23 wfro Exp $
5  * Description: openCRX simple client
6  * Revision: $Revision: 1.12 $
7  * Owner: CRIXP AG, Switzerland, http://www.crixp.com
8  * Date: $Date: 2006/03/24 01:07:23 $
9  * ====================================================================
10  *
11  * This software is published under the BSD license
12  * as listed below.
13  *
14  * Copyright (c) 2004, CRIXP Corp., Switzerland
15  * All rights reserved.
16  *
17  * Redistribution and use in source and binary forms, with or without
18  * modification, are permitted provided that the following conditions
19  * are met:
20  *
21  * * Redistributions of source code must retain the above copyright
22  * notice, this list of conditions and the following disclaimer.
23  *
24  * * Redistributions in binary form must reproduce the above copyright
25  * notice, this list of conditions and the following disclaimer in
26  * the documentation and/or other materials provided with the
27  * distribution.
28  *
29  * * Neither the name of CRIXP Corp. nor the names of the contributors
30  * to openCRX may be used to endorse or promote products derived
31  * from this software without specific prior written permission
32  *
33  *
34  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
35  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
36  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
37  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
39  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
40  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
41  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
43  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46  * POSSIBILITY OF SUCH DAMAGE.
47  *
48  * ------------------
49  *
50  * This product includes software developed by the Apache Software
51  * Foundation (http://www.apache.org/).
52  *
53  * This product includes software developed by contributors to
54  * openMDX (http://www.openmdx.org/)
55  */

56 package org.opencrx.demo.kernel;
57
58 import java.math.BigDecimal JavaDoc;
59 import java.util.Collection JavaDoc;
60 import java.util.Date JavaDoc;
61 import java.util.Iterator JavaDoc;
62
63 import javax.jmi.reflect.RefException;
64 import javax.naming.Context JavaDoc;
65 import javax.naming.InitialContext JavaDoc;
66 import javax.naming.NamingException JavaDoc;
67
68 import org.opencrx.kernel.account1.cci.Account;
69 import org.opencrx.kernel.account1.cci.AccountAddress;
70 import org.opencrx.kernel.account1.cci.AccountFilter;
71 import org.opencrx.kernel.address1.cci.EMailAddressable;
72 import org.opencrx.kernel.address1.cci.PhoneNumberAddressable;
73 import org.opencrx.kernel.address1.cci.PostalAddressable;
74 import org.opencrx.kernel.address1.cci.WebAddressable;
75 import org.opencrx.kernel.base.cci.ExportResult;
76 import org.opencrx.kernel.client.ClientHelper;
77 import org.opencrx.kernel.product1.cci.Product;
78 import org.openmdx.base.accessor.generic.cci.ObjectFactory_1_0;
79 import org.openmdx.base.accessor.jmi.cci.JmiServiceException;
80 import org.openmdx.base.accessor.jmi.cci.RefPackage_1_0;
81 import org.openmdx.base.accessor.jmi.spi.RefRootPackage_1;
82 import org.openmdx.base.exception.ServiceException;
83 import org.openmdx.compatibility.base.query.FilterOperators;
84
85 public class SimpleClient {
86
87     //-----------------------------------------------------------------------
88
public static void main(
89         String JavaDoc[] args
90     ) {
91         try {
92             Context JavaDoc componentEnvironment = (Context JavaDoc)new InitialContext JavaDoc().lookup("java:comp/env");
93             
94             // Get openMDX object factory
95
ObjectFactory_1_0 of = ClientHelper.createObjectFactory(
96                 componentEnvironment,
97                 "openmdx/data"
98             );
99             
100             // Get JMI root package. For more information about
101
// 'Programming a JMI client for openMDX' see
102
// http://www.openmdx.org/openmdx/1.3/example-lab/htmlsingle/Example-Lab.html
103
RefPackage_1_0 rootPkg = new RefRootPackage_1(
104                 of,
105                 null, // impls
106
null, // context
107
"cci",
108                 false // throw NOT_FOUND
109
);
110
111             // Load models required by client
112
ClientHelper.loadModels(
113                 componentEnvironment,
114                 rootPkg
115             );
116
117             // get JMI packages
118
org.opencrx.kernel.account1.cci.account1Package account1Pkg =
119                 (org.opencrx.kernel.account1.cci.account1Package)rootPkg.refPackage(
120                     org.opencrx.kernel.account1.cci.account1Package.class.getName()
121                 );
122             org.opencrx.kernel.product1.cci.product1Package product1Pkg =
123                 (org.opencrx.kernel.product1.cci.product1Package)rootPkg.refPackage(
124                     org.opencrx.kernel.product1.cci.product1Package.class.getName()
125                 );
126             org.opencrx.kernel.contract1.cci.contract1Package contract1Pkg =
127                 (org.opencrx.kernel.contract1.cci.contract1Package)rootPkg.refPackage(
128                     org.opencrx.kernel.contract1.cci.contract1Package.class.getName()
129                 );
130             
131             // Get provider and segment name
132
String JavaDoc providerName = (String JavaDoc)componentEnvironment.lookup("providerName");
133             String JavaDoc segmentName = (String JavaDoc)componentEnvironment.lookup("segmentName");
134
135             // get account segment
136
org.opencrx.kernel.account1.cci.Segment accountSegment =
137                 (org.opencrx.kernel.account1.cci.Segment)rootPkg.refObject(
138                     "xri:@openmdx:org.opencrx.kernel.account1/provider/" + providerName + "/segment/" + segmentName
139                 );
140             // get product segment
141
org.opencrx.kernel.product1.cci.Segment productSegment =
142                 (org.opencrx.kernel.product1.cci.Segment)rootPkg.refObject(
143                     "xri:@openmdx:org.opencrx.kernel.product1/provider/" + providerName + "/segment/" + segmentName
144                 );
145             // Get contract segment
146
org.opencrx.kernel.contract1.cci.Segment contractSegment =
147                 (org.opencrx.kernel.contract1.cci.Segment)rootPkg.refObject(
148                     "xri:@openmdx:org.opencrx.kernel.contract1/provider/" + providerName + "/segment/" + segmentName
149                 );
150             
151             // Get parameters
152
String JavaDoc name = (String JavaDoc)componentEnvironment.lookup("name");
153             Boolean JavaDoc xmlExport = (Boolean JavaDoc)componentEnvironment.lookup("xmlExport");
154             
155             // find accounts matching the given name
156
System.out.println("openCRX SimpleClient: Find accounts with name LIKE " + name);
157
158             // find accounts
159
AccountFilter filter = account1Pkg.createAccountFilter();
160             filter.thereExistsFullName(
161                 FilterOperators.IS_LIKE,
162                 new String JavaDoc[]{name + "%"}
163             );
164             Collection JavaDoc accounts = accountSegment.getAccount(filter);
165             
166             // print accounts and their addresses
167
for(
168                 Iterator JavaDoc i = accounts.iterator();
169                 i.hasNext();
170             ) {
171                 Account account = (Account)i.next();
172                 // call operation exportXml
173
System.out.println();
174                 System.out.println("account=" + account.refMofId());
175                 System.out.println(" full name=" + account.getFullName());
176                 if(xmlExport.booleanValue()) {
177                     ExportResult result = account.exportXml("", "");
178                     System.out.println(" xml export size=" + result.getItem().length + "; mimeType=" + result.getItemMimeType());
179                 }
180                 
181                 // get all addresses of account and iterate
182
Collection JavaDoc addresses = account.getAddress();
183                 for(
184                     Iterator JavaDoc j = addresses.iterator();
185                     j.hasNext();
186                 ) {
187                     AccountAddress address = (AccountAddress)j.next();
188                     System.out.println(" address=" + address.refMofId());
189                     if(address instanceof PostalAddressable) {
190                         PostalAddressable postal = (PostalAddressable)address;
191                         System.out.println(" address lines=" + postal.getPostalAddressLine() + "; " + postal.getPostalStreet() + "; " + postal.getPostalCode() + "; " + postal.getPostalCity());
192                     }
193                     else if(address instanceof EMailAddressable) {
194                         System.out.println(" email=" + ((EMailAddressable)address).getEmailAddress());
195                     }
196                     else if(address instanceof WebAddressable) {
197                         System.out.println(" web url=" + ((WebAddressable)address).getWebUrl());
198                     }
199                     else if(address instanceof PhoneNumberAddressable) {
200                         System.out.println(" phone number=" + ((PhoneNumberAddressable)address).getPhoneLocalNumber());
201                     }
202                 }
203             }
204             
205             // Get any account
206
Account customer = (Account)accountSegment.getAccount().iterator().next();
207             // Get any product
208
Product product = (Product)productSegment.getProduct().iterator().next();
209             
210             // Create invoice
211
org.opencrx.kernel.contract1.cci.Invoice invoice = contract1Pkg.getInvoiceClass().createInvoice();
212             invoice.setName("Invoice created by SimpleClient");
213             invoice.setCustomer(customer);
214             contract1Pkg.refBegin();
215             contractSegment.addInvoice(invoice);
216             contract1Pkg.refCommit();
217             System.out.println("Invoice 'Invoice created by SimpleClient'.refMofId()=" + invoice.refMofId());
218                         
219             // Create invoice position
220
BigDecimal JavaDoc quantity = new BigDecimal JavaDoc(11.5);
221             org.opencrx.kernel.contract1.cci.CreatePositionParams params = contract1Pkg.createCreatePositionParams(
222                 "position name",
223                 null,
224                 null,
225                 null,
226                 product,
227                 quantity,
228                 null
229             );
230             contract1Pkg.refBegin();
231             org.opencrx.kernel.contract1.cci.CreatePositionResult result = invoice.createPosition(params);
232             contract1Pkg.refCommit();
233             System.out.println("position=" + result.getPosition().refMofId());
234
235             // Create delivery information
236
org.opencrx.kernel.contract1.cci.InvoicePosition invoicePosition =
237                 (org.opencrx.kernel.contract1.cci.InvoicePosition)rootPkg.refObject(result.getPosition().refMofId());
238             org.opencrx.kernel.contract1.cci.DeliveryInformation deliveryInformation =
239                 contract1Pkg.getDeliveryInformationClass().createDeliveryInformation();
240             deliveryInformation.setActualDeliveryOn(new Date JavaDoc());
241             deliveryInformation.setQuantityShipped(new BigDecimal JavaDoc(5.0));
242             contract1Pkg.refBegin();
243             invoicePosition.addDeliveryInformation(deliveryInformation);
244             contract1Pkg.refCommit();
245             
246         }
247         catch (NamingException JavaDoc e) {
248             e.printStackTrace();
249         }
250         catch (ServiceException e) {
251             e.printStackTrace();
252         }
253         catch (JmiServiceException e) {
254             e.printStackTrace();
255         }
256         catch (RefException e) {
257             e.printStackTrace();
258         }
259     }
260         
261 }
262
263 //--- End of File -----------------------------------------------------------
264
Popular Tags