KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > UserRegistrationApple


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.frontend;
17
18 import org.outerj.daisy.frontend.util.AbstractDaisyApple;
19 import org.outerj.daisy.frontend.util.FormHelper;
20 import org.outerj.daisy.frontend.components.userregistrar.UserRegistrar;
21 import org.outerj.daisy.repository.Repository;
22 import org.apache.avalon.framework.service.Serviceable;
23 import org.apache.avalon.framework.service.ServiceManager;
24 import org.apache.avalon.framework.service.ServiceException;
25 import org.apache.avalon.framework.activity.Disposable;
26 import org.apache.cocoon.forms.formmodel.Form;
27 import org.apache.cocoon.forms.FormContext;
28 import org.apache.cocoon.components.flow.apples.AppleRequest;
29 import org.apache.cocoon.components.flow.apples.AppleResponse;
30 import org.apache.cocoon.environment.Request;
31
32 import java.util.Locale JavaDoc;
33 import java.util.Map JavaDoc;
34 import java.util.HashMap JavaDoc;
35
36 public class UserRegistrationApple extends AbstractDaisyApple implements Serviceable, Disposable {
37     private ServiceManager serviceManager;
38     private boolean init = false;
39     private Locale JavaDoc locale;
40     private Form form;
41     private Repository repository;
42     private Map JavaDoc viewData;
43     private String JavaDoc returnTo;
44     private UserRegistrar userRegistrar;
45
46     public void service(ServiceManager serviceManager) throws ServiceException {
47         this.serviceManager = serviceManager;
48         userRegistrar = (UserRegistrar)serviceManager.lookup(UserRegistrar.ROLE);
49     }
50
51     public void dispose() {
52         if (userRegistrar != null)
53             serviceManager.release(userRegistrar);
54     }
55
56     protected void processInternal(AppleRequest appleRequest, AppleResponse appleResponse) throws Exception JavaDoc {
57         if (!init) {
58             Request request = appleRequest.getCocoonRequest();
59
60             returnTo = request.getParameter("returnTo");
61             if (returnTo == null || returnTo.equals(""))
62                 returnTo = getMountPoint() + "/";
63             repository = WikiHelper.getRepository(appleRequest.getCocoonRequest(), serviceManager);
64             locale = WikiHelper.getLocale(appleRequest.getCocoonRequest());
65             form = FormHelper.createForm(serviceManager, "resources/form/userregistration_definition.xml");
66
67             String JavaDoc path = getMountPoint() + "/registration/" + getContinuationId();
68
69             viewData = new HashMap JavaDoc();
70             viewData.put("pageContext", new PageContext(getMountPoint(), repository, getLayoutType(), getSkin(), SkinConfHelper.getGlobalSkinConf(serviceManager), getContext()));
71             viewData.put("submitPath", path);
72             viewData.put("locale", locale);
73             viewData.put("returnTo", returnTo);
74             viewData.put("CocoonFormsInstance", form);
75
76             init = true;
77
78             appleResponse.redirectTo(path);
79         } else {
80             String JavaDoc methodName = appleRequest.getCocoonRequest().getMethod();
81             if (methodName.equals("GET")) {
82                 // display the form
83
appleResponse.sendPage("Form-userregistration-Pipe", viewData);
84             } else if (methodName.equals("POST")) {
85                 // handle a form submit
86
boolean endProcessing = form.process(new FormContext(appleRequest.getCocoonRequest(), locale));
87
88                 if (!endProcessing) {
89                     appleResponse.sendPage("Form-userregistration-Pipe", viewData);
90                 } else {
91                     String JavaDoc login = (String JavaDoc)form.getChild("login").getValue();
92                     String JavaDoc email = (String JavaDoc)form.getChild("email").getValue();
93                     String JavaDoc firstName = (String JavaDoc)form.getChild("firstName").getValue();
94                     String JavaDoc lastName = (String JavaDoc)form.getChild("lastName").getValue();
95                     String JavaDoc password = (String JavaDoc)form.getChild("password").getValue();
96
97                     Request request = appleRequest.getCocoonRequest();
98                     String JavaDoc server = RequestUtil.getServer(request);
99
100                     userRegistrar.registerNewUser(login, password, email, firstName, lastName, server, getMountPoint(), locale);
101
102                     Map JavaDoc viewData = new HashMap JavaDoc();
103                     viewData.put("pageContext", new PageContext(getMountPoint(), repository, getLayoutType(), getSkin(), SkinConfHelper.getGlobalSkinConf(serviceManager), getContext()));
104                     viewData.put("locale", locale);
105                     viewData.put("returnTo", returnTo);
106                     viewData.put("email", email);
107
108                     appleResponse.sendPage("Message-registrationsuccess-Pipe", viewData);
109                 }
110             } else {
111                 throw new Exception JavaDoc("Unspported HTTP method: " + methodName);
112             }
113         }
114     }
115 }
116
Popular Tags