LaunchManagerImpl.java 64.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.cq.launches.api.Launch
 *  com.adobe.cq.launches.api.LaunchException
 *  com.adobe.cq.launches.api.LaunchManager
 *  com.adobe.cq.launches.api.LaunchManager$CreateOptions
 *  com.adobe.cq.launches.api.LaunchPromotionParameters
 *  com.adobe.cq.launches.api.LaunchPromotionScope
 *  com.adobe.cq.launches.api.LaunchResourceStatus
 *  com.adobe.cq.launches.api.LaunchResourceStatus$LaunchStatusType
 *  com.adobe.cq.launches.api.LaunchSource
 *  com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap
 *  com.day.cq.commons.jcr.JcrUtil
 *  com.day.cq.wcm.api.Page
 *  com.day.cq.wcm.api.PageManager
 *  com.day.cq.wcm.api.PageManager$CopyOptions
 *  com.day.cq.wcm.api.Revision
 *  com.day.cq.wcm.api.WCMException
 *  com.day.cq.wcm.msm.api.LiveCopy
 *  com.day.cq.wcm.msm.api.LiveRelationship
 *  com.day.cq.wcm.msm.api.LiveRelationshipManager
 *  com.day.cq.wcm.msm.api.LiveStatus
 *  com.day.cq.wcm.msm.api.RolloutConfig
 *  com.day.cq.wcm.msm.api.RolloutConfigManager
 *  com.day.cq.wcm.msm.api.RolloutManager
 *  com.day.cq.wcm.msm.api.RolloutManager$Trigger
 *  com.day.text.Text
 *  javax.jcr.Node
 *  javax.jcr.NodeIterator
 *  javax.jcr.Property
 *  javax.jcr.RangeIterator
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.Value
 *  org.apache.commons.lang.StringUtils
 *  org.apache.sling.api.resource.PersistenceException
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.api.resource.ValueMap
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.cq.wcm.launches.impl;

