cancel
Showing results for 
Search instead for 
Did you mean: 

Query For Node by sys:node-uuid?

csbrown
Champ in-the-making
Champ in-the-making
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.");
        }
        else
        {
           
            // 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?

Thanks,
3 REPLIES 3

jlbarrera
Champ in-the-making
Champ in-the-making
Change the query:

    // 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, "(ID:\"" + 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.");
        }
        else
        {
               
            // Get the id of the first result
            String firstResultId = rows[0].getNode().getId();
            Reference reference = new Reference(STORE, firstResultId, null);

        }

csbrown
Champ in-the-making
Champ in-the-making
Thanks for the suggestion, but I had no luck getting that to work like the original I presented.  I does seem simpler, but I'm not sure why it won't work.

I think the problem is the query is starting at the top of the "SpacesStore", but the file I'm searching for by the uuid is placed into a Samples folder under the SpacesStore. 

workspace->SpacesStore->Web Service Sample Folder-><file I'm looking for>

Is there any quick way to search all sub-spaces or a store?

Thanks,

Colby

jlbarrera
Champ in-the-making
Champ in-the-making
Yes .. You should find by "PATH" in the query