/** * Map query specific node types. * * @param queryCriteria the query criteria * @param queryBuilder the query builder * @param nodeTypeManager the node type manager * @throws Exception the exception */ private void mapQuerySpecificNodeTypes( final QueryCriteria queryCriteria, final SQLQueryBuilder queryBuilder, final NodeTypeManager nodeTypeManager) throws Exception { String[] contentTypes = queryCriteria.getContentTypes(); NodeType fistType = nodeTypeManager.getNodeType(contentTypes[0]); queryBuilder.openGroup(LOGICAL.AND); if (fistType.isMixin()) { queryBuilder.like("jcr:mixinTypes", contentTypes[0], LOGICAL.NULL); } else { queryBuilder.equal("jcr:primaryType", contentTypes[0], LOGICAL.NULL); } if (contentTypes.length > 1) { for (int i = 1; i < contentTypes.length; i++) { String type = contentTypes[i]; NodeType nodetype = nodeTypeManager.getNodeType(type); if (nodetype.isMixin()) { queryBuilder.like("jcr:mixinTypes", type, LOGICAL.OR); } else { queryBuilder.equal("jcr:primaryType", type, LOGICAL.OR); } } } queryBuilder.closeGroup(); // Remove some specific mimtype queryBuilder.openGroup(LOGICAL.AND_NOT); queryBuilder.like("jcr:mixinTypes", "exo:cssFile", LOGICAL.NULL); queryBuilder.like("jcr:mixinTypes", "exo:jsFile", LOGICAL.OR); queryBuilder.closeGroup(); }
/** * Map query types. * * @param queryCriteria the query criteria * @param queryBuilder the query builder * @throws Exception the exception */ private void mapQueryTypes(final QueryCriteria queryCriteria, final SQLQueryBuilder queryBuilder) throws Exception { queryBuilder.selectTypes(null); // Searh on nt:base queryBuilder.fromNodeTypes(queryCriteria.getNodeTypes()); ManageableRepository currentRepository = repositoryService.getCurrentRepository(); NodeTypeManager manager = currentRepository.getNodeTypeManager(); // Query all documents for specific content types String[] contentTypes = queryCriteria.getContentTypes(); if ((contentTypes != null && contentTypes.length > 0 && queryCriteria.getKeyword() == null) || queryCriteria.isSearchWebpage()) { mapQuerySpecificNodeTypes(queryCriteria, queryBuilder, manager); return; } List<String> selectedNodeTypes = templateService.getDocumentTemplates(); queryBuilder.openGroup(LOGICAL.AND); queryBuilder.equal("jcr:primaryType", "nt:resource", LOGICAL.NULL); // query on exo:rss-enable nodetypes for title, summary field queryBuilder.equal("jcr:mixinTypes", "exo:rss-enable", LOGICAL.OR); // query on metadata nodetype List<String> publicatioTypes = new ArrayList<String>(4); for (NodeTypeIterator iterator = manager.getAllNodeTypes(); iterator.hasNext(); ) { NodeType nodeType = iterator.nextNodeType(); if (nodeType.isNodeType("publication:webpagesPublication")) { publicatioTypes.add(nodeType.getName()); continue; } if (!nodeType.isNodeType("exo:metadata")) continue; if (nodeType.isMixin()) { queryBuilder.equal("jcr:mixinTypes", nodeType.getName(), LOGICAL.OR); } else { queryBuilder.equal("jcr:primaryType", nodeType.getName(), LOGICAL.OR); } } for (String type : selectedNodeTypes) { NodeType nodetype = manager.getNodeType(type); if (nodetype.isMixin()) { queryBuilder.like("jcr:mixinTypes", type, LOGICAL.OR); } else { queryBuilder.equal("jcr:primaryType", type, LOGICAL.OR); } } queryBuilder.closeGroup(); // unwanted document types: exo:cssFile, exo:jsFile if (excludeMimeTypes.size() < 1) return; queryBuilder.openGroup(LOGICAL.AND_NOT); String[] mimetypes = excludeMimeTypes.toArray(new String[] {}); queryBuilder.equal("jcr:mimeType", mimetypes[0], LOGICAL.NULL); for (int i = 1; i < mimetypes.length; i++) { queryBuilder.equal("jcr:mimeType", mimetypes[i], LOGICAL.OR); } queryBuilder.closeGroup(); // Unwanted document by mixin nodetypes queryBuilder.openGroup(LOGICAL.AND_NOT); queryBuilder.like("jcr:mixinTypes", "exo:cssFile", LOGICAL.NULL); queryBuilder.like("jcr:mixinTypes", "exo:jsFile", LOGICAL.OR); queryBuilder.closeGroup(); }
public static List<String> getListAllowedFileType( Node currentNode, TemplateService templateService) throws Exception { List<String> nodeTypes = new ArrayList<String>(); NodeTypeManager ntManager = currentNode.getSession().getWorkspace().getNodeTypeManager(); NodeType currentNodeType = currentNode.getPrimaryNodeType(); NodeDefinition[] childDefs = currentNodeType.getChildNodeDefinitions(); List<String> templates = templateService.getDocumentTemplates(); try { for (int i = 0; i < templates.size(); i++) { String nodeTypeName = templates.get(i).toString(); NodeType nodeType = ntManager.getNodeType(nodeTypeName); NodeType[] superTypes = nodeType.getSupertypes(); boolean isCanCreateDocument = false; for (NodeDefinition childDef : childDefs) { NodeType[] requiredChilds = childDef.getRequiredPrimaryTypes(); for (NodeType requiredChild : requiredChilds) { if (nodeTypeName.equals(requiredChild.getName())) { isCanCreateDocument = true; break; } } if (nodeTypeName.equals(childDef.getName()) || isCanCreateDocument) { if (!nodeTypes.contains(nodeTypeName)) nodeTypes.add(nodeTypeName); isCanCreateDocument = true; } } if (!isCanCreateDocument) { for (NodeType superType : superTypes) { for (NodeDefinition childDef : childDefs) { for (NodeType requiredType : childDef.getRequiredPrimaryTypes()) { if (superType.getName().equals(requiredType.getName())) { if (!nodeTypes.contains(nodeTypeName)) nodeTypes.add(nodeTypeName); isCanCreateDocument = true; break; } } if (isCanCreateDocument) break; } if (isCanCreateDocument) break; } } } } catch (Exception e) { if (LOG.isErrorEnabled()) { LOG.error("Unexpected error", e); } } return nodeTypes; }
/** * Get allowed folder types in current path. * * @param currentNode * @param currentDrive * @return List<String> of node types * @throws Exception */ public static List<String> getAllowedFolderTypesInCurrentPath( Node currentNode, DriveData currentDrive) throws Exception { List<String> allowedTypes = new ArrayList<String>(); NodeTypeImpl currentNodeType = (NodeTypeImpl) currentNode.getPrimaryNodeType(); String[] arrFoldertypes = currentDrive.getAllowCreateFolders().split(","); NodeTypeManager ntManager = currentNode.getSession().getWorkspace().getNodeTypeManager(); for (String strFolderType : arrFoldertypes) { if (strFolderType.isEmpty()) continue; NodeType folderType = ntManager.getNodeType(strFolderType); if ((currentNodeType) .isChildNodePrimaryTypeAllowed( Constants.JCR_ANY_NAME, ((NodeTypeImpl) folderType).getQName())) { allowedTypes.add(strFolderType); } } return allowedTypes; }
@Test public void shouldAddNodeTypesAndNamespaces() throws Exception { File file = new File("src/test/resources/config/configRepository.xml"); assertThat(file.exists(), is(true)); assertThat(file.canRead(), is(true)); assertThat(file.isFile(), is(true)); configuration.loadFrom("src/test/resources/config/configRepository.xml"); // Verify that the configration was loaded correctly ... assertThat(configuration.repository("Car Repository").getSource(), is("Cars")); // ModeShapeConfiguration.ConfigurationDefinition content1 = // configuration.getConfigurationDefinition(); // Subgraph subgraph1 = content1.graph().getSubgraphOfDepth(6).at("/"); // Load the node types from the CND file, and save the configuration ... InputStream nodeTypes = getClass().getResourceAsStream("/tck/tck_test_types.cnd"); configuration.repository("Car Repository").addNodeTypes(nodeTypes); configuration.save(); // ModeShapeConfiguration.ConfigurationDefinition content2 = // configuration.getConfigurationDefinition(); // Subgraph subgraph2 = content2.graph().getSubgraphOfDepth(6).at("/"); // Verify there were no problems loading the CND file ... assertThat(configuration.getProblems().isEmpty(), is(true)); assertThat( configuration .getConfigurationDefinition() .getContext() .getNamespaceRegistry() .isRegisteredNamespaceUri("http://www.modeshape.org/test/1.0"), is(true)); // Verify that the graph has been updated correctly ... ModeShapeConfiguration.ConfigurationDefinition content = configuration.getConfigurationDefinition(); Subgraph subgraph = content.graph().getSubgraphOfDepth(6).at("/"); assertThat( subgraph.getNode("/mode:repositories").getChildren(), hasChild(segment("Car Repository"))); assertThat(subgraph.getNode("/mode:repositories/Car Repository"), is(notNullValue())); assertThat( subgraph.getNode("/mode:repositories/Car Repository"), hasProperty(ModeShapeLexicon.SOURCE_NAME, "Cars")); assertThat( subgraph.getNode("/mode:repositories/Car Repository").getChildren(), hasChild(segment("mode:options"))); assertThat( subgraph.getNode("/mode:repositories/Car Repository/jcr:nodeTypes"), is(notNullValue())); // for (Location child : subgraph.getNode("/mode:repositories/Car // Repository/mode:nodeTypes").getChildren()) { // System.out.println(child.getPath().getLastSegment().getString(context().getNamespaceRegistry())); // } assertThat( subgraph.getNode("/mode:repositories/Car Repository/jcr:nodeTypes").getChildren(), hasChildren( segment("modetest:noSameNameSibs"), segment("modetest:referenceableUnstructured"), segment("modetest:nodeWithMandatoryProperty"), segment("modetest:nodeWithMandatoryChild"), segment("modetest:unorderableUnstructured"))); assertThat( subgraph.getNode("/mode:repositories/Car Repository/mode:namespaces"), is(notNullValue())); // Check that the namespace in the CND file was persisted correctly ... assertThat( subgraph.getNode("/mode:repositories/Car Repository/mode:namespaces").getChildren(), hasChild(segment("modetest"))); assertThat( subgraph.getNode("/mode:repositories/Car Repository/mode:namespaces/modetest"), hasProperty(ModeShapeLexicon.URI, "http://www.modeshape.org/test/1.0")); // Initialize IDTrust and a policy file (which defines the "modeshape-jcr" login config name) JaasTestUtil.initJaas("security/jaas.conf.xml"); // Create and start the engine ... engine = configuration.build(); engine.start(); Repository repository = engine.getRepository("Car Repository"); assertThat(repository, is(notNullValue())); // Create a session, authenticating using one of the usernames defined by our JAAS policy // file(s) ... Session session = null; try { session = repository.login(new SimpleCredentials("superuser", "superuser".toCharArray())); // Check that the namespace showed up ... assertThat(session.getNamespacePrefix("http://www.modeshape.org/test/1.0"), is("modetest")); // Check that some of the node types showed up ... NodeTypeManager ntm = session.getWorkspace().getNodeTypeManager(); assertThat( ntm.getNodeType("modetest:noSameNameSibs"), is(notNullValue())); // throws exception assertThat( ntm.getNodeType("modetest:referenceableUnstructured"), is(notNullValue())); // throws exception assertThat( ntm.getNodeType("modetest:nodeWithMandatoryProperty"), is(notNullValue())); // throws exception assertThat( ntm.getNodeType("modetest:nodeWithMandatoryChild"), is(notNullValue())); // throws exception assertThat( ntm.getNodeType("modetest:unorderableUnstructured"), is(notNullValue())); // throws exception } finally { if (session != null) session.logout(); } }
/** * Map query types. * * @param queryCriteria the query criteria * @param queryBuilder the query builder * @throws Exception the exception */ private void mapQueryTypes(final QueryCriteria queryCriteria, final SQLQueryBuilder queryBuilder) throws Exception { queryBuilder.selectTypes(null); // Searh on nt:base queryBuilder.fromNodeTypes(queryCriteria.getNodeTypes()); ManageableRepository currentRepository = repositoryService.getCurrentRepository(); NodeTypeManager manager = currentRepository.getNodeTypeManager(); // Query all documents for specific content types String[] contentTypes = queryCriteria.getContentTypes(); if ((contentTypes != null && contentTypes.length > 0 && queryCriteria.getKeyword() == null) || queryCriteria.isSearchWebpage()) { mapQuerySpecificNodeTypes(queryCriteria, queryBuilder, manager); return; } List<String> selectedNodeTypes = (contentTypes != null && contentTypes.length > 0) ? Arrays.asList(contentTypes) : templateService.getDocumentTemplates(); queryBuilder.openGroup(LOGICAL.AND); if (selectedNodeTypes.contains("nt:file")) { queryBuilder.equal("jcr:primaryType", "nt:resource", LOGICAL.NULL); } else { // searching only document, not file. In this case, search nt:resource with // exo:webContentChild mixin queryBuilder.openGroup(null); queryBuilder.equal( NodetypeConstant.JCR_PRIMARY_TYPE, NodetypeConstant.NT_RESOURCE, LOGICAL.NULL); queryBuilder.equal( NodetypeConstant.JCR_MIXIN_TYPES, NodetypeConstant.EXO_WEBCONTENT_CHILD, LOGICAL.AND); queryBuilder.closeGroup(); } // query on exo:rss-enable nodetypes for title, summary field queryBuilder.equal("jcr:mixinTypes", "exo:rss-enable", LOGICAL.OR); for (String type : selectedNodeTypes) { NodeType nodetype = manager.getNodeType(type); if (nodetype.isMixin()) { if (selectedNodeTypes.contains("nt:file") || !NodetypeConstant.EXO_CSS_FILE.equals(type) && !NodetypeConstant.EXO_JS_FILE.equals(type) && !NodetypeConstant.EXO_HTML_FILE.equals(type)) { queryBuilder.like("jcr:mixinTypes", type, LOGICAL.OR); } else { // searching only document, not file. In this case, search nt:resource with // exo:webContentChild mixin queryBuilder.openGroup(LOGICAL.OR); queryBuilder.equal(NodetypeConstant.JCR_MIXIN_TYPES, type, LOGICAL.NULL); queryBuilder.equal( NodetypeConstant.JCR_MIXIN_TYPES, NodetypeConstant.EXO_WEBCONTENT_CHILD, LOGICAL.AND); queryBuilder.closeGroup(); } } else { queryBuilder.equal("jcr:primaryType", type, LOGICAL.OR); } } queryBuilder.closeGroup(); // unwanted document types: exo:cssFile, exo:jsFile if (excludeMimeTypes.size() < 1) return; queryBuilder.openGroup(LOGICAL.AND_NOT); String[] mimetypes = excludeMimeTypes.toArray(new String[] {}); queryBuilder.equal("jcr:mimeType", mimetypes[0], LOGICAL.NULL); for (int i = 1; i < mimetypes.length; i++) { queryBuilder.equal("jcr:mimeType", mimetypes[i], LOGICAL.OR); } queryBuilder.closeGroup(); // Unwanted document by mixin nodetypes queryBuilder.openGroup(LOGICAL.AND_NOT); queryBuilder.like("jcr:mixinTypes", "exo:cssFile", LOGICAL.NULL); queryBuilder.like("jcr:mixinTypes", "exo:jsFile", LOGICAL.OR); queryBuilder.closeGroup(); }