KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > dotmarketing > cms > myaccount > action > MyAccountAction


1 package com.dotmarketing.cms.myaccount.action;
2
3 import javax.servlet.http.HttpServletRequest JavaDoc;
4 import javax.servlet.http.HttpServletResponse JavaDoc;
5
6 import org.apache.commons.beanutils.BeanUtils;
7 import org.apache.struts.action.ActionErrors;
8 import org.apache.struts.action.ActionForm;
9 import org.apache.struts.action.ActionForward;
10 import org.apache.struts.action.ActionMapping;
11 import org.apache.struts.action.ActionMessage;
12 import org.apache.struts.action.ActionMessages;
13 import org.apache.struts.actions.DispatchAction;
14
15 import com.dotmarketing.beans.UserProxy;
16 import com.dotmarketing.cms.factories.PublicEncryptionFactory;
17 import com.dotmarketing.cms.factories.PublicUserFactory;
18 import com.dotmarketing.cms.myaccount.struts.MyAccountForm;
19 import com.dotmarketing.factories.InodeFactory;
20 import com.dotmarketing.factories.UserProxyFactory;
21 import com.dotmarketing.util.Mailer;
22 import com.dotmarketing.util.Validator;
23 import com.dotmarketing.util.WebKeys;
24 import com.liferay.portal.model.User;
25
26
27 public class MyAccountAction extends DispatchAction {
28
29     public ActionForward unspecified(ActionMapping mapping, ActionForm lf,
30             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
31             throws Exception JavaDoc {
32
33         if (request.getSession().getAttribute(WebKeys.CMS_USER) == null) {
34              return new ActionForward("/dotCMS/login");
35         }
36         
37         // HttpSession session = request.getSession();
38
MyAccountForm form = (MyAccountForm) lf;
39
40         //Getting the user from the session
41
User user = (User) request.getSession().getAttribute(WebKeys.CMS_USER);
42         String JavaDoc userId = user.getUserId();
43
44         loadUserInfoInRequest(form, userId, request);
45
46         return mapping.findForward("myAccountPage");
47     }
48
49     public ActionForward saveUserInfo(ActionMapping mapping, ActionForm lf,
50             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
51             throws Exception JavaDoc {
52
53         if (request.getSession().getAttribute(WebKeys.CMS_USER) == null) {
54              return new ActionForward("/dotCMS/login");
55         }
56         
57         MyAccountForm form = (MyAccountForm) lf;
58         String JavaDoc userEmail = form.getUserName();
59
60         if (!Validator.validate(request, lf, mapping))
61             return mapping.findForward("myAccountPage");
62
63         // Saving Personal Information
64

65         User user = PublicUserFactory.getUserByEmail(userEmail);
66
67         UserProxy userProxy = UserProxyFactory.getUserProxy(user);
68         
69         user.setFirstName(form.getFirstName());
70         user.setLastName(form.getLastName());
71
72         userProxy.setOrganization(form.getOrganization());
73         userProxy.setWebsite(form.getWebsite());
74         userProxy.setMailSubscription(form.isMailSubscription());
75         
76         // User Name and password
77
if (form.isPasswordChanged()) {
78             if (!user.getPassword().equals(PublicEncryptionFactory.digestString(form.getOldpassword())))
79             {
80                 ActionErrors errors = new ActionErrors ();
81                 errors.add("password", new ActionMessage ("current.password.incorrect"));
82                 saveMessages(request, errors);
83                 return mapping.findForward("myAccountPage");
84             }
85             user.setPassword(PublicEncryptionFactory.digestString(form.getPassword1()));
86             user.setPasswordEncrypted(true);
87         }
88
89         if (form.isMailSubscription()) {
90             subscribeDotCMSMailingList (user);
91         } else {
92             unSubsribeDotCMSMailingList (user);
93         }
94         
95         PublicUserFactory.save(user);
96         InodeFactory.saveInode(userProxy);
97         
98         loadUserInfoInRequest(form, user.getUserId(), request);
99         
100         ActionErrors ae = new ActionErrors();
101         ae.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("message.createaccount.success"));
102         saveMessages(request, ae);
103         
104         return mapping.findForward("myAccountPage");
105     }
106
107     
108
109     private void loadUserInfoInRequest(MyAccountForm form, String JavaDoc userId,
110             HttpServletRequest JavaDoc request) throws Exception JavaDoc {
111
112         
113         User user = PublicUserFactory.getUserByUserId(userId);
114
115         // Retriving info from db
116
UserProxy userProxy = UserProxyFactory.getUserProxy(user);
117         
118
119         if (userProxy.getInode() == 0) {
120             userProxy.setUserId(user.getUserId());
121             InodeFactory.saveInode(userProxy);
122         }
123
124         // Copy the attributes
125
BeanUtils.copyProperties(form, user);
126         
127         BeanUtils.copyProperties(form, userProxy);
128         
129
130         // Extra user info
131
form.setUserName(user.getEmailAddress());
132
133     }
134
135     public static boolean subscribeDotCMSMailingList(User user) {
136         String JavaDoc to = "dotcms-subscribe@yahoogroups.com";
137         String JavaDoc from = user.getEmailAddress();
138         String JavaDoc subject = "Subscribe to dotCMS mailing list";
139         return sendEmailForMailingList(to, from, subject);
140     }
141
142     public static boolean unSubsribeDotCMSMailingList(User user) {
143         String JavaDoc to = "dotcms-unsubscribe@yahoogroups.com";
144         String JavaDoc from = user.getEmailAddress();
145         String JavaDoc subject = "UnSubscribe to dotCMS mailing list";
146         return sendEmailForMailingList(to, from, subject);
147     }
148
149     protected static boolean sendEmailForMailingList(String JavaDoc to, String JavaDoc from, String JavaDoc subject) {
150         Mailer m = new Mailer();
151         m.setToEmail(to);
152         m.setFromEmail(from);
153         m.setSubject(subject);
154         return m.sendMessage();
155     }
156     
157     public ActionForward back(ActionMapping mapping, ActionForm lf,
158             HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
159             throws Exception JavaDoc {
160
161         if (request.getSession().getAttribute(WebKeys.CMS_USER) == null) {
162              return new ActionForward("/dotCMS/login");
163         }
164         
165         // HttpSession session = request.getSession();
166
MyAccountForm form = (MyAccountForm) lf;
167
168         //Getting the user from the session
169
User user = (User) request.getSession().getAttribute(WebKeys.CMS_USER);
170         String JavaDoc userId = user.getUserId();
171
172         loadUserInfoInRequest(form, userId, request);
173
174         return mapping.findForward("myAccountPage");
175     }
176 }
177
178
Popular Tags