KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hero > client > samples > SampleProjectApi


1 package hero.client.samples;
2
3 /*
4 *
5 * Inscription.java -
6 * Copyright (C) 2004 Ecoo Team
7 * Anne.Geron@bull.net
8 *
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */

24
25
26 import javax.security.auth.login.LoginContext JavaDoc;
27 import hero.client.test.SimpleCallbackHandler;
28
29 import hero.interfaces.ProjectSessionHome;
30 import hero.interfaces.ProjectSession;
31 import hero.interfaces.ProjectSessionUtil;
32 import hero.interfaces.UserRegistration;
33 import hero.interfaces.UserRegistrationHome;
34 import hero.interfaces.UserRegistrationUtil;
35 import hero.interfaces.BnRoleLocal;
36
37
38 import hero.interfaces.Constants;
39
40 import java.util.*;
41
42 public class SampleProjectApi {
43     
44     static public void main(String JavaDoc[] args) throws Exception JavaDoc{
45     
46           // User registration interface creation
47
UserRegistrationHome urHome = UserRegistrationUtil.getHome();
48           UserRegistration userReg=urHome.create();
49           
50           try{
51               // User "jack" (customer) creation in Bonita database
52
userReg.userCreate("jack","jack","miguel.valdes-faura@ext.bull.net");
53              // User "john" (service customer) creation in Bonita database
54
userReg.userCreate("john","john","miguel.valdes-faura@ext.bull.net");
55           }catch(Exception JavaDoc e){} // Maybe user exists
56

57           userReg.remove();
58           
59           // User Admin login
60
char[] password={'t','o','t','o'};
61           SimpleCallbackHandler handler = new SimpleCallbackHandler("admin",password);
62           LoginContext JavaDoc lc = new LoginContext JavaDoc("TestClient", handler);
63           lc.login();
64
65          // Project Session Bean Creation using Remote Interface
66
ProjectSessionHome prjHome= (ProjectSessionHome) ProjectSessionUtil.getHome();
67           ProjectSession prjSession = prjHome.create();
68                             
69           //***************************************************************/
70
//************** API Documentation - Sample 1 *******************/
71
//************** Initiating ProjectSessionBean *****************/
72
//***************************************************************/
73

74           //Process creation by user admin
75

76       System.out.println("Original Process creation ");
77           prjSession.initModel("Original Process");
78                 // if "Original Process" does not exists, it is created.
79
// Process definition : see following sections
80
// adding activities, edges, ...
81
//
82

83       System.out.println("Original Process cloning into Clone Process ");
84           try {
85                prjSession.initProject("Original Process", "Clone Process");
86           } catch(Exception JavaDoc e) {System.out.println(e);} //Maybe project does not exists
87

88       System.out.println("Original Process : current Process");
89           prjSession.initModel("Original Process");
90
91           /***************************************************************/
92           /************** API Documentation - Sample 2 *******************/
93           //************** Project properties *****************/
94
/***************************************************************/
95
96         String JavaDoc processName = prjSession.getName() ;
97         System.out.println("Current Process : " + processName) ;
98
99         try {
100                String JavaDoc parentName = prjSession.getParent();
101                 System.out.println("Parent Process : " + parentName) ;
102           } catch(Exception JavaDoc e) {System.out.println(e);} //Maybe there is no parent
103

104
105         try {
106                String JavaDoc creatorName = prjSession.getCreator();
107                 System.out.println("Process Creator : " + creatorName) ;
108           } catch(Exception JavaDoc e) {System.out.println(e);} //Maybe there is a problem
109

110         try {
111                 prjSession.setProperty("userId","user1");
112                 prjSession.setProperty("recordId","1111");
113                 prjSession.setProperty("orderId","0001");
114           } catch(Exception JavaDoc e) {System.out.println(e);} //Maybe there is a problem
115

116
117           // First way to get properties values
118
System.out.println("First way to access proprerty values : ");
119
120           Collection properties = prjSession.getProperties() ;
121           Iterator i = properties.iterator();
122           while (i.hasNext())
123           {
124                 hero.interfaces.BnProjectPropertyValue property = (hero.interfaces.BnProjectPropertyValue)i.next();
125                 try {
126                         String JavaDoc propertyKeyName = property.getTheKey();
127                         String JavaDoc propertyValue = (String JavaDoc)property.getTheValue();
128                         System.out.println("Property (Key, Value) : " + propertyKeyName + "/" + propertyValue);
129                 } catch(Exception JavaDoc e) {System.out.println(e);} //Maybe there is a problem
130
}
131
132
133           // Second way to get properties values
134
System.out.println("Second way to access proprerty values : ");
135           properties = prjSession.getPropertiesKey() ;
136           i = properties.iterator();
137           while (i.hasNext())
138           {
139                 String JavaDoc propertyKey = (String JavaDoc)i.next();
140                 try {
141                         hero.interfaces.BnProjectPropertyValue propertyValue = prjSession.getProperty(propertyKey);
142                         System.out.println("Property (Key, Value) : " + i + "/" + propertyValue);
143                 } catch(Exception JavaDoc e) {System.out.println(e);} //Maybe there is a problem
144
}
145
146
147         //Deleting Property
148
try {
149            prjSession.deleteProperty("orderId");
150         } catch(Exception JavaDoc e) {System.out.println(e);} //Maybe there is a problem
151

152         //Verification
153

154       System.out.println("Properties after one deletion : ");
155           Collection propertiesLeft = prjSession.getPropertiesKey() ;
156           Iterator j = properties.iterator();
157           while (j.hasNext())
158           {
159                 String JavaDoc propertyLeftKey = (String JavaDoc)j.next();
160                 try {
161                         hero.interfaces.BnProjectPropertyValue propertyValue = prjSession.getProperty(propertyLeftKey);
162                         System.out.println("Property (Key, Value) : " + i + "/" + propertyValue);
163
164                 } catch(Exception JavaDoc e) {System.out.println(e);} //Maybe there is a problem
165
}
166
167
168
169           /***************************************************************/
170           /************** API Documentation - Sample 3 *******************/
171           //************** Activities in Project *****************/
172
/***************************************************************/
173
174       System.out.println("Activities creation ... ");
175           try {
176             prjSession.addNode("Activity 1",Constants.Nd.AND_JOIN_NODE);
177       } catch(Exception JavaDoc e) {System.out.println(" --> " + e);} //Maybe something is wrong
178
try {
179             prjSession.addNode("Activity 2",Constants.Nd.AND_JOIN_NODE);
180       } catch(Exception JavaDoc e) {System.out.println(" --> " + e);} //Maybe something is wrong
181
try {
182             prjSession.addNode("Activity 3",Constants.Nd.AND_JOIN_NODE);
183       } catch(Exception JavaDoc e) {System.out.println(" --> " + e);} //Maybe something is wrong
184

185       System.out.println("Activity 3 definition ... ");
186           try {
187             Date dateLim = new Date(2005,05,02) ;
188         prjSession.setNodeDeadline("Activity 3",dateLim.getTime()) ;
189         prjSession.setNodeDescription("Activity 3","Activity 3 Description") ;
190       } catch(Exception JavaDoc e) {System.out.println(" --> " + e);} //Maybe something is wrong
191

192       System.out.println("Setting Activities types");
193           try {
194             prjSession.setNodeTraditional("Activity 1");
195             prjSession.setNodeAutomatic("Activity 2");
196             prjSession.setNodeTraditional("Activity 3");
197       } catch(Exception JavaDoc e) {System.out.println(" --> " + e);} //Maybe something is wrong
198

199       System.out.println("Setting node properties which will not be propagated to other nodes");
200           try {
201         prjSession.setNodeProperty("Activity 1","color","blue",false);
202         System.out.println("Setting node properties which will be propagated to other nodes");
203         prjSession.setNodeProperty("Activity 1","price","expensive",true);
204         prjSession.setNodeProperty("Activity 1","shape","square");
205       } catch(Exception JavaDoc e) {System.out.println(" --> " + e);} //Maybe something is wrong
206

207       System.out.println("Adding edges between activities");
208           try {
209         prjSession.addEdge("Activity 1","Activity 2");
210         prjSession.addEdge("Activity 2","Activity 3");
211       } catch(Exception JavaDoc e) {System.out.println(" --> " + e);} //Maybe something is wrong
212

213       System.out.println("Getting names of all the nodes in the project");
214           Collection nodesNames = prjSession.getNodesNames() ;
215           j = nodesNames.iterator();
216           while (j.hasNext())
217           {
218                 String JavaDoc nodeName = (String JavaDoc)j.next();
219                 System.out.println("Node : " + nodeName + " (anticipable : " + prjSession.getNodeAnticipable(nodeName) + " )");
220                 Collection nodeProperties = prjSession.getNodeProperties(nodeName) ;
221                 Iterator k = nodeProperties.iterator() ;
222                 while (k.hasNext())
223                 {
224                         hero.interfaces.BnNodePropertyValue nodeProperty = (hero.interfaces.BnNodePropertyValue)k.next();
225                         try {
226                             String JavaDoc nodePropertyKeyName = nodeProperty.getTheKey();
227                             String JavaDoc nodePropertyValue = nodeProperty.getTheValue();
228                             System.out.println(" --> Property (Key, Value) : " + nodePropertyKeyName + "/" + nodePropertyValue);
229                         } catch(Exception JavaDoc e) {System.out.println(" --> " + e);} //Maybe something is wrong
230
}
231           }
232
233       System.out.println("Node deletion");
234       try {
235         prjSession.deleteNode("Activity 3") ;
236       } catch(Exception JavaDoc e) {System.out.println(" --> " + e);} //Maybe something is wrong
237

238       System.out.println("Node deletion verification");
239       try {
240             nodesNames = prjSession.getNodesNames() ;
241             j = nodesNames.iterator();
242             while (j.hasNext())
243             {
244                 String JavaDoc nodeName = (String JavaDoc)j.next();
245                 System.out.println("Node : " + nodeName ); }
246       } catch(Exception JavaDoc e) {System.out.println(" --> " + e);} //Maybe something is wrong
247

248
249       System.out.println("Adding a role in the project (before instantiation) ");
250       try {
251           prjSession.addRole("Customer","customer") ;
252       } catch(Exception JavaDoc e) {System.out.println(" --> " + e);} //Maybe something is wrong
253

254       System.out.println(" Getting role names of the project ");
255       try {
256             Collection rolesNames = prjSession.getRolesNames() ;
257             j = rolesNames.iterator();
258             while (j.hasNext())
259             {
260                 String JavaDoc roleName = (String JavaDoc)j.next();
261                 System.out.println("Role : " + roleName ); }
262       } catch(Exception JavaDoc e) {System.out.println(" --> " + e);} //Maybe something is wrong
263

264           /***************************************************************/
265           /* "Original Process" instantiation */
266           /***************************************************************/
267       System.out.println("Creating project new intance");
268           try {
269                prjSession.instantiateProject("Original Process");
270           } catch(Exception JavaDoc e) {System.out.println(" --> " + e);} //Maybe project does not exists
271
// The new instance becomes the current project
272

273         processName = prjSession.getName() ;
274         System.out.println("Current Process : " + processName) ;
275
276       System.out.println(" Getting role names of the instance ");
277       try {
278             Collection rolesNames = prjSession.getRolesNames() ;
279             j = rolesNames.iterator();
280             while (j.hasNext())
281             {
282                 String JavaDoc roleName = (String JavaDoc)j.next();
283                 System.out.println("Role : " + roleName ); }
284       } catch(Exception JavaDoc e) {System.out.println(" --> " + e);} //Maybe something is wrong
285

286           /***************************************************************/
287           /************** API Documentation - Sample 4 *******************/
288           //************** Users in Project *****************/
289
/***************************************************************/
290
291       System.out.println(" Getting users names of the project ");
292       try {
293             Collection usersNames = prjSession.getUsers() ;
294             j = usersNames.iterator();
295             while (j.hasNext())
296             {
297                 String JavaDoc userName = (String JavaDoc)j.next();
298                 System.out.println("User : " + userName ); }
299       } catch(Exception JavaDoc e) {System.out.println(" --> " + e);} //Maybe something is wrong
300

301       System.out.println(" Adding John in the project ");
302       try {
303         prjSession.addUser("john") ;
304       } catch(Exception JavaDoc e) {System.out.println(" --> " + e);} //Maybe something is wrong
305

306         processName = prjSession.getName() ;
307         System.out.println(" --> Current Process : " + processName + " contains john :" + prjSession.containsUser("john") ) ;
308
309           /***************************************************************/
310           /************** API Documentation - Sample 5 *******************/
311           //************** Roles in Project *****************/
312
/***************************************************************/
313
314       System.out.println("Adding a Custumer role for john in the current project ");
315       try {
316           prjSession.setUserRole("john","Customer") ;
317       } catch(Exception JavaDoc e) {System.out.println(" --> " + e);} //Maybe something is wrong
318

319       System.out.println(" Getting role names of the project ");
320       try {
321             Collection rolesNames = prjSession.getRolesNames() ;
322             j = rolesNames.iterator();
323             while (j.hasNext())
324             {
325                 String JavaDoc roleName = (String JavaDoc)j.next();
326                 System.out.println("Role : " + roleName ); }
327       } catch(Exception JavaDoc e) {System.out.println(" --> " + e);} //Maybe something is wrong
328

329       System.out.println(" Getting role names for john user in this project ");
330       try {
331             Collection johnRolesNames = prjSession.getRolesNames() ;
332             j = johnRolesNames.iterator();
333             while (j.hasNext())
334             {
335                 String JavaDoc johnRoleName = (String JavaDoc)j.next();
336                 System.out.println("John role : " + johnRoleName );
337         }
338       } catch(Exception JavaDoc e) {System.out.println(" --> " + e);} //Maybe something is wrong
339

340       System.out.println(" Setting role names for an activites of this project ");
341       try {
342              Collection prjNodesNames = prjSession.getNodesNames() ;
343          j = prjNodesNames.iterator() ;
344          while (j.hasNext())
345            {
346                 String JavaDoc prjNodeName = (String JavaDoc)j.next();
347         System.out.println("Node --> " + prjNodeName ) ;
348         System.out.println(" --> Getting the actuel role names for Activities ");
349         try {
350                 System.out.println(" Activity role : " + prjSession.getNodeRoleName(prjNodeName));
351         } catch(Exception JavaDoc e) {System.out.println(" --> " + e);} //Maybe something is wrong
352

353                 System.out.println(" --> Setting activities new roles " );
354         try {
355             prjSession.setNodeRole(prjNodeName,"admin") ;
356             // prjSession.setNodeRole("Activity 2","Customer") ;
357
} catch(Exception JavaDoc e) {System.out.println(" --> " + e);} //Maybe something is wrong
358

359         System.out.println(" --> Getting the new role names for Activities ");
360         try {
361                 System.out.println(" Activity role : " + prjSession.getNodeRoleName(prjNodeName));
362         } catch(Exception JavaDoc e) {System.out.println(" --> " + e);} //Maybe something is wrong
363

364           }
365        } catch(Exception JavaDoc e) {System.out.println(" --> " + e);} //Maybe something is wrong
366

367
368
369     } //End Main
370

371 } // End SampleProjectApi Class
372

373
Popular Tags