import com.adobe.cq.launches.api.Launch;
import com.adobe.cq.launches.api.LaunchException;
import com.adobe.cq.launches.api.LaunchManager;
import com.adobe.cq.launches.api.LaunchPromotionParameters;
import com.adobe.cq.launches.api.LaunchPromotionScope;
import com.adobe.cq.launches.api.LaunchResourceStatus;
import com.adobe.cq.launches.api.LaunchSource;
import com.adobe.cq.wcm.launches.impl.LaunchImpl;
import com.adobe.cq.wcm.launches.impl.LaunchResourceStatusHelper;
import com.adobe.cq.wcm.launches.impl.LaunchSourceImpl;
import com.adobe.cq.wcm.launches.impl.ReverseLiveRelationship;
import com.adobe.cq.wcm.launches.utils.LaunchUtils;
import com.day.cq.commons.inherit.HierarchyNodeInheritanceValueMap;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.cq.wcm.api.Revision;
import com.day.cq.wcm.api.WCMException;
import com.day.cq.wcm.msm.api.LiveCopy;
import com.day.cq.wcm.msm.api.LiveRelationship;
import com.day.cq.wcm.msm.api.LiveRelationshipManager;
import com.day.cq.wcm.msm.api.LiveStatus;
import com.day.cq.wcm.msm.api.RolloutConfig;
import com.day.cq.wcm.msm.api.RolloutConfigManager;
import com.day.cq.wcm.msm.api.RolloutManager;
import com.day.text.Text;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;
import java.util.TreeSet;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.RangeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Value;
import org.apache.commons.lang.StringUtils;
import org.apache.sling.api.resource.PersistenceException;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class LaunchManagerImpl
implements LaunchManager {
    private static final String CQ_LIVE_COPY = "cq:LiveCopy";
    private static final String CQ_LIVE_SYNC_CONFIG = "cq:LiveSyncConfig";
    private static final String NT_FILE = "nt:file";
    private static final Logger log = LoggerFactory.getLogger(LaunchManagerImpl.class);
    private static final Long TIME_DELTA = 1000;
    private final ResourceResolver resolver;
    private final Session session;
    private RolloutManager rolloutManager;

    protected LaunchManagerImpl(ResourceResolver resolver, RolloutManager rolloutManager) {
        this.resolver = resolver;
        this.session = (Session)resolver.adaptTo(Session.class);
        this.rolloutManager = rolloutManager;
    }

    public boolean isInLaunch(Resource resource) {
        return !this.getLaunches(resource).isEmpty();
    }

    public Launch createLaunch(Resource srcResource, String title, Calendar liveDate, boolean isDeep, boolean isLiveCopy) throws LaunchException {
        LaunchManager.CreateOptions options = new LaunchManager.CreateOptions();
        options.resource = srcResource;
        options.title = title;
        options.liveDate = liveDate;
        options.isDeep = isDeep;
        options.isLiveCopy = isLiveCopy;
        return this.createLaunch(options);
    }

    public Launch createLaunch(LaunchManager.CreateOptions options) throws LaunchException {
        try {
            CreateLaunch launch = new CreateLaunch(options.resource, options.launchSourceList, options.title, options.liveDate, options.isDeep, options.isLiveCopy, options.template, options.sourceRolloutConfigs, options.promoteRolloutConfigs);
            launch.write(this.session);
            this.session.save();
            log.info("Created launch [{}] for [{}]", (Object)launch.getResource().getName(), (Object)launch.getSourceRootResource().getPath());
            if (options.isLiveCopy) {
                try {
                    if (options.launchSourceList != null) {
                        for (LaunchSource launchSource : options.launchSourceList) {
                            Resource inputResource = launchSource.getSourceRootResource();
                            String inputResourcePath = inputResource.getPath();
                            inputResourcePath = inputResourcePath.substring(1);
                            Resource rootResource = launch.getResource().getChild(inputResourcePath);
                            this.createLaunchLiveRelationship(launch, rootResource, inputResource, launchSource.isDeep(), options.sourceRolloutConfigs);
                        }
                    } else {
                        this.createLaunchLiveRelationship(launch, launch.getRootResource(), options.resource, options.isDeep, options.sourceRolloutConfigs);
                    }
                }
                catch (LaunchException e) {
                    try {
                        this.deleteLaunch(launch);
                    }
                    catch (LaunchException de) {
                        log.warn("Could not removed incomplete launch [{}] after live relationship could not be established", (Object)launch.getResource().getPath(), (Object)de);
                    }
                    throw e;
                }
            }
            return launch;
        }
        catch (RepositoryException e) {
            this.revertChanges();
            throw new LaunchException("Unable to create launch", (Throwable)e);
        }
    }

    public Launch getLaunch(String absPath) {
        if (absPath == null) {
            throw new IllegalArgumentException("Launch path cannot be null");
        }
        try {
            return new LaunchImpl(this.resolver.getResource(absPath));
        }
        catch (LaunchException e) {
            return null;
        }
    }

    public Collection<Launch> getLaunches(Resource resource) throws LaunchException {
        TreeSet<Launch> launches = new TreeSet<Launch>();
        String query = "/jcr:root/content/launches//*[@sling:resourceType=\"wcm/launches/components/launch\"]";
        Iterator it = this.resolver.findResources(query, null);
        while (it.hasNext()) {
            Launch l = (Launch)((Resource)it.next()).adaptTo(Launch.class);
            if (l == null || resource != null && !l.containsResource(resource) && (l.getSourceRootResource() == null || !l.getSourceRootResource().getPath().equals(resource.getPath()))) continue;
            launches.add(l);
        }
        return launches;
    }

    public static LaunchSource findNearestLaunchSource(Resource inputResource, List<LaunchSource> launchSourceList) {
        LaunchSource startResourceSource = null;
        for (LaunchSource source : launchSourceList) {
            Resource sourceResource = source.getSourceRootResource();
            if (inputResource.getPath().indexOf(sourceResource.getPath()) == -1) continue;
            if (startResourceSource == null) {
                startResourceSource = source;
                continue;
            }
            if (sourceResource.getPath().length() <= startResourceSource.getSourceRootResource().getPath().length()) continue;
            startResourceSource = source;
        }
        return startResourceSource;
    }

    public void promoteLaunch(Launch launch, LaunchPromotionParameters params) throws LaunchException {
        ArrayList<LaunchSourceImpl> launchSources = launch.getLaunchSources();
        if (LaunchPromotionScope.RESOURCE == params.getPromotionScope() || LaunchPromotionScope.DEEP == params.getPromotionScope()) {
            LaunchSource nearestLaunch = LaunchManagerImpl.findNearestLaunchSource(params.getResource(), launchSources);
            launchSources = new ArrayList<LaunchSourceImpl>();
            String strResourcePath = params.getResource().getPath();
            String strLaunchPath = launch.getResource().getPath();
            Resource paramResource = params.getResource();
            if (strResourcePath.indexOf(strLaunchPath) == 0) {
                strResourcePath = strResourcePath.substring(strLaunchPath.length());
                paramResource = this.resolver.getResource(strResourcePath);
            }
            launchSources.add(new LaunchSourceImpl(paramResource, nearestLaunch.isDeep()));
        }
        for (LaunchSource source : launchSources) {
            this.promoteLaunchForEachPath(launch, params, source);
        }
    }

    private void promoteLaunchForEachPath(Launch launch, LaunchPromotionParameters params, LaunchSource launchSource) {
        boolean isDeepLaunchPromotion;
        boolean productionPageExists;
        String strLaunchPath = launch.getResource().getPath();
        String launchBasedPath = launchSource.getSourceRootResource().getPath();
        if (launchBasedPath.indexOf(strLaunchPath) == -1) {
            launchBasedPath = strLaunchPath + launchBasedPath;
        }
        Resource launchBasedResource = this.resolver.getResource(launchBasedPath);
        Launch target = params.getTarget();
        LaunchPromotionScope promotionScope = params.getPromotionScope();
        PageManager pageManager = (PageManager)this.resolver.adaptTo(PageManager.class);
        Resource productionResource = LaunchUtils.getTargetResource(launchBasedResource, target);
        Page productionPage = (Page)productionResource.adaptTo(Page.class);
        boolean bl = productionPageExists = productionPage != null;
        if (!productionPageExists && productionResource.getParent().adaptTo(Page.class) == null) {
            throw new LaunchException("Corresponding parent production page does not exist");
        }
        boolean bl2 = isDeepLaunchPromotion = LaunchPromotionScope.FULL.equals((Object)promotionScope) || LaunchPromotionScope.DEEP.equals((Object)promotionScope) || LaunchPromotionScope.SMART.equals((Object)promotionScope);
        if (isDeepLaunchPromotion && !launchSource.isDeep()) {
            isDeepLaunchPromotion = false;
        }
        ArrayList<LaunchResourceStatus> newStatuses = new ArrayList<LaunchResourceStatus>();
        ArrayList<Revision> revisions = new ArrayList<Revision>();
        if (productionPageExists) {
            String versionLabel = this.buildVersionLabel(launch);
            RangeIterator changedResources = this.getResourcesStatus(launch, launchBasedResource, promotionScope, target);
            while (changedResources.hasNext()) {
                LaunchResourceStatus status = (LaunchResourceStatus)changedResources.next();
                Page page = pageManager.getPage(status.getResourcePath());
                boolean bAllowed = true;
                if (promotionScope == LaunchPromotionScope.SMART && status.getType() == LaunchResourceStatus.LaunchStatusType.UNCHANGED) {
                    bAllowed = false;
                }
                if (!bAllowed) continue;
                if (page != null) {
                    this.addToWorkflowPackage(params.getResourceCollectionPath(), this.resolver.getResource(status.getResourcePath()));
                    try {
                        String versionComment = this.buildVersionComment(page, launch, params);
                        revisions.add(pageManager.createRevision(page, versionLabel, versionComment));
                        continue;
                    }
                    catch (WCMException e) {
                        throw new LaunchException("Unable to create a backup revision of [" + status.getResourcePath() + "] before promoting launch [" + launch.getResource().getPath() + "]", (Throwable)e);
                    }
                }
                newStatuses.add(status);
            }
        }
        LiveRelationshipManager liveRelationshipManager = (LiveRelationshipManager)this.resolver.adaptTo(LiveRelationshipManager.class);
        List<String> disabledLiveRelationships = null;
        try {
            String[] rolloutConfigs = (String[])launch.getResource().getChild("jcr:content").getValueMap().get("promoteRolloutConfigs", String[].class);
            disabledLiveRelationships = this.pauseOtherLiveRelationships(liveRelationshipManager, productionPage);
            Page launchBasedPage = (Page)launchBasedResource.adaptTo(Page.class);
            LinkedHashMap<String, ReverseLiveRelationship> reverseRels = new LinkedHashMap<String, ReverseLiveRelationship>();
            this.calculateReverseRelationships(reverseRels, launch, launchBasedPage, productionPage, isDeepLaunchPromotion, rolloutConfigs != null ? this.getRolloutConfigs(rolloutConfigs) : null, target);
            for (ReverseLiveRelationship r : reverseRels.values()) {
                this.rolloutManager.rollout(this.resolver, (LiveRelationship)r, true, false);
            }
            this.session.save();
            this.reenableOtherLiveRelationships(liveRelationshipManager, productionPage, disabledLiveRelationships);
            for (LaunchResourceStatus status : newStatuses) {
                Page page = pageManager.getPage(status.getResourcePath());
                if (page == null) continue;
                this.addToWorkflowPackage(params.getResourceCollectionPath(), this.resolver.getResource(status.getResourcePath()));
            }
            this.updateLastPromoted(launch);
        }
        catch (Exception e) {
            log.error("Unable to promote launch with {}", (Object)params.toString(), (Object)e);
            this.revertChanges();
            if (productionPageExists) {
                if (disabledLiveRelationships != null && !disabledLiveRelationships.isEmpty()) {
                    try {
                        disabledLiveRelationships = this.pauseOtherLiveRelationships(liveRelationshipManager, productionPage);
                    }
                    catch (RepositoryException re) {
                        log.error("Unable to pause existing live relationships for: {}", (Object)productionPage.getPath(), (Object)re);
                    }
                    catch (WCMException wcme) {
                        log.error("Unable to pause existing live relationships for: {}", (Object)productionPage.getPath(), (Object)wcme);
                    }
                }
                for (Revision revision : revisions) {
                    try {
                        pageManager.restore(revision.getParentPath(), revision.getId());
                    }
                    catch (WCMException wcme) {
                        log.error("Unable to restore revision {} [{}]", new Object[]{revision.getId(), revision.getParentPath(), wcme});
                    }
                }
                try {
                    this.reenableOtherLiveRelationships(liveRelationshipManager, productionPage, disabledLiveRelationships);
                }
                catch (RepositoryException re) {
                    log.error("Unable to reenable disabled live relationships: {}", disabledLiveRelationships, (Object)re);
                }
                catch (WCMException wcme) {
                    log.error("Unable to reenable disabled live relationships: {}", disabledLiveRelationships, (Object)wcme);
                }
            }
            throw new LaunchException("Unable to promote launch", (Throwable)e);
        }
    }

    private void createRevisionRecursively(Page page) throws WCMException {
        page.getPageManager().createRevision(page);
        Iterator children = page.listChildren();
        while (children.hasNext()) {
            this.createRevisionRecursively((Page)children.next());
        }
    }

    public void deleteLaunch(Launch launch) throws LaunchException {
        try {
            Resource launchResource = launch.getResource();
            Page page = (Page)launchResource.adaptTo(Page.class);
            this.createRevisionRecursively(page);
            Session session = (Session)launchResource.getResourceResolver().adaptTo(Session.class);
            session.removeItem(launchResource.getPath());
            session.save();
        }
        catch (WCMException e) {
            throw new LaunchException("Unable to delete launch, failed to create revision", (Throwable)e);
        }
        catch (RepositoryException e) {
            throw new LaunchException("Unable to delete launch", (Throwable)e);
        }
    }

    public Launch cloneLaunch(Launch launch, String cloneTitle, Calendar liveDate, boolean isLiveCopy) throws LaunchException {
        Launch clone;
        try {
            Resource launchResource = launch.getResource();
            PageManager pageManager = (PageManager)launchResource.getResourceResolver().adaptTo(PageManager.class);
            Resource cloneRes = pageManager.copy(launchResource, launchResource.getPath() + "_clone", launchResource.getName(), false, true, true);
            Node cloneNode = (Node)cloneRes.adaptTo(Node.class);
            if (cloneNode.hasNode("jcr:content")) {
                Node cloneContentNode = cloneNode.getNode("jcr:content");
                if (cloneTitle != null) {
                    cloneContentNode.setProperty("jcr:title", cloneTitle);
                }
                cloneContentNode.setProperty("liveDate", liveDate);
                cloneContentNode.setProperty("isLiveCopy", isLiveCopy);
                cloneContentNode.setProperty("lastPromoted", (Calendar)null);
                cloneContentNode.setProperty("lastPromotedBy", (String)null);
                if (cloneContentNode.hasProperty("isProductionReady")) {
                    cloneContentNode.getProperty("isProductionReady").remove();
                }
                cloneContentNode.getSession().save();
            }
            clone = (Launch)cloneRes.adaptTo(Launch.class);
        }
        catch (WCMException e) {
            throw new LaunchException("Unable to clone launch", (Throwable)e);
        }
        catch (RepositoryException e) {
            throw new LaunchException("Unable to reset clone launch status", (Throwable)e);
        }
        if (clone != null) {
            LiveRelationshipManager liveRelationshipManager = (LiveRelationshipManager)this.resolver.adaptTo(LiveRelationshipManager.class);
            if (isLiveCopy) {
                if (!launch.isLiveCopy()) {
                    try {
                        boolean isDeep = true;
                        LiveRelationship liveRelationship = liveRelationshipManager.getLiveRelationship(launch.getRootResource(), true);
                        if (liveRelationship != null) {
                            isDeep = liveRelationship.getLiveCopy().isDeep();
                        }
                        String[] sourceRolloutConfigs = (String[])launch.getResource().getChild("jcr:content").getValueMap().get("sourceRolloutConfigs", String[].class);
                        this.createLaunchLiveRelationship(clone, clone.getRootResource(), clone.getSourceRootResource(), isDeep, sourceRolloutConfigs);
                    }
                    catch (Exception e) {
                        try {
                            this.deleteLaunch(clone);
                        }
                        catch (LaunchException de) {
                            log.warn("Could not removed incomplete cloned launch [{}] after live relationship could not be established", (Object)clone.getResource().getPath(), (Object)de);
                        }
                        throw new LaunchException("Live relationship could not be set for cloned launch [" + clone.getResource().getPath() + "]");
                    }
                }
            } else if (liveRelationshipManager.hasLiveRelationship(clone.getRootResource())) {
                try {
                    liveRelationshipManager.endRelationship(clone.getRootResource(), true);
                }
                catch (WCMException e) {
                    throw new LaunchException("Could not remove live relationship for cloned launch [" + clone.getResource().getPath() + "]", (Throwable)e);
                }
            }
        }
        return clone;
    }

    public RangeIterator getResourcesStatus(Launch launch, Resource startResource, boolean isDeep) throws LaunchException {
        return this.getResourcesStatus(launch, startResource, isDeep, null);
    }

    public RangeIterator getResourcesStatus(Launch launch, Resource startResource, boolean isDeep, Launch target) throws LaunchException {
        LaunchPromotionScope scope = isDeep ? LaunchPromotionScope.DEEP : LaunchPromotionScope.RESOURCE;
        return this.getResourcesStatus(launch, startResource, scope, target);
    }

    public RangeIterator getResourcesStatus(Launch launch, Resource startResource, LaunchPromotionScope launchPromotionScope, Launch target) throws LaunchException {
        return new LaunchResourceStatusIterator(launch, startResource, launchPromotionScope, target);
    }

    private boolean isManuallyUpdated(Node node) {
        try {
            return node == null || !node.isNodeType("cq:LiveRelationship") || node.isNodeType("cq:LiveSyncCancelled");
        }
        catch (RepositoryException e) {
            throw new LaunchException("Unable to examine resource properties.", (Throwable)e);
        }
    }

    private boolean isResourceContentUpdated(Node contentNode) {
        try {
            NodeIterator children = contentNode.getNodes();
            while (children.hasNext()) {
                Node child = children.nextNode();
                if (this.isManuallyUpdated(child)) {
                    if (child.isNodeType("nt:file") || child.getName().equals("cq:LiveSyncConfig") && child.isNodeType("cq:LiveCopy")) {
                        return this.isResourceContentUpdated(child);
                    }
                    return true;
                }
                if (!child.hasNodes()) continue;
                return this.isResourceContentUpdated(child);
            }
        }
        catch (RepositoryException e) {
            throw new LaunchException("Unable to examine resource content.", (Throwable)e);
        }
        return false;
    }

    private List<RolloutConfig> getRolloutConfigs(String[] configPaths) throws WCMException {
        RolloutConfigManager rolloutConfigManager = (RolloutConfigManager)this.resolver.adaptTo(RolloutConfigManager.class);
        ArrayList<RolloutConfig> configs = new ArrayList<RolloutConfig>();
        for (String path : configPaths) {
            RolloutConfig config = rolloutConfigManager.getRolloutConfig(path);
            if (config == null) continue;
            configs.add(config);
        }
        return configs;
    }

    private void revertChanges() {
        try {
            if (this.session.hasPendingChanges()) {
                this.session.refresh(false);
            }
        }
        catch (RepositoryException e) {
            log.error("Unable to refresh session: ", (Throwable)e);
        }
    }

    private String buildVersionLabel(Launch launch) {
        StringBuilder sb = new StringBuilder();
        sb.append("launch").append(" ").append(launch.getResource().getName()).append(" ").append(System.currentTimeMillis());
        return JcrUtil.createValidName((String)sb.toString());
    }

    private String buildVersionComment(Page sourcePage, Launch launch, LaunchPromotionParameters params) {
        return "Created jcr:revision of [" + sourcePage.getPath() + "] before promoting launch [" + launch.getResource().getName() + "] using paramaters: " + params.toString();
    }

    private void createLaunchLiveRelationship(Launch launch, Resource launchRootResource, Resource srcResource, boolean isDeep, String[] sourceRolloutConfigs) throws LaunchException {
        try {
            LiveRelationshipManager liveRelationshipManager = (LiveRelationshipManager)this.resolver.adaptTo(LiveRelationshipManager.class);
            if (liveRelationshipManager.hasLiveRelationship(launchRootResource)) {
                liveRelationshipManager.endRelationship(launchRootResource, true);
                log.debug("Deleting existing live relationship between [{}] and [{}]", (Object)srcResource.getPath(), (Object)launchRootResource.getPath());
            }
            log.debug("Creating a live relationship between [{}] and [{}]", (Object)srcResource.getPath(), (Object)launchRootResource.getPath());
            LiveRelationship liveRelationship = liveRelationshipManager.establishRelationship((Page)srcResource.adaptTo(Page.class), (Page)launchRootResource.adaptTo(Page.class), isDeep, true, this.getRolloutConfigs(sourceRolloutConfigs).toArray((T[])new RolloutConfig[sourceRolloutConfigs.length]));
            log.debug("Launch [{}] now is a live copy of [{}]", (Object)launch.getResource().getPath(), (Object)srcResource.getPath());
            try {
                this.rolloutManager.rollout(this.resolver, liveRelationship, true);
            }
            catch (WCMException e) {
                log.warn("Could not perform initial rollout of launch [{}]", (Object)launch.getResource().getPath(), (Object)e);
            }
        }
        catch (Exception e) {
            throw new LaunchException("Live relationship could not be set for [" + launch.getResource().getPath() + "]", (Throwable)e);
        }
    }

    private void calculateReverseRelationships(Map<String, ReverseLiveRelationship> reverseRels, Launch launch, Page sourcePage, Page targetPage, boolean isDeep, List<RolloutConfig> rolloutConfigs, Launch target) throws RepositoryException {
        if (sourcePage != null) {
            Resource srcContentRes = sourcePage.getContentResource();
            if (srcContentRes != null) {
                this.calculateReverseRelationships(reverseRels, launch, srcContentRes, LaunchUtils.getTargetResource(srcContentRes, target), rolloutConfigs, target);
            }
            if (isDeep) {
                Iterator srcChildren = sourcePage.listChildren();
                while (srcChildren.hasNext()) {
                    Page srcChild = (Page)srcChildren.next();
                    Resource srcChildResource = (Resource)srcChild.adaptTo(Resource.class);
                    Resource targetChildResource = LaunchUtils.getTargetResource(srcChildResource, target);
                    Page targetChild = targetChildResource == null ? null : (Page)targetChildResource.adaptTo(Page.class);
                    this.calculateReverseRelationships(reverseRels, launch, srcChild, targetChild, isDeep, rolloutConfigs, target);
                }
            }
        }
        if (targetPage != null) {
            Resource targetContentRes = targetPage.getContentResource();
            if (targetContentRes != null) {
                this.calculateReverseRelationships(reverseRels, launch, LaunchUtils.getLaunchResource(launch, targetContentRes), targetContentRes, rolloutConfigs, target);
            }
            if (isDeep) {
                Iterator targetChildren = targetPage.listChildren();
                while (targetChildren.hasNext()) {
                    Page targetChild = (Page)targetChildren.next();
                    Resource targetChildRes = (Resource)targetChild.adaptTo(Resource.class);
                    Resource launchResourceChild = LaunchUtils.getLaunchResource(launch, targetChildRes);
                    Page launchPageChild = launchResourceChild == null ? null : (Page)launchResourceChild.adaptTo(Page.class);
                    this.calculateReverseRelationships(reverseRels, launch, launchPageChild, targetChild, isDeep, rolloutConfigs, target);
                }
            }
        }
    }

    private void calculateReverseRelationships(Map<String, ReverseLiveRelationship> reverseRels, Launch launch, Resource sourceRes, Resource targetRes, List<RolloutConfig> rolloutConfigs, Launch target) throws RepositoryException {
        Resource childResource;
        String targetResPath;
        sourceRes = sourceRes != null ? sourceRes : LaunchUtils.getLaunchResource(launch, targetRes);
        targetRes = targetRes != null ? targetRes : LaunchUtils.getTargetResource(sourceRes, target);
        String sourceResPath = sourceRes != null ? sourceRes.getPath() : LaunchUtils.getLaunchResourcePath(launch, targetRes);
        String string = targetResPath = targetRes != null ? targetRes.getPath() : LaunchUtils.getTargetResourcePath(sourceRes, target);
        if (!reverseRels.containsKey(targetResPath)) {
            boolean isSourceExisting;
            boolean bl = isSourceExisting = sourceRes != null;
            if (isSourceExisting && sourceRes.adaptTo(Page.class) == null) {
                isSourceExisting = !sourceRes.isResourceType("wcm/msm/components/ghost");
            }
            reverseRels.put(targetResPath, new ReverseLiveRelationship(launch, sourceResPath, isSourceExisting, targetResPath, targetRes != null, rolloutConfigs, false));
        }
        if (sourceRes != null) {
            Iterator srcChildren = sourceRes.listChildren();
            while (srcChildren.hasNext()) {
                childResource = (Resource)srcChildren.next();
                this.calculateReverseRelationships(reverseRels, launch, childResource, LaunchUtils.getTargetResource(childResource, target), rolloutConfigs, target);
            }
        }
        if (targetRes != null) {
            Iterator targetChildren = targetRes.listChildren();
            while (targetChildren.hasNext()) {
                childResource = (Resource)targetChildren.next();
                this.calculateReverseRelationships(reverseRels, launch, LaunchUtils.getLaunchResource(launch, childResource), childResource, rolloutConfigs, target);
            }
        }
    }

    private List<String> pauseOtherLiveRelationships(LiveRelationshipManager liveRelationshipManager, Page page) throws WCMException, RepositoryException {
        Collection allLiveRelationships;
        ArrayList<String> paths = new ArrayList<String>();
        if (page != null && (allLiveRelationships = liveRelationshipManager.getLiveRelationships(page, null, null, false)) != null) {
            for (LiveRelationship liveRelationship : allLiveRelationships) {
                if (!liveRelationship.getStatus().isTargetExisting()) continue;
                liveRelationshipManager.cancelRelationship(this.resolver, liveRelationship, liveRelationship.getLiveCopy().isDeep(), false);
                paths.add(liveRelationship.getTargetPath());
            }
            this.session.save();
        }
        return paths;
    }

    private void reenableOtherLiveRelationships(LiveRelationshipManager liveRelationshipManager, Page page, List<String> disabledTargets) throws WCMException, RepositoryException {
        Collection allLiveRelationships;
        if (page != null && (allLiveRelationships = liveRelationshipManager.getLiveRelationships(page, null, null, false)) != null) {
            for (LiveRelationship liveRelationship : allLiveRelationships) {
                if (disabledTargets == null || !disabledTargets.contains(liveRelationship.getTargetPath())) continue;
                liveRelationshipManager.reenableRelationship(this.resolver, liveRelationship, false);
            }
            this.session.save();
        }
    }

    private void addToWorkflowPackage(String rcPath, Resource resource) {
        if (StringUtils.isNotEmpty((String)rcPath)) {
            try {
                Node rcNode = this.session.getNode(rcPath);
                if (rcNode.hasNode("jcr:content/vlt:definition")) {
                    Node filterNode;
                    Node definitionNode = rcNode.getNode("jcr:content/vlt:definition");
                    if (definitionNode.hasNode("filter")) {
                        filterNode = definitionNode.getNode("filter");
                    } else {
                        filterNode = definitionNode.addNode("filter", "nt:unstructured");
                        filterNode.setProperty("sling:resourceType", "cq/workflow/components/collection/definition/resourcelist");
                    }
                    Node rcResNode = JcrUtil.createUniqueNode((Node)filterNode, (String)"resource", (String)"nt:unstructured", (Session)this.session);
                    rcResNode.setProperty("root", resource.getPath());
                    rcResNode.setProperty("sling:resourceType", "cq/workflow/components/collection/definition/resource");
                    this.session.save();
                }
            }
            catch (RepositoryException e) {
                log.warn("Unable to add resource [{}] to workflow package [{}]", new Object[]{resource.getPath(), rcPath, e});
            }
        }
    }

    private void updateLastPromoted(Launch launch) throws RepositoryException, LaunchException {
        Node launchNode = (Node)launch.getResource().adaptTo(Node.class);
        if (launchNode.hasNode("jcr:content")) {
            Node launchContentNode = launchNode.getNode("jcr:content");
            launchContentNode.setProperty("lastPromoted", Calendar.getInstance());
            launchContentNode.setProperty("lastPromotedBy", launch.getResource().getResourceResolver().getUserID());
            this.session.save();
        }
    }

    private List<Resource> getChildrenResources(Launch launch, Resource startResource, Launch target) {
        ArrayList<Resource> resources = new ArrayList<Resource>();
        Page startPage = (Page)startResource.adaptTo(Page.class);
        if (startPage == null) {
            return resources;
        }
        Iterator children = startResource.listChildren();
        resources.add(startResource);
        Resource productionRes = startResource.getPath().startsWith(launch.getResource().getPath()) ? LaunchUtils.getTargetResource(startResource, target) : startResource;
        Resource launchRes = LaunchUtils.getLaunchResource(launch, productionRes);
        if (launchRes != null) {
            Iterator launchResources = launchRes.listChildren();
            while (launchResources.hasNext()) {
                Resource launchProdChildRes;
                Resource launchChildRes = (Resource)launchResources.next();
                Page launchChildPage = (Page)launchChildRes.adaptTo(Page.class);
                if (launchChildRes.getPath().endsWith("jcr:content") || launchChildPage == null || (launchProdChildRes = LaunchUtils.getTargetResource(launchChildRes, target)) != null) continue;
                resources.add(launchChildRes);
            }
        }
        while (children.hasNext()) {
            Resource child = (Resource)children.next();
            Page childPage = (Page)child.adaptTo(Page.class);
            if (child.getPath().endsWith("jcr:content") || childPage == null) continue;
            resources.addAll(this.getChildrenResources(launch, child, target));
        }
        return resources;
    }

    private LaunchResourceStatus buildResourceStatus(Launch launch, Resource startResource, Launch target) {
        Resource productionResource;
        log.debug("Building status for resource:" + startResource.getPath());
        Page startPage = (Page)startResource.adaptTo(Page.class);
        Long launchLastSync = launch.getLastPromoted() != null ? launch.getLastPromoted().getTimeInMillis() : launch.getModified().getTimeInMillis();
        Resource resource = productionResource = startResource.getPath().startsWith(launch.getResource().getPath()) ? LaunchUtils.getTargetResource(startResource, target) : startResource;
        if (productionResource != null && launch.containsResource(productionResource)) {
            Resource launchRes = LaunchUtils.getLaunchResource(launch, productionResource);
            Page launchPage = (Page)launchRes.adaptTo(Page.class);
            Long lastLaunchPageModif = launchPage.getLastModified() != null ? launchPage.getLastModified().getTimeInMillis() : 0;
            Page productionPage = (Page)productionResource.adaptTo(Page.class);
            Long lastProdPageModif = productionPage.getLastModified() != null ? productionPage.getLastModified().getTimeInMillis() : 0;
            if (launch.isLiveCopy()) {
                Node jcrContentNode;
                Calendar lastRootLaunchResRolCal = (Calendar)((Page)launch.getRootResource().adaptTo(Page.class)).getProperties().get("cq:lastRolledout", Calendar.class);
                Calendar lastLaunchPageRolCal = (Calendar)launchPage.getProperties().get("cq:lastRolledout", (Object)lastRootLaunchResRolCal);
                Long lastLaunchPageRol = lastLaunchPageRolCal != null ? lastLaunchPageRolCal.getTimeInMillis() : 0;
                if (lastLaunchPageRol == 0) {
                    HierarchyNodeInheritanceValueMap inheritanceMap = new HierarchyNodeInheritanceValueMap((Resource)launchPage.adaptTo(Resource.class));
                    lastLaunchPageRolCal = (Calendar)inheritanceMap.getInherited("cq:lastRolledout", (Object)lastRootLaunchResRolCal);
                    lastLaunchPageRol = lastLaunchPageRolCal != null ? lastLaunchPageRolCal.getTimeInMillis() : 0;
                }
                if (lastLaunchPageModif > lastLaunchPageRol || lastProdPageModif > lastLaunchPageRol) {
                    return LaunchResourceStatusHelper.buildModifiedResStatus(productionResource.getPath(), this.getPageTitle(productionPage), launchPage.getLastModified(), productionPage.getLastModified(), launchPage.getLastModifiedBy(), productionPage.getLastModifiedBy());
                }
                log.debug("Retrieving not synchronized child nodes of page: " + launchRes.getPath());
                Resource contentRes = launchRes.getChild("jcr:content");
                if (contentRes != null && this.isResourceContentUpdated(jcrContentNode = (Node)contentRes.adaptTo(Node.class))) {
                    return LaunchResourceStatusHelper.buildModifiedResStatus(productionResource.getPath(), this.getPageTitle(productionPage), launchPage.getLastModified(), productionPage.getLastModified(), launchPage.getLastModifiedBy(), productionPage.getLastModifiedBy());
                }
                return LaunchResourceStatusHelper.buildUnchangedResStatus(productionResource.getPath(), this.getPageTitle(productionPage), launchPage.getLastModified(), productionPage.getLastModified(), launchPage.getLastModifiedBy(), productionPage.getLastModifiedBy());
            }
            if (lastLaunchPageModif - launchLastSync >= TIME_DELTA || lastProdPageModif > launchLastSync) {
                return LaunchResourceStatusHelper.buildModifiedResStatus(productionResource.getPath(), this.getPageTitle(productionPage), launchPage.getLastModified(), productionPage.getLastModified(), launchPage.getLastModifiedBy(), productionPage.getLastModifiedBy());
            }
            return LaunchResourceStatusHelper.buildUnchangedResStatus(productionResource.getPath(), this.getPageTitle(productionPage), launchPage.getLastModified(), productionPage.getLastModified(), launchPage.getLastModifiedBy(), productionPage.getLastModifiedBy());
        }
        if (productionResource == null) {
            Calendar modifDateCal = startPage.getLastModified() != null ? startPage.getLastModified() : (Calendar)startPage.getProperties().get("jcr:created", Calendar.class);
            String userId = startPage.getLastModifiedBy() != null ? startPage.getLastModifiedBy() : (String)startPage.getProperties().get("jcr:createdBy", String.class);
            return LaunchResourceStatusHelper.buildCreatedResStatus(startResource.getPath().substring(launch.getResource().getPath().length()), this.getPageTitle(startPage), modifDateCal, userId);
        }
        Page productionPage = (Page)productionResource.adaptTo(Page.class);
        Calendar prodModifDateCal = productionPage.getLastModified();
        String prodUserId = productionPage.getLastModifiedBy();
        return LaunchResourceStatusHelper.buildDeletedResStatus(productionResource.getPath(), this.getPageTitle(productionPage), prodModifDateCal, prodUserId);
    }

    private String getPageTitle(Page page) {
        String title = page.getTitle();
        if (title == null || title.equals("")) {
            title = page.getName();
        }
        return title;
    }

    public Launch updateLaunchSources(Launch launch, List<LaunchSource> newList) throws LaunchException {
        List originalList = launch.getLaunchSources();
        ArrayList<LaunchSource> addList = new ArrayList<LaunchSource>();
        ArrayList<LaunchSource> deleteList = new ArrayList<LaunchSource>();
        ArrayList<LaunchSource> updateList = new ArrayList<LaunchSource>();
        this.calculateUpdatedListSet(originalList, newList, addList, deleteList, updateList);
        Page launchPage = (Page)launch.getResource().adaptTo(Page.class);
        PageManager pageManager = launchPage.getPageManager();
        Resource launchPageResource = (Resource)launchPage.adaptTo(Resource.class);
        for (LaunchSource source : deleteList) {
            this.deleteLaunchSource(launch, source);
        }
        for (LaunchSource launchSource : addList) {
            Resource sourceRootResource = launchSource.getSourceRootResource();
            Page sourceRootPage = (Page)sourceRootResource.adaptTo(Page.class);
            LaunchManagerImpl.addLaunchSourceToExistingLaunch(this.resolver, launchPageResource, sourceRootResource, sourceRootPage, pageManager, launchPage, launchSource, null);
        }
        if (launch.isLiveCopy()) {
            try {
                Property property;
                String[] sourceRolloutConfigs = null;
                Node launchContentNode = (Node)launchPage.getContentResource().adaptTo(Node.class);
                if (launchContentNode.hasProperty("sourceRolloutConfigs") && (property = launchContentNode.getProperty("sourceRolloutConfigs")).isMultiple()) {
                    Value[] values = property.getValues();
                    sourceRolloutConfigs = new String[values.length];
                    for (int index = 0; index < values.length; ++index) {
                        sourceRolloutConfigs[index] = values[index].getString();
                    }
                }
                for (LaunchSource launchSource2 : addList) {
                    Resource inputResource = launchSource2.getSourceRootResource();
                    String inputResourcePath = inputResource.getPath();
                    inputResourcePath = inputResourcePath.substring(1);
                    Resource rootResource = launch.getResource().getChild(inputResourcePath);
                    this.createLaunchLiveRelationship(launch, rootResource, inputResource, launchSource2.isDeep(), sourceRolloutConfigs);
                }
            }
            catch (RepositoryException e) {
                log.error("Error while creating live copy {}", (Throwable)e);
                throw new LaunchException("Unable to edit launch page", (Throwable)e);
            }
        }
        Node launchContentNode = (Node)launchPage.getContentResource().adaptTo(Node.class);
        LaunchManagerImpl.updateSourceListInLaunch(launchContentNode, newList, this.session);
        return this.getLaunch(launch.getResource().getPath());
    }

    public static void updateSourceListInLaunch(Node launchContentNode, List<LaunchSource> inputLaunchResourceList, Session session) throws LaunchException {
        try {
            if (launchContentNode.hasNode("sources")) {
                Node sourcesNode = launchContentNode.getNode("sources");
                sourcesNode.remove();
            }
            Node parentNode = launchContentNode.addNode("sources", "nt:unstructured");
            for (int index = 0; index < inputLaunchResourceList.size(); ++index) {
                LaunchSource launchSource = inputLaunchResourceList.get(index);
                String strNodeName = String.format("source_%d", index);
                Node childNode = parentNode.addNode(strNodeName, "nt:unstructured");
                childNode.setProperty("isDeep", launchSource.isDeep());
                childNode.setProperty("sourceRootResource", launchSource.getSourceRootResource().getPath());
            }
            session.save();
        }
        catch (RepositoryException ex) {
            throw new LaunchException("Unable to update launch source list", (Throwable)ex);
        }
    }

    private static Resource addLaunchSourceToExistingLaunch(ResourceResolver resolver, Resource launchPageResource, Resource sourceRootResource, Page sourceRootPage, PageManager pageManager, Page launchPage, LaunchSource launchSource, String template) {
        Resource launchDestinationResource;
        block8 : {
            Resource inputResource = launchSource.getSourceRootResource();
            boolean inputResourceIsDeep = launchSource.isDeep();
            String destinationPath = Text.makeCanonicalPath((String)(launchPage.getPath() + "/" + inputResource.getPath()));
            String currentPath = launchPageResource.getPath();
            StringTokenizer pathElems = new StringTokenizer(inputResource.getPath(), "/");
            while (pathElems.hasMoreTokens() && !(currentPath = currentPath + "/" + pathElems.nextToken()).equals(destinationPath)) {
                if (resolver.getResource(currentPath) != null) continue;
                String currentName = Text.getName((String)currentPath);
                try {
                    pageManager.create(Text.getRelativeParent((String)currentPath, (int)1), currentName, "/libs/launches/templates/outofscope", currentName).adaptTo(Resource.class);
                    continue;
                }
                catch (WCMException e) {
                    throw new LaunchException("Unable to create intermediate launch path: " + currentPath, (Throwable)e);
                }
            }
            launchDestinationResource = null;
            try {
                if (template == null) {
                    PageManager.CopyOptions options = new PageManager.CopyOptions();
                    options.resource = inputResource;
                    options.destination = destinationPath;
                    options.shallow = !inputResourceIsDeep;
                    options.autoSave = true;
                    options.adjustReferences = inputResourceIsDeep;
                    launchDestinationResource = pageManager.copy(options);
                    LaunchManagerImpl.cleanCopyRelationship((LiveRelationshipManager)resolver.adaptTo(LiveRelationshipManager.class), launchDestinationResource, inputResourceIsDeep);
                    try {
                        resolver.commit();
                        break block8;
                    }
                    catch (PersistenceException e) {
                        LaunchManagerImpl.rollbackLaunchResource(resolver, launchDestinationResource);
                        throw new LaunchException("Unable to create launch page at: " + destinationPath, (Throwable)e);
                    }
                }
                String parentPath = launchPage.getPath() + sourceRootResource.getParent().getPath();
                launchDestinationResource = (Resource)pageManager.create(parentPath, sourceRootPage.getName(), template, sourceRootPage.getTitle()).adaptTo(Resource.class);
            }
            catch (WCMException e) {
                throw new LaunchException("Unable to create launch page at: " + destinationPath, (Throwable)e);
            }
        }
        return launchDestinationResource;
    }

    private static void rollbackLaunchResource(ResourceResolver resourceResolver, Resource launchResource) throws LaunchException {
        if (resourceResolver != null && launchResource != null) {
            try {
                resourceResolver.delete(launchResource);
                resourceResolver.commit();
            }
            catch (PersistenceException e) {
                throw new LaunchException("Unable to create launch page at: " + launchResource.getPath(), (Throwable)e);
            }
        }
    }

    private static void cleanCopyRelationship(LiveRelationshipManager liveRelationshipManager, Resource resource, boolean bDeep) throws WCMException {
        boolean bCleanupDone = false;
        if (liveRelationshipManager != null) {
            Iterable childList;
            if (resource.adaptTo(Page.class) != null && liveRelationshipManager.hasLiveRelationship(resource)) {
                liveRelationshipManager.endRelationship(resource, false);
                log.debug("Deleting existing live relationship [{}]", (Object)resource.getPath());
                bCleanupDone = true;
            }
            if (!bCleanupDone && bDeep && (childList = resource.getChildren()) != null) {
                for (Resource child : childList) {
                    LaunchManagerImpl.cleanCopyRelationship(liveRelationshipManager, child, bDeep);
                }
            }
        }
    }

    private void deleteLaunchSource(Launch launch, LaunchSource source) throws LaunchException {
        Node sourceNode;
        Resource rootResource = launch.getResource();
        String strPath = source.getSourceRootResource().getPath();
        Resource sourceLaunchResource = rootResource.getChild(strPath = strPath.substring(1));
        if (sourceLaunchResource != null && (sourceNode = (Node)sourceLaunchResource.adaptTo(Node.class)) != null) {
            try {
                sourceNode.remove();
            }
            catch (RepositoryException ex) {
                throw new LaunchException("Unable to delete launch source list", (Throwable)ex);
            }
        }
    }

    private void calculateUpdatedListSet(List<LaunchSource> originalList, List<LaunchSource> latestList, List<LaunchSource> addList, List<LaunchSource> deleteList, List<LaunchSource> updateList) {
        for (LaunchSource source : originalList) {
            LaunchSource newOne = this.findLaunchSourceFromList(latestList, source);
            if (newOne == null) {
                deleteList.add(source);
                continue;
            }
            if (this.isLaunchSourceEquals(source, newOne)) continue;
            updateList.add(newOne);
        }
        for (LaunchSource newOne : latestList) {
            if (this.findLaunchSourceFromList(originalList, newOne) != null) continue;
            addList.add(newOne);
        }
    }

    private boolean isLaunchSourceEquals(LaunchSource source, LaunchSource newOne) {
        if (source != null && newOne != null) {
            return source.isDeep() == newOne.isDeep() && source.getSourceRootResource().getPath().equals(newOne.getSourceRootResource().getPath());
        }
        return false;
    }

    private LaunchSource findLaunchSourceFromList(List<LaunchSource> sourceList, LaunchSource source) {
        for (LaunchSource item : sourceList) {
            if (!item.getSourceRootResource().getPath().equals(source.getSourceRootResource().getPath())) continue;
            return item;
        }
        return null;
    }

    private class LaunchResourceStatusIterator
    implements RangeIterator {
        private final Launch launch;
        private final LaunchPromotionScope launchPromotionScope;
        private final Launch target;
        private ListIterator<Resource> childNodes;
        private LaunchResourceStatus next;
        private long pos;
        private int size;

        private LaunchResourceStatusIterator(Launch launch, Resource startResource, LaunchPromotionScope launchPromotionScope, Launch target) throws LaunchException {
            this.pos = -1;
            this.size = -1;
            if (launch == null) {
                throw new IllegalArgumentException("Launch must not be null");
            }
            this.launch = launch;
            this.launchPromotionScope = launchPromotionScope;
            this.target = target;
            ArrayList<LaunchSourceImpl> launchSourceList = launch.getLaunchSources();
            if (!this.isFull() && !this.isSmart()) {
                boolean bResourceDeep = launch.isDeep();
                LaunchSource startResourceSource = null;
                if (startResource == null) {
                    startResourceSource = (LaunchSource)launchSourceList.get(0);
                    startResource = ((LaunchSource)launchSourceList.get(0)).getSourceRootResource();
                } else {
                    startResourceSource = LaunchManagerImpl.findNearestLaunchSource(startResource, launchSourceList);
                }
                launchSourceList = new ArrayList<LaunchSourceImpl>();
                if (startResourceSource != null) {
                    bResourceDeep = startResourceSource.isDeep();
                }
                launchSourceList.add(new LaunchSourceImpl(startResource, bResourceDeep));
            }
            ArrayList<Resource> totalChildList = new ArrayList<Resource>();
            ArrayList<String> resourcePathList = new ArrayList<String>();
            for (LaunchSource launchSource : launchSourceList) {
                List candidates;
                Resource existingResource = launchSource.getSourceRootResource();
                if (!this.isResourceScope() && launchSource.isDeep()) {
                    candidates = LaunchManagerImpl.this.getChildrenResources(launch, existingResource, target);
                } else {
                    candidates = new ArrayList<Resource>();
                    if (existingResource != null) {
                        candidates.add((Resource)existingResource);
                    } else {
                        Resource launchResource = LaunchUtils.getLaunchResource(launch, existingResource);
                        if (launchResource != null) {
                            candidates.add(launchResource);
                        }
                    }
                }
                for (Resource resourceTemp : candidates) {
                    if (resourcePathList.contains(resourceTemp.getPath())) continue;
                    resourcePathList.add(resourceTemp.getPath());
                    totalChildList.add(resourceTemp);
                }
            }
            this.childNodes = totalChildList.listIterator();
            this.seek();
        }

        private boolean isResourceScope() {
            return this.launchPromotionScope == LaunchPromotionScope.RESOURCE;
        }

        private boolean isSmart() {
            return this.launchPromotionScope == LaunchPromotionScope.SMART;
        }

        private boolean isFull() {
            return this.launchPromotionScope == LaunchPromotionScope.FULL;
        }

        private void seek() throws LaunchException {
            while (this.next == null && this.childNodes.hasNext()) {
                Resource currentRes = this.childNodes.next();
                LaunchResourceStatus status = LaunchManagerImpl.this.buildResourceStatus(this.launch, currentRes, this.target);
                if (status != null && status.getType() == LaunchResourceStatus.LaunchStatusType.UNCHANGED && this.isSmart()) {
                    status = null;
                }
                this.next = status;
                ++this.pos;
            }
        }

        public void skip(long skipNum) {
            for (long i = 0; i < skipNum; ++i) {
                this.next();
            }
        }

        public long getSize() {
            return this.size;
        }

        public long getPosition() {
            return this.pos;
        }

        public boolean hasNext() {
            return this.next != null;
        }

        public Object next() {
            if (this.next == null) {
                throw new NoSuchElementException("Call hasNext first");
            }
            LaunchResourceStatus status = this.next;
            this.next = null;
            this.seek();
            return status;
        }

        public void remove() {
            throw new UnsupportedOperationException("Result can not be modified");
        }
    }

    private class CreateLaunch
    implements Launch {
        private Resource sourceRootResource;
        private List<LaunchSource> inputLaunchResourceList;
        private String title;
        private String name;
        private Calendar liveDate;
        private boolean isDeep;
        private boolean isLiveCopy;
        private String template;
        private Resource launchPageResource;
        private Resource rootResource;
        private String[] sourceRolloutConfigs;
        private String[] promoteRolloutConfigs;

        CreateLaunch(Resource sourceRootResource, List<LaunchSource> inputLaunchResourceList, String title, Calendar liveDate, boolean isDeep, boolean isLiveCopy, String template, String[] sourceRolloutConfigs, String[] promoteRolloutConfigs) {
            this.rootResource = null;
            this.sourceRootResource = sourceRootResource;
            this.inputLaunchResourceList = inputLaunchResourceList;
            if (this.inputLaunchResourceList == null) {
                this.inputLaunchResourceList = new ArrayList<LaunchSource>();
            }
            if (this.inputLaunchResourceList.isEmpty()) {
                this.inputLaunchResourceList.add(new LaunchSourceImpl(sourceRootResource, isDeep));
            }
            this.title = title;
            this.name = JcrUtil.createValidName((String)title);
            this.liveDate = liveDate;
            this.isDeep = isDeep;
            this.isLiveCopy = isLiveCopy;
            this.template = template;
            this.sourceRolloutConfigs = sourceRolloutConfigs;
            this.promoteRolloutConfigs = promoteRolloutConfigs;
        }

        public Resource getResource() {
            return this.launchPageResource;
        }

        public Resource getRootResource() {
            return this.rootResource;
        }

        public Resource getSourceRootResource() {
            return this.sourceRootResource;
        }

        public String getTitle() {
            return this.title;
        }

        public Calendar getLiveDate() {
            return this.liveDate;
        }

        public boolean isProductionReady() {
            throw new UnsupportedOperationException("not implemented");
        }

        public boolean isLiveCopy() {
            return this.isLiveCopy;
        }

        public boolean isDeep() {
            return this.isDeep;
        }

        public Calendar getCreated() {
            throw new UnsupportedOperationException("not implemented");
        }

        public String getCreatedBy() {
            throw new UnsupportedOperationException("not implemented");
        }

        public Calendar getModified() {
            throw new UnsupportedOperationException("not implemented");
        }

        public String getModifiedBy() {
            throw new UnsupportedOperationException("not implemented");
        }

        public Calendar getLastPromoted() {
            throw new UnsupportedOperationException("not implemented");
        }

        public String getLastPromotedBy() {
            throw new UnsupportedOperationException("not implemented");
        }

        public boolean containsResource(Resource productionResource) {
            throw new UnsupportedOperationException("not implemented");
        }

        public int compareTo(Launch launch) {
            return 0;
        }

        void write(Session session) throws RepositoryException, LaunchException {
            String rootPath;
            Page sourceRootPage;
            Page launchPage;
            if (this.inputLaunchResourceList != null && !this.inputLaunchResourceList.isEmpty()) {
                Collections.sort(this.inputLaunchResourceList, new Comparator<LaunchSource>(){

                    @Override
                    public int compare(LaunchSource o1, LaunchSource o2) {
                        return o1.getSourceRootResource().getPath().compareTo(o2.getSourceRootResource().getPath());
                    }
                });
                this.sourceRootResource = this.inputLaunchResourceList.get(0).getSourceRootResource();
                this.isDeep = this.inputLaunchResourceList.get(0).isDeep();
            }
            if (!session.nodeExists(rootPath = "/content/launches/" + new SimpleDateFormat("yyyy/MM/dd").format(new Date()))) {
                JcrUtil.createPath((String)rootPath, (String)"sling:Folder", (Session)session);
                session.save();
            }
            if ((sourceRootPage = (Page)this.sourceRootResource.adaptTo(Page.class)) == null) {
                throw new LaunchException("Source root resource is not a page: " + this.sourceRootResource.getPath());
            }
            PageManager pageManager = sourceRootPage.getPageManager();
            try {
                launchPage = pageManager.create(rootPath, this.name, null, this.title);
            }
            catch (WCMException e) {
                throw new LaunchException("Unable to create launch page", (Throwable)e);
            }
            this.launchPageResource = (Resource)launchPage.adaptTo(Resource.class);
            Node launchContentNode = (Node)launchPage.getContentResource().adaptTo(Node.class);
            launchContentNode.setProperty("sling:resourceType", "wcm/launches/components/launch");
            launchContentNode.setProperty("isLiveCopy", this.isLiveCopy());
            Calendar liveDate = this.getLiveDate();
            if (liveDate != null) {
                launchContentNode.setProperty("liveDate", liveDate);
            }
            launchContentNode.setProperty("sourceRootResource", this.getLeastCommonAncestorPath());
            launchContentNode.setProperty("isDeep", this.isDeep);
            if (this.sourceRolloutConfigs != null) {
                launchContentNode.setProperty("sourceRolloutConfigs", this.sourceRolloutConfigs);
            }
            if (this.promoteRolloutConfigs != null) {
                launchContentNode.setProperty("promoteRolloutConfigs", this.promoteRolloutConfigs);
            }
            if (this.template != null) {
                launchContentNode.setProperty("template", this.template);
            }
            this.updateSourceListInLaunch(launchContentNode);
            session.save();
            for (LaunchSource launchSource : this.inputLaunchResourceList) {
                Resource launchDestinationResource = LaunchManagerImpl.addLaunchSourceToExistingLaunch(LaunchManagerImpl.this.resolver, this.launchPageResource, this.sourceRootResource, sourceRootPage, pageManager, launchPage, launchSource, this.template);
                if (this.rootResource != null) continue;
                this.rootResource = launchDestinationResource;
            }
        }

        private void updateSourceListInLaunch(Node launchContentNode) throws RepositoryException {
            LaunchManagerImpl.updateSourceListInLaunch(launchContentNode, this.inputLaunchResourceList, LaunchManagerImpl.this.session);
        }

        private String getLeastCommonAncestorPath() {
            String strRetVal = null;
            if (this.inputLaunchResourceList != null && this.inputLaunchResourceList.size() > 0) {
                Resource currentTopParent = null;
                for (LaunchSource launchSource : this.inputLaunchResourceList) {
                    currentTopParent = this.findCommonParent(currentTopParent, launchSource.getSourceRootResource());
                }
                strRetVal = currentTopParent.getPath();
            } else {
                strRetVal = this.sourceRootResource.getPath();
            }
            return strRetVal;
        }

        private Resource findCommonParent(Resource currentTopParent, Resource resource) {
            if (currentTopParent == null) {
                return resource;
            }
            if (resource != null) {
                do {
                    if (resource.getPath().indexOf(currentTopParent.getPath()) == -1) continue;
                    return currentTopParent;
                } while ((currentTopParent = currentTopParent.getParent()) != null);
            }
            return null;
        }

        public List<LaunchSource> getLaunchSources() {
            return this.inputLaunchResourceList;
        }

    }

}