1 package com.dotmarketing.portlets.cmsmaintenance.action; 2 3 import java.io.BufferedInputStream ; 4 import java.io.BufferedOutputStream ; 5 import java.io.File ; 6 import java.io.FileInputStream ; 7 import java.io.FileNotFoundException ; 8 import java.io.FileOutputStream ; 9 import java.io.FilenameFilter ; 10 import java.io.IOException ; 11 import java.io.InputStream ; 12 import java.io.OutputStream ; 13 import java.io.PrintWriter ; 14 import java.lang.reflect.InvocationTargetException ; 15 import java.util.Date ; 16 import java.util.HashSet ; 17 import java.util.Iterator ; 18 import java.util.List ; 19 import java.util.Map ; 20 import java.util.Set ; 21 import java.util.regex.Matcher ; 22 import java.util.regex.Pattern ; 23 import java.util.zip.ZipEntry ; 24 import java.util.zip.ZipInputStream ; 25 import java.util.zip.ZipOutputStream ; 26 27 import javax.portlet.ActionRequest; 28 import javax.portlet.ActionResponse; 29 import javax.portlet.PortletConfig; 30 import javax.portlet.RenderRequest; 31 import javax.portlet.RenderResponse; 32 import javax.portlet.WindowState; 33 import javax.servlet.ServletException ; 34 import javax.servlet.http.HttpServletRequest ; 35 import javax.servlet.http.HttpServletResponse ; 36 import javax.servlet.jsp.PageContext ; 37 38 import net.sf.hibernate.HibernateException; 39 import net.sf.hibernate.metadata.ClassMetadata; 40 41 import org.apache.commons.beanutils.BeanUtils; 42 import org.apache.struts.action.ActionForm; 43 import org.apache.struts.action.ActionForward; 44 import org.apache.struts.action.ActionMapping; 45 46 import com.dotmarketing.beans.Clickstream; 47 import com.dotmarketing.beans.ClickstreamRequest; 48 import com.dotmarketing.beans.Inode; 49 import com.dotmarketing.cache.BannerCache; 50 import com.dotmarketing.cache.HostCache; 51 import com.dotmarketing.cache.IdentifierCache; 52 import com.dotmarketing.cache.LiveCache; 53 import com.dotmarketing.cache.PageNotFoundCache; 54 import com.dotmarketing.cache.PermissionCache; 55 import com.dotmarketing.cache.VirtualLinksCache; 56 import com.dotmarketing.cache.WorkingCache; 57 import com.dotmarketing.cms.factories.PublicCompanyFactory; 58 import com.dotmarketing.cms.factories.PublicRoleFactory; 59 import com.dotmarketing.cms.factories.PublicUserFactory; 60 import com.dotmarketing.db.DotHibernate; 61 import com.dotmarketing.factories.InodeFactory; 62 import com.dotmarketing.portal.struts.DotPortletAction; 63 import com.dotmarketing.portlets.cmsmaintenance.struts.CmsMaintenanceForm; 64 import com.dotmarketing.portlets.containers.factories.ContainerFactory; 65 import com.dotmarketing.portlets.containers.model.Container; 66 import com.dotmarketing.portlets.contentlet.factories.ContentletFactory; 67 import com.dotmarketing.portlets.contentlet.model.Contentlet; 68 import com.dotmarketing.portlets.htmlpages.factories.HTMLPageFactory; 69 import com.dotmarketing.portlets.htmlpages.model.HTMLPage; 70 import com.dotmarketing.portlets.links.factories.LinkFactory; 71 import com.dotmarketing.portlets.links.model.Link; 72 import com.dotmarketing.portlets.templates.factories.TemplateFactory; 73 import com.dotmarketing.portlets.templates.model.Template; 74 import com.dotmarketing.util.Config; 75 import com.dotmarketing.util.Logger; 76 import com.dotmarketing.util.LuceneUtils; 77 import com.dotmarketing.util.UtilMethods; 78 import com.dotmarketing.util.WebKeys; 79 import com.liferay.portal.model.Company; 80 import com.liferay.portal.model.Role; 81 import com.liferay.portal.model.User; 82 import com.liferay.portlet.ActionRequestImpl; 83 import com.liferay.portlet.ActionResponseImpl; 84 import com.liferay.util.servlet.SessionMessages; 85 import com.oreilly.servlet.MultipartRequest; 86 import com.thoughtworks.xstream.XStream; 87 import com.thoughtworks.xstream.io.xml.DomDriver; 88 ; 89 90 97 public class ViewCmsMaintenanceAction extends DotPortletAction { 98 99 100 103 String backupFilePath = "../backup"; 104 105 108 String backupTempFilePath = "../backup/temp"; 109 110 111 public ActionForward render( 112 ActionMapping mapping, ActionForm form, PortletConfig config, 113 RenderRequest req, RenderResponse res) 114 throws Exception { 115 116 Logger.debug(this, "Running ViewCmsMaintenanceAction!!!! = " + req.getWindowState()); 117 118 try { 119 _initCacheValues(req); 121 122 if (req.getWindowState().equals(WindowState.MAXIMIZED)) { 123 Logger.debug(this, "Showing view action cms maintenance maximized"); 124 return mapping.findForward("portlet.ext.cmsmaintenance.view_cms_maintenance"); 125 } else { 126 Logger.debug(this, "Showing view action cms maintenance minimized"); 127 return mapping.findForward("portlet.ext.cmsmaintenance.view"); 128 } 129 } 130 catch (Exception e) { 131 req.setAttribute(PageContext.EXCEPTION, e); 132 return mapping.findForward(com.liferay.portal.util.Constants.COMMON_ERROR); 133 } 134 } 135 136 public void processAction( 137 ActionMapping mapping, ActionForm form, PortletConfig config, 138 ActionRequest req, ActionResponse res) 139 throws Exception 140 { 141 String message = ""; 142 CmsMaintenanceForm ccf = (CmsMaintenanceForm) form; 143 String cmd = req.getParameter("cmd"); 144 145 if(cmd.equals("cache")){ 147 148 String cacheName = ccf.getCacheName(); 149 if (cacheName.equals(com.dotmarketing.util.WebKeys.Cache.CACHE_OPTIMIZE_INDEX)) { 150 LuceneUtils.optimizeCurrentIndex(); 151 message = "message.cmsmaintenance.cache.indexoptimized"; 152 } else if (cacheName.equals(com.dotmarketing.util.WebKeys.Cache.CACHE_CONTENTS_INDEX)) { 153 if (ContentletFactory.reIndexAllContentlets()) 154 message = "message.cmsmaintenance.cache.indexrebuilt"; 155 else 156 message = "message.cmsmaintenance.cache.failedtorebuild"; 157 } else if (!(cacheName.equals(com.dotmarketing.util.WebKeys.Cache.CACHE_WORKING_FILES) || cacheName.equals(com.dotmarketing.util.WebKeys.Cache.CACHE_LIVE_FILES))) 158 { 159 _flush(com.dotmarketing.util.WebKeys.Cache.CACHE_WORKING_FILES); 160 message = (cacheName.equals(WebKeys.Cache.CACHE_ALL_CACHES) ? "message.cmsmaintenance.cache.flushallcache" : "message.cmsmaintenance.cache.flushcache"); 161 } else { 162 _deleteFiles(com.dotmarketing.util.WebKeys.Cache.CACHE_LIVE_FILES); 163 _deleteFiles(com.dotmarketing.util.WebKeys.Cache.CACHE_WORKING_FILES); 164 165 message = "message.cmsmaintenance.cache.deletefiles"; 166 } 167 } else if(cmd.equals("searchandreplace")){ 169 170 message = "Search and Replace complete <br>" + searchAndReplace(ccf); 171 172 _deleteFiles(com.dotmarketing.util.WebKeys.Cache.CACHE_LIVE_FILES); 173 _deleteFiles(com.dotmarketing.util.WebKeys.Cache.CACHE_WORKING_FILES); 174 _flush(com.dotmarketing.util.WebKeys.Cache.CACHE_ALL_CACHES); 175 176 } else { 178 179 File f = new File (Config.CONTEXT.getRealPath(backupFilePath)); 180 f.mkdirs(); 181 f = new File (Config.CONTEXT.getRealPath(backupTempFilePath)); 182 f.mkdirs(); 183 deleteTempFiles(); 184 185 if(cmd.equals("createZip")) { 186 187 message = "Creating XML Files. "; 188 createXMLFiles(); 189 String x = UtilMethods.dateToJDBC(new Date ()).replace(':', '-').replace(' ', '_'); 190 File zipFile = new File (Config.CONTEXT.getRealPath(backupFilePath + "/backup_" + x + "_.zip")); 191 message +="Zipping up to file:" + zipFile.getAbsolutePath(); 192 BufferedOutputStream bout = new BufferedOutputStream (new FileOutputStream (zipFile)); 193 194 zipTempDirectoryToStream(bout); 195 message +=". Done."; 196 197 }else if(cmd.equals("wipeOutDotCMSDatabase")) { 198 199 message = "Deleted dotCMS Database"; 200 deleteDotCMS(); 201 202 }else if(cmd.equals("downloadZip")) { 203 204 message ="File Downloaded"; 205 String x = UtilMethods.dateToJDBC(new Date ()).replace(':', '-').replace(' ', '_'); 206 File zipFile = new File (Config.CONTEXT.getRealPath(backupFilePath + "/backup_" + x + "_.zip")); 207 208 ActionResponseImpl responseImpl = (ActionResponseImpl) res; 209 HttpServletResponse httpResponse = responseImpl.getHttpServletResponse(); 210 httpResponse.setHeader("Content-type", ""); 211 httpResponse.setHeader("Content-Disposition", "attachment; filename=" + zipFile.getName()); 212 213 createXMLFiles(); 214 215 zipTempDirectoryToStream(httpResponse.getOutputStream()); 216 217 }else if(cmd.equals("upload")) { 218 ActionResponseImpl responseImpl = (ActionResponseImpl) res; 219 HttpServletResponse httpResponse = responseImpl.getHttpServletResponse(); 220 221 ActionRequestImpl requestImpl = (ActionRequestImpl) req; 222 HttpServletRequest httpRequest = requestImpl.getHttpServletRequest(); 223 224 message ="file upload Done."; 225 doUpload(httpRequest, httpResponse); 226 } 227 } 228 229 SessionMessages.add(req, "message",message); 230 String referer = req.getParameter("referer"); 231 setForward(req,referer); 232 } 233 234 235 private void _flush(String cacheName) 236 throws Exception 237 { 238 if (cacheName.equals(BannerCache.class.getName())) 239 { 240 BannerCache.clearCache(); 242 } 243 else if (cacheName.equals(HostCache.class.getName())) 244 { 245 HostCache.clearCache(); 247 } 248 else if (cacheName.equals(IdentifierCache.class.getName())) 249 { 250 IdentifierCache.clearCache(); 252 } 253 else if (cacheName.equals(LiveCache.class.getName())) 254 { 255 LiveCache.clearCache(); 257 } 258 else if (cacheName.equals(PageNotFoundCache.class.getName())) 259 { 260 PageNotFoundCache.clearCache(); 262 } 263 else if (cacheName.equals(PermissionCache.class.getName())) 264 { 265 PermissionCache.clearCache(); 267 } 268 else if (cacheName.equals(VirtualLinksCache.class.getName())) 269 { 270 VirtualLinksCache.clearCache(); 272 } 273 else if (cacheName.equals(WorkingCache.class.getName())) 274 { 275 WorkingCache.clearCache(); 277 } 278 else if (cacheName.equals(WebKeys.Cache.CACHE_ALL_CACHES)) 279 { 280 BannerCache.clearCache(); 282 HostCache.clearCache(); 283 IdentifierCache.clearCache(); 284 LiveCache.clearCache(); 285 PageNotFoundCache.clearCache(); 286 PermissionCache.clearCache(); 287 WorkingCache.clearCache(); 288 289 VirtualLinksCache.clearCache(); 291 } 292 } 293 294 private void _deleteFiles(String cacheName) 295 throws Exception 296 { 297 try 298 { 299 String realPath = ""; 300 301 String velocityRootPath = Config.getStringProperty("VELOCITY_ROOT"); 302 if (velocityRootPath.startsWith("/WEB-INF")) { 303 velocityRootPath = Config.CONTEXT.getRealPath(velocityRootPath); 304 } 305 306 if (cacheName.equals(com.dotmarketing.util.WebKeys.Cache.CACHE_LIVE_FILES)) 307 { 308 realPath = velocityRootPath + File.separator + "live"; 309 } 310 else 311 { 312 realPath = velocityRootPath + File.separator + "working"; 313 } 314 File file = new File (realPath); 315 if (file.isDirectory()) 316 { 317 _deleteRoot(file); 318 Logger.debug(ViewCmsMaintenanceAction.class,"The directory " + realPath + " has been deleted"); 319 } 320 } 321 catch(Exception ex) 322 { 323 Logger.error(ViewCmsMaintenanceAction.class,ex.toString()); 324 throw ex; 325 } 326 } 327 328 private boolean _deleteRoot(File root) throws Exception 329 { 330 boolean returnValue = true; 331 File [] childs = root.listFiles(); 332 for(int i = 0; i < childs.length; i++) 333 { 334 File child = childs[i]; 335 if (child.isFile()) 336 { 337 returnValue = returnValue && child.delete(); 338 } 339 if (child.isDirectory()) 340 { 341 returnValue = returnValue && _deleteRoot(child); 342 } 343 } 344 return returnValue; 345 } 346 347 private int _numberRoot(File root) throws Exception 348 { 349 int returnValue = 0; 350 File [] childs = root.listFiles(); 351 for(int i = 0; i < childs.length; i++) 352 { 353 File child = childs[i]; 354 if (child.isFile()) 355 { 356 returnValue++; 357 } 358 if (child.isDirectory()) 359 { 360 returnValue += _numberRoot(child); 361 } 362 } 363 return returnValue; 364 } 365 366 370 private void _initCacheValues(RenderRequest req) 371 { 372 int liveCount = 0; 373 int workingCount = 0; 374 try 375 { 376 String velocityRootPath = Config.getStringProperty("VELOCITY_ROOT"); 377 if (velocityRootPath.startsWith("/WEB-INF")) { 378 velocityRootPath = Config.CONTEXT.getRealPath(velocityRootPath); 379 } 380 String livePath = velocityRootPath + File.separator + "live"; 381 String workingPath = velocityRootPath + File.separator + "working"; 382 383 File file = new File (livePath); 385 liveCount = _numberRoot(file); 386 387 file = new File (workingPath); 389 workingCount = _numberRoot(file); 390 } 391 catch(Exception ex) 392 { 393 Logger.error(ViewCmsMaintenanceAction.class,"Error calculating the number of files"); 394 } 395 finally 396 { 397 req.setAttribute(WebKeys.Cache.CACHE_NUMBER_LIVE_FILES,new Integer (liveCount)); 398 req.setAttribute(WebKeys.Cache.CACHE_NUMBER_WORKING_FILES,new Integer (workingCount)); 399 } 400 } 401 402 403 private String searchAndReplace(CmsMaintenanceForm form){ 404 String message=""; 405 boolean isAdmin = false; 406 407 if (UtilMethods.isSet(form.getUserId())) { 408 List <Role> roles = com.dotmarketing.factories.RoleFactory.getAllRolesForUser(form.getUserId()); 409 for (Role role : roles) { 410 if (role.getName().equals(Config.getStringProperty("ADMINISTRATOR_ROLE"))) { 411 isAdmin = true; 412 break; 413 } 414 } 415 } 416 417 if (isAdmin) { 418 419 String searchString = form.getSearchString(); 420 String replaceString = form.getReplaceString(); 421 if (UtilMethods.isSet(searchString) && UtilMethods.isSet(replaceString)) { 422 DotHibernate db = new DotHibernate(Contentlet.class); 423 db.startTransaction(); 424 425 try{ 426 427 String originalPatter = searchString; 429 String newPatter = replaceString; 430 Pattern patter = Pattern.compile(originalPatter); 431 Matcher matcher = patter.matcher(""); 432 int cicleValue = 1000; 433 int offSet = 0; 434 int contentletsCounter = 0; 435 436 StringBuffer sqlQuery = new StringBuffer (); 438 sqlQuery.append("SELECT {contentlet.*} FROM contentlet, inode contentlet_1_ "); 439 sqlQuery.append("WHERE contentlet.inode = contentlet_1_.inode"); 440 sqlQuery.append(" ORDER BY contentlet.inode ASC"); 441 442 String query = sqlQuery.toString(); 444 db.setSQLQuery(query); 445 db.setFirstResult(offSet); 446 db.setMaxResults(cicleValue); 447 448 450 List <Contentlet> contentlets = db.list(); 451 int size = contentlets.size(); 452 while(size > 0) 453 { 454 for(int i = 0;i < size;i++) 455 { 456 Contentlet contentlet = contentlets.get(i); 457 if (UtilMethods.isSet(contentlet.getText1())) 459 { 460 matcher.reset(contentlet.getText1()); 461 contentlet.setText1(matcher.replaceAll(newPatter)); 462 } 463 if (UtilMethods.isSet(contentlet.getText2())) 465 { 466 matcher.reset(contentlet.getText2()); 467 contentlet.setText2(matcher.replaceAll(newPatter)); 468 } 469 if (UtilMethods.isSet(contentlet.getText3())) 471 { 472 matcher.reset(contentlet.getText3()); 473 contentlet.setText3(matcher.replaceAll(newPatter)); 474 } 475 if (UtilMethods.isSet(contentlet.getText4())) 477 { 478 matcher.reset(contentlet.getText4()); 479 contentlet.setText4(matcher.replaceAll(newPatter)); 480 } 481 if (UtilMethods.isSet(contentlet.getText5())) 483 { 484 matcher.reset(contentlet.getText5()); 485 contentlet.setText5(matcher.replaceAll(newPatter)); 486 } 487 if (UtilMethods.isSet(contentlet.getText6())) 489 { 490 matcher.reset(contentlet.getText6()); 491 contentlet.setText6(matcher.replaceAll(newPatter)); 492 } 493 if (UtilMethods.isSet(contentlet.getText7())) 495 { 496 matcher.reset(contentlet.getText7()); 497 contentlet.setText7(matcher.replaceAll(newPatter)); 498 } 499 if (UtilMethods.isSet(contentlet.getText8())) 501 { 502 matcher.reset(contentlet.getText8()); 503 contentlet.setText8(matcher.replaceAll(newPatter)); 504 } 505 if (UtilMethods.isSet(contentlet.getText9())) 507 { 508 matcher.reset(contentlet.getText9()); 509 contentlet.setText9(matcher.replaceAll(newPatter)); 510 } 511 if (UtilMethods.isSet(contentlet.getText10())) 513 { 514 matcher.reset(contentlet.getText10()); 515 contentlet.setText10(matcher.replaceAll(newPatter)); 516 } 517 if (UtilMethods.isSet(contentlet.getText11())) 519 { 520 matcher.reset(contentlet.getText11()); 521 contentlet.setText11(matcher.replaceAll(newPatter)); 522 } 523 if (UtilMethods.isSet(contentlet.getText12())) 525 { 526 matcher.reset(contentlet.getText12()); 527 contentlet.setText12(matcher.replaceAll(newPatter)); 528 } 529 if (UtilMethods.isSet(contentlet.getText13())) 531 { 532 matcher.reset(contentlet.getText13()); 533 contentlet.setText13(matcher.replaceAll(newPatter)); 534 } 535 if (UtilMethods.isSet(contentlet.getText14())) 537 { 538 matcher.reset(contentlet.getText14()); 539 contentlet.setText14(matcher.replaceAll(newPatter)); 540 } 541 if (UtilMethods.isSet(contentlet.getText15())) 543 { 544 matcher.reset(contentlet.getText15()); 545 contentlet.setText15(matcher.replaceAll(newPatter)); 546 } 547 if (UtilMethods.isSet(contentlet.getText16())) 549 { 550 matcher.reset(contentlet.getText16()); 551 contentlet.setText16(matcher.replaceAll(newPatter)); 552 } 553 if (UtilMethods.isSet(contentlet.getText17())) 555 { 556 matcher.reset(contentlet.getText17()); 557 contentlet.setText17(matcher.replaceAll(newPatter)); 558 } 559 if (UtilMethods.isSet(contentlet.getText18())) 561 { 562 matcher.reset(contentlet.getText18()); 563 contentlet.setText18(matcher.replaceAll(newPatter)); 564 } 565 if (UtilMethods.isSet(contentlet.getText19())) 567 { 568 matcher.reset(contentlet.getText19()); 569 contentlet.setText19(matcher.replaceAll(newPatter)); 570 } 571 if (UtilMethods.isSet(contentlet.getText20())) 573 { 574 matcher.reset(contentlet.getText20()); 575 contentlet.setText20(matcher.replaceAll(newPatter)); 576 } 577 if (UtilMethods.isSet(contentlet.getText21())) 579 { 580 matcher.reset(contentlet.getText21()); 581 contentlet.setText21(matcher.replaceAll(newPatter)); 582 } 583 if (UtilMethods.isSet(contentlet.getText22())) 585 { 586 matcher.reset(contentlet.getText22()); 587 contentlet.setText22(matcher.replaceAll(newPatter)); 588 } 589 if (UtilMethods.isSet(contentlet.getText23())) 591 { 592 matcher.reset(contentlet.getText23()); 593 contentlet.setText23(matcher.replaceAll(newPatter)); 594 } 595 if (UtilMethods.isSet(contentlet.getText24())) 597 { 598 matcher.reset(contentlet.getText24()); 599 contentlet.setText24(matcher.replaceAll(newPatter)); 600 } 601 if (UtilMethods.isSet(contentlet.getText25())) 603 { 604 matcher.reset(contentlet.getText25()); 605 contentlet.setText25(matcher.replaceAll(newPatter)); 606 } 607 if (UtilMethods.isSet(contentlet.getText_area1())) 609 { 610 matcher.reset(contentlet.getText_area1()); 611 contentlet.setText_area1(matcher.replaceAll(newPatter)); 612 } 613 if (UtilMethods.isSet(contentlet.getText_area2())) 615 { 616 matcher.reset(contentlet.getText_area2()); 617 contentlet.setText_area2(matcher.replaceAll(newPatter)); 618 } 619 if (UtilMethods.isSet(contentlet.getText_area3())) 621 { 622 matcher.reset(contentlet.getText_area3()); 623 contentlet.setText_area3(matcher.replaceAll(newPatter)); 624 } 625 if (UtilMethods.isSet(contentlet.getText_area4())) 627 { 628 matcher.reset(contentlet.getText_area4()); 629 contentlet.setText_area4(matcher.replaceAll(newPatter)); 630 } 631 if (UtilMethods.isSet(contentlet.getText_area5())) 633 { 634 matcher.reset(contentlet.getText_area5()); 635 contentlet.setText_area5(matcher.replaceAll(newPatter)); 636 } 637 if (UtilMethods.isSet(contentlet.getText_area6())) 639 { 640 matcher.reset(contentlet.getText_area6()); 641 contentlet.setText_area6(matcher.replaceAll(newPatter)); 642 } 643 if (UtilMethods.isSet(contentlet.getText_area7())) 645 { 646 matcher.reset(contentlet.getText_area7()); 647 contentlet.setText_area7(matcher.replaceAll(newPatter)); 648 } 649 if (UtilMethods.isSet(contentlet.getText_area8())) 651 { 652 matcher.reset(contentlet.getText_area8()); 653 contentlet.setText_area8(matcher.replaceAll(newPatter)); 654 } 655 if (UtilMethods.isSet(contentlet.getText_area9())) 657 { 658 matcher.reset(contentlet.getText_area9()); 659 contentlet.setText_area9(matcher.replaceAll(newPatter)); 660 } 661 if (UtilMethods.isSet(contentlet.getText_area10())) 663 { 664 matcher.reset(contentlet.getText_area10()); 665 contentlet.setText_area10(matcher.replaceAll(newPatter)); 666 } 667 if (UtilMethods.isSet(contentlet.getText_area11())) 669 { 670 matcher.reset(contentlet.getText_area11()); 671 contentlet.setText_area11(matcher.replaceAll(newPatter)); 672 } 673 if (UtilMethods.isSet(contentlet.getText_area12())) 675 { 676 matcher.reset(contentlet.getText_area12()); 677 contentlet.setText_area12(matcher.replaceAll(newPatter)); 678 } 679 if (UtilMethods.isSet(contentlet.getText_area13())) 681 { 682 matcher.reset(contentlet.getText_area13()); 683 contentlet.setText_area13(matcher.replaceAll(newPatter)); 684 } 685 if (UtilMethods.isSet(contentlet.getText_area14())) 687 { 688 matcher.reset(contentlet.getText_area14()); 689 contentlet.setText_area14(matcher.replaceAll(newPatter)); 690 } 691 if (UtilMethods.isSet(contentlet.getText_area15())) 693 { 694 matcher.reset(contentlet.getText_area15()); 695 contentlet.setText_area15(matcher.replaceAll(newPatter)); 696 } 697 if (UtilMethods.isSet(contentlet.getText_area16())) 699 { 700 matcher.reset(contentlet.getText_area16()); 701 contentlet.setText_area16(matcher.replaceAll(newPatter)); 702 } 703 if (UtilMethods.isSet(contentlet.getText_area17())) 705 { 706 matcher.reset(contentlet.getText_area17()); 707 contentlet.setText_area17(matcher.replaceAll(newPatter)); 708 } 709 if (UtilMethods.isSet(contentlet.getText_area18())) 711 { 712 matcher.reset(contentlet.getText_area18()); 713 contentlet.setText_area18(matcher.replaceAll(newPatter)); 714 } 715 if (UtilMethods.isSet(contentlet.getText_area19())) 717 { 718 matcher.reset(contentlet.getText_area19()); 719 contentlet.setText_area19(matcher.replaceAll(newPatter)); 720 } 721 if (UtilMethods.isSet(contentlet.getText_area20())) 723 { 724 matcher.reset(contentlet.getText_area20()); 725 contentlet.setText_area20(matcher.replaceAll(newPatter)); 726 } 727 if (UtilMethods.isSet(contentlet.getText_area21())) 729 { 730 matcher.reset(contentlet.getText_area21()); 731 contentlet.setText_area21(matcher.replaceAll(newPatter)); 732 } 733 if (UtilMethods.isSet(contentlet.getText_area22())) 735 { 736 matcher.reset(contentlet.getText_area22()); 737 contentlet.setText_area22(matcher.replaceAll(newPatter)); 738 } 739 if (UtilMethods.isSet(contentlet.getText_area23())) 741 { 742 matcher.reset(contentlet.getText_area23()); 743 contentlet.setText_area23(matcher.replaceAll(newPatter)); 744 } 745 if (UtilMethods.isSet(contentlet.getText_area24())) 747 { 748 matcher.reset(contentlet.getText_area24()); 749 contentlet.setText_area24(matcher.replaceAll(newPatter)); 750 } 751 if (UtilMethods.isSet(contentlet.getText_area25())) 753 { 754 matcher.reset(contentlet.getText_area25()); 755 contentlet.setText_area25(matcher.replaceAll(newPatter)); 756 } 757 InodeFactory.saveInode(contentlet); 758 contentletsCounter++; 759 760 } 761 offSet += cicleValue; 762 db.setFirstResult(offSet); 763 db.setMaxResults(cicleValue); 764 contentlets = db.list(); 765 size = contentlets.size(); 766 } 767 768 769 int linksCounter = 0; 771 772 List links = LinkFactory.getLinkByCondition("url like '%" + searchString + "%'"); 773 Iterator it = links.iterator(); 774 while (it.hasNext()) { 775 Link link = (Link)it.next(); 776 String url = link.getUrl(); 777 url = url.replaceAll(searchString, replaceString); 778 link.setUrl(url); 779 InodeFactory.saveInode(link); 780 linksCounter++; 781 } 782 783 int containersCounter = 0; 785 List containers = ContainerFactory.getContainerByCondition("(code like '%" + searchString + "%' or pre_loop like '%" + searchString + "%' or post_loop like '%" + searchString + "%')"); 786 it = containers.iterator(); 787 while (it.hasNext()) { 788 Container container = (Container)it.next(); 789 String code = container.getCode(); 790 code = code.replaceAll(searchString, replaceString); 791 container.setCode(code); 792 String preLoop = container.getPreLoop(); 793 preLoop = preLoop.replaceAll(searchString, replaceString); 794 container.setPreLoop(preLoop); 795 String postLoop = container.getPostLoop(); 796 postLoop = postLoop.replaceAll(searchString, replaceString); 797 container.setPostLoop(postLoop); 798 InodeFactory.saveInode(container); 799 containersCounter++; 800 } 801 802 803 int templatesCounter = 0; 805 806 List templates = TemplateFactory.getTemplateByCondition("body like '%" + searchString + "%'"); 807 it = templates.iterator(); 808 while (it.hasNext()) { 809 Template template = (Template)it.next(); 810 String body = template.getBody(); 811 body = body.replaceAll(searchString, replaceString); 812 template.setBody(body); 813 InodeFactory.saveInode(template); 814 templatesCounter++; 815 } 816 817 818 int pagesCounter = 0; 820 List pages = HTMLPageFactory.getHTMLPageByCondition("redirect like '%" + searchString + "%'"); 821 it = pages.iterator(); 822 while (it.hasNext()) { 823 HTMLPage htmlpage = (HTMLPage)it.next(); 824 String redirect = htmlpage.getRedirect(); 825 redirect = redirect.replaceAll(searchString, replaceString); 826 htmlpage.setRedirect(redirect); 827 InodeFactory.saveInode(htmlpage); 828 pagesCounter++; 829 } 830 831 db.commitTransaction(); 832 833 StringBuffer buf = new StringBuffer ("Modifications Summary<br>"); 834 buf.append(contentletsCounter+" Contentlets proccesed!!<br>"); 835 buf.append(linksCounter+" Links modified!!<br>"); 836 buf.append(containersCounter+" Containers modified!!<br>"); 837 buf.append(templatesCounter+" Templates modified!!<br>"); 838 buf.append(pagesCounter+" HTMLPages modified!!<br>"); 839 message=buf.toString(); 840 841 }catch(Exception e){ 842 message ="<h1>A error occured running the replace, the transaction was rollbacked!!</h1><br><h2>Exception message: <%=e.getMessage()%></h2>"; 843 db.rollbackTransaction(); 844 e.printStackTrace(); 845 } 846 847 } 848 } 849 return message; 850 } 851 852 864 865 private void doXMLFileImport(File f, PrintWriter out) { 866 BufferedInputStream _bin = null; 867 try { 868 XStream _xstream = null; 869 String _className = null; 870 Class _importClass = null; 871 DotHibernate _dh = null; 872 873 _className = f.getName().substring(0, f.getName().lastIndexOf(".")); 874 _xstream = new XStream(new DomDriver()); 875 _importClass = Class.forName(_className); 876 out.println("Importing:\t" + _className); 877 if (_importClass.equals(User.class)) { 878 879 } else if (_importClass.equals(Company.class)) { 880 881 } else if (_importClass.equals(Role.class)) { 882 883 } else { 884 885 _dh = new DotHibernate(_importClass); 886 _bin = new BufferedInputStream (new FileInputStream (f)); 887 List l = (List ) _xstream.fromXML(_bin); 888 out.println("Found :\t" + l.size() + " " + _className + "(s)"); 889 String id = _dh.getSession().getSessionFactory().getClassMetadata(_importClass).getIdentifierPropertyName(); 890 for (int j = 0; j < l.size(); j++) { 891 Object obj = l.get(j); 892 if (UtilMethods.isSet(id)) { 893 String prop = BeanUtils.getProperty(obj, id); 894 895 try { 896 Long myId = new Long (Long.parseLong(prop)); 897 _dh.saveWithPrimaryKey(obj, myId); 898 } catch (Exception e) { 899 _dh.saveWithPrimaryKey(obj, prop); 900 } 901 902 } else { 903 _dh.save(obj); 904 } 905 } 906 907 } 908 } catch (FileNotFoundException e) { 909 e.printStackTrace(); 911 } catch (ClassNotFoundException e1) { 912 e1.printStackTrace(); 913 } catch (HibernateException e) { 914 e.printStackTrace(); 916 } catch (IllegalAccessException e) { 917 e.printStackTrace(); 919 } catch (InvocationTargetException e) { 920 e.printStackTrace(); 922 } catch (NoSuchMethodException e) { 923 e.printStackTrace(); 925 } finally { 926 try { 927 if (_bin != null) { 928 _bin.close(); 929 } 930 } catch (IOException e) { 931 e.printStackTrace(); 933 } 934 } 935 936 } 937 938 947 private void createXMLFiles() throws ServletException , IOException { 948 949 deleteTempFiles(); 950 951 Set <Class > _tablesToDump = new HashSet <Class >(); 952 try { 953 954 955 Map map = DotHibernate.getSession().getSessionFactory().getAllClassMetadata(); 956 Iterator it = map.entrySet().iterator(); 957 while (it.hasNext()) { 958 Map.Entry pairs = (Map.Entry ) it.next(); 959 Class x = (Class ) pairs.getKey(); 960 if (!x.equals(Inode.class) && !x.equals(Clickstream.class) && !x.equals(ClickstreamRequest.class)) 961 _tablesToDump.add(x); 962 963 } 964 XStream _xstream = null; 965 DotHibernate _dh = null; 966 List _list = null; 967 File _writing = null; 968 BufferedOutputStream _bout = null; 969 970 for (Class clazz : _tablesToDump) { 971 _xstream = new XStream(new DomDriver()); 972 973 978 979 _writing = new File (Config.CONTEXT.getRealPath(backupTempFilePath + "/" + clazz.getName() + ".xml")); 980 _bout = new BufferedOutputStream (new FileOutputStream (_writing)); 981 _dh = new DotHibernate(clazz); 982 _dh.setQuery("from " + clazz.getName()); 983 984 _list = _dh.list(); 985 System.out.println("writing : " + _list.size() + " records to " + clazz.getName()); 986 _xstream.toXML(_list, _bout); 987 988 _bout.close(); 989 _list = null; 990 _dh = null; 991 _bout = null; 992 System.gc(); 993 } 994 995 996 997 _list = PublicCompanyFactory.getCompanies(); 998 _xstream = new XStream(new DomDriver()); 999 _writing = new File (Config.CONTEXT.getRealPath(backupTempFilePath + "/" + Company.class.getName() + ".xml")); 1000 _bout = new BufferedOutputStream (new FileOutputStream (_writing)); 1001 _xstream.toXML(_list, _bout); 1002 _bout.close(); 1003 _list = null; 1004 _bout = null; 1005 1006 1007 _list = PublicUserFactory.getAllUsers(); 1008 _xstream = new XStream(new DomDriver()); 1009 _writing = new File (Config.CONTEXT.getRealPath(backupTempFilePath + "/" + User.class.getName() + ".xml")); 1010 _bout = new BufferedOutputStream (new FileOutputStream (_writing)); 1011 _xstream.toXML(_list, _bout); 1012 _bout.close(); 1013 _list = null; 1014 _bout = null; 1015 1016 1017 _list = PublicRoleFactory.getAllRoles(); 1018 _xstream = new XStream(new DomDriver()); 1019 _writing = new File (Config.CONTEXT.getRealPath(backupTempFilePath + "/" + Role.class.getName() + ".xml")); 1020 _bout = new BufferedOutputStream (new FileOutputStream (_writing)); 1021 _xstream.toXML(_list, _bout); 1022 _bout.close(); 1023 _list = null; 1024 _bout = null; 1025 1026 1027 1028 } catch (HibernateException e) { 1029 1030 e.printStackTrace(); 1031 } 1032 1033 } 1034 1035 1044 private void zipTempDirectoryToStream(OutputStream out) throws IOException { 1045 1046 byte b[] = new byte[512]; 1047 ZipOutputStream zout = new ZipOutputStream (out); 1048 File f = new File (Config.CONTEXT.getRealPath(backupTempFilePath)); 1049 String [] s = f.list(); 1050 for (int i = 0; i < s.length; i++) { 1051 InputStream in = new BufferedInputStream (new FileInputStream (f = new File (Config.CONTEXT.getRealPath(backupTempFilePath + "/" + s[i])))); 1052 ZipEntry e = new ZipEntry (s[i].replace(File.separatorChar, '/')); 1053 zout.putNextEntry(e); 1054 int len = 0; 1055 while ((len = in.read(b)) != -1) { 1056 zout.write(b, 0, len); 1057 } 1058 zout.closeEntry(); 1059 in.close(); 1060 } 1061 zout.close(); 1062 out.close(); 1063 } 1064 1065 1069 private void deleteTempFiles() { 1070 File f = new File (Config.CONTEXT.getRealPath(backupTempFilePath)); 1071 String [] _tempFiles = f.list(); 1072 for (int i = 0; i < _tempFiles.length; i++) { 1073 f = new File (Config.CONTEXT.getRealPath(backupTempFilePath + "/" + _tempFiles[i])); 1074 f.delete(); 1075 } 1076 1077 } 1078 1079 1090 private void unzip(ZipInputStream zin, String s) throws IOException { 1091 System.out.println("unzipping " + s); 1092 File f = new File (Config.CONTEXT.getRealPath(backupTempFilePath + "/" + s)); 1093 BufferedOutputStream out = new BufferedOutputStream (new FileOutputStream (f)); 1094 byte[] b = new byte[512]; 1095 int len = 0; 1096 while ((len = zin.read(b)) != -1) { 1097 out.write(b, 0, len); 1098 } 1099 out.close(); 1100 } 1101 1102 1108 private class XMLFileNameFilter implements FilenameFilter { 1109 1110 public boolean accept(File f, String s) { 1111 if (s.toLowerCase().endsWith(".xml")) { 1112 return true; 1113 } else { 1114 return false; 1115 } 1116 } 1117 1118 } 1119 1120 1124 private void deleteDotCMS() { 1125 try { 1126 1127 Set <Class > _tablesToDump = new HashSet <Class >(); 1128 Map map; 1129 1130 map = DotHibernate.getSession().getSessionFactory().getAllClassMetadata(); 1131 1132 Iterator it = map.entrySet().iterator(); 1133 while (it.hasNext()) { 1134 Map.Entry pairs = (Map.Entry ) it.next(); 1135 ClassMetadata cmd = (ClassMetadata) pairs.getValue(); 1136 1137 } 1138 } catch (HibernateException e) { 1139 e.printStackTrace(); 1141 } 1142 1143 } 1144 1145 1153 private void doUpload(HttpServletRequest request, HttpServletResponse response) throws IOException { 1154 deleteTempFiles(); 1155 String tempdir = Config.CONTEXT.getRealPath(backupTempFilePath); 1156 1157 MultipartRequest mpr; 1158 try { 1159 mpr = new MultipartRequest(request, tempdir, 1000000000); 1160 File importFile = mpr.getFile("fileUpload"); 1161 1162 1165 if (importFile != null && importFile.getName().toLowerCase().endsWith(".zip")) { 1166 1167 InputStream in = new BufferedInputStream (new FileInputStream (importFile)); 1168 ZipInputStream zin = new ZipInputStream (in); 1169 ZipEntry e; 1170 1171 while ((e = zin.getNextEntry()) != null) { 1172 unzip(zin, e.getName()); 1173 } 1174 zin.close(); 1175 importFile.delete(); 1176 } 1177 1178 } catch (IOException e) { 1179 e.printStackTrace(); 1181 } 1182 File f = new File (Config.CONTEXT.getRealPath(backupTempFilePath)); 1183 String [] _tempFiles = f.list(new XMLFileNameFilter()); 1184 PrintWriter out = response.getWriter(); 1185 out.println("<pre>Found " + _tempFiles.length + " files to import"); 1186 for (int i = 0; i < _tempFiles.length; i++) { 1187 File _importFile = new File (Config.CONTEXT.getRealPath(backupTempFilePath + "/" + _tempFiles[i])); 1188 doXMLFileImport(_importFile, out); 1189 out.flush(); 1190 } 1191 out.println("Done Importing"); 1192 1193 } 1194 1195} 1196 | Popular Tags |