1 package com.dotmarketing.portlets.mailinglists.action; 2 3 import java.io.BufferedReader ; 4 import java.io.File ; 5 import java.io.FileReader ; 6 import java.io.IOException ; 7 import java.io.LineNumberReader ; 8 import java.util.List ; 9 import java.util.StringTokenizer ; 10 11 import javax.portlet.ActionRequest; 12 import javax.portlet.ActionResponse; 13 import javax.portlet.PortletConfig; 14 import javax.servlet.ServletOutputStream ; 15 import javax.servlet.http.HttpServletResponse ; 16 import javax.servlet.jsp.PageContext ; 17 18 import org.apache.commons.beanutils.BeanUtils; 19 import org.apache.struts.action.ActionForm; 20 import org.apache.struts.action.ActionMapping; 21 22 import com.dotmarketing.beans.UserProxy; 23 import com.dotmarketing.cms.factories.PublicAddressFactory; 24 import com.dotmarketing.cms.factories.PublicCompanyFactory; 25 import com.dotmarketing.cms.factories.PublicUserFactory; 26 import com.dotmarketing.cms.login.factories.LoginFactory; 27 import com.dotmarketing.db.DotHibernate; 28 import com.dotmarketing.factories.InodeFactory; 29 import com.dotmarketing.factories.UserProxyFactory; 30 import com.dotmarketing.portlets.mailinglists.factories.MailingListFactory; 31 import com.dotmarketing.portlets.mailinglists.model.MailingList; 32 import com.dotmarketing.portlets.mailinglists.struts.MailingListForm; 33 import com.dotmarketing.util.Logger; 34 import com.dotmarketing.util.UtilMethods; 35 import com.dotmarketing.util.Validator; 36 import com.dotmarketing.util.WebKeys; 37 import com.liferay.portal.ejb.UserLocalManagerUtil; 38 import com.liferay.portal.language.LanguageUtil; 39 import com.liferay.portal.model.Address; 40 import com.liferay.portal.model.Company; 41 import com.liferay.portal.model.User; 42 import com.liferay.portal.struts.ActionException; 43 import com.liferay.portal.struts.PortletAction; 44 import com.liferay.portal.util.Constants; 45 import com.liferay.portal.util.PortalUtil; 46 import com.liferay.portlet.ActionResponseImpl; 47 import com.liferay.util.FileUtil; 48 import com.liferay.util.ParamUtil; 49 import com.liferay.util.servlet.SessionMessages; 50 import com.liferay.util.servlet.UploadPortletRequest; 51 54 55 public class EditMailingListAction extends PortletAction { 56 57 public void processAction( 58 ActionMapping mapping, ActionForm form, PortletConfig config, 59 ActionRequest req, ActionResponse res) 60 throws Exception { 61 62 String cmd = req.getParameter(Constants.CMD); 63 64 User user = null; 66 try { 67 user = com.liferay.portal.util.PortalUtil.getUser(req); 68 } 69 catch (Exception e) { 70 req.setAttribute(PageContext.EXCEPTION, e); 71 setForward(req, Constants.COMMON_ERROR); 72 } 73 74 78 try { 79 _retrieveMailingList(req, res, config, form); 80 } 81 catch (Exception ae) { 82 Logger.error(this, "Error retrieving the list", ae); 83 req.setAttribute(PageContext.EXCEPTION, ae); 84 setForward(req, Constants.COMMON_ERROR); 85 } 86 87 91 MailingList ml = (MailingList) req.getAttribute(WebKeys.MAILING_LIST_EDIT); 92 if (ml.getInode() > 0 93 && !ml.getUserId().equals(user.getUserId()) 94 && ml.getInode() != MailingListFactory.getUnsubscribersMailingList().getInode()) { 95 96 SessionMessages.add(req, "message", "message.mailinglist.cannotEdit"); 97 setForward(req, "portlet.ext.mailinglists.view_mailinglists"); 98 } 99 100 104 if ((cmd != null) && cmd.equals(Constants.ADD)) { 105 try { 106 107 if (Validator.validate(req,form,mapping)) { 108 _saveMailingList(req, res, config, form, user); 109 } 110 } 111 catch (Exception ae) { 112 Logger.error(this, "Error Saving Maling List", ae); 113 req.setAttribute(PageContext.EXCEPTION, ae); 114 setForward(req, Constants.COMMON_ERROR); 115 } 116 } 117 118 122 if ((cmd != null) && cmd.equals(Constants.UPDATE)) { 123 try { 124 125 if (Validator.validate(req,form,mapping)) { 126 _saveSubscriber(req, res, config, form, user); 127 } 128 } 129 catch (Exception ae) { 130 Logger.error(this, "Error Saving Subscribers", ae); 131 req.setAttribute(PageContext.EXCEPTION, ae); 132 setForward(req, Constants.COMMON_ERROR); 133 } 134 } 135 136 140 else if ((cmd != null) && cmd.equals(com.dotmarketing.util.Constants.DELETE_LIST)) { 141 try { 142 _deleteMailingList(req, res, config, form,user); 143 } 144 catch (Exception ae) { 145 Logger.error(this, ae.toString(), ae); 146 req.setAttribute(PageContext.EXCEPTION, ae); 147 setForward(req, Constants.COMMON_ERROR); 148 } 149 Logger.debug(this, "Returning to view mailing lists page"); 150 res.sendRedirect(ParamUtil.getString(req, "redirect")); 151 } 152 153 157 else if ((cmd != null) && cmd.equals("deleteSubs")) { 158 BeanUtils.copyProperties(form, req.getAttribute(WebKeys.MAILING_LIST_EDIT)); 159 try { 160 _deleteSubscribers(req, res, config, form); 161 } 162 catch (ActionException ae) { 163 Logger.error(this, ae.toString(), ae); 164 } 165 cmd = Constants.EDIT; 166 } 167 168 169 173 if ((cmd != null) && cmd.equals(Constants.EDIT)) { 174 BeanUtils.copyProperties(form, req.getAttribute(WebKeys.MAILING_LIST_EDIT)); 175 } 176 177 181 if ((cmd != null) && cmd.equals("export")) { 182 _exportSubscribers (req, res, config, form); 183 return; 184 } 185 186 190 setForward(req, "portlet.ext.mailinglists.edit_mailinglist"); 191 } 192 193 194 201 private void _exportSubscribers(ActionRequest req, ActionResponse res, PortletConfig config, ActionForm form) throws Exception { 202 203 ActionResponseImpl resImpl = (ActionResponseImpl)res; 204 HttpServletResponse httpRes = resImpl.getHttpServletResponse(); 205 206 MailingList ml = (MailingList) req.getAttribute(com.dotmarketing.util.WebKeys.MAILING_LIST_EDIT); 207 List sl = InodeFactory.getChildrenClass(ml, UserProxy.class); 208 209 httpRes.setContentType("application/octet-stream"); 210 httpRes.setHeader("Content-Disposition", "attachment; filename=\"mailingList" + System.currentTimeMillis() +".csv\""); 211 212 ServletOutputStream out = httpRes.getOutputStream(); 213 214 out.print("First Name, Middle Name, Last Name, Email Address, Street1, Street2, City, State, Zip, Country, Phone,"); 215 216 217 try { 218 Company comp = PublicCompanyFactory.getDefaultCompany(); 219 String var1 = LanguageUtil.get(comp.getCompanyId(), comp.getLocale(), "mailing.list.var1"); 220 String var2 = LanguageUtil.get(comp.getCompanyId(), comp.getLocale(), "mailing.list.var2"); 221 String var3 = LanguageUtil.get(comp.getCompanyId(), comp.getLocale(), "mailing.list.var3"); 222 String var4 = LanguageUtil.get(comp.getCompanyId(), comp.getLocale(), "mailing.list.var4"); 223 String var5 = LanguageUtil.get(comp.getCompanyId(), comp.getLocale(), "mailing.list.var5"); 224 String var6 = LanguageUtil.get(comp.getCompanyId(), comp.getLocale(), "mailing.list.var6"); 225 String var7 = LanguageUtil.get(comp.getCompanyId(), comp.getLocale(), "mailing.list.var7"); 226 String var8 = LanguageUtil.get(comp.getCompanyId(), comp.getLocale(), "mailing.list.var8"); 227 String var9 = LanguageUtil.get(comp.getCompanyId(), comp.getLocale(), "mailing.list.var9"); 228 String var10 = LanguageUtil.get(comp.getCompanyId(), comp.getLocale(), "mailing.list.var10"); 229 String var11 = LanguageUtil.get(comp.getCompanyId(), comp.getLocale(), "mailing.list.var11"); 230 String var12 = LanguageUtil.get(comp.getCompanyId(), comp.getLocale(), "mailing.list.var12"); 231 String var13 = LanguageUtil.get(comp.getCompanyId(), comp.getLocale(), "mailing.list.var13"); 232 String var14 = LanguageUtil.get(comp.getCompanyId(), comp.getLocale(), "mailing.list.var14"); 233 String var15 = LanguageUtil.get(comp.getCompanyId(), comp.getLocale(), "mailing.list.var15"); 234 String var16 = LanguageUtil.get(comp.getCompanyId(), comp.getLocale(), "mailing.list.var16"); 235 String var17 = LanguageUtil.get(comp.getCompanyId(), comp.getLocale(), "mailing.list.var17"); 236 String var18 = LanguageUtil.get(comp.getCompanyId(), comp.getLocale(), "mailing.list.var18"); 237 String var19 = LanguageUtil.get(comp.getCompanyId(), comp.getLocale(), "mailing.list.var19"); 238 String var20 = LanguageUtil.get(comp.getCompanyId(), comp.getLocale(), "mailing.list.var20"); 239 String var21 = LanguageUtil.get(comp.getCompanyId(), comp.getLocale(), "mailing.list.var21"); 240 String var22 = LanguageUtil.get(comp.getCompanyId(), comp.getLocale(), "mailing.list.var22"); 241 String var23 = LanguageUtil.get(comp.getCompanyId(), comp.getLocale(), "mailing.list.var23"); 242 String var24 = LanguageUtil.get(comp.getCompanyId(), comp.getLocale(), "mailing.list.var24"); 243 String var25 = LanguageUtil.get(comp.getCompanyId(), comp.getLocale(), "mailing.list.var25"); 244 out.print(var1 + "," + var2 + "," + var3 + "," + var4 + "," + var5 + "," + var6 + "," + var7 + "," + var8 + "," + var9 + "," + var10 + ","); 245 out.print(var11 + "," + var12 + "," + var13 + "," + var14 + "," + var15 + "," + var16 + "," + var17 + "," + var18 + "," + var19 + "," + var20 + ","); 246 out.print(var21 + "," + var22 + "," + var23 + "," + var24 + "," + var25); 247 248 } catch (Exception nsue) { 249 } 250 out.print("\r\n"); 251 252 for (int k=0;k<sl.size();k++) { 253 UserProxy s = (UserProxy) sl.get(k); 254 User member = new User(); 255 try { 256 String companyId = PublicCompanyFactory.getDefaultCompanyId(); 257 member = UserLocalManagerUtil.getUserById(companyId, s.getUserId()); 258 } catch (Exception e) { 259 Logger.error(LoginFactory.class, "deliverCampaigns Failed" + e); 260 e.printStackTrace(System.out); 261 } 262 263 out.print((member.getFirstName() == null ? "," : "\"" + member.getFirstName() + "\",")); 264 out.print((member.getMiddleName() == null ? "," : "\"" + member.getMiddleName() + "\",")); 265 out.print((member.getLastName() == null ? "," : "\"" + member.getLastName() + "\",")); 266 out.print((member.getEmailAddress() == null ? "," : "\"" + member.getEmailAddress() + "\",")); 267 268 List adds = PublicAddressFactory.getAddressesByUserId(s.getUserId()); 269 Address address = new Address(); 270 if (adds != null && adds.size() > 0) { 271 address = (Address) adds.get(0); 272 } 273 274 out.print((address.getStreet1() == null ? "," : "\"" + address.getStreet1() + "\",")); 275 out.print((address.getStreet2() == null ? "," : "\"" + address.getStreet2() + "\",")); 276 out.print((address.getCity() == null ? "," : "\"" + address.getCity() + "\",")); 277 out.print((address.getState() == null ? "," : "\"" + address.getState() + "\",")); 278 out.print((address.getZip() == null ? "," : "\"" + address.getZip() + "\",")); 279 out.print((address.getCountry() == null ? "," : "\"" + address.getCountry() + "\",")); 280 out.print((address.getPhone() == null ? "," : "\"" + address.getPhone() + "\",")); 281 out.print((s.getVar1() == null ? "," : "\"" + s.getVar1() + "\",")); 283 out.print((s.getVar2() == null ? "," : "\"" + s.getVar2() + "\",")); 284 out.print((s.getVar3() == null ? "," : "\"" + s.getVar3() + "\",")); 285 out.print((s.getVar4() == null ? "," : "\"" + s.getVar4() + "\",")); 286 out.print((s.getVar5() == null ? "," : "\"" + s.getVar5() + "\",")); 287 out.print((s.getVar6() == null ? "," : "\"" + s.getVar6() + "\",")); 288 out.print((s.getVar7() == null ? "," : "\"" + s.getVar7() + "\",")); 289 out.print((s.getVar8() == null ? "," : "\"" + s.getVar8() + "\",")); 290 out.print((s.getVar9() == null ? "," : "\"" + s.getVar9() + "\",")); 291 out.print((s.getVar10() == null ? "," : "\"" + s.getVar10() + "\",")); 292 out.print((s.getVar11() == null ? "," : "\"" + s.getVar11() + "\",")); 293 out.print((s.getVar12() == null ? "," : "\"" + s.getVar12() + "\",")); 294 out.print((s.getVar13() == null ? "," : "\"" + s.getVar13() + "\",")); 295 out.print((s.getVar14() == null ? "," : "\"" + s.getVar14() + "\",")); 296 out.print((s.getVar15() == null ? "," : "\"" + s.getVar15() + "\",")); 297 out.print((s.getVar16() == null ? "," : "\"" + s.getVar16() + "\",")); 298 out.print((s.getVar17() == null ? "," : "\"" + s.getVar17() + "\",")); 299 out.print((s.getVar18() == null ? "," : "\"" + s.getVar18() + "\",")); 300 out.print((s.getVar19() == null ? "," : "\"" + s.getVar19() + "\",")); 301 out.print((s.getVar20() == null ? "," : "\"" + s.getVar20() + "\",")); 302 out.print((s.getVar21() == null ? "," : "\"" + s.getVar21() + "\",")); 303 out.print((s.getVar22() == null ? "," : "\"" + s.getVar22() + "\",")); 304 out.print((s.getVar23() == null ? "," : "\"" + s.getVar23() + "\",")); 305 out.print((s.getVar24() == null ? "," : "\"" + s.getVar24() + "\",")); 306 out.print((s.getVar25() == null ? "," : "\"" + s.getVar25() + "\"")); 307 out.print("\r\n"); 308 309 } 310 311 out.flush(); 312 out.close(); 313 DotHibernate.closeSession(); 314 315 } 316 317 318 private void _retrieveMailingList(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form) 319 throws Exception { 320 MailingList ml = (MailingList) InodeFactory.getInode(req.getParameter("inode"),MailingList.class); 321 req.setAttribute(WebKeys.MAILING_LIST_EDIT, ml); 322 } 323 324 private void _saveSubscriber(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 325 throws Exception { 326 BeanUtils.copyProperties(form,req.getAttribute(WebKeys.MAILING_LIST_EDIT)); 328 ((MailingListForm)form).setNewSubscribers(""); 329 ((MailingListForm)form).setFirstName(""); 330 ((MailingListForm)form).setMiddleName(""); 331 ((MailingListForm)form).setLastName(""); 332 ((MailingListForm)form).setEmail(""); 333 ((MailingListForm)form).setStreet1(""); 334 ((MailingListForm)form).setStreet2(""); 335 ((MailingListForm)form).setCity(""); 336 ((MailingListForm)form).setState(""); 337 ((MailingListForm)form).setZip(""); 338 ((MailingListForm)form).setCountry(""); 339 ((MailingListForm)form).setPhone(""); 340 } 341 342 private boolean _validateCSVFile (File f) throws IOException { 343 BufferedReader reader = new BufferedReader (new FileReader (f)); 344 String line = reader.readLine(); 345 for (;line!= null;line = reader.readLine()) { 346 StringTokenizer st = new StringTokenizer (line, ",", true); 347 if (st.countTokens() <= 1) 348 return false; 349 } 350 return true; 351 } 352 353 private void _saveMailingList(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form, User user) 354 throws Exception { 355 356 BeanUtils.copyProperties(req.getAttribute(WebKeys.MAILING_LIST_EDIT),form); 357 358 MailingList ml = (MailingList) req.getAttribute(WebKeys.MAILING_LIST_EDIT); 359 MailingListForm mlForm = (MailingListForm) form; 360 361 362 367 368 if(ml.getInode() == 0 || ml.getUserId().equals(user.getUserId())){ 369 ml.setUserId(user.getUserId()); 370 InodeFactory.saveInode(ml); 371 SessionMessages.add(req,"message", "message.mailinglist.save"); 372 } 373 else{ 374 SessionMessages.add(req, "message", "message.mailinglist.cannotEdit"); 375 } 376 377 UploadPortletRequest uploadReq = PortalUtil.getUploadPortletRequest(req); 378 java.io.File uploadedFile = uploadReq.getFile("newSubscribersFile"); 379 byte[] bytes = FileUtil.getBytes(uploadedFile); 380 381 int subsAdded = 0; 382 if(bytes!=null && bytes.length >0){ 383 384 FileReader freader = new FileReader (uploadedFile); 385 LineNumberReader lnreader = new LineNumberReader (freader); 386 List lines = UtilMethods.specialSplit(lnreader, ",", "\""); 387 388 if (!_validateCSVFile(uploadedFile)) { 389 SessionMessages.add(req, "message", "message.mailinglist.invalidCSVFile"); 390 return; 391 } 392 393 int init = 0; 395 if (mlForm.isIgnoreHeaders()) { 397 init = 1; 398 } 399 for (int k=init; k < lines.size(); k++) { 400 401 String [] lineTok = (String []) lines.get(k); 402 403 String firstName = ""; 404 String middleName = ""; 405 String lastName = ""; 406 String email = ""; 407 String street1 = ""; 408 String street2 = ""; 409 String city = ""; 410 String state = ""; 411 String zip = ""; 412 String country = ""; 413 String phone = ""; 414 String var1 = ""; 416 String var2 = ""; 417 String var3 = ""; 418 String var4 = ""; 419 String var5 = ""; 420 String var6 = ""; 421 String var7 = ""; 422 String var8 = ""; 423 String var9 = ""; 424 String var10 = ""; 425 String var11 = ""; 426 String var12 = ""; 427 String var13 = ""; 428 String var14 = ""; 429 String var15 = ""; 430 String var16 = ""; 431 String var17 = ""; 432 String var18 = ""; 433 String var19 = ""; 434 String var20 = ""; 435 String var21 = ""; 436 String var22 = ""; 437 String var23 = ""; 438 String var24 = ""; 439 String var25 = ""; 440 441 try { 442 int i = 0; 443 firstName = lineTok[i++]; 444 middleName = lineTok[i++]; 445 lastName = lineTok[i++]; 446 email = lineTok[i++]; 447 448 if (UtilMethods.isValidEmail(email)) { 449 450 street1 = lineTok[i++]; 451 street2 = lineTok[i++]; 452 city = lineTok[i++]; 453 state = lineTok[i++]; 454 zip = lineTok[i++]; 455 country = lineTok[i++]; 456 phone = lineTok[i++]; 457 458 var1 = lineTok[i++]; 460 var2 = lineTok[i++]; 461 var3 = lineTok[i++]; 462 var4 = lineTok[i++]; 463 var5 = lineTok[i++]; 464 var6 = lineTok[i++]; 465 var7 = lineTok[i++]; 466 var8 = lineTok[i++]; 467 var9 = lineTok[i++]; 468 var10 = lineTok[i++]; 469 var11 = lineTok[i++]; 470 var12 = lineTok[i++]; 471 var13 = lineTok[i++]; 472 var14 = lineTok[i++]; 473 var15 = lineTok[i++]; 474 var16 = lineTok[i++]; 475 var17 = lineTok[i++]; 476 var18 = lineTok[i++]; 477 var19 = lineTok[i++]; 478 var20 = lineTok[i++]; 479 var20 = lineTok[i++]; 480 var21 = lineTok[i++]; 481 var22 = lineTok[i++]; 482 var23 = lineTok[i++]; 483 var24 = lineTok[i++]; 484 var25 = lineTok[i++]; 485 } 486 } catch (Exception e) { } 487 488 User Subscriber = PublicUserFactory.getUserByEmail(email); 489 UserProxy s = UserProxyFactory.getUserProxy(Subscriber); 490 491 s.setUserId(Subscriber.getUserId()); 492 493 s.setVar1(var1); 495 s.setVar2(var2); 496 s.setVar3(var3); 497 s.setVar4(var4); 498 s.setVar5(var5); 499 s.setVar6(var6); 500 s.setVar7(var7); 501 s.setVar8(var8); 502 s.setVar9(var9); 503 s.setVar10(var10); 504 s.setVar11(var11); 505 s.setVar12(var12); 506 s.setVar13(var13); 507 s.setVar14(var14); 508 s.setVar15(var15); 509 s.setVar16(var16); 510 s.setVar17(var17); 511 s.setVar18(var18); 512 s.setVar19(var19); 513 s.setVar20(var20); 514 s.setVar21(var21); 515 s.setVar22(var22); 516 s.setVar23(var23); 517 s.setVar24(var24); 518 s.setVar25(var25); 519 520 UserProxyFactory.saveUserProxy(s); 521 subsAdded++; 522 ml.addChild(s); 523 } 524 } 525 DotHibernate.closeSession(); 526 527 ml.setSubscriberCount(InodeFactory.getChildrenClass(ml, UserProxy.class).size()); 528 InodeFactory.saveInode(ml); 529 if (subsAdded > 0) 530 SessionMessages.add(req, "message", "message.mailinglist.subscribers.added"); 531 else 532 SessionMessages.add(req, "message", "message.mailinglist.save"); 533 534 BeanUtils.copyProperties(form,req.getAttribute(WebKeys.MAILING_LIST_EDIT)); 536 ((MailingListForm)form).setNewSubscribers(""); 537 } 538 539 private void _deleteMailingList(ActionRequest req, ActionResponse res,PortletConfig config, ActionForm form , User user) 540 throws Exception { 541 542 MailingList ml = (MailingList) req.getAttribute(WebKeys.MAILING_LIST_EDIT); 543 544 if(ml.getUserId().equals(user.getUserId())){ 545 InodeFactory.deleteInode(ml); 546 SessionMessages.add(req, "message", "message.mailinglist.delete"); 548 } 549 else{ 550 SessionMessages.add(req, "message", "message.mailinglist.cannotEdit"); 552 } 553 554 } 555 556 557 private void _deleteSubscribers(ActionRequest req, ActionResponse res,PortletConfig config,ActionForm form) 558 throws Exception { 559 560 MailingList ml = (MailingList) req.getAttribute(WebKeys.MAILING_LIST_EDIT); 561 String [] deletes = req.getParameterValues("subscribers"); 562 for (int i = 0; i < deletes.length; i++) { 563 UserProxy s = (UserProxy) InodeFactory.getInode(deletes[i],UserProxy.class); 564 ml.deleteChild(s); 565 } 566 DotHibernate.closeSession(); 567 568 ml.setSubscriberCount(InodeFactory.getChildrenClass(ml, UserProxy.class).size()); 569 InodeFactory.saveInode(ml); 570 SessionMessages.add(req, "message", "message.mailinglist.subscribers.deleted"); 571 572 573 } 574 575 } 576 | Popular Tags |