cancel
Showing results for 
Search instead for 
Did you mean: 

Delete node through the WS API[SOLVED]

sisao
Champ in-the-making
Champ in-the-making
Hi,

i'm going to use the labs version of alfresco as a repository for my application….using various API to get this fully automated and transparent….i can import content to alfresco via my application forms…doing all the session and writing job using the WS API which i find really fast. I'm anyway not found methods that let me handle nodes after they're imported…such as renaming that i can still do with changing the CML fields or most important i cant find a delete method for nodes….i've only found a clear content thing which is not what i was expecting to use.
I am now trying this through the JCR API but the whole login session is more articulated than the simple session handshake provided with the WS API that does not require any explicit spring beans injection.
How can i solve this problem….maybe i've just not checked the whole WS API properly.

Regards,
sees.
11 REPLIES 11

openpj
Elite Collaborator
Elite Collaborator
To delete a node via Alfresco Web Services API you can use CMLDelete class and then you can invoke update method with this object as an argument, here an example:

query = new Query(Constants.QUERY_LANG_LUCENE, "PATH:\"/app:company_home/cm:yourSpace/cm:yourFileName\"");
ResultSet rs =repositoryService.query(storeRef, query, false).getResultSet();
if(rs.getTotalRowCount() > 0){
ResultSetRow[] rsRows = rs.getRows();
log.debug("Removing file " + rsRows[0].getColumns(1).getValue());
Reference reference = new Reference(storeRef, rsRows[0].getNode().getId(), null);
Predicate predicate = new Predicate(new Reference[]{reference}, null, null);
CMLDelete delete = new CMLDelete(predicate);
cml.setDelete(new CMLDelete[] {delete});
repositoryService.update(cml);

Hope this helps.

sisao
Champ in-the-making
Champ in-the-making
Hello,

i'm trying to use the method you provide but i basically cant execute that query, even in the node browser.
Supposing the document is into /company_home/myapp/myimage.jpg:

1)PATH:"/company_home/myapp" —> success
2)PATH:"/company_home/myapp/*" —> success (note that in this case the name provided are altered like x001_myimage.jpg)
3)PATH:"/company_home/myapp/myimage.jpg" —> FAIL

The above is done through the node browser's lucene engine.
Below is the code i'm using based on your shot, with some debug lines:


protected static final Store STORE = new Store(Constants.WORKSPACE_STORE, "SpacesStore");
protected static final Reference companyHome = new Reference(STORE, "null", "/app:company_home/cm:myapp/cm:094026.jpg");
AuthenticationUtils.startSession(username, password);
            WebServiceFactory.setEndpointAddress("http://localhost:8088/alfresco/api");
   
            RepositoryServiceSoapBindingStub repositoryService = WebServiceFactory.getRepositoryService();
          
            ticket = AuthenticationUtils.getTicket();
            Query query = new Query(Constants.QUERY_LANG_LUCENE, "PATH:\"/app:company_home/cm:myapp/094026.jpg\"");
            System.out.println("STORE: "+STORE.toString());
            System.out.println(STORE.getAddress());
            System.out.println("QUERY_STATEMENT: "+query.getStatement());
            QueryResult queryResult = repositoryService.query(STORE, query, false);
            System.out.println(repositoryService.queryChildren(companyHome).toString());
            //System.out.println("RS: "+rs.toString());
            //System.out.println("RSTotalRowCount: "+rs.getTotalRowCount());
            //if(rs.getTotalRowCount() > 0){
            //ResultSetRow[] rsRows = rs.getRows();
            //log.debug("Removing file " + rsRows[0].getColumns(1).getValue());
            //Reference reference = new Reference(STORE, rsRows[0].getNode().getId(), null);
            //Predicate predicate = new Predicate(new Reference[]{companyHome}, null, null);
            //CMLDelete delete = new CMLDelete(predicate);
            //CML cml = new CML();
            //cml.setDelete(new CMLDelete[] {delete});
            //repositoryService.update(cml);
          
             //}

The error i got is the following:
Exception in thread "main" AxisFault
faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.generalException
faultSubcode:
faultString:
faultActor:
faultNode:
faultDetail:
   {http://www.alfresco.org/ws/service/repository/1.0}RepositoryFault:<ns1:errorCode>0</ns1:errorCode><ns1:message>org.alfresco.service.cmr.repository.InvalidNodeRefException: Node does not exist: workspace://SpacesStore/null</ns1:message>
   {http://xml.apache.org/axis/}exceptionName:org.alfresco.repo.webservice.repository.RepositoryFault
   {http://xml.apache.org/axis/}stackTrace:
   at org.alfresco.repo.webservice.repository.RepositoryWebService.executeQuery(RepositoryWebService.java:176)

Thx for the support provided,
sisao.

sisao
Champ in-the-making
Champ in-the-making
has lucene some naming issue i am ignoring?
i can query myimage.jpg successfully but not 21213910.jpg…..any idea?

openpj
Elite Collaborator
Elite Collaborator
I think you must set content model prefix on filename too, you can try to set Lucene query in this way:

Query query = new Query(Constants.QUERY_LANG_LUCENE, "PATH:\"/app:company_home/cm:myapp/cm:yourFileName\"");
Hope this helps.

sisao
Champ in-the-making
Champ in-the-making
I have already included the cm: prefix as you can see in the code i've posted above. It's just some sort of issue with the naming support of the search queries provided by lucene…searching around about this actually….your piece of code works like a charm, i just have to apply a naming rule in order to get lucene to successfully locate the node i should delete.
Thx for your support.

EDIT: the lucene search engine searches for the qname, not the cm:name property…that's why my searches fails probably.

sisao
Champ in-the-making
Champ in-the-making
everything is working fine with adding an encode to ISO9075 instruction before passing the filename to a lucene query…applying the SOLVED tag.
Thx.

openpj
Elite Collaborator
Elite Collaborator
I was answering you about coding but you have answered before me  :wink:

yuvaraj
Champ in-the-making
Champ in-the-making
Hi all,

    Can anybody tell me how to delete a workspace using the WS api in alfresco. I am unable to find any method corresponding to a workspace delete ?
    Thanks in advance.

Yuvaraj.T

shamabbas
Champ in-the-making
Champ in-the-making
You can try this: hope it will work.

protected void deleteSpace(Reference space) throws Exception
{
      CMLDelete delete = new CMLDelete(new Predicate(new Reference[]{space},null,null));
      
      CML cml = new CML();
      cml.setDelete(new CMLDelete[]{delete});
      
      //Execute the CMLDelete statement
       
       try {
              System.out.println("Deleting the space " + space.getPath());
         WebServiceFactory.getRepositoryService().update(cml);
       
        } catch (Exception e2) {
            
            System.err.println("Can not delete the space.");
            throw e2;
        }    
}

Checkout this link for further details
http://wiki.alfresco.com/wiki/IngresTutorial_Alfresco_Web_Service_API_for_Java#Delete_a_node

    If above post helps you please click on yes button given on right side of ur window. thnx