Cq62WorkflowContentUpgrade.java 27.7 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
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.granite.security.user.UserManagementService
 *  javax.jcr.AccessDeniedException
 *  javax.jcr.Item
 *  javax.jcr.ItemNotFoundException
 *  javax.jcr.Node
 *  javax.jcr.NodeIterator
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Session
 *  javax.jcr.Workspace
 *  javax.jcr.nodetype.NodeType
 *  javax.jcr.query.Query
 *  javax.jcr.query.QueryManager
 *  javax.jcr.query.QueryResult
 *  javax.jcr.version.Version
 *  javax.jcr.version.VersionManager
 *  org.apache.jackrabbit.util.Text
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.day.cq.compat.codeupgrade.impl.cq62;

import com.adobe.granite.security.user.UserManagementService;
import com.day.cq.compat.codeupgrade.impl.UpgradeUtil;
import com.day.cq.compat.codeupgrade.internal.api.ProgressInfoProvider;
import com.day.jcr.diff.Diff;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.jcr.AccessDeniedException;
import javax.jcr.Item;
import javax.jcr.ItemNotFoundException;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.nodetype.NodeType;
import javax.jcr.query.Query;
import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
import javax.jcr.version.Version;
import javax.jcr.version.VersionManager;
import org.apache.jackrabbit.util.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class Cq62WorkflowContentUpgrade
implements ProgressInfoProvider {
    private final Logger log;
    private String progressInfo;
    private static final String WORKFLOW_MODEL_PATH_SUFFIX = "/jcr:content/model";
    private static final int SAVE_THRESHOLD = 100;
    private static final String PN_STATUS = Cq62WorkflowContentUpgrade.class.getSimpleName();
    private static final String STATUS_PATH = "/var/upgrade/status/";
    private static final String STATUS_PROPERTY_PATH = "/var/upgrade/status/" + PN_STATUS;
    private static final String NN_BACKUP_LOG = PN_STATUS + ".log";
    private static final String[] MODEL_EXCLUDES = new String[]{"jcr:baseVersion", "jcr:versionHistory", "jcr:uuid", "jcr:isCheckedOut", "jcr:predecessors"};
    private static final String[] FILE_EXCLUDES = new String[]{"jcr:lastModified", "jcr:lastModifiedBy", "jcr:uuid", "jcr:created"};
    private static final String NT_WORKFLOW = "cq:Workflow";
    private static final String NT_WORKFLOW_MODEL = "cq:WorkflowModel";
    private static final String NT_WORKFLOW_LAUNCHER = "cq:WorkflowLauncher";
    private static final String PN_SLING_RESOURCE_TYPE = "sling:resourceType";
    private static final String PN_TAGS = "tags";
    private static final String NN_METADATA = "metaData";
    private static final String NN_SCRIPTS = "scripts";
    private static final String NN_NOTIFICATION = "notification";
    private static final String NN_INSTANCES = "instances";
    private static final String NN_MODELS = "models";
    private static final String NN_ITEM_STORE = "workItems";
    private static final String NN_LAUNCHER_CONFIG = "launcher/config";
    private static final String NN_WCM_TPLS = "wcm/templates";
    private static final String MODEL_RES_TYPE = "cq/workflow/components/model";
    private static final String Q_ALL_WORKITEMS = "//element(*, cq:WorkItem)";
    private static final String Q_ALL_UPGRADED_WORKITEMS = "/jcr:root/var/upgrade//element(*, cq:WorkItem)";
    private static final String WORKFLOW_PATH = "/etc/workflow";
    private static final String BACKUP_ROOT = "/var/upgrade";
    private static final String BACKUP_PATH = "/var/upgrade/WorkflowPreserveContentHook";
    private static String userHomeDir = null;
    private static final Map<String, LauncherConfig> LAUNCHER_CFG_MAPPING = new HashMap<String, LauncherConfig>(){
        private static final long serialVersionUID = -6020044206570715266L;

        @Override
        public LauncherConfig put(String key, LauncherConfig value) {
            if (this.containsKey(key)) {
                throw new RuntimeException("Key '" + key + "' already exists, invalid LAUNCHER_CFG_MAPPING");
            }
            return HashMap.super.put(key, value);
        }
    };

    Cq62WorkflowContentUpgrade(UserManagementService userManagementService) {
        this.log = LoggerFactory.getLogger(this.getClass());
        this.progressInfo = "No info yet";
        userHomeDir = userManagementService.getUserRootPath();
    }

    @Override
    public String getProgressInfo() {
        return this.progressInfo;
    }

    void setProgressInfo(String info) {
        this.progressInfo = info;
        this.log.info(this.progressInfo);
    }

    void doUpgrade(Session sess) throws Exception {
        if (!sess.itemExists("/var/upgrade/WorkflowPreserveContentHook")) {
            this.setProgressInfo("/var/upgrade/WorkflowPreserveContentHook does not exist, no workflow content to upgrade");
            return;
        }
        Node backup = (Node)sess.getItem("/var/upgrade/WorkflowPreserveContentHook");
        String upgradePhase = "";
        try {
            Map<Node, Node> launcherConflicts;
            Map<Node, Node> models;
            Map<Node, Node> tplConflicts;
            Node conflict;
            upgradePhase = "Migrating workflow models";
            this.setProgressInfo(upgradePhase);
            Map modelConflicts = new HashMap();
            if (backup.hasNode("models") && !(modelConflicts = this.copyModelBackup(sess, models = this.adaptModelBackup(backup.getNode("models"), sess))).keySet().isEmpty()) {
                Iterator cIt = modelConflicts.keySet().iterator();
                this.log.info("Please check the following models in backup. They were either customized or have changed in the new version.");
                while (cIt.hasNext()) {
                    conflict = (Node)cIt.next();
                    this.log.info("C {}", (Object)conflict.getPath());
                }
            }
            upgradePhase = "Migrating workflow launcher configurations";
            this.setProgressInfo(upgradePhase);
            if (backup.hasNode("launcher/config") && !(launcherConflicts = this.checkLauncherConfigs(backup.getNode("launcher/config"), modelConflicts)).keySet().isEmpty()) {
                Iterator<Node> lcIt = launcherConflicts.keySet().iterator();
                this.log.info("Please check the following launcher configs. They were either changed or refering to conflicting workflows.");
                while (lcIt.hasNext()) {
                    conflict = lcIt.next();
                    this.log.info("C {}", (Object)conflict.getPath());
                }
            }
            upgradePhase = "Migrating WCM templates";
            this.setProgressInfo(upgradePhase);
            if (backup.hasNode("wcm/templates") && !(tplConflicts = this.copyWcmTemplates(backup.getNode("wcm/templates"), modelConflicts)).keySet().isEmpty()) {
                Iterator<Node> tplcIt = tplConflicts.keySet().iterator();
                this.log.info("Please check the following auto assign configs. They are refering to conflicting workflows.", (Object)"");
                while (tplcIt.hasNext()) {
                    conflict = tplcIt.next();
                    this.log.info("C {}", (Object)conflict.getPath());
                }
            }
        }
        catch (Exception e) {
            this.log.error("Exception in " + upgradePhase + ":", (Throwable)e);
        }
        try {
            this.setProgressInfo("Migrating workflow scripts");
            if (backup.hasNode("scripts")) {
                this.checkAndCopyFiles(backup.getNode("scripts"));
            }
        }
        catch (Exception e) {
            this.log.error("Exception merge scripts phase", (Throwable)e);
        }
        try {
            this.setProgressInfo("Migrating notification templates");
            if (backup.hasNode("notification")) {
                this.checkAndCopyFiles(backup.getNode("notification"));
            }
        }
        catch (Exception e) {
            this.log.error("Exception in merge notification templates phase", (Throwable)e);
        }
        try {
            this.setProgressInfo("Migrating workflow instances");
            String serverInstancePath = "/etc/workflow/instances/server0";
            if (sess.itemExists(serverInstancePath)) {
                Node serverInstanceNode = (Node)sess.getItem(serverInstancePath);
                serverInstanceNode.remove();
                sess.save();
            }
            Workspace workspace = sess.getWorkspace();
            int processed = 0;
            String backupPath = "/var/upgrade/WorkflowPreserveContentHook/instances";
            Node backupInstances = (Node)sess.getItem(backupPath);
            NodeIterator instanceNodes = backupInstances.getNodes();
            while (instanceNodes.hasNext()) {
                Node bucket = instanceNodes.nextNode();
                if (!bucket.hasNodes() || !"sling:Folder".equals(bucket.getPrimaryNodeType().getName())) continue;
                String backupSourcePath = bucket.getPath();
                int idx = backupSourcePath.lastIndexOf(47);
                String bucketName = backupSourcePath.substring(idx + 1);
                String targetPath = "/etc/workflow/instances/" + bucketName;
                workspace.move(backupSourcePath, targetPath);
                processed = this.checkSaveThreshold(sess, processed);
            }
            this.save(sess);
        }
        catch (Exception e) {
            this.log.error("Exception in copy instances phase", (Throwable)e);
        }
        try {
            Node workItem;
            Property prop;
            this.setProgressInfo("Migrating inbox items");
            this.setProgressInfo("Migrating inbox items - change resource type");
            QueryManager qm = sess.getWorkspace().getQueryManager();
            Query q = qm.createQuery("//element(*, cq:WorkItem)", "xpath");
            int processed = 0;
            NodeIterator items = q.execute().getNodes();
            while (items.hasNext()) {
                workItem = null;
                try {
                    workItem = items.nextNode();
                    if (workItem.hasProperty("sling:resourceType")) {
                        prop = workItem.getProperty("sling:resourceType");
                        String path = prop.getPath();
                        prop.remove();
                        this.log.info("D {}", (Object)path);
                    }
                    if (!workItem.hasProperty("assignee")) {
                        prop = workItem.setProperty("assignee", "system");
                        this.log.info("M {}", (Object)prop.getPath());
                    }
                    if ("workItems".equals(workItem.getParent().getName())) {
                        prop = workItem.setProperty("status", "ACTIVE");
                        this.log.info(prop.isNew() ? "A {}" : "M {}", (Object)prop.getPath());
                    } else {
                        prop = workItem.setProperty("status", "COMPLETE");
                        this.log.info(prop.isNew() ? "A {}" : "M {}", (Object)prop.getPath());
                    }
                    processed = this.checkSaveThreshold(sess, processed);
                }
                catch (Exception e) {
                    this.log.error("Moving work item {} failed: ", (Object)workItem.getPath(), (Object)e);
                }
            }
            this.save(sess);
            q = qm.createQuery("/jcr:root/var/upgrade//element(*, cq:WorkItem)", "xpath");
            this.setProgressInfo("Migrating inbox items - marking workitems in /var/upgrade/* so they do not show up in inbox queries");
            processed = 0;
            items = q.execute().getNodes();
            while (items.hasNext()) {
                workItem = null;
                try {
                    workItem = items.nextNode();
                    if (workItem.hasProperty("status")) {
                        prop = workItem.getProperty("status");
                        prop = workItem.setProperty("status", prop.getString() + "-upgraded");
                        this.log.info("M {}", (Object)prop.getPath());
                    }
                    processed = this.checkSaveThreshold(sess, processed);
                }
                catch (Exception e) {
                    this.log.error("Moving work item {} failed: {}", (Object)workItem.getPath(), (Object)e);
                }
            }
            this.save(sess);
        }
        catch (Exception e) {
            this.log.error("Exception in inbox items migration phase", (Throwable)e);
        }
        try {
            this.setProgressInfo("Migrating additional nodes below /etc/workflow");
            HashSet<String> exclusions = new HashSet<String>();
            exclusions.add("/etc/workflow/forum");
            exclusions.add("/etc/workflow/geometrixx");
            exclusions.add("/etc/workflow/instances");
            exclusions.add("/etc/workflow/launcher");
            exclusions.add("/etc/workflow/models");
            exclusions.add("/etc/workflow/notification");
            exclusions.add("/etc/workflow/scripts");
            exclusions.add("/etc/workflow/wcm");
            this.moveAdditionalNodes(backup, exclusions, sess);
        }
        catch (Exception e) {
            this.log.error("Exception in copy additional nodes phase", (Throwable)e);
        }
        this.setProgressInfo("Writing status property");
        if (!sess.itemExists("/var/upgrade/status/")) {
            this.createPath("/var/upgrade/status/", sess);
        }
        Node status = sess.getNode("/var/upgrade/status/");
        Calendar now = Calendar.getInstance();
        status.setProperty(PN_STATUS, now);
        this.log.info("M {}", (Object)STATUS_PROPERTY_PATH);
        this.save(sess);
        this.setProgressInfo("All done.");
    }

    private void moveAdditionalNodes(Node parent, Set<String> exclusions, Session sess) throws Exception {
        NodeIterator nodes = parent.getNodes();
        while (nodes.hasNext()) {
            String targetPath;
            Node node = nodes.nextNode();
            if (node.isNodeType("rep:ACL") || exclusions.contains(targetPath = this.getTargetPath(node))) continue;
            if (!sess.nodeExists(targetPath)) {
                sess.getWorkspace().move(node.getPath(), targetPath);
                this.log.info("A", (Object)targetPath);
                this.log.info("D", (Object)node.getPath());
                continue;
            }
            this.moveAdditionalNodes(node, exclusions, sess);
        }
    }

    private int checkSaveThreshold(Session sess, int processed) throws RepositoryException {
        if (processed < 100) {
            ++processed;
        } else {
            processed = 0;
            this.save(sess);
        }
        return processed;
    }

    private void checkAndCopyFiles(Node root) throws Exception {
        Session sess = root.getSession();
        NodeIterator backups = root.getNodes();
        while (backups.hasNext()) {
            Node backup = backups.nextNode();
            if (backup.isNodeType("nt:file")) {
                String originalPath = this.getTargetPath(backup);
                if (sess.nodeExists(originalPath)) {
                    Node script = sess.getNode(originalPath);
                    Diff diff = Diff.compareNodes(backup, script, FILE_EXCLUDES);
                    if (!diff.hasChanges()) continue;
                    this.onConflict(backup);
                    continue;
                }
                String targetPath = this.getTargetPath(backup);
                this.createPath(UpgradeUtil.getParentPath(targetPath), sess);
                sess.getWorkspace().copy(backup.getPath(), targetPath);
                this.log.info("A {}", (Object)targetPath);
                continue;
            }
            this.checkAndCopyFiles(backup);
        }
    }

    private Map<Node, Node> checkLauncherConfigs(Node cfgBackupHome, Map<Node, Node> modelConflicts) throws Exception {
        Session sess = cfgBackupHome.getSession();
        HashMap<Node, Node> conflicts = new HashMap<Node, Node>();
        NodeIterator cfgs = cfgBackupHome.getNodes();
        while (cfgs.hasNext()) {
            Node cfg = cfgs.nextNode();
            if (cfg.isNodeType("cq:WorkflowLauncher")) {
                String targetPath = this.getTargetPath(cfg);
                boolean targetExists = sess.nodeExists(targetPath);
                if (LAUNCHER_CFG_MAPPING.containsKey(cfg.getName()) && targetExists) {
                    Node newCfg;
                    Diff diff;
                    Node cfgHome;
                    LauncherConfig mapping = LAUNCHER_CFG_MAPPING.get(cfg.getName());
                    boolean changed = false;
                    changed = changed || cfg.hasProperty("eventType") && cfg.getProperty("eventType").getLong() != mapping.eventType.longValue();
                    changed = changed || cfg.hasProperty("glob") && !cfg.getProperty("glob").getString().equals(mapping.glob);
                    changed = changed || cfg.hasProperty("nodetype") && !cfg.getProperty("nodetype").getString().equals(mapping.nodetype);
                    boolean bl = changed = changed || cfg.hasProperty("workflow") && !cfg.getProperty("workflow").getString().equals(mapping.workflow);
                    if (changed) {
                        this.onConflict(cfg);
                        this.addLauncherConflict(modelConflicts, conflicts, cfg);
                        continue;
                    }
                    String cfgHomePath = this.getTargetPath(cfgBackupHome);
                    if (!sess.nodeExists(cfgHomePath) || !(diff = Diff.compareNodes(cfg, newCfg = (cfgHome = sess.getNode(cfgHomePath)).getNode(mapping.newName))).hasChanges() || diff.getSize() == 1 && diff.getChanges().containsKey("workflow")) continue;
                    this.onConflict(cfg);
                    this.addLauncherConflict(modelConflicts, conflicts, cfg);
                    continue;
                }
                if (!targetExists) {
                    this.createPath(UpgradeUtil.getParentPath(targetPath), sess);
                    sess.getWorkspace().copy(cfg.getPath(), targetPath);
                    this.log.info("A {}", (Object)targetPath);
                    this.addLauncherConflict(modelConflicts, conflicts, cfg);
                    continue;
                }
                this.log.error("Existing Custom launcher config (cannot copy): {}", (Object)targetPath);
                continue;
            }
            conflicts.putAll(this.checkLauncherConfigs(cfg, modelConflicts));
        }
        return conflicts;
    }

    private void addLauncherConflict(Map<Node, Node> modelConflicts, Map<Node, Node> conflicts, Node cfg) throws RepositoryException {
        if (cfg.hasProperty("workflow")) {
            String workflow = cfg.getProperty("workflow").getString();
            Node conflictModel = this.findConflictModel(modelConflicts, workflow);
            if (conflictModel != null) {
                conflicts.put(cfg, conflictModel);
            }
        } else {
            conflicts.put(cfg, null);
        }
    }

    private Node findConflictModel(Map<Node, Node> modelConflicts, String workflow) throws RepositoryException {
        for (Node backup : modelConflicts.keySet()) {
            Node model = modelConflicts.get((Object)backup);
            if (!model.getPath().equals(workflow)) continue;
            return model;
        }
        return null;
    }

    private void onConflict(Node cfg) throws RepositoryException {
        this.log.info("C {}", (Object)cfg.getPath());
    }

    private Map<Node, Node> copyWcmTemplates(Node cfgHome, Map<Node, Node> modelConflicts) throws RepositoryException {
        HashMap<Node, Node> conflicts = new HashMap<Node, Node>();
        NodeIterator tplCfgs = cfgHome.getNodes();
        while (tplCfgs.hasNext()) {
            Node tplCfg = tplCfgs.nextNode();
            NodeIterator wfCfgs = tplCfg.getNodes();
            while (wfCfgs.hasNext()) {
                String model;
                Node conflictModel;
                Node wfCfg = wfCfgs.nextNode();
                if (!wfCfg.hasProperty("workflowModel") || (conflictModel = this.findConflictModel(modelConflicts, model = wfCfg.getProperty("workflowModel").getString())) == null) continue;
                conflicts.put(wfCfg, conflictModel);
            }
            String targetPath = this.getTargetPath(tplCfg);
            this.createPath(UpgradeUtil.getParentPath(targetPath), cfgHome.getSession());
            cfgHome.getSession().getWorkspace().copy(tplCfg.getPath(), targetPath);
            this.log.info("A {}", (Object)targetPath);
        }
        return conflicts;
    }

    private Map<Node, Node> copyModelBackup(Session sess, Map<Node, Node> models) throws Exception {
        HashMap<Node, Node> conflicts = new HashMap<Node, Node>();
        Iterator<Node> adaptedModels = models.keySet().iterator();
        while (adaptedModels.hasNext()) {
            Node backup = adaptedModels.next();
            Node model = models.get((Object)backup);
            if (model != null) {
                Diff diff = Diff.compareNodes(backup, model, MODEL_EXCLUDES);
                if (!diff.hasChanges()) continue;
                this.log.info("C {}", (Object)backup.getPath());
                conflicts.put(backup, model);
                continue;
            }
            if (backup.getPath().endsWith("/jcr:content/model")) {
                backup = backup.getParent().getParent();
            }
            String targetPath = this.getTargetPath(backup);
            try {
                Node node;
                this.ensureParentExists(sess, targetPath);
                sess.getWorkspace().move(backup.getPath(), targetPath);
                String targetModelPath = targetPath + "/jcr:content/model";
                if (sess.nodeExists(targetModelPath) && (node = sess.getNode(targetModelPath)).isCheckedOut()) {
                    try {
                        sess.getWorkspace().getVersionManager().checkin(node.getPath());
                        this.log.info("V {}", (Object)node.getPath());
                    }
                    catch (RepositoryException re) {
                        this.log.error("Cannot check in model: " + backup.getPath(), (Throwable)re);
                    }
                }
            }
            catch (RepositoryException re) {
                String message = "Cannot moving custom model back: " + backup.getPath() + " due to: " + re.getMessage();
                this.log.error(message);
                this.log.info("E {}", (Object)message);
            }
            this.log.info("A {}", (Object)targetPath);
        }
        return conflicts;
    }

    private void ensureParentExists(Session sess, String path) throws RepositoryException {
        String parentPath = Text.getRelativeParent((String)path, (int)1);
        if (!sess.nodeExists(parentPath)) {
            this.createPath(parentPath, sess);
        }
    }

    private Map<Node, Node> adaptModelBackup(Node backup, Session sess) throws RepositoryException {
        HashMap<Node, Node> processed = new HashMap<Node, Node>();
        NodeIterator backupModels = backup.getNodes();
        while (backupModels.hasNext()) {
            Node backupModel = backupModels.nextNode();
            if (backupModel.isNodeType("cq:WorkflowModel")) {
                Node backupModelMeta;
                if (!backupModel.isCheckedOut()) {
                    VersionManager vm = sess.getWorkspace().getVersionManager();
                    vm.checkout(backupModel.getPath());
                }
                Property prop = backupModel.setProperty("sling:resourceType", "cq/workflow/components/model");
                this.log.info("M {}", (Object)prop.getPath());
                String originalPath = this.getTargetPath(backupModel);
                if (!backupModel.getPath().endsWith("/jcr:content/model")) {
                    originalPath = originalPath + "/jcr:content/model";
                }
                Node model = null;
                if (sess.nodeExists(originalPath)) {
                    model = sess.getNode(originalPath);
                }
                if (!(backupModelMeta = backupModel.hasNode("metaData") ? backupModel.getNode("metaData") : backupModel.addNode("metaData")).hasProperty("tags")) {
                    if (model != null) {
                        Node modelMeta = model.getNode("metaData");
                        if (modelMeta.hasProperty("tags")) {
                            prop = backupModelMeta.setProperty("tags", modelMeta.getProperty("tags").getString());
                            this.log.info("M {}", (Object)prop.getPath());
                        }
                    } else {
                        prop = backupModelMeta.setProperty("tags", "");
                        this.log.info("M {}", (Object)prop.getPath());
                    }
                }
                processed.put(backupModel, model);
                continue;
            }
            processed.putAll(this.adaptModelBackup(backupModel, sess));
        }
        this.save(sess);
        return processed;
    }

    private void createPath(String path, Session sess) throws RepositoryException {
        int idx = path.indexOf(47, 1);
        String prefix = path.substring(0, idx);
        String tempPath = path.substring(idx);
        Node parent = sess.getNode(prefix);
        for (String label : tempPath.split("/")) {
            if (label.equals("")) continue;
            if (!parent.hasNode(label)) {
                parent = parent.addNode(label, "sling:Folder");
                this.addMixins(parent, sess);
                this.log.info("A {}", (Object)parent.getPath());
                continue;
            }
            parent = parent.getNode(label);
            this.addMixins(parent, sess);
        }
        this.save(sess);
    }

    private void addMixins(Node node, Session session) throws RepositoryException {
        if (node.getPath().startsWith("/etc/workflow")) {
            String backupPath = "/var/upgrade/WorkflowPreserveContentHook" + node.getPath().substring("/etc/workflow".length());
            Node backupNode = session.getNode(backupPath);
            ArrayList<String> nodeMixinNames = new ArrayList<String>();
            for (NodeType nodeMix : node.getMixinNodeTypes()) {
                nodeMixinNames.add(nodeMix.getName());
            }
            for (NodeType mix : backupNode.getMixinNodeTypes()) {
                if (nodeMixinNames.contains(mix.getName())) continue;
                if (this.log.isDebugEnabled()) {
                    this.log.debug("addMixins -  adding mixin: {} to {}", (Object)mix.getName(), (Object)node.getPath());
                }
                node.addMixin(mix.getName());
            }
        }
    }

    private void save(Session sess) throws RepositoryException {
        if (sess.hasPendingChanges()) {
            this.log.info("Saving changes");
            sess.save();
        }
    }

    private String getTargetPath(Node backup) throws RepositoryException, ItemNotFoundException, AccessDeniedException {
        String targetPath = backup.getPath().substring("/var/upgrade/WorkflowPreserveContentHook".length());
        return "/etc/workflow" + targetPath;
    }

    static class LauncherConfig {
        public Long eventType;
        public String newName;
        public String glob;
        public String nodetype;
        public String workflow;

        public LauncherConfig(Long eventType, String newName, String glob, String nodetype, String workflow) {
            this.eventType = eventType;
            this.newName = newName;
            this.glob = glob;
            this.nodetype = nodetype;
            this.workflow = workflow;
        }
    }

}