StatusUtil.java 17 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
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.day.cq.wcm.msm.api.LiveStatus
 *  com.day.text.Text
 *  javax.jcr.ItemNotFoundException
 *  javax.jcr.Node
 *  javax.jcr.NodeIterator
 *  javax.jcr.Property
 *  javax.jcr.RepositoryException
 *  javax.jcr.Value
 *  org.apache.sling.api.resource.Resource
 *  org.apache.sling.api.resource.ResourceResolver
 *  org.apache.sling.jcr.resource.JcrPropertyMap
 */
package com.day.cq.wcm.msm.impl;

import com.day.cq.wcm.msm.api.LiveStatus;
import com.day.cq.wcm.msm.impl.LiveStatusImpl;
import com.day.cq.wcm.msm.impl.Utils;
import com.day.text.Text;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;
import javax.jcr.ItemNotFoundException;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Value;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.jcr.resource.JcrPropertyMap;

public class StatusUtil {
    private static final StatusPropertyReader[] PROPERTY_READER = new StatusPropertyReader[]{new CancelStateReader(), new RolloutTimeStampReader()};
    private static final StatusPropertyReader[] EXTENDED_STATUS_READER = new StatusPropertyReader[]{new SkippedPageFilterReader(), new ManuallyCreatedReader()};

    private StatusUtil() {
    }

    public static LiveStatusImpl createLiveStatus(ResourceResolver resolver, String srcPath, String targetPath, String liveCopyPath, boolean extendedStatus) throws RepositoryException {
        StatusPropertyReader[] readers;
        String parent;
        boolean slaveExists;
        LiveStatusImpl status = new LiveStatusImpl();
        Node source = Utils.getNode(resolver, srcPath);
        Node target = Utils.getNode(resolver, targetPath);
        boolean masterExists = source != null;
        boolean bl = slaveExists = target != null;
        if (masterExists) {
            status.setPage(source.getParent().isNodeType("cq:Page"));
        } else if (slaveExists) {
            status.setPage(target.getParent().isNodeType("cq:Page"));
        } else {
            status.setPage(!srcPath.contains("jcr:content") || srcPath.endsWith("/jcr:content"));
        }
        status.setSlaveExisting(target != null && !Utils.isGhost(target));
        status.setMasterExisting(source != null && !Utils.isGhost(source));
        if (extendedStatus) {
            readers = Arrays.copyOf(EXTENDED_STATUS_READER, EXTENDED_STATUS_READER.length + PROPERTY_READER.length);
            System.arraycopy(PROPERTY_READER, 0, readers, EXTENDED_STATUS_READER.length, PROPERTY_READER.length);
        } else {
            readers = PROPERTY_READER;
        }
        StatusUtil.setTargetState(status, target, null, readers);
        String child = Utils.getPagePath(targetPath);
        while (Text.isDescendantOrEqual((String)liveCopyPath, (String)(parent = Text.getRelativeParent((String)child, (int)1)))) {
            Resource targetParent = resolver.getResource(parent);
            if (targetParent != null) {
                try {
                    StatusUtil.setTargetState(status, Utils.getWorkingNode((Node)targetParent.adaptTo(Node.class)), child, readers);
                }
                catch (ItemNotFoundException ignore) {
                    // empty catch block
                }
            }
            child = parent;
        }
        if (extendedStatus) {
            status.setAdvancedStatus("msm:isTargetModified", StatusUtil.isTargetModified(target));
            status.setAdvancedStatus("msm:isSourceModified", StatusUtil.isSourceModified(source, target));
            status.setCancelledProperties(StatusUtil.readCancelledProperties(target));
            if (!masterExists && slaveExists) {
                status.setAdvancedStatus("msm:isSourceDeleted", status.getAdvancedStatus("msm:isTargetManuallyCreated") == false);
            } else {
                status.setAdvancedStatus("msm:isSourceDeleted", false);
            }
            if (status.isPage() && !slaveExists) {
                status.setAdvancedStatus("msm:isTargetDeleted", false);
            } else {
                status.setAdvancedStatus("msm:isTargetDeleted", target != null && Utils.isGhost(target));
            }
            if (slaveExists && status.isPage()) {
                List<String> hasType = StatusUtil.hasChildOfTypes(target, "cq:LiveSyncCancelled", "cq:PropertyLiveSyncCancelled");
                status.setAdvancedStatus("msm:isTargetCancelledChild", hasType.contains("cq:LiveSyncCancelled"));
                status.setAdvancedStatus("msm:isTargetCancelledProperty", status.getCanceledProperties().size() > 0 || hasType.contains("cq:PropertyLiveSyncCancelled"));
            }
        }
        return status;
    }

    static /* varargs */ void skipPages(Node parentNode, String ... childrenNames) throws RepositoryException {
        SkippedPagesFilter filter = SkippedPagesFilter.read(parentNode);
        for (String childName : childrenNames) {
            filter.add(childName, false);
        }
        filter.write();
    }

    static /* varargs */ void unskipPages(Node parentNode, String ... childrenNames) throws RepositoryException {
        SkippedPagesFilter filter = SkippedPagesFilter.read(parentNode);
        for (String childName : childrenNames) {
            filter.remove(childName, false);
        }
        filter.write();
    }

