AnsweredAssumed Answered

Query For Node by sys:node-uuid?

Question asked by csbrown on Apr 24, 2006
Latest reply on Apr 26, 2006 by jlbarrera
I am trying to figure out how we can add new Content nodes to the repository using the repository service while keeping a unique ID of the new Content node. 

At some later time I would like to retrieve the contents of that node using the saved unique id.  I have found a process that works for the test case but wanted to make sure it is safe to do in practice.

I have slightly modified some of the WebServices examples to do this.  Here's the code.

            // Create content
            NamedValue[] properties2 = new NamedValue[]{new NamedValue(Constants.PROP_NAME, "SampleContent.txt")};
            CMLCreate create2 = new CMLCreate("1", parentReference2, Constants.TYPE_CONTENT, properties2);
            CML cml2 = new CML();
            cml2.setCreate(new CMLCreate[]{create2});
            UpdateResult[] results2 = WebServiceFactory.getRepositoryService().update(cml2); 

            // Save teh node id for later retrieval
            String savedNodeId = results2[0].getDestination().getUuid();
            // Set content
            ContentFormat format = new ContentFormat(Constants.MIMETYPE_TEXT_PLAIN, "UTF-8");
            byte[] content = "This is some test content provided by the Alfresco development team!".getBytes();
            WebServiceFactory.getContentService().write(results2[0].getDestination(), Constants.PROP_CONTENT, content, format);

// Now retrieve the node reference
        // Get a reference to the respository web service
        RepositoryServiceSoapBindingStub repositoryService = WebServiceFactory.getRepositoryService();        
        // Create a query object, looking for all items with alfresco in the name of text
        Query query = new Query(QueryLanguageEnum.lucene, "@sys\\:node-uuid:\"" + savedNodeId + "\"");
        // Execute the query
        QueryResult queryResult = repositoryService.query(STORE, query, true);
        // Display the results
        ResultSet resultSet = queryResult.getResultSet();
        ResultSetRow[] rows = resultSet.getRows();
        if (rows == null)
            System.out.println("No query results found.");
            // Get the id of the first result
            String firstResultId = rows[0].getNode().getId();
            Reference reference = new Reference(STORE, firstResultId, null);


I am assuming that a more direct way to go, assuming you know the node is there, is to just prepared the reference with the saved id, like this.

            Reference reference = new Reference(STORE, savedNodeId, null);

Can anyone offer any suggestions for a better way?