Gestion de los documentos con Java API

RECU-0062 (Recurso Ejemplo)
Tabla de contenidos

Descripción

Obtenemos el servicio de Registro,necesario para poder realizar cualquier acción.

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

El siguiente paso sera el de identificarnos en el sistema

        AuthenticationService authenticationService = serviceRegistry.getAuthenticationService();
        authenticationService.authenticate("admin", "admin".toCharArray());

Obtenemos el nodo Company Home,que sera en el cual realizaremos las pruebas

SearchService searchService = serviceRegistry.getSearchService();
        StoreRef storeRef = new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore");
        ResultSet resultSet = searchService.query(storeRef, SearchService.LANGUAGE_LUCENE, "PATH:\"/app:company_home\"");
        NodeRef companyHome = resultSet.getNodeRef(0);

Creamos el mapa de propiedades y establecemos las propiedades para el documento

String name = "sample API JAVA (" + System.currentTimeMillis() + ").txt";
        Map<QName, Serializable> contentProps = new HashMap<QName, Serializable>();
        contentProps.put(ContentModel.PROP_NAME, name);

Creamos el documento

NodeService nodeService = serviceRegistry.getNodeService();
        ChildAssociationRef association = nodeService.createNode(companyHome,
                ContentModel.ASSOC_CONTAINS,
                QName.createQName(NamespaceService.CONTENT_MODEL_PREFIX, name),
                ContentModel.TYPE_CONTENT,
                contentProps);
        NodeRef content = association.getChildRef();

Añadimos el aspecto Titled (para la Visualización en el cliente Web)

Map<QName, Serializable> titledProps = new HashMap<QName, Serializable>();
        titledProps.put(ContentModel.PROP_TITLE, name);
        titledProps.put(ContentModel.PROP_DESCRIPTION, name);
        nodeService.addAspect(content, ContentModel.ASPECT_TITLED, titledProps);

Insertamos contenido en el documento

ContentService contentService = serviceRegistry.getContentService();
        ContentWriter writer = contentService.getWriter(content, ContentModel.PROP_CONTENT, true);
        writer.setMimetype(MimetypeMap.MIMETYPE_TEXT_PLAIN);
        writer.setEncoding("UTF-8");
        String text = "Texto de ejemplo para el documento sample API JAVA";
        writer.putContent(text);
       
       
        System.out.println("Finalización creación Documento");