Search in sources :

Example 1 with NodeTypeManager

use of org.modeshape.jcr.api.nodetype.NodeTypeManager in project kylo by Teradata.

the class FeedVersioningRemovalUpgradeAction method upgradeTo.

@Override
public void upgradeTo(KyloVersion startingVersion) {
    log.info("Removing feed versioning: {}", startingVersion);
    Session session = JcrMetadataAccess.getActiveSession();
    try {
        Node feedsNode = session.getRootNode().getNode("metadata/feeds");
        NodeTypeManager typeMgr = (NodeTypeManager) session.getWorkspace().getNodeTypeManager();
        NodeType currentFeedType = typeMgr.getNodeType("tba:feed");
        List<String> currentSupertypes = Arrays.asList(currentFeedType.getDeclaredSupertypeNames());
        if (currentSupertypes.contains("mix:versionable")) {
            log.info("Removing versionable feed type {} ", currentFeedType);
            // Remove feed version history
            for (Node catNode : JcrUtil.getNodesOfType(feedsNode, "tba:category")) {
                for (Node feedNode : JcrUtil.getNodesOfType(catNode, "tba:feed")) {
                    log.debug("Removing prior versions of feed: {}.{}", catNode.getName(), feedNode.getName());
                    if (JcrVersionUtil.isVersionable(feedNode)) {
                        VersionManager versionManager = session.getWorkspace().getVersionManager();
                        VersionHistory versionHistory = versionManager.getVersionHistory(feedNode.getPath());
                        VersionIterator vIt = versionHistory.getAllVersions();
                        int count = 0;
                        String last = "";
                        while (vIt.hasNext()) {
                            Version version = vIt.nextVersion();
                            String versionName = version.getName();
                            String baseVersion = "";
                            if (!"jcr:rootVersion".equals(versionName)) {
                                // baseVersion requires actual versionable node to get the base version name
                                baseVersion = JcrVersionUtil.getBaseVersion(feedNode).getName();
                            }
                            if (!"jcr:rootVersion".equals(versionName) && !versionName.equalsIgnoreCase(baseVersion)) {
                                last = version.getName();
                                // removeVersion writes directly to workspace, no session.save is necessary
                                versionHistory.removeVersion(version.getName());
                                count++;
                            }
                        }
                        if (count > 0) {
                            log.info("Removed {} versions through {} of feed {}", count, last, feedNode.getName());
                        } else {
                            log.debug("Feed {} had no versions", feedNode.getName());
                        }
                    }
                }
            }
            // Redefine the NodeType of tba:feed to remove versionable but retain the versionable properties with weaker constraints
            // Retaining the properties seems to override some residual properties on feed nodes that causes a failure later.
            // In particular, jcr:predecessors was accessed later but redefining all mix:versionable properties to be safe.
            NodeTypeTemplate template = typeMgr.createNodeTypeTemplate(currentFeedType);
            List<String> newSupertypes = currentSupertypes.stream().filter(type -> !type.equals("mix:versionable")).collect(Collectors.toList());
            template.setDeclaredSuperTypeNames(newSupertypes.toArray(new String[newSupertypes.size()]));
            @SuppressWarnings("unchecked") List<PropertyDefinitionTemplate> propTemplates = template.getPropertyDefinitionTemplates();
            PropertyDefinitionTemplate prop = typeMgr.createPropertyDefinitionTemplate();
            prop.setName("jcr:versionHistory");
            prop.setRequiredType(PropertyType.WEAKREFERENCE);
            propTemplates.add(prop);
            prop = typeMgr.createPropertyDefinitionTemplate();
            prop.setName("jcr:baseVersion");
            prop.setRequiredType(PropertyType.WEAKREFERENCE);
            propTemplates.add(prop);
            prop = typeMgr.createPropertyDefinitionTemplate();
            prop.setName("jcr:predecessors");
            prop.setRequiredType(PropertyType.WEAKREFERENCE);
            prop.setMultiple(true);
            propTemplates.add(prop);
            prop = typeMgr.createPropertyDefinitionTemplate();
            prop.setName("jcr:mergeFailed");
            prop.setRequiredType(PropertyType.WEAKREFERENCE);
            propTemplates.add(prop);
            prop = typeMgr.createPropertyDefinitionTemplate();
            prop.setName("jcr:activity");
            prop.setRequiredType(PropertyType.WEAKREFERENCE);
            propTemplates.add(prop);
            prop = typeMgr.createPropertyDefinitionTemplate();
            prop.setName("jcr:configuration");
            prop.setRequiredType(PropertyType.WEAKREFERENCE);
            propTemplates.add(prop);
            log.info("Replacing the versionable feed type '{}' with a non-versionable type", currentFeedType);
            NodeType newType = typeMgr.registerNodeType(template, true);
            log.info("Replaced with new feed type '{}' with a non-versionable type", newType);
            // This step may not be necessary.
            for (Node catNode : JcrUtil.getNodesOfType(feedsNode, "tba:category")) {
                for (Node feedNode : JcrUtil.getNodesOfType(catNode, "tba:feed")) {
                    feedNode.setPrimaryType(newType.getName());
                    if (feedNode.hasProperty("jcr:predecessors")) {
                        feedNode.getProperty("jcr:predecessors").setValue(new Value[0]);
                        ;
                        feedNode.getProperty("jcr:predecessors").remove();
                    }
                }
            }
        }
    } catch (RepositoryException e) {
        log.error("Failure while attempting to remove versioning from feeds", e);
        throw new UpgradeException("Failure while attempting to remove versioning from feeds", e);
    }
}
Also used : Arrays(java.util.Arrays) VersionHistory(javax.jcr.version.VersionHistory) NodeTypeManager(org.modeshape.jcr.api.nodetype.NodeTypeManager) Version(javax.jcr.version.Version) VersionIterator(javax.jcr.version.VersionIterator) LoggerFactory(org.slf4j.LoggerFactory) RepositoryException(javax.jcr.RepositoryException) JcrVersionUtil(com.thinkbiganalytics.metadata.modeshape.support.JcrVersionUtil) Node(javax.jcr.Node) UpgradeException(com.thinkbiganalytics.server.upgrade.UpgradeException) VersionManager(javax.jcr.version.VersionManager) UpgradeState(com.thinkbiganalytics.server.upgrade.UpgradeState) Logger(org.slf4j.Logger) Session(javax.jcr.Session) PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate) PropertyType(javax.jcr.PropertyType) Collectors(java.util.stream.Collectors) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) Profile(org.springframework.context.annotation.Profile) List(java.util.List) Component(org.springframework.stereotype.Component) Value(javax.jcr.Value) NodeType(javax.jcr.nodetype.NodeType) JcrUtil(com.thinkbiganalytics.metadata.modeshape.support.JcrUtil) KyloVersion(com.thinkbiganalytics.KyloVersion) JcrMetadataAccess(com.thinkbiganalytics.metadata.modeshape.JcrMetadataAccess) KyloUpgrader(com.thinkbiganalytics.server.upgrade.KyloUpgrader) NodeTypeManager(org.modeshape.jcr.api.nodetype.NodeTypeManager) Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException) VersionManager(javax.jcr.version.VersionManager) VersionHistory(javax.jcr.version.VersionHistory) UpgradeException(com.thinkbiganalytics.server.upgrade.UpgradeException) NodeTypeTemplate(javax.jcr.nodetype.NodeTypeTemplate) PropertyDefinitionTemplate(javax.jcr.nodetype.PropertyDefinitionTemplate) Version(javax.jcr.version.Version) KyloVersion(com.thinkbiganalytics.KyloVersion) NodeType(javax.jcr.nodetype.NodeType) VersionIterator(javax.jcr.version.VersionIterator) Session(javax.jcr.Session)

Aggregations

KyloVersion (com.thinkbiganalytics.KyloVersion)1 JcrMetadataAccess (com.thinkbiganalytics.metadata.modeshape.JcrMetadataAccess)1 JcrUtil (com.thinkbiganalytics.metadata.modeshape.support.JcrUtil)1 JcrVersionUtil (com.thinkbiganalytics.metadata.modeshape.support.JcrVersionUtil)1 KyloUpgrader (com.thinkbiganalytics.server.upgrade.KyloUpgrader)1 UpgradeException (com.thinkbiganalytics.server.upgrade.UpgradeException)1 UpgradeState (com.thinkbiganalytics.server.upgrade.UpgradeState)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Node (javax.jcr.Node)1 PropertyType (javax.jcr.PropertyType)1 RepositoryException (javax.jcr.RepositoryException)1 Session (javax.jcr.Session)1 Value (javax.jcr.Value)1 NodeType (javax.jcr.nodetype.NodeType)1 NodeTypeTemplate (javax.jcr.nodetype.NodeTypeTemplate)1 PropertyDefinitionTemplate (javax.jcr.nodetype.PropertyDefinitionTemplate)1 Version (javax.jcr.version.Version)1 VersionHistory (javax.jcr.version.VersionHistory)1