1 package com.dotmarketing.portlets.usermanager.action; 2 3 import java.util.List ; 4 5 import javax.portlet.ActionRequest; 6 import javax.portlet.ActionResponse; 7 import javax.portlet.PortletConfig; 8 import javax.servlet.http.HttpServletRequest ; 9 10 import org.apache.commons.beanutils.BeanUtils; 11 import org.apache.struts.action.ActionForm; 12 import org.apache.struts.action.ActionMapping; 13 import com.dotmarketing.beans.UserProxy; 14 import com.dotmarketing.cms.factories.PublicAddressFactory; 15 import com.dotmarketing.cms.factories.PublicEncryptionFactory; 16 import com.dotmarketing.cms.factories.PublicUserFactory; 17 import com.dotmarketing.db.DotHibernate; 18 import com.dotmarketing.factories.InodeFactory; 19 import com.dotmarketing.factories.UserProxyFactory; 20 import com.dotmarketing.portal.struts.DotPortletAction; 21 import com.dotmarketing.portlets.categories.factories.CategoryFactory; 22 import com.dotmarketing.portlets.categories.model.Category; 23 import com.dotmarketing.portlets.organization.factories.OrganizationFactory; 24 import com.dotmarketing.portlets.organization.model.Organization; 25 import com.dotmarketing.portlets.usermanager.struts.UserManagerForm; 27 import com.dotmarketing.portlets.usermanager.struts.UserManagerListSearchForm; 28 import com.dotmarketing.util.Config; 29 import com.dotmarketing.util.Logger; 30 import com.dotmarketing.util.UtilMethods; 31 import com.dotmarketing.util.Validator; 32 import com.dotmarketing.util.WebKeys; 33 import com.liferay.portal.ejb.UserLocalManagerUtil; 34 import com.liferay.portal.model.Address; 35 import com.liferay.portal.model.User; 36 import com.liferay.portlet.ActionRequestImpl; 37 import com.liferay.portlet.RenderRequestImpl; 38 import com.liferay.util.servlet.SessionMessages; 39 40 43 public class EditUserManagerAction extends DotPortletAction{ 44 45 public static boolean debug = false; 46 47 public void processAction(ActionMapping mapping, ActionForm form, PortletConfig config, ActionRequest req, 48 ActionResponse res) throws Exception { 49 50 DotHibernate.startTransaction(); 51 String cmd = req.getParameter(com.liferay.portal.util.Constants.CMD); 52 String referer = req.getParameter("referer"); 53 54 _editUserManager(form, req, res, cmd); 56 req.setAttribute(WebKeys.USERMANAGER_EDIT_FORM, form); 57 58 if (com.liferay.portal.util.Constants.SAVE.equals(cmd)) { 60 Logger.debug(this, "Saving UserInfo"); 61 62 if (!Validator.validate(req, form, mapping)) { 64 Logger.debug(this, "Form Validation Failed"); 65 req.setAttribute(WebKeys.USERMANAGER_EDIT_FORM, form); 66 BeanUtils.copyProperties(form, req.getAttribute(WebKeys.USERMANAGER_EDIT_FORM)); 67 setForward(req, "portlet.ext.usermanager.edit_usermanager"); 68 return; 69 } else { 70 try { 71 _save(form, req, res); 72 } catch (Exception e) { 73 _handleException(e, req); 74 } 75 } 76 } 77 78 if (com.liferay.portal.util.Constants.DELETE.equals(cmd)) { 80 try { 81 _delete(form, req, res); 82 } catch (Exception e) { 83 _handleException(e, req); 84 } 85 _sendToReferral(req,res,referer); 86 return; 87 88 } 89 req.setAttribute(WebKeys.USERMANAGER_EDIT_FORM, form); 90 BeanUtils.copyProperties(form, req.getAttribute(WebKeys.USERMANAGER_EDIT_FORM)); 91 setForward(req, "portlet.ext.usermanager.edit_usermanager"); 92 93 DotHibernate.commitTransaction(); 94 } 95 96 97 98 private void _editUserManager(ActionForm form, ActionRequest req, ActionResponse res, String cmd) 100 throws Exception { 101 102 UserManagerForm userForm = (UserManagerForm) form; 103 104 String companyId = com.dotmarketing.cms.factories.PublicCompanyFactory.getDefaultCompany().getCompanyId(); 105 String userId = req.getParameter("userID"); 106 User user = UserLocalManagerUtil.getUserById(companyId, userId); 107 108 if (user.getUserId() != null && !com.liferay.portal.util.Constants.SAVE.equals(cmd)) { 109 110 UserProxy userProxy = UserProxyFactory.getUserProxy(user); 112 113 Address address = retrieveAddress(user.getUserId()); 114 115 BeanUtils.copyProperties(form, user); 117 118 if(!UtilMethods.isSet(userProxy.getPrefix())){ 119 userProxy.setPrefix("other"); 120 } 121 122 BeanUtils.copyProperties(form, userProxy); 123 124 if(!UtilMethods.isSet(address.getDescription())){ 125 address.setDescription("other"); 126 } 127 128 BeanUtils.copyProperties(form, address); 129 130 userForm.setUserID(user.getUserId()); 132 133 134 List <Category> oldcategories = InodeFactory.getChildrenClass(userProxy,Category.class); 135 136 List <Category> catList = InodeFactory.getChildrenClass(userProxy,Category.class); 138 if(catList.size() > 0){ 139 String [] categories = new String [catList.size()]; 140 for(int i = 0 ; i < catList.size() ;++i){ 141 Category cat = catList.get(i); 142 categories[i] = String.valueOf(cat.getInode()); 143 userProxy.addChild(cat); 144 } 145 } 146 BeanUtils.copyProperties(form, address); 147 148 BeanUtils.copyProperties(form, address); 149 150 } 151 152 } 153 154 private Address retrieveAddress(String userID) throws Exception { 155 String companyId = com.dotmarketing.cms.factories.PublicCompanyFactory.getDefaultCompany().getCompanyId(); 156 157 Address address = null; 158 if (UtilMethods.isSet(userID)) { 159 List addresses = PublicAddressFactory.getAddressesByUserId(userID); 160 if (addresses.size() == 0) { 161 address = PublicAddressFactory.getInstance(); 162 address.setCompanyId(companyId); 163 address.setUserId(userID); 164 } else { 165 address = (Address) addresses.get(0); 166 } 167 } else { 168 address = PublicAddressFactory.getInstance(); 169 address.setCompanyId(companyId); 170 address.setCreateDate(new java.util.Date ()); 171 address.setNew(true); 172 address.setUserId(userID); 173 } 174 return address; 175 } 176 177 private void _delete(ActionForm form, ActionRequest req, ActionResponse res) 179 throws Exception { 180 181 UserManagerForm userForm = (UserManagerForm) form; 182 String userId = userForm.getUserID(); 183 184 String companyId = com.dotmarketing.cms.factories.PublicCompanyFactory.getDefaultCompany().getCompanyId(); 186 User user = UserLocalManagerUtil.getUserById(companyId, userId); 187 UserProxy userProxy = UserProxyFactory.getUserProxy(user); 188 189 InodeFactory.deleteInode(userProxy); 191 UserLocalManagerUtil.deleteUser(userId); 193 } 194 195 private void _save(ActionForm form, ActionRequest req, ActionResponse res) 197 throws Exception { 198 199 UserManagerForm userForm = (UserManagerForm) form; 200 String userId = userForm.getUserID(); 201 202 String companyId = com.dotmarketing.cms.factories.PublicCompanyFactory.getDefaultCompany().getCompanyId(); 204 User user = UserLocalManagerUtil.getUserById(companyId, userId); 205 UserProxy userProxy = UserProxyFactory.getUserProxy(user); 206 207 user.setFirstName(userForm.getFirstName()); 208 user.setLastName(userForm.getLastName()); 209 210 if (!userForm.getPrefix().equals("other")) 211 userProxy.setPrefix(userForm.getPrefix()); 212 else 213 userProxy.setPrefix(userForm.getOtherPrefix()); 214 215 userProxy.setSuffix(userForm.getSuffix()); 216 userProxy.setTitle(userForm.getTitle()); 217 218 if (!user.getEmailAddress().equals(userForm.getEmailAddress())) 220 user.setEmailAddress(userForm.getEmailAddress()); 221 if (!userForm.getNewPassword().equals("")) { 222 user.setPassword(PublicEncryptionFactory.digestString(userForm.getNewPassword())); 223 user.setPasswordEncrypted(true); 224 } 225 226 PublicUserFactory.save(user); 227 228 if(userProxy.getInode() == 0 ){ 229 userProxy.setUserId(user.getUserId()); 230 InodeFactory.saveInode(userProxy); 231 232 } else { 233 InodeFactory.saveInode(userProxy); 234 235 } 236 237 Address address = retrieveAddress(user.getUserId()); 239 240 address.setUserName(user.getFullName()); 241 address.setClassName(user.getClass().getName()); 242 address.setClassPK(user.getUserId()); 243 address.setDescription(userForm.getDescription()); 244 address.setStreet1(userForm.getStreet1()); 245 address.setStreet2(userForm.getStreet2()); 246 address.setCity(userForm.getCity()); 247 address.setState(userForm.getState()); 248 address.setZip(userForm.getZip()); 249 address.setPhone(userForm.getPhone()); 250 address.setFax(userForm.getFax()); 251 252 PublicAddressFactory.save(address); 253 254 if(userForm.getCategory() != null){ 256 String [] categories = userForm.getCategory(); 257 for(int i = 0 ; i < categories.length ;++i){ 258 Category cat = CategoryFactory.getCategory(categories[i]); 259 userProxy.addChild(cat); 260 } 261 } 262 263 DotHibernate.flush(); 264 265 SessionMessages.add(req, "message", "message.usermanager.saved"); 266 267 _editUserManager(form, req, res, ""); 268 } 269 270 } 271 | Popular Tags |