KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > guiapp > xui > XuiSession


1 /*
2  * $Id: XuiSession.java 6011 2005-10-24 21:57:50Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.guiapp.xui;
26
27 import java.util.HashMap JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import org.ofbiz.base.util.Debug;
31 import org.ofbiz.base.util.GeneralException;
32 import org.ofbiz.base.util.UtilMisc;
33 import org.ofbiz.base.util.UtilValidate;
34 import org.ofbiz.webapp.control.LoginWorker;
35 import org.ofbiz.entity.GenericDelegator;
36 import org.ofbiz.entity.GenericEntityException;
37 import org.ofbiz.entity.GenericValue;
38 import org.ofbiz.service.GenericServiceException;
39 import org.ofbiz.service.LocalDispatcher;
40 import org.ofbiz.service.ServiceUtil;
41
42 /**
43  *
44  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
45  * @version $Rev: 6011 $
46  * @since 3.1
47  */

48 public class XuiSession {
49
50     public static final String JavaDoc module = XuiSession.class.getName();
51
52     protected GenericDelegator delegator = null;
53     protected LocalDispatcher dispatcher = null;
54     protected GenericValue userLogin = null;
55     protected XuiContainer container = null;
56     protected Map JavaDoc attributes = new HashMap JavaDoc();
57     protected String JavaDoc id = null;
58
59
60     public XuiSession(String JavaDoc id, GenericDelegator delegator, LocalDispatcher dispatcher, XuiContainer container) {
61         this.id = id;
62         this.delegator = delegator;
63         this.dispatcher = dispatcher;
64         this.container = container;
65         Debug.logInfo("Created XuiSession [" + id + "]", module);
66     }
67
68     public XuiContainer getContainer() {
69         return this.container;
70     }
71     
72     public GenericDelegator getDelegator() {
73         return this.delegator;
74     }
75
76     public LocalDispatcher getDispatcher() {
77         return this.dispatcher;
78     }
79
80     public GenericValue getUserLogin() {
81         return this.userLogin;
82     }
83
84     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
85         this.attributes.put(name, value);
86     }
87
88     public Object JavaDoc getAttribute(String JavaDoc name) {
89         return this.attributes.get(name);
90     }
91
92     public String JavaDoc getId() {
93         return this.id;
94     }
95
96     public String JavaDoc getUserId() {
97         if (this.userLogin == null) {
98             return null;
99         } else {
100             return this.userLogin.getString("userLoginId");
101         }
102     }
103
104     public void logout() {
105         if (this.userLogin != null) {
106             LoginWorker.setLoggedOut(this.userLogin.getString("userLoginId"), this.getDelegator());
107             this.userLogin = null;
108         }
109     }
110
111     public void login(String JavaDoc username, String JavaDoc password) throws UserLoginFailure {
112         // if already logged in; verify for lock
113
if (this.userLogin != null) {
114             if (!userLogin.getString("userLoginId").equals(username)) {
115                 throw new UserLoginFailure("Username does not match already logged in user!");
116             }
117         }
118         this.userLogin = this.checkLogin(username, password);
119     }
120
121     public GenericValue checkLogin(String JavaDoc username, String JavaDoc password) throws UserLoginFailure {
122         // check the required parameters and objects
123
if (dispatcher == null) {
124             throw new UserLoginFailure("Unable to log in; XUI not configured propertly");
125         }
126         if (UtilValidate.isEmpty(username)) {
127             throw new UserLoginFailure("Username is missing");
128         }
129         if (UtilValidate.isEmpty(password)) {
130             throw new UserLoginFailure("Password is missing");
131         }
132
133         // call the login service
134
Map JavaDoc result = null;
135         try {
136             result = dispatcher.runSync("userLogin", UtilMisc.toMap("login.username", username, "login.password", password));
137         } catch (GenericServiceException e) {
138             Debug.logError(e, module);
139             throw new UserLoginFailure(e);
140         } catch (Throwable JavaDoc t) {
141             Debug.logError(t, "Thowable caught!", module);
142         }
143
144         // check for errors
145
if (ServiceUtil.isError(result)) {
146             throw new UserLoginFailure(ServiceUtil.getErrorMessage(result));
147         } else {
148             GenericValue ul = (GenericValue) result.get("userLogin");
149             if (ul == null) {
150                 throw new UserLoginFailure("UserLogin return was not valid (null)");
151             }
152             return ul;
153         }
154     }
155
156     public boolean hasRole(GenericValue userLogin, String JavaDoc roleTypeId) {
157         if (userLogin == null || roleTypeId == null) {
158             return false;
159         }
160         String JavaDoc partyId = userLogin.getString("partyId");
161         GenericValue partyRole = null;
162         try {
163             partyRole = delegator.findByPrimaryKey("PartyRole", UtilMisc.toMap("partyId", partyId, "roleTypeId", roleTypeId));
164         } catch (GenericEntityException e) {
165             Debug.logError(e, module);
166             return false;
167         }
168
169         if (partyRole == null) {
170             return false;
171         }
172
173         return true;
174     }
175
176     public class UserLoginFailure extends GeneralException {
177         public UserLoginFailure() {
178             super();
179         }
180
181         public UserLoginFailure(String JavaDoc str) {
182             super(str);
183         }
184
185         public UserLoginFailure(String JavaDoc str, Throwable JavaDoc nested) {
186             super(str, nested);
187         }
188
189         public UserLoginFailure(Throwable JavaDoc nested) {
190             super(nested);
191         }
192     }
193 }
194
Popular Tags