    private static List<String> readCancelledProperties(Node target) throws RepositoryException {
        ArrayList<String> list = new ArrayList<String>();
        if (target != null && target.hasProperty("cq:propertyInheritanceCancelled")) {
            for (Value v : target.getProperty("cq:propertyInheritanceCancelled").getValues()) {
                list.add(v.getString());
            }
        }
        return list;
    }

    private static void setTargetState(LiveStatusImpl status, Node target, String pagePath, StatusPropertyReader[] readers) throws RepositoryException {
        for (StatusPropertyReader reader : readers) {
            if (reader.isSatisfied(status)) continue;
            if (pagePath == null) {
                reader.read(target, status);
                continue;
            }
            reader.readFor(target, pagePath, status);
        }
    }

    private static boolean isSourceModified(Node source, Node target) throws RepositoryException {
        if (source == null || target == null) {
            return false;
        }
        JcrPropertyMap targetMap = new JcrPropertyMap(target);
        Calendar slaveLastMod = (Calendar)targetMap.get("cq:lastRolledout", targetMap.get("cq:lastModified", Calendar.class));
        if (slaveLastMod != null && source.hasProperty("cq:lastModified")) {
            Calendar masterLastMod = source.getProperty("cq:lastModified").getDate();
            return slaveLastMod.before(masterLastMod);
        }
        return false;
    }

    private static boolean isTargetModified(Node target) throws RepositoryException {
        if (target != null && target.hasProperty("cq:lastRolledout") && target.hasProperty("cq:lastModified")) {
            Calendar slaveLastRol = target.getProperty("cq:lastRolledout").getDate();
            Calendar slaveLastMod = target.getProperty("cq:lastModified").getDate();
            return slaveLastRol.before(slaveLastMod);
        }
        return false;
    }

    private static /* varargs */ List<String> hasChildOfTypes(Node node, String ... types) throws RepositoryException {
        ArrayList<String> todo = new ArrayList<String>(Arrays.asList(types));
        NodeIterator itr = node.getNodes();
        while (itr.hasNext()) {
            Node check = Utils.getWorkingNode(itr.nextNode());
            Iterator<String> typeIter = todo.iterator();
            while (typeIter.hasNext()) {
                String type = typeIter.next();
                if (!check.isNodeType(type)) continue;
                todo.remove(type);
                typeIter = todo.iterator();
            }
            if (todo.isEmpty()) continue;
            todo.removeAll(StatusUtil.hasChildOfTypes(check, todo.toArray(new String[todo.size()])));
        }
        ArrayList<String> found = new ArrayList<String>(types.length);
        for (String type : types) {
            if (todo.contains(type)) continue;
            found.add(type);
        }
        return found;
    }

    private static class SkippedPagesFilter {
        private static final String PATTERN_ALL = ".*";
        private final Node persistence;
        private ArrayList<FilterEntry> list;

        private SkippedPagesFilter(Node persistence) {
            this.persistence = persistence;
            this.list = new ArrayList();
            String[] saved = (String[])new JcrPropertyMap(persistence).get("cq:skippedPages", String[].class);
            if (saved != null) {
                for (String raw : saved) {
                    this.list.add(new FilterEntry(raw));
                }
            }
        }

        public void add(String filter, boolean include) {
            if (this.filters(filter) == include && !this.list.remove(new FilterEntry(filter, !include))) {
                this.list.add(new FilterEntry(filter, include));
            }
        }

        public void remove(String filter, boolean include) {
            if (this.filters(filter) != include && !this.list.remove(new FilterEntry(filter, include))) {
                this.list.add(new FilterEntry(filter, !include));
            }
        }

        public boolean contains(String filter, boolean include) {
            return this.list.contains(new FilterEntry(filter, include));
        }

        public boolean filters(String name) {
            boolean filter = false;
            for (FilterEntry entry : this.list) {
                if (!entry.matches(name)) continue;
                filter = entry.deny;
            }
            return filter;
        }

        public void write() throws RepositoryException {
            ArrayList<String> tmp = new ArrayList<String>(this.list.size());
            boolean denysAll = this.contains(".*", false);
            boolean allowsAll = this.contains(".*", true);
            for (FilterEntry filter : this.list) {
                if (denysAll | allowsAll && !filter.regexp.equals(".*") && !(!filter.deny & denysAll) && !filter.deny & allowsAll) continue;
                tmp.add(filter.toString());
            }
            this.persistence.setProperty("cq:skippedPages", tmp.toArray(new String[tmp.size()]));
        }

        private static SkippedPagesFilter read(Node persistence) {
            return new SkippedPagesFilter(persistence);
        }

        private static final class FilterEntry {
            private final boolean deny;
            private final String regexp;
            private final String raw;

            private FilterEntry(String raw) {
                this.raw = raw;
                if (raw.indexOf("|") > 0) {
                    this.regexp = raw.substring(0, raw.indexOf("|"));
                    this.deny = !raw.substring(raw.indexOf("|") + 1).equals("+");
                } else {
                    this.deny = true;
                    this.regexp = raw;
                }
            }

