Petter S
Petter S

Reputation: 49

Get property of person Alfresco in JAVA

I'm using Alfresco 5.1 Community, and i'm trying to get a property value of a current person logged for example, in the user I have:

 "{http://www.someco.org/model/people/1.0}customProperty"

How can I obtain this in java?

Is a custom property, so, in http://localhost:8080/alfresco/service/api/people it does not appear. How can I do this?

I try this to obtain at least nodeRef:

protected ServiceRegistry getServiceRegistry() {
        ProcessEngineConfigurationImpl config = Context.getProcessEngineConfiguration();
        if (config != null) {
            // Fetch the registry that is injected in the activiti spring-configuration
            ServiceRegistry registry = (ServiceRegistry) config.getBeans().get(ActivitiConstants.SERVICE_REGISTRY_BEAN_KEY);

            if (registry == null) {
                throw new RuntimeException("Service-registry not present in ProcessEngineConfiguration beans, expected ServiceRegistry with key" + ActivitiConstants.SERVICE_REGISTRY_BEAN_KEY);
            }

            return registry;
        }
        throw new IllegalStateException("No ProcessEngineConfiguration found in active context");
    }

    public void writeToCatalina() {
        PersonService personService = getServiceRegistry().getPersonService();
        System.out.println("test");
        String name = AuthenticationUtil.getFullyAuthenticatedUser();
        System.out.println(name);
        NodeRef personRef = personService.getPerson(name);
        System.out.println(personRef);
    }

But I got:

No ProcessEngineConfiguration found in active context

Help me !

Upvotes: 0

Views: 1306

Answers (4)

Sanjay
Sanjay

Reputation: 2503

This will give you current logged in user name and detail.

        String name = AuthenticationUtil.getFullyAuthenticatedUser();
        System.out.println("Current user:"+name);
        PersonService personService=serviceRegistry.getPersonService();
        NodeRef node=personService.getPerson(name);
        NodeService nodeService=serviceRegistry.getNodeService();
        Map<QName, Serializable> props=nodeService.getProperties(node);

        for (Entry<QName, Serializable> entry : props.entrySet())
        {
            System.out.println(entry.getKey() + "/" + entry.getValue());
        }

Upvotes: 0

Andrea Girardi
Andrea Girardi

Reputation: 4437

You can query Alfresco using CMIS and call the API:

GET /alfresco/service/api/people/{userName}.

For first you can define the method to create the session CmisSession:

public Session getCmisSession() {

    logger.debug("Starting: getCmisSession()");

    // default factory implementation
    SessionFactory factory = SessionFactoryImpl.newInstance();
    Map<String, String> parameter = new HashMap<String, String>();

    // connection settings
    parameter.put(SessionParameter.ATOMPUB_URL, url + ATOMPUB_URL);
    parameter.put(SessionParameter.BINDING_TYPE, BindingType.ATOMPUB.value());
    parameter.put(SessionParameter.AUTH_HTTP_BASIC, "true");
    parameter.put(SessionParameter.USER, username);
    parameter.put(SessionParameter.PASSWORD, password);
    parameter.put(SessionParameter.OBJECT_FACTORY_CLASS, "org.alfresco.cmis.client.impl.AlfrescoObjectFactoryImpl");

    List<Repository> repositories = factory.getRepositories(parameter);

    return repositories.get(0).createSession();
}

Then execute the query (this method returns more than one result, you probably need to change it):

public void doQuery(String cql, int maxItems) {

    Session cmisSession = getCmisSession();

    OperationContext oc = new OperationContextImpl();
    oc.setMaxItemsPerPage(maxItems);

    ItemIterable<QueryResult> results = cmisSession.query(cql, false, oc);

    for (QueryResult result : results) {
        for (PropertyData<?> prop : result.getProperties()) {
            logger.debug(prop.getQueryName() + ": " + prop.getFirstValue());
        }

    }

}

If you need to get the token, use this:

public String getAuthenticationTicket() {

    try {   

        logger.info("ALFRESCO: Starting connection...");

        RestTemplate restTemplate = new RestTemplate();
        Map<String, String> params = new HashMap<String, String>();
        params.put("user", username);
        params.put("password", password);
        Source result = restTemplate.getForObject(url + AFMConstants.URL_LOGIN_PARAM, Source.class, params);

        logger.info("ALFRESCO: CONNECTED!");

        XPathOperations xpath = new Jaxp13XPathTemplate();          
        return xpath.evaluateAsString("//ticket", result);
    }
    catch (RestClientException ex) {
        logger.error("FATAL ERROR - Alfresco Authentication failed - getAuthenticationTicket() - "  + ex );
        return null;
    }       
    catch (Exception ex) {          
        logger.error("FATAL ERROR - Alfresco Authentication failed - getAuthenticationTicket() - "  + ex );
        return null;
    }

}

Upvotes: 2

Younes Regaieg
Younes Regaieg

Reputation: 4156

You need to obtain your user noderef using this API then access its properties this way!


Edit : You need to inject service registry on your bean!

Upvotes: 1

PRVS
PRVS

Reputation: 1690

String name = AuthenticationUtil.getFullyAuthenticatedUser()

You can use this. Let me know if it works.

Upvotes: 0

Related Questions