Categorías y Aspectos con Java API

RECU-0061 (Recurso Ejemplo)
Tabla de contenidos

Descripción

Obtenemos el objeto ServiceRegistry el cual nos permitira acceder a los servicios necesarios.

ApplicationContext ctx = ApplicationContextHelper.getApplicationContext();        
        final ServiceRegistry serviceRegistry = (ServiceRegistry) ctx.getBean(ServiceRegistry.SERVICE_REGISTRY);

Declaramos los servicios necesarios

AuthenticationService authenticationService = serviceRegistry.getAuthenticationService();
        CategoryService serviceCategory=serviceRegistry.getCategoryService();
        SearchService searchService = serviceRegistry.getSearchService();
        NodeService serviceNode= serviceRegistry.getNodeService();

El siguiente paso sera identificarnos en el sistema

authenticationService.authenticate("admin", "admin".toCharArray());

Declaramos los qnames necesarios

QName QNamecategories = QName.createQName("{http://www.alfresco.org/model/content/1.0}categories");
        QName QNamedoc = QName.createQName("{http://www.alfresco.org/model/content/1.0}name");
        QName QNameAspect = QName.createQName("{http://www.alfresco.org/model/content/1.0}generalclassifiable");

Declaramos el storeref con el cual trabajeremos

StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");

Buscamos la ruta general de las categorias

ResultSet resultSet = searchService.query(storeRef, SearchService.LANGUAGE_LUCENE, "PATH:\"/cm:generalclassifiable\"");
        NodeRef usersStorageSpace = resultSet.getNodeRef(0);

Creamos la categoria

NodeRef categoria= serviceCategory.createCategory(usersStorageSpace, "pruebadecategoria");

Obtenemos un documento con el que trabajaremos

String query="PATH:\"/app:company_home/*\" AND TYPE:\"cm:content\"  ";
 ResultSet resultSet2 = searchService.query(storeRef, SearchService.LANGUAGE_LUCENE, query);
 List<NodeRef> spaces = resultSet2.getNodeRefs();
 NodeRef doc=null;        
        for(int i=0;i<spaces.size();i++)
        {
            String cadena=serviceNode.getProperty(resultSet2.getRow(i).getNodeRef(), QNamedoc).toString();          
            if(cadena.equals( "prueba.txt" ))
            {
                System.out.println("entra en el archivo");
                doc=(NodeRef) spaces.get(i);   
            }
        }

Si el documento existe empezamos a trabajar sobre el.

if(doc!=null)
        {

Añadimos el aspecto al documento

if(!serviceNode.hasAspect(doc, QNameAspect))
            {
            serviceNode.addAspect(doc,QNameAspect  , null);
            }

Buscamos la categoria y obtenemos el NodeRef correspondiente

String query2="PATH:\"/cm:generalclassifiable//cm:pruebadecategoria\"";
            ResultSet resultSet3 = searchService.query(storeRef, SearchService.LANGUAGE_LUCENE,query2 );
            List<NodeRef> Categorias = resultSet3.getNodeRefs();
            NodeRef categoriaobtenida=Categorias.get(0);

Añadimos la categoria al documento

ArrayList<NodeRef>categorias1=(ArrayList<NodeRef>) serviceNode.getProperty(doc, QNamecategories);
categorias1.add(categoriaobtenida);
serviceNode.setProperty(doc, ContentModel.PROP_CATEGORIES, categorias1);

Eliminamos la categoria del documento

ArrayList<NodeRef>categorias2=(ArrayList<NodeRef>) serviceNode.getProperty(doc, QNamecategories);
     for(int i=0;i<categorias2.size();i++)
     {
            if(categorias2.get(i).getId().equals(categoriaobtenida.getId()))
            {
    categorias2.remove(i);
           }                                       
     }               
     serviceNode.setProperty(doc, ContentModel.PROP_CATEGORIES, categorias2);

Borramos la categoria

serviceCategory.deleteCategory(categoriaobtenida) ;

Finalizamos la estructura del if

}
else
{
     System.out.println("el documento es nulo");   
}

Eliminamos el aspecto del documento

serviceNode.removeAspect(doc, QNameAspect);