            private FilterEntry(String filter, boolean include) {
                this.deny = !include;
                this.regexp = filter;
                this.raw = include ? filter + "|+" : filter;
            }

            boolean matches(String check) {
                return Pattern.matches(this.regexp, check);
            }

            public String toString() {
                return this.raw;
            }

            public int hashCode() {
                return this.raw.hashCode();
            }

            public boolean equals(Object obj) {
                return obj instanceof FilterEntry && this.raw.equals(obj.toString());
            }
        }

    }

    private static class RolloutTimeStampReader
    implements StatusPropertyReader {
        private RolloutTimeStampReader() {
        }

        @Override
        public boolean isSatisfied(LiveStatus status) {
            return !status.isTargetExisting() || status.getLastRolledOut() != null;
        }

        @Override
        public void read(Node node, LiveStatusImpl status) throws RepositoryException {
            if (node.isNodeType("cq:LiveRelationship")) {
                if (node.hasProperty("cq:lastRolledout")) {
                    status.setRolledOutOn(node.getProperty("cq:lastRolledout").getDate().getTime());
                }
                if (node.hasProperty("cq:lastRolledoutBy")) {
                    status.setRolledOutBy(node.getProperty("cq:lastRolledoutBy").getString());
                }
            }
        }

        @Override
        public void readFor(Node node, String childPath, LiveStatusImpl status) throws RepositoryException {
        }
    }

    private static final class CancelStateReader
    implements StatusPropertyReader {
        private CancelStateReader() {
        }

        @Override
        public boolean isSatisfied(LiveStatus status) {
            return false;
        }

        @Override
        public void read(Node node, LiveStatusImpl status) throws RepositoryException {
            CancelState state;
            if (node != null && (state = CancelState.stateFromNode(node)) != null && state.cancelled) {
                status.setCancelled(true);
                status.setCancelledForChildren(state.deep);
            }
        }

        @Override
        public void readFor(Node node, String childPath, LiveStatusImpl status) throws RepositoryException {
            CancelState state = CancelState.stateFromNode(node);
            if (state != null && (state.deep || CancelStateReader.isComponent(node, childPath))) {
                status.setCancelled(state.cancelled);
                status.setCancelledForChildren(state.deep);
                status.setCancelledPath(state.root);
            }
        }

        private static boolean isComponent(Node node, String childPath) throws RepositoryException {
            return node.getParent().isNodeType("cq:Page") && Text.isDescendant((String)node.getPath(), (String)childPath);
        }

        private static final class CancelState {
            private final boolean cancelled;
            private final boolean deep;
            private final String root;

            CancelState(boolean canceled, boolean deep, String root) {
                this.cancelled = canceled;
                this.deep = deep;
                this.root = root;
            }

            private static CancelState stateFromNode(Node checkNode) throws RepositoryException {
                if (checkNode.isNodeType("cq:LiveSyncCancelled")) {
                    return new CancelState(true, checkNode.hasProperty("cq:isCancelledForChildren") && checkNode.getProperty("cq:isCancelledForChildren").getBoolean(), Utils.getPagePath(checkNode.getPath()));
                }
                return null;
            }
        }

    }

    private static final class ManuallyCreatedReader
    implements StatusPropertyReader {
        private ManuallyCreatedReader() {
        }

        @Override
        public boolean isSatisfied(LiveStatus status) {
            return status.getAdvancedStatus("msm:isTargetManuallyCreated") != null;
        }

        @Override
        public void read(Node node, LiveStatusImpl status) throws RepositoryException {
            if (node != null) {
                status.setAdvancedStatus("msm:isTargetManuallyCreated", ManuallyCreatedReader.isManuallyCreated(node));
            }
        }

        @Override
        public void readFor(Node node, String childPath, LiveStatusImpl status) throws RepositoryException {
            this.read(node, status);
        }

        private static boolean isManuallyCreated(Node target) throws RepositoryException {
            return !target.isNodeType("cq:LiveRelationship");
        }
    }

    private static final class SkippedPageFilterReader
    implements StatusPropertyReader {
        private SkippedPageFilterReader() {
        }

        @Override
        public boolean isSatisfied(LiveStatus status) {
            return status.getAdvancedStatus("msm:isTargetSkipped") == Boolean.TRUE;
        }

        @Override
        public void read(Node node, LiveStatusImpl status) throws RepositoryException {
            status.setAdvancedStatus("msm:isTargetSkipped", false);
        }

        @Override
        public void readFor(Node node, String childPath, LiveStatusImpl status) throws RepositoryException {
            SkippedPagesFilter f = SkippedPagesFilter.read(node);
            status.setAdvancedStatus("msm:isTargetSkipped", f.filters(Text.getName((String)childPath)));
        }
    }

    private static interface StatusPropertyReader {
        public boolean isSatisfied(LiveStatus var1);

        public void read(Node var1, LiveStatusImpl var2) throws RepositoryException;

        public void readFor(Node var1, String var2, LiveStatusImpl var3) throws RepositoryException;
    